// 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;
using System.Threading;
using System.Collections.Generic;
namespace Raytheon.Common
{
///
/// Singleton for holding onto message objects
///
public class AutomationRxMsgBuffer
{
// class variables
private static AutomationRxMsgBuffer _AutomationRxMsgBufferInstance;
private static object _syncObj = new object();
private Dictionary> _msgList;
private Dictionary _receivedMsgEvents;
private List _msgIds;
///
/// The way to get access to this singleton
///
/// the instance to this class
public static AutomationRxMsgBuffer Instance(List msgIds = null)
{
if (_AutomationRxMsgBufferInstance == null)
{
_AutomationRxMsgBufferInstance = new AutomationRxMsgBuffer(msgIds);
}
return _AutomationRxMsgBufferInstance;
}
///
/// Add a message to this buffer
///
/// The message to add
/// flag for if the other messages of this type should be deleted from the buffer
public void AddMsg(AutomationMessage msg, bool shouldWeDeleteOthers = true)
{
lock (_syncObj)
{
uint msgId = msg.GetMessageId();
if (shouldWeDeleteOthers == true)
{
ClearList(msgId);
}
if (_msgList.ContainsKey(msgId) == false)
{
_msgList[msgId] = new List();
}
_msgList[msgId].Add(msg);
_receivedMsgEvents[msgId].Set();
}
}
///
/// Remove all messages from the buffer
///
public void ClearAllMsgs()
{
lock (_syncObj)
{
foreach (uint id in _msgList.Keys)
{
ClearList(id);
}
}
}
///
/// Remove all messages of the command type from the buffer
///
/// The message id to remove
public void ClearList(uint id)
{
lock (_syncObj)
{
if (_msgList.ContainsKey(id) == true)
{
_msgList[id].Clear();
_msgList.Remove(id);
}
}
}
///
/// Gets the oldest message in the buffer
///
/// The id of the message to get
/// The oldest message in the buffer
public AutomationMessage GetOldestMessage(uint id)
{
lock (_syncObj)
{
if (_msgList.ContainsKey(id))
{
List list = _msgList[id];
AutomationMessage oldestMsg = list[0];
list.RemoveAt(0);
return oldestMsg;
}
else
{
throw new Exception("no message exists with id: " + id.ToString());
}
}
}
///
/// Gets the most recent message from the buffer
///
/// The id of the message to get
/// flag controlling if the other messages of this type should be deleted
/// The message
public AutomationMessage GetNewestMessage(uint id, bool shouldWeDeleteOthers = true)
{
lock (_syncObj)
{
if (_msgList.ContainsKey(id))
{
List list = _msgList[id];
AutomationMessage newestMsg = list[list.Count - 1];
list.RemoveAt(list.Count - 1);
if (shouldWeDeleteOthers == true)
{
ClearList(id);
}
return newestMsg;
}
else
{
throw new Exception("no message exists with id: " + id.ToString());
}
}
}
///
/// Get the number of messages of this type in the buffer
///
/// The id of the message to get
/// the number of messages of this type in the buffer
public int GetNumMsgsInQueue(uint id)
{
lock (_syncObj)
{
if (_msgList.ContainsKey(id) == true)
{
return _msgList[id].Count;
}
else
{
return 0;
}
}
}
///
///
///
///
public void ResetRxEvent(uint id)
{
lock (_syncObj)
{
_receivedMsgEvents[id].Reset();
}
}
///
/// Wait for a message to get added to the buffer
///
/// The message id to wait for
/// The amount of time in ms to wait
/// true if the message arrived, false if it did not
public bool WaitForRspMsg(uint id, int timeoutMs)
{
return _receivedMsgEvents[id].WaitOne(timeoutMs);
}
///
/// The constructor
///
private AutomationRxMsgBuffer(List msgIds)
{
_msgList = new Dictionary>();
_msgIds = msgIds;
// create an event for each msg.
_receivedMsgEvents = new Dictionary();
foreach (uint id in _msgIds)
{
_receivedMsgEvents[id] = new AutoResetEvent(false);
}
}
}
}