// UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.
THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.
UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using System;
namespace FpgaMeasurementInstrumentsLib
{
///
/// An abstract base class for FPGARspMessages that go between the client and server
///
internal abstract class FPGARspMessage : ICloneable
{
#region PrivateClassMembers
protected FPGARspMessageHeader m_header;
private readonly string m_description;
#endregion
#region PrivateFunctions
///
/// The constructor that the children will call
///
/// The message id
/// The message description
protected FPGARspMessage(FPGARspMsgIds commandType, string description)
{
try
{
m_description = description;
m_header = new FPGARspMessageHeader(commandType);
}
catch (Exception)
{
throw;
}
}
///
/// Copy Constructor
///
/// The FPGARspMessage to copy from
protected FPGARspMessage(FPGARspMessage FPGARspMessage)
{
try
{
m_header = new FPGARspMessageHeader(FPGARspMessage.m_header);
m_description = FPGARspMessage.m_description;
}
catch (Exception)
{
throw;
}
}
///
/// A function to create a copy of the object for all children to implement
///
///
protected abstract object CloneSelf();
///
/// A function to encode data for sending
///
/// A pointer to the spot to put the encoded data
protected abstract void FormatData(IntPtr pData);
///
/// Get the size of the payload
///
/// The size of the message payload
protected abstract int GetPayloadSize();
///
/// A function to decode data that was received
///
/// A pointer to the data to decode
protected abstract void ParseData(IntPtr pData);
#endregion
#region PublicFunctions
///
/// Get a copy of the FPGARspMessage object
///
///
public object Clone()
{
// tell the child to clone itself
return this.CloneSelf();
}
///
/// Encode the FPGARspMessage into a byte array for sending
///
/// The buffer to put the FPGARspMessage items
public void Format(IntPtr pData)
{
try
{
m_header.Format(pData);
IntPtr pPayload = IntPtr.Add(pData, (int)GetHeaderLength());
// ask child class to format its data
FormatData(pPayload);
}
catch (Exception)
{
throw;
}
}
///
/// A getter function for the children to implement
///
/// The address that was read
public abstract uint GetAddress();
///
/// A getter function for the children to implement
///
/// The data that was read
public abstract uint GetData();
///
/// Getter for this message description
///
/// The description
public string GetDescription()
{
return m_description;
}
///
/// Getter for the number of bytes in the entire message
///
/// The number of bytes in the FPGARspMessage, including header
public uint GetEntireMsgLength()
{
return GetHeaderLength() + (uint)GetPayloadSize();
}
///
/// getter for the number of bytes in the header
///
/// The number of bytes in the head
public uint GetHeaderLength()
{
return m_header.GetHeaderLength();
}
///
/// Takes an array of bytes and populates the FPGARspMessage object
///
/// The FPGARspMessage in byte form
public void Parse(IntPtr pData)
{
try
{
m_header.Parse(pData);
IntPtr pPayLoad = IntPtr.Add(pData, (int)GetHeaderLength());
ParseData(pPayLoad);
}
catch (Exception)
{
throw;
}
}
///
/// Convert this FPGARspMessage into string form
///
/// The FPGARspMessage in string form
public override string ToString()
{
return "Description: " + GetDescription() + "\n" + m_header.ToString();
}
#endregion
}
}