Example #1
0
        /// <summary>
        /// Handles the messages.
        /// </summary>
        public override void HandleMessage(Message msg)
        {
            if (msg.What == (int)EnLocalMessageType.MESSAGE_READ)                       //package message(remote)
            {
                byte [] data = (byte [])msg.Obj;

                //Get the type of the message from the firs byte of the message
                EnPackageType type = (EnPackageType)data [0];

                if (type == EnPackageType.ACK)
                {
                    if (isSlave())
                    {
                        //if i am a slave i remove the message from the list of the message to send
                        _writeThread [0].Remove(PackageBase.getMessageFromAck(data));
                    }
                    else
                    {
                        //otherwise i am the master so i remove the message only from the list of the sender
                        byte [] mess = PackageBase.getMessageFromAck(data);
                        _writeThread.ForEach(delegate(BTWriteThread thred) {
                            if (thred.Connected == PackageBase.getAddressFromAck(data))
                            {
                                thred.Remove(mess);
                            }
                        });
                        if (mess [0] == (byte)EnPackageType.TERMINATE)
                        {
                            bool end = true;
                            _writeThread.ForEach(delegate(BTWriteThread Thread) {
                                end = Thread.NothingToSend() && end;
                            });
                            if (end)
                            {
                                lock (_monitorMaster) {
                                    Monitor.Pulse(_monitorMaster);
                                }
                            }
                        }
                    }
                }
                else if (type != EnPackageType.NONE)
                {
                    PackageBase pkg = PackageBase.createPackage(data);

                    //ACK consists of the type ACK followed by the message received
                    if (BTManager.Instance.isSlave())
                    {
                        _writeThread [0].Add(pkg.getAckMessage());
                        if (pkg == EnPackageType.TERMINATE)
                        {
                            lock (_monitorSlave) {
                                Monitor.Wait(_monitorSlave);
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < _writeThread.Count; i++)
                        {
                            if (_writeThread [i] != null)
                            {
                                _writeThread [i].Add(pkg.getAckMessage());
                            }
                        }
                    }
                    if (eventPackageReceived != null)
                    {
                        eventPackageReceived(pkg);
                    }
                }
                // all other messages that are not message_read (STATE_CHANGE, DEVICE_ADDRESS ecc) are initializing events
            }
            else if (eventLocalMessageReceived != null)                 //local message
            {
                eventLocalMessageReceived(msg);
            }
        }