void IMessageChannel.SendInMessage(Message message) { if (!_inputChannel.IsOpened) { _inputChannel.Open(); } _inputChannel.SendInMessage(message); }
private void AdapterOnNewOutMessage(Message message) { if (!_outputChannel.IsOpened) { _outputChannel.Open(); } _outputChannel.SendInMessage(message); }
public static void SendAndReceiveSingleMessage(IMessageChannel channel) { channel.Open(); channel.Clear(); var m = GetNewMessage(); channel.Send(m); var m2 = channel.ReceiveSingle(TimeSpan.MaxValue); Assert.NotNull(m2); Assert.AreEqual(m.Identifier, m2.Identifier); channel.Close(); }
public static void Clear(IMessageChannel channel) { channel.Open(); for (var i = 0; i < 5; i++) { var m = new FakeMessage(); channel.Send(m); } channel.Clear(); var messages = channel.ReceiveMany(5, TimeSpan.MaxValue); Assert.AreEqual(0, messages.Count()); channel.Close(); }
public static void ReceiveTimeout(IMessageChannel channel) { var ts = new TimeSpan(0, 0, 0, 0, 500); channel.Open(); channel.Clear(); var m = new FakeMessage(); var m2 = new FakeMessage(); channel.Send(m); channel.Send(m2); var count = 0; foreach (var msg in channel.ReceiveMany(2, ts)) { count++; Thread.Sleep(500); } Assert.AreEqual(1, count); }
public static void TestSendingMessageOnClosedTransport(IMessageChannel channel) { channel.Open(); channel.Close(); var m = GetNewMessage(); Assert.Throws(typeof(InvalidOperationException), () => channel.Send(m)); }
public static void StateTransitions(IMessageChannel channel) { Assert.AreNotEqual(ChannelState.Closed, channel.State); var newState = channel.Open(); channel.Clear(); Assert.AreEqual(ChannelState.Open, newState); newState = channel.Close(); Assert.AreEqual(ChannelState.Closed, newState); }
public static void TestThroughputSynchronously( IMessageChannel channel, int howManyMessages, int? maxMessagesToReceive) { var start = DateTime.Now; channel.Open(); Console.WriteLine("Sending {0} messages through the {1} channel", howManyMessages, channel.GetType().FullName); SendMessages(channel, howManyMessages); Console.WriteLine("Sent {0} messages in {1} seconds", howManyMessages, DateTime.Now.Subtract(start).TotalSeconds); start = DateTime.Now; var receivedMessageCount = 0; var numberTimesToLoop = 1; if (maxMessagesToReceive.HasValue) { numberTimesToLoop = howManyMessages / maxMessagesToReceive.Value + 1; } else { maxMessagesToReceive = howManyMessages; } for (var i = 0; i < numberTimesToLoop; i++) { foreach (var message in channel.ReceiveMany(maxMessagesToReceive.Value, TimeSpan.MaxValue)) { receivedMessageCount++; } if (howManyMessages - receivedMessageCount < maxMessagesToReceive) { maxMessagesToReceive = howManyMessages - receivedMessageCount; } } channel.Close(); Console.WriteLine("Received {0} messages in {1}", receivedMessageCount, DateTime.Now.Subtract(start).TotalSeconds); }
public static void TestThroughputAsynchronously( IMessageChannel channel, int howManyMessages, int howManyThreads, int? maxMessagesToReceive = null) { channel.Open(); var start = DateTime.Now; var numberTimesToLoop = 1; if (maxMessagesToReceive.HasValue) { var numberMessagesPerThread = howManyMessages / howManyThreads + 2; do { numberMessagesPerThread--; numberTimesToLoop = howManyMessages / (numberMessagesPerThread * howManyThreads) + 1; } while (numberMessagesPerThread > maxMessagesToReceive); Assert.LessOrEqual(numberMessagesPerThread, maxMessagesToReceive); maxMessagesToReceive = numberMessagesPerThread; } else { maxMessagesToReceive = howManyMessages / howManyThreads + 1; } Console.WriteLine( "Sending {0} messages through the {1} channel across {2} threads in batches of {3}", maxMessagesToReceive * howManyThreads * numberTimesToLoop, channel.GetType().FullName, howManyThreads, maxMessagesToReceive); for (var i = 0; i < numberTimesToLoop; i++) { var results = Parallel.For( 0, howManyThreads, x => { SendMessages(channel, maxMessagesToReceive.Value); channel.ReceiveMany(maxMessagesToReceive.Value, TimeSpan.MaxValue); }); Assert.True(results.IsCompleted); } Console.WriteLine( "Received {0} messages in {1} seconds", maxMessagesToReceive * howManyThreads * numberTimesToLoop, DateTime.Now.Subtract(start).TotalSeconds); channel.Close(); }
public Task Communication(CancellationToken cancellationToken) { return(Task.Run(() => { try { var lastHeartBeat = Stopwatch.StartNew(); _messageChannel.Open(); Dictionary <int, int> lastMsgIdFromNodes = new Dictionary <int, int>(); var retryList = new Dictionary <int, RetryOutgoingMessage>(); while (!cancellationToken.IsCancellationRequested) { //Read everything that can be Logger.Trace(() => "Try read from channel"); var inMsg = _messageChannel.TryReadNext(); while (inMsg != null) { //Process SendingStatus if (inMsg is SendingStatusReport sc) { Logger.Debug(() => $"Sending report for msg {sc.MessageId} found in channel with status {(sc.Success ? "OK" : "NOK")}."); if (!retryList.ContainsKey(sc.MessageId)) { Logger.Warn(() => $"MessageId {sc.MessageId} not found in retry list. Ignoring"); } else { var retryMsg = retryList[sc.MessageId]; if (sc.Success) { _incomingMessages.Add(new ActionConfirmedReport { Rssi = inMsg.Rssi, FromRfAddress = inMsg.FromRfAddress, ConfigmedAction = retryMsg.Message }); retryList.Remove(sc.MessageId); } else if (retryMsg.RetryCount >= retryMsg.MaxRetry) { retryList.Remove(sc.MessageId); Logger.Warn(() => $"Last try failed for message with id {sc.MessageId}. Removed from retryQueue."); } else { retryMsg.NextTry = _timeProvider.UtcNow.AddMilliseconds(1000); retryMsg.ReadyForRetry = true; } } } //Process other incoming messages else { if (inMsg is NodeStartedReport) { lastMsgIdFromNodes[inMsg.FromRfAddress] = -1; } if (lastMsgIdFromNodes.ContainsKey(inMsg.FromRfAddress) && lastMsgIdFromNodes[inMsg.FromRfAddress] == inMsg.MsgId) { Logger.Info(() => $"Message with Id {inMsg.MsgId} coming from RFAddr {inMsg.FromRfAddress} was already added in queue and will be dismissed."); } else { Logger.Debug(() => $"Message of type {inMsg.GetType().Name} found in channel. Will enqueue."); _incomingMessages.Add(inMsg, cancellationToken); lastMsgIdFromNodes[inMsg.FromRfAddress] = inMsg.MsgId; } } inMsg = _messageChannel.TryReadNext(); } //HeartBear if (lastHeartBeat.ElapsedMilliseconds > 3000) { lastHeartBeat.Restart(); _messageChannel.Send(new HeartBeatCommand { ToRfAddress = 1 }); } //Write any left message from outgoingQueue Logger.Trace(() => "Try read from Queue"); while (!_outgoingMessages.IsCompleted && _outgoingMessages.Count > 0) { if (_outgoingMessages.TryTake(out var outMsg, 10)) { Logger.Debug(() => $"Message of type {outMsg.GetType().Name} with id {outMsg.MessageId} found in queue. Will send."); _messageChannel.Send(outMsg); retryList.Add(outMsg.MessageId, new RetryOutgoingMessage(outMsg)); } } //Retry Logger.Trace(() => "Retry sendings"); var pivot = _timeProvider.UtcNow; foreach (var retryMsg in retryList.Values) { if (retryMsg.ReadyForRetry && retryMsg.NextTry < pivot) { Logger.Debug(() => $"Will retry messageId {retryMsg.Message.MessageId} ({retryMsg.Message.GetType().Name})"); _messageChannel.Send(retryMsg.Message); retryMsg.ReadyForRetry = false; retryMsg.RetryCount++; } } } _incomingMessages.CompleteAdding(); _messageChannel.Close(); } catch (Exception e) { Logger.Error(e, $"Exception in Communication Task : {e.Message}"); } }, cancellationToken)); }