public Engine() { peer.OnReceiveDelta += new NodePeer.ReceiveDeltaHandler( delegate(NodePeer fromPeer, FieldGuid basedOnMessageID) { // This is responsible for returning the message // the delta is based on so that the communication // manager can reconstruct the entire message. // WATCH OUT - RUNS ON A SEPARATE THREAD if (basedOnMessageID == runtimeApplication.ID) { return(runtimeApplication); } else { return(lastMessage); } }); peer.OnReceive += new NodePeer.ReceiveHandler( delegate(NodePeer fromPeer, NodeBase message) { // New message has arrived. // WATCH OUT - RUNS ON A SEPARATE THREAD lastMessage = message; var runtimeApp = message as NodeRuntimeApplication; var telegram = message as NodeTelegram; if (runtimeApp != null) { // peer is sending a new version of the // runtime application - store it and we'll // "see" it on the next logic scan. runtimeApplication = runtimeApp; } else if (telegram != null) { try { var toPeer = fromPeer; var telegramType = (TelegramType)Enum.Parse(typeof(TelegramType), telegram.MessageType.ToString()); switch (telegramType) { case TelegramType.READCONFIGURATION: // peer is requesting an I/O configuration // so do a scan and send one back peer.SendToPeer(toPeer, getDeviceConfiguration()); break; case TelegramType.START: Start(); peer.SendToPeer(toPeer, telegram.SetPayload(m_TrueResponse)); break; case TelegramType.STOP: Stop(); peer.SendToPeer(toPeer, telegram.SetPayload(m_TrueResponse)); break; case TelegramType.RUNNING: var response = m_FalseResponse; if (run) { response = m_TrueResponse; } peer.SendToPeer(toPeer, telegram.SetPayload(response)); break; case TelegramType.RUNTIMEID: FieldBase64 runtimeIdResponse = m_EmptyResponse; if (runtimeApplication != null) { runtimeIdResponse = FieldBase64.Encode(runtimeApplication.RuntimeId.ToString()); } peer.SendToPeer(toPeer, telegram.SetPayload(runtimeIdResponse)); break; case TelegramType.RUNTIMEVERSIONID: FieldBase64 runtimeVersionIdResponse = m_EmptyResponse; if (runtimeApplication != null) { runtimeVersionIdResponse = FieldBase64.Encode(runtimeApplication.ID.ToString()); } peer.SendToPeer(toPeer, telegram.SetPayload(runtimeVersionIdResponse)); break; case TelegramType.UPLOAD: peer.SendToPeer(toPeer, runtimeApplication); break; case TelegramType.READSIGNALS: peer.SendToPeer(toPeer, readSignals(telegram)); break; } } catch (ArgumentException) { // means message type wasn't parsed to an enum - shouldn't happen, but whatever } } }); Load(); }