/// <summary> /// Add a new message as a Publish Subscribe Message to the List /// of Publish Subscibe messages. /// A reference to the Global Publish Subscribe Message will be added /// to the message passed in. /// Each Manager should add all messages it needs to work with in the /// Global Message Container. /// It is assumed that messages with the same name are the same Message /// type and are being used for the same purpose across all Managers. /// The first Manager to all the publish subscribe message will establish /// the message in the Global WorkSpace. All other managers will simply /// have their message updated with a reference to the Global Message. /// </summary> /// <param name="messageName"></param> /// <param name="msg"></param> /// /// <returns>true if Error, false if ok.</returns> public bool AddPublishSubscribeMessage(string messageName, RabitMessage msg) { bool error = false; //rwlPublishSubscribeDict.EnterWriteLock(); if (!publishSubcribeMsgDict.ContainsKey(messageName)) { msg.SetTimeToNow(); //Ensure the Current Time is set PSMsgContainer psMsgContainer; psMsgContainer.MgrMsgRef = msg; //Reference to the orginal message psMsgContainer.PSMsg = new PublishSubscribeMessage((RabitMessage)msg.Clone()); msg.GlobalPublishSubscribeMessageRef = psMsgContainer.PSMsg; publishSubcribeMsgDict.Add(messageName, psMsgContainer); } else { if (publishSubcribeMsgDict[messageName].PSMsg.MsgType == msg.GetType()) { //Update the incoming message with the Publish Subscribe //Message Reference. msg.GlobalPublishSubscribeMessageRef = publishSubcribeMsgDict[messageName].PSMsg; } else { //Error! Console.WriteLine("AddPublishSubscribeMessage: A new message is being added that conflicts with a current message. MessageName:{0}, CurrentMsgType:{1}, NewMsgType:{2}", messageName, publishSubcribeMsgDict[messageName].PSMsg.MsgType.ToString(), msg.GetType().ToString()); error = true; } } //rwlPublishSubscribeDict.ExitWriteLock(); return(error); }
public override void CopyMessage(RabitMessage msg) { //Ensure that all base parameters/variables are copied. base.CopyMessage(msg); //Cast Message into the Concrete Message. ManagerStatusMessage cmsg = (ManagerStatusMessage)msg; ManagerStatus = cmsg.ManagerStatus; }
public override void CopyMessage(RabitMessage msg) { //Ensure that all base parameters/variables are copied. base.CopyMessage(msg); //Cast Message into the Concrete Message. ManagerControlMessage cmsg = (ManagerControlMessage)msg; ShutDownAllManagers = cmsg.ShutDownAllManagers; RunState = cmsg.RunState; }
/// <summary> /// CopyIntoThisMessag() This method copys all the data from msg to this message. /// The Copy method is an integral part of the Publish Subscribe /// mechanism... so this method must reliably hand the copy process. /// If the message contains arrays, lists, dictionaries, or similar /// types... these objects must be properly copied... don't just /// copy the pointer or there will be all sorts of crazy errors caused /// in the system. /// The base CopyIntoThisMessage verifies that the message type /// being copied is the same message type as this message. /// A System.ArgumentException() is thrown if they are not. /// </summary> /// <param name="mess">The message with the data to be copied into /// this message.</param> virtual public void CopyMessage(RabitMessage msg) { //Verify we are coping the same concrete message type... //This is a serious error if we are not. if (msg.GetType() != this.GetType()) { throw new System.ArgumentException(); } Timestamp = msg.Timestamp; }
/// <summary> /// Get a copy of a Publish Subscribe message for the given message name. /// Returns a copy of the message or null if no message by that name. /// </summary> /// <param name="messageName"></param> /// <returns></returns> public RabitMessage GetMessage(string messageName) { RabitMessage msg = null; if (publishSubcribeMsgDict.ContainsKey(messageName)) { msg = publishSubcribeMsgDict[messageName].PSMsg.GetCopyOfMessage(); } return(msg); }
/// <summary> /// Clone() creates a new message object which is a copy of the /// Message object. /// Clone must be overriden if the message contains arrays, lists, dictionaries, /// or similar types. Clone is an integral part of the Publish Subscribe System. /// Note: the Message Clone intentionally removes the Message ewhWaitHandle /// and the globalPublishSubscribeMessageRef. Normally the new message does /// not need these references. If the new message does need them... then they /// must be manually re-establish. /// </summary> /// <returns>Message object</returns> virtual public object Clone() { RabitMessage msgClone = (RabitMessage)this.MemberwiseClone(); //Do not carry the Event Wait Handle or Global Publish Subscibe references forward... //This could cause strange behaviors... they must be re-established if needed... //in general they are not required. msgClone.ewhWaitHandle = null; msgClone.globalPublishSubscribeMessageRef = null; return(msgClone); }
/// <summary> /// ForceFetchMessage() copies the message information into the message object /// passed in. ForceFetchMessage() is used so that new objects are not created /// only to be distroyed later, which can be time consumming. /// A read lock is used to ensure an <c>Update</c> /// is not occurring when the copy is made. /// </summary> /// <param name="mess">A reference to a Message object which /// will recieve the Global message's information</param> public void ForceFetchMessage(RabitMessage msg) { rwl.EnterReadLock(); try { msg.CopyMessage(statusMessage); } finally { rwl.ExitReadLock(); } }
/// <summary> /// Post a message to the Publish Subscribe message. /// </summary> /// <param name="messageName"></param> /// <param name="msg"></param> /// <returns>true if error... no message of the given name or the message types do not match, /// false otherwise.</returns> public bool PostMessage(string messageName, RabitMessage msg) { bool error = true; if (publishSubcribeMsgDict.ContainsKey(messageName)) { if (publishSubcribeMsgDict[messageName].PSMsg.MsgType == msg.GetType()) { publishSubcribeMsgDict[messageName].PSMsg.PostMessage(msg); } } return(error); }
/// <summary> /// PublishSubscibeMsgArray with an array size which is the largest /// number of messages to store in the array. /// </summary> /// <param name="arraySize"></param> /// <param name="defaultMessage">If defaultMessage not null the MessageArray will be /// filled with copies of the default message, otherwise the message array /// will be filled with nulls.</param> public PublishSubscibeMsgArray(int arraySize, RabitMessage defaultMessage) { MaxNoMessages = arraySize < 5 ? 5 : arraySize; messageArray = new RabitMessage[MaxNoMessages]; if (defaultMessage != null) { for (int i = 0; i < MaxNoMessages; i++) { messageArray[i] = (RabitMessage)defaultMessage.Clone(); } } rwl = new ReaderWriterLockSlim(); }
/// <summary> /// Update() This method updates the master copy of the message information. /// A write lock is obtained before updating the master object's information. /// This ensures another manager thread is not trying to copy the information out /// at the same time. /// After updating the message information, the <c>OnChange</c> method is called /// to inform other managers that the information has been changed. /// </summary> /// <param name="mess">a PublishSubscribeMessage object containing the new /// message information.</param> public void PostMessage(RabitMessage msg) { rwl.EnterWriteLock(); try { //Set the time the message was posted. msg.SetTimeToNow(); statusMessage.CopyMessage(msg); } finally { rwl.ExitWriteLock(); } OnChange(); }
/// <summary> /// getCopyOfMessageIfNewer() copies the message information into the message object /// passed in, if the message is newer in timestamep than the message passed in. /// </summary> /// <param name="mess">A reference to a Message object which /// will recieve the Global message's information</param> /// <returns>true if a copy was made, false if a copy was not made</returns> public bool FetchMessage(RabitMessage mess) { bool copyMade = false; if (statusMessage.Timestamp != mess.Timestamp) { rwl.EnterReadLock(); try { mess.CopyMessage(statusMessage); copyMade = true; } finally { rwl.ExitReadLock(); } } return(copyMade); }
/// <summary> /// FetchMessage() copies the message information into the message object /// passed in, if the message is newer in timestamep than the message passed in. /// </summary> /// <param name="msgIdx">index to message</param> /// <param name="mess">A reference to a Message object which /// will recieve the Global message's information</param> /// <returns>true if a copy was made, false if a copy was not made</returns> public bool FetchMessage(int msgIdx, RabitMessage msg) { bool copyMade = false; if (msgIdx >= 0 && msgIdx < MaxNoMessages && messageArray[msgIdx] != null && (messageArray[msgIdx].Timestamp != msg.Timestamp)) { rwl.EnterReadLock(); try { msg.CopyMessage(messageArray[msgIdx]); copyMade = true; } finally { rwl.ExitReadLock(); } } return(copyMade); }
/// <summary> /// Gets a copy of the message at index msgIdx. /// </summary> /// <param name="msgIdx"></param> /// <returns>a copy of the message if it exits, null otherwise</returns> public RabitMessage GetCopyOfMessage(int msgIdx) { RabitMessage obj = null; if (msgIdx >= 0 && msgIdx < MaxNoMessages) { rwl.EnterReadLock(); try { if (messageArray[msgIdx] != null) { obj = (RabitMessage)messageArray[msgIdx].Clone(); } } finally { rwl.ExitReadLock(); } } return(obj); }
/// <summary> /// ForceFetchMessage() copies the message information into the message object /// passed in. ForceFetchMessage() is used so that new objects are not created /// only to be distroyed later, which can be time consumming. /// A read lock is used to ensure an <c>Update</c> /// is not occurring when the copy is made. /// </summary> /// <param name="msgIdx">index to message</param> /// <param name="mess">A reference to a Message object which /// will recieve the Global message's information</param> /// <returns>true if a copy was made, false if the message array is empty at that point</returns> public bool ForceFetchMessage(int msgIdx, RabitMessage msg) { bool copyMade = false; if (msgIdx >= 0 && msgIdx < MaxNoMessages) { rwl.EnterReadLock(); try { if (messageArray[msgIdx] != null) { msg.CopyMessage(messageArray[msgIdx]); copyMade = true; } } finally { rwl.ExitReadLock(); } } return(copyMade); }
/// <summary> /// Post() This method updates the master copy of the message information. /// A write lock is obtained before updating the master object's information. /// This ensures another manager thread is not trying to copy the information out /// at the same time. /// After updating the message information, the <c>OnChange</c> method is called /// to inform other managers that the information has been changed. /// </summary> /// <param name="msgIdx"></param> /// <param name="mess">a SSMU_StatusMessage object containing the new /// message information.</param> public void PostMessage(int msgIdx, RabitMessage msg) { if (msgIdx >= 0 && msgIdx < MaxNoMessages) { rwl.EnterWriteLock(); try { if (messageArray[msgIdx] != null) { messageArray[msgIdx].CopyMessage(msg); } else { messageArray[msgIdx] = (RabitMessage)msg.Clone(); } } finally { rwl.ExitWriteLock(); } OnChange(); } }
/// <summary> /// Add a new message as a Publish Subscribe Message to the List /// of Publish Subscibe messages. /// </summary> /// <param name="messageName"></param> /// <param name="msg"></param> /// /// <returns>true if Error, false if ok.</returns> public bool AddPublishSubscribeMessage(string messageName, RabitMessage msg) { return(RabitWorkspace.GetWorkspace().AddPublishSubscribeMessage(messageName, msg)); }
public PublishSubscribeMessage(RabitMessage stMessage) { statusMessage = stMessage; rwl = new ReaderWriterLockSlim(); }
public int CompareTime(RabitMessage msg) { return(this.timestamp.CompareTo(msg.timestamp)); }