Beispiel #1
0
        private void HandleReceivedLetter(ILetter receivedLetter, AckState ackState)
        {
            switch (receivedLetter.Type)
            {
            case LetterType.Initialize:
                RemoteNodeId = new Guid(receivedLetter.Parts[0]);
                HandleInitialize();
                break;

            case LetterType.Shutdown:
                _remoteShutdownRequested = true;
                ChannelDisconnecting(this, ShutdownReason.Remote);
                break;

            case LetterType.User:
                Received(receivedLetter, CreateReceivedEventArgs(ackState));
                break;

            case LetterType.Batch:
                for (int i = 0; i < receivedLetter.Parts.Length; i++)
                {
                    ILetter batchedLetter = _letterDeserializer.Deserialize(receivedLetter.Parts[i]);
                    Received(batchedLetter, CreateReceivedEventArgs(ackState));
                }
                break;
            }
        }
Beispiel #2
0
        private void InsertPersistentMessage(PeerId peerId, DateTime timestamp, AckState ackState = AckState.Acked)
        {
            var message = new PersistentMessage
            {
                PeerId   = peerId.ToString(),
                BucketId = BucketIdHelper.GetBucketId(timestamp),
                IsAcked  = ackState == AckState.Acked,
                UniqueTimestampInTicks = timestamp.Ticks,
                TransportMessage       = new byte[0]
            };

            DataContext.PersistentMessages.Insert(message).Execute();
        }
Beispiel #3
0
 private static void SendCallback(this AckState ackState, IAsyncResult asyncResult)
 {
     ackState.AsyncResult = asyncResult;
     ackState.TryAndLog(
         ack =>
     {
         var bytesSent = ack.Handler.EndSend(ack.AsyncResult);
         Log.Success(
             "Sent {0} bytes from {1} connected to {2}",
             bytesSent,
             ack.Handler.LocalEndPoint,
             ack.Handler.RemoteEndPoint
             );
     }
         );
 }
Beispiel #4
0
        private static void BeginSend(this AckState ackState)
        {
            ackState.TryAndLog(
                ack =>
            {
                AckQueueDictionary.AddOrUpdate(
                    ack.RemoteEndPoint,
                    key => ack.Queue,
                    (key, current) =>
                {
                    current.CompleteAdding();
                    return(ack.Queue);
                }
                    );
                while (!ack.Queue.IsCompleted)
                {
                    var command = ack.Queue.Take();
                    Command.Command ackCommand;
                    try
                    {
                        switch (command.Operation)
                        {
                        case Operation.Connect:
                            ackCommand = GetAckCommand(
                                command,
                                value: 1
                                );
                            break;

                        case Operation.WiringPiSetupPiFace:
                            ackCommand = GetAckCommand(
                                command,
                                value: PiFace.WiringPiSetupPiFace()
                                );
                            break;

                        case Operation.PullUpDnControl:
                            ackCommand = GetAckCommand(
                                command,
                                value: PiFace.PullUpDnControl(command.Pin, command.Value)
                                );
                            break;

                        case Operation.DigitalWrite:
                            ackCommand = GetAckCommand(
                                command,
                                value: PiFace.DigitalWrite(command.Pin, command.Value)
                                );
                            break;

                        case Operation.DigitalRead:
                            ackCommand = GetAckCommand(
                                command,
                                value: PiFace.DigitalRead(command.Pin)
                                );
                            break;

                        case Operation.MonitorInput:
                            var monitor = MonitorInputDictionary.AddOrUpdate(
                                ackState.RemoteEndPoint,
                                key =>
                            {
                                var value          = new Command.Command[8];
                                value[command.Pin] = command;
                                return(value);
                            },
                                (key, value) =>
                            {
                                value[command.Pin] = command;
                                return(value);
                            }
                                );
                            var result = monitor[command.Pin];
                            ackCommand = GetAckCommand(
                                command,
                                value: (result.Id == command.Id && command.Value == result.Value)
                                                   ? 1
                                                   : 0
                                );
                            if (PiFace.InputChanged == null)
                            {
                                PiFace.InputChanged = MonitorInputChanged;
                            }
                            break;

                        case Operation.MonitorInputChanged:
                            ackCommand = command;
                            break;

                        // ReSharper disable RedundantCaseLabel
                        case Operation.None:
                        default:
                            // ReSharper restore RedundantCaseLabel
                            ackCommand = GetAckCommand(
                                command,
                                operation: Operation.None,
                                value: PiFace.DigitalRead(command.Pin)
                                );
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.LogException();
                        ackCommand = GetAckCommand(
                            command,
                            value: -1
                            );
                    }

                    var ackCommandBytes = ackCommand.ToArray();
                    Log.Info(
                        "Sending {0} bytes from {1} connected to {2}",
                        ackCommandBytes.Length,
                        ack.Handler.LocalEndPoint,
                        ack.Handler.RemoteEndPoint
                        );
                    ackState.Handler.BeginSend(
                        ackCommandBytes,
                        0,
                        ackCommandBytes.Length,
                        0,
                        ackState.SendCallback,
                        null
                        );
                }
            }
                );
        }
Beispiel #5
0
 private ReceivedEventArgs CreateReceivedEventArgs(AckState ackState)
 {
     return(new ReceivedEventArgs {
         AckState = ackState, RemoteNodeId = RemoteNodeId
     });
 }
Beispiel #6
0
        private void HandleReceivedLetter(ILetter receivedLetter, AckState ackState)
        {
            switch(receivedLetter.Type) {
                case LetterType.Initialize:
                    RemoteNodeId = new Guid(receivedLetter.Parts[0]);
                    HandleInitialize();
                    break;

                case LetterType.Shutdown:
                    _remoteShutdownRequested = true;
                    ChannelDisconnecting(this, ShutdownReason.Remote);
                    break;

                case LetterType.User:
                    Received(receivedLetter, CreateReceivedEventArgs(ackState));
                    break;

                case LetterType.Batch:
                    for(int i = 0; i < receivedLetter.Parts.Length; i++) {
                        ILetter batchedLetter = _letterDeserializer.Deserialize(receivedLetter.Parts[i]);
                        Received(batchedLetter, CreateReceivedEventArgs(ackState));
                    }
                    break;
            }
        }
Beispiel #7
0
 private ReceivedEventArgs CreateReceivedEventArgs(AckState ackState)
 {
     return new ReceivedEventArgs {AckState = ackState, RemoteNodeId = RemoteNodeId};
 }