Пример #1
0
        private void OnOperationReceived(InputHeader operation)
        {
            if (operation.Content.SourceAddress == 0)
            {
                operation.Content.SourceAddress = this.NodeAddress;
            }


            if (ConnectionState == ConnectionStates.Identifying &&
                operation.Content is OperationMessage &&
                ((OperationMessage)operation.Content).OpCode == OperationMessage.OPCodes.PingResponse)
            {
                // Ping response for Indentify
                this.NodeAddress     = ((OperationMessage)operation.Content).SourceAddress;
                this.Identified      = true;
                this.ConnectionState = ConnectionStates.Connected;

                this.InitializeNode();
            }
            else if (operation.Confirmation != InputHeader.ConfirmationType.None)
            {
                if (operation.MessageId == this.currentMessageId)
                {
                    Debug.WriteLine(this.PortName + " Confirmation received: " + operation.Confirmation.ToString());

                    // Data confirmation response
                    if (pendingConfirmationTask != null && !pendingConfirmationTask.Task.IsCompleted)
                    {
                        pendingConfirmationTask.SetResult(operation.Confirmation == InputHeader.ConfirmationType.Ok);
                    }
                }
                else if (operation.MessageId < this.currentMessageId)
                {
                    Debug.WriteLine(this.PortName + " Old confirmation received: " + operation.Confirmation.ToString());
                }
                else if (operation.MessageId == 255)
                {
                    Debug.WriteLine(this.PortName + " Internal confirmation received: " + operation.Confirmation.ToString());
                }
            }
            else
            {
                Debug.WriteLine(this.PortName + " Operation Message received: " + ((OperationMessage)operation.Content).OpCode.ToString());

                if (operation.Content is OperationMessage && ((OperationMessage)operation.Content).OpCode == OperationMessage.OPCodes.WakeUp)
                {
                    // Node just wake up
                    this.InitializeNode();
                }

                // Real operation response
                if (this.MessageReceived != null)
                {
                    this.MessageReceived(this, operation);
                }
            }
        }
Пример #2
0
 private Filter.OriginTypes GetOriginType(InputHeader message, NodeConnection connection)
 {
     if (connection.NodeAddress == message.Content.SourceAddress)
     {
         return(Filter.OriginTypes.FromNode);
     }
     else if (connection.NodeAddress == MASTER_ADDRESS)
     {
         return(Filter.OriginTypes.FromMaster);
     }
     else
     {
         return(Filter.OriginTypes.Any);
     }
 }
Пример #3
0
        private async void connection_OperationReceived(object sender, InputHeader e)
        {
            NodeConnection connection = (NodeConnection)sender;

            List <Task> taskList = new List <Task>();

            //Send Message to the placeHolders
            foreach (var module in this.modulesList.Where(m => m.Filter.CheckMessage(e, GetOriginType(e, connection))))
            {
                var moduleTask = new Task(() =>
                {
                    module.ProcessReceivedMessage(e.Content);
                });
                moduleTask.Start();

                taskList.Add(moduleTask);
            }

            await Task.WhenAll(taskList);
        }
        private void OnOperationReceived(InputHeader operation)
        {
            if (operation.Content.SourceAddress == 0)
                operation.Content.SourceAddress = this.NodeAddress;


            if (ConnectionState == ConnectionStates.Identifying &&
                operation.Content is OperationMessage &&
                ((OperationMessage)operation.Content).OpCode == OperationMessage.OPCodes.PingResponse)
            {
                // Ping response for Indentify
                this.NodeAddress = ((OperationMessage)operation.Content).SourceAddress;
                this.Identified = true;
                this.ConnectionState = ConnectionStates.Connected;

                this.InitializeNode();
            }
            else if (operation.Confirmation != InputHeader.ConfirmationType.None)
            {
                if (operation.MessageId == this.currentMessageId)
                {
                    Debug.WriteLine(this.PortName + " Confirmation received: " + operation.Confirmation.ToString());

                    // Data confirmation response
                    if (pendingConfirmationTask != null && !pendingConfirmationTask.Task.IsCompleted)
                        pendingConfirmationTask.SetResult(operation.Confirmation == InputHeader.ConfirmationType.Ok);
                }
                else if (operation.MessageId < this.currentMessageId)
                {
                    Debug.WriteLine(this.PortName + " Old confirmation received: " + operation.Confirmation.ToString());
                }
                else if (operation.MessageId == 255)
                {
                    Debug.WriteLine(this.PortName + " Internal confirmation received: " + operation.Confirmation.ToString());
                }
            }
            else
            {
                Debug.WriteLine(this.PortName + " Operation Message received: " + ((OperationMessage)operation.Content).OpCode.ToString());

                if (operation.Content is OperationMessage && ((OperationMessage)operation.Content).OpCode == OperationMessage.OPCodes.WakeUp)
                {
                    // Node just wake up
                    this.InitializeNode();
                }

                // Real operation response
                if (this.MessageReceived != null)
                    this.MessageReceived(this, operation);
            }
        }
        private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte c;

            while (this.serialPort.BytesToRead > 0)
            {
                bool acceptByte = false;

                c = (byte)this.serialPort.ReadByte();

                switch (this.receiverState)
                {
                    case SerialReceiverStates.IDLE_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            this.receiverState = SerialReceiverStates.SOF_RX_STATE;
                            this.currentReceiverMessage.Clear();
                            this.checkSum = 0;
                        }
                        break;

                    case SerialReceiverStates.SOF_RX_STATE:
                        if (SOF[1] == c)
                        {
                            this.receiverState = SerialReceiverStates.DATA_RX_STATE;
                        }
                        else
                        {
                            this.receiverState = SerialReceiverStates.IDLE_RX_STATE;
                        }
                        break;

                    case SerialReceiverStates.DATA_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            this.receiverState = SerialReceiverStates.MAGIC_RX_STATE;
                        }
                        else
                        {
                            acceptByte = true;
                        }
                        break;

                    case SerialReceiverStates.MAGIC_RX_STATE:
                        if (APP_MAGIC_SYMBOL == c)
                        {
                            this.receiverState = SerialReceiverStates.DATA_RX_STATE;
                            acceptByte = true;
                        }
                        else if (EOF[1] == c)
                        {
                            this.receiverState = SerialReceiverStates.EOF_RX_STATE;
                        }
                        else
                        {
                            this.receiverState = SerialReceiverStates.IDLE_RX_STATE;
                            Debug.Print("INVALID FRAME RECEIVED IN " + this.PortName);
                        }
                        break;

                    case SerialReceiverStates.EOF_RX_STATE:
                        if (checkSum == c)
                        {
                            this.receiverState = SerialReceiverStates.IDLE_RX_STATE;
                            InputHeader inputMessage = new InputHeader();
                            inputMessage.FromBinary(this.currentReceiverMessage.ToArray());

                            OnOperationReceived(inputMessage);
                        }
                        else
                        {
                            this.receiverState = SerialReceiverStates.IDLE_RX_STATE;
                            Debug.Print("INVALID CHECKSUM RECEIVED IN " + this.PortName);
                        }
                        break;

                    default:
                        break;
                }

                checkSum += c;

                if (acceptByte)
                {
                    this.currentReceiverMessage.Add(c);
                }
            }
        }
Пример #6
0
        private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte c;

            while (this.serialPort.BytesToRead > 0)
            {
                bool acceptByte = false;

                c = (byte)this.serialPort.ReadByte();

                switch (this.receiverState)
                {
                case SerialReceiverStates.IDLE_RX_STATE:
                    if (APP_MAGIC_SYMBOL == c)
                    {
                        this.receiverState = SerialReceiverStates.SOF_RX_STATE;
                        this.currentReceiverMessage.Clear();
                        this.checkSum = 0;
                    }
                    break;

                case SerialReceiverStates.SOF_RX_STATE:
                    if (SOF[1] == c)
                    {
                        this.receiverState = SerialReceiverStates.DATA_RX_STATE;
                    }
                    else
                    {
                        this.receiverState = SerialReceiverStates.IDLE_RX_STATE;
                    }
                    break;

                case SerialReceiverStates.DATA_RX_STATE:
                    if (APP_MAGIC_SYMBOL == c)
                    {
                        this.receiverState = SerialReceiverStates.MAGIC_RX_STATE;
                    }
                    else
                    {
                        acceptByte = true;
                    }
                    break;

                case SerialReceiverStates.MAGIC_RX_STATE:
                    if (APP_MAGIC_SYMBOL == c)
                    {
                        this.receiverState = SerialReceiverStates.DATA_RX_STATE;
                        acceptByte         = true;
                    }
                    else if (EOF[1] == c)
                    {
                        this.receiverState = SerialReceiverStates.EOF_RX_STATE;
                    }
                    else
                    {
                        this.receiverState = SerialReceiverStates.IDLE_RX_STATE;
                        Debug.Print("INVALID FRAME RECEIVED IN " + this.PortName);
                    }
                    break;

                case SerialReceiverStates.EOF_RX_STATE:
                    if (checkSum == c)
                    {
                        this.receiverState = SerialReceiverStates.IDLE_RX_STATE;
                        InputHeader inputMessage = new InputHeader();
                        inputMessage.FromBinary(this.currentReceiverMessage.ToArray());

                        OnOperationReceived(inputMessage);
                    }
                    else
                    {
                        this.receiverState = SerialReceiverStates.IDLE_RX_STATE;
                        Debug.Print("INVALID CHECKSUM RECEIVED IN " + this.PortName);
                    }
                    break;

                default:
                    break;
                }

                checkSum += c;

                if (acceptByte)
                {
                    this.currentReceiverMessage.Add(c);
                }
            }
        }