//******************************************************************************************************
// PacketBase.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 07/27/2007 - Pinal C. Patel
// Generated original version of source code.
// 04/21/2009 - Pinal C. Patel
// Converted to C#.
// 09/15/2009 - Stephen C. Wills
// Added new header and license agreement.
// 10/11/2010 - Mihir Brahmbhatt
// Updated header and license agreement.
// 11/30/2011 - J. Ritchie Carroll
// Modified to support buffer optimized ISupportBinaryImage.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Collections.Generic;
using GSF.Parsing;
namespace GSF.Historian.Packets
{
///
/// Base class for a binary packet received by a historian.
///
///
public abstract class PacketBase : IPacket
{
#region [ Members ]
// Constants
///
/// Specifies the number of bytes in the binary image of the packet.
///
/// A value of -1 indicates that the binary image of the packet is of variable length.
public const int FixedLength = -1;
// Fields
private IArchive m_archive;
private Func> m_processHandler;
private Func> m_preProcessHandler;
private ICommonHeader m_commonHeader;
private readonly short m_typeID;
private Guid m_source;
#endregion
#region [ Constructors ]
///
/// Initializes a new instance of the packet.
///
/// Numeric identifier for the packet type.
protected PacketBase(short packetID)
{
m_typeID = packetID;
}
#endregion
#region [ Properties ]
///
/// Gets the length of the packet's binary representation.
///
public abstract int BinaryLength
{
get;
}
///
/// Gets or sets the current .
///
public IArchive Archive
{
get
{
return m_archive;
}
set
{
m_archive = value;
}
}
///
/// Gets or sets the that processes the packet.
///
///
/// returns an object containing the binary data to be sent back to the packet sender.
///
public Func> ProcessHandler
{
get
{
return m_processHandler;
}
set
{
m_processHandler = value;
}
}
///
/// Gets or sets the that pre-processes the packet.
///
///
/// returns an object containing the binary data to be sent back to the packet sender.
///
public Func> PreProcessHandler
{
get
{
return m_preProcessHandler;
}
set
{
m_preProcessHandler = value;
}
}
///
/// Gets or sets the for the packet.
///
public ICommonHeader CommonHeader
{
get
{
return m_commonHeader;
}
set
{
m_commonHeader = value;
}
}
///
/// Gets or sets the numeric identifier for the packet type.
///
public short TypeID
{
get
{
return m_typeID;
}
}
///
/// Gets or sets the data source identifier of the frame image.
///
public Guid Source
{
get
{
return m_source;
}
set
{
m_source = value;
}
}
///
/// Gets flag that determines if frame image can queued for publication or should be processed immediately.
///
///
/// Some frames, e.g., a configuration or key frame, may be critical to processing of other frames. In this
/// case, these types of frames should be published immediately so that subsequent frame parsing can have
/// access to needed critical information.
///
public bool AllowQueuedPublication
{
get
{
return true;
}
}
#endregion
#region [ Methods ]
///
/// Initializes packet by parsing the specified containing a binary image.
///
/// Buffer containing binary image to parse.
/// 0-based starting index in the to start parsing.
/// Valid number of bytes within from .
/// The number of bytes used for initialization in the (i.e., the number of bytes parsed).
///
/// Implementers should validate and against length.
/// The method can be used to perform this validation.
///
public abstract int ParseBinaryImage(byte[] buffer, int startIndex, int length);
///
/// Generates binary image of the packet and copies it into the given buffer, for bytes.
///
/// Buffer used to hold generated binary image of the source object.
/// 0-based starting index in the to start writing.
/// The number of bytes written to the .
///
/// Implementers should validate and against length.
/// The method can be used to perform this validation.
///
public abstract int GenerateBinaryImage(byte[] buffer, int startIndex);
///
/// Extracts time-series data from the packet.
///
/// An of s if the packet contains time-series data; otherwise null.
public abstract IEnumerable ExtractTimeSeriesData();
#endregion
}
}