예제 #1
0
        private void MessageProcessingLoop(object queueObj)
        {
            LocalMessageQueue <MessageBase> queue = queueObj as LocalMessageQueue <MessageBase>;

            try
            {
                while (!_cancellation.IsCancellationRequested)
                {
                    MessageBase message = queue.WaitUntilMessageCome();
                    if (null == message)
                    {
                        continue;
                    }
                    bool operationContinue = GetConsumer(message).HandleMessage(message);
                    // 如果消息执行后确认需要停止,则结束消息队列的处理。
                    if (!operationContinue)
                    {
                        break;
                    }
                }
                GlobalInfo.LogService.Print(LogLevel.Info, CommonConst.PlatformSession,
                                            $"Listen thread: {Thread.CurrentThread.Name} is stopped.");
            }
            catch (ThreadAbortException)
            {
                GlobalInfo.LogService.Print(LogLevel.Warn, CommonConst.PlatformLogSession,
                                            $"thread {Thread.CurrentThread.Name} is stopped abnormally");
            }
            catch (Exception ex)
            {
                GlobalInfo.EventQueue.Enqueue(new ExceptionEventInfo(ex));
                GlobalInfo.LogService.Print(LogLevel.Fatal, CommonConst.PlatformLogSession, ex);
                this.Stop();
            }
        }
        public async Task PubAndUnSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue(null);

            mq.Subscribe("topic", msg => { Interlocked.Increment(ref count); });

            int i = 0;

            Task.Factory.StartNew(async() =>
            {
                for (; i < 50; ++i)
                {
                    await mq.PublishAsync("topic", "a");
                    await Task.Delay(100);
                }
            }).ConfigureAwait(false).GetAwaiter();
            await Task.Delay(1500);

            mq.Unsubscribe("topic");

            while (i < 50)
            {
                await Task.Delay(100);
            }

            Assert.True(count < 100);
        }
예제 #3
0
        public MessageTransceiver(SlaveContext contextManager, int session)
        {
            this._slaveContext = contextManager;

            // 创建上行队列
            _formatterType = contextManager.GetProperty <FormatterType>("EngineQueueFormat");
            MessengerOption receiveOption = new MessengerOption(CoreConstants.UpLinkMQName, typeof(ControlMessage),
                                                                typeof(DebugMessage), typeof(RmtGenMessage), typeof(StatusMessage), typeof(TestGenMessage))
            {
                Type = contextManager.GetProperty <MessengerType>("MessengerType")
            };

            _uplinkMessenger = Messenger.GetMessenger(receiveOption);
            // 创建下行队列
            MessengerOption sendOption = new MessengerOption(CoreConstants.DownLinkMQName, typeof(ControlMessage),
                                                             typeof(DebugMessage), typeof(RmtGenMessage), typeof(StatusMessage), typeof(TestGenMessage))
            {
                Type = contextManager.GetProperty <MessengerType>("MessengerType")
            };

            _downLinkMessenger = Messenger.GetMessenger(sendOption);

            _messageQueue  = new LocalMessageQueue <MessageBase>(CoreConstants.DefaultEventsQueueSize);
            this.SessionId = session;
        }
예제 #4
0
        public MessageTransceiver(SlaveContext contextManager, int session)
        {
            this._slaveContext = contextManager;

            // 创建上行队列
            FormatterType   formatterType = contextManager.GetProperty <FormatterType>("EngineQueueFormat");
            MessengerOption receiveOption = new MessengerOption(CoreConstants.UpLinkMQName, GetMessageType)
            {
                Type        = contextManager.GetProperty <MessengerType>("MessengerType"),
                Formatter   = formatterType,
                ReceiveType = ReceiveType.Synchronous
            };

            _uplinkMessenger = Messenger.GetMessenger(receiveOption);
            // 创建下行队列
            MessengerOption sendOption = new MessengerOption(CoreConstants.DownLinkMQName, GetMessageType)
            {
                Type        = contextManager.GetProperty <MessengerType>("MessengerType"),
                Formatter   = formatterType,
                ReceiveType = ReceiveType.Synchronous
            };

            _downLinkMessenger = Messenger.GetMessenger(sendOption);

            _messageQueue  = new LocalMessageQueue <MessageBase>(CoreConstants.DefaultEventsQueueSize);
            this.SessionId = session;
        }
예제 #5
0
        public async Task PubAndSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue(CreateLogger <LocalMessageQueue>());

            mq.Subscribe("topic", msg =>
            {
                Interlocked.Increment(ref count);
                return(Task.CompletedTask);
            });
            for (int i = 0; i < 100; ++i)
            {
                await mq.PublishAsync("topic", "a");
            }

            int j = 0;

            while (count < 100 && j < 150)
            {
                Thread.Sleep(500);
                ++j;
            }

            Assert.Equal(100, count);
        }
예제 #6
0
        public void StartListen()
        {
            // 打印状态日志
            _context.LogSession.Print(LogLevel.Debug, _context.SessionId,
                                      $"Downlink message processer started, thread:{Thread.CurrentThread.ManagedThreadId}.");

            // 首先接收RmtGenMessage
            _messageQueue = _context.MessageTransceiver.MessageQueue;
            MessageBase   message       = _messageQueue.WaitUntilMessageCome();
            RmtGenMessage rmtGenMessage = (RmtGenMessage)message;

            if (null == rmtGenMessage)
            {
                throw new TestflowRuntimeException(ModuleErrorCode.InvalidMessageReceived,
                                                   _context.I18N.GetFStr("InvalidMessageReceived", message.GetType().Name));
            }
            _context.RmtGenMessage = rmtGenMessage;

            while (!_context.Cancellation.IsCancellationRequested)
            {
                message = _messageQueue.WaitUntilMessageCome();
                if (null == message)
                {
                    continue;
                }
                switch (message.Type)
                {
                case MessageType.Ctrl:
                    ProcessControlMessage((ControlMessage)message);
                    break;

                case MessageType.Debug:
                    ProcessDebugMessage((DebugMessage)message);
                    break;

                case MessageType.Sync:
                    ProcessSyncMessage((ResourceSyncMessage)message);
                    break;

                case MessageType.CallBack:
                    ProcessCallBackMessage((CallBackMessage)message);
                    break;

                // 暂未在Master端实现发送RuntimeError消息的错误
                case MessageType.RmtGen:
                case MessageType.RuntimeError:
                case MessageType.Status:
                case MessageType.TestGen:
                default:
                    throw new TestflowRuntimeException(ModuleErrorCode.InvalidMessageReceived,
                                                       _context.I18N.GetFStr("InvalidMessageReceived", message.GetType().Name));
                }
            }
            _context.LogSession.Print(LogLevel.Debug, _context.SessionId,
                                      $"Downlink message processor stopped, Thread:{Thread.CurrentThread.ManagedThreadId}");
        }
예제 #7
0
        public void LocalMessageQueue_SubscriberIgnoresMessages()
        {
            var queue      = new LocalMessageQueue <Int32>();
            var subscriber = new MockMessageSubscriber();

            queue.Subscribe(subscriber, 1);
            queue.Publish(2, null);
            queue.Process();

            TheResultingValue(subscriber.ReceivedMessage).ShouldBe(false);
        }
예제 #8
0
        public void LocalMessageQueue_UnsubscribeAllRemovesSubscriber()
        {
            var queue      = new LocalMessageQueue <Int32>();
            var subscriber = new MockMessageSubscriber();

            queue.Subscribe(subscriber, 1);
            queue.Unsubscribe(subscriber);
            queue.Publish(1, null);
            queue.Process();

            TheResultingValue(subscriber.ReceivedMessage).ShouldBe(false);
        }
예제 #9
0
        public async Task PubAndSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue();

            mq.Subscribe("topic", msg => { Interlocked.Increment(ref count); });
            for (int i = 0; i < 1000; ++i)
            {
                await mq.PublishAsync("topic", "a");
            }

            Thread.Sleep(2000);
            Assert.Equal(1000, count);
        }
        public void ParallelPubAndSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue(null);

            mq.Subscribe("topic", msg => { Interlocked.Increment(ref count); });

            Parallel.For(0, 100, async(i) => { await mq.PublishAsync("topic", "a"); });
            int j = 0;

            while (count < 100 && j < 150)
            {
                Thread.Sleep(500);
                ++j;
            }

            Assert.Equal(100, count);
        }
예제 #11
0
        public void ParallelPubAndSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue(CreateLogger <LocalMessageQueue>());

            mq.Subscribe <string>("topic", msg => { Interlocked.Increment(ref count); });

            Parallel.For(0, 100, async i =>
            {
                await mq.PublishAsync("topic", new MessageData <string>
                {
                    Data = "a"
                });
            });
            int j = 0;

            while (count < 100 && j < 150)
            {
                Thread.Sleep(500);
                ++j;
            }

            Assert.Equal(100, count);
        }
예제 #12
0
 private void TestStateMonitoring()
 {
     LocalMessageQueue <MessageBase> messageQueue = _slaveContext.MessageTransceiver.MessageQueue;
     // 首先接收RmtGenMessage
     MessageBase message = messageQueue.WaitUntilMessageCome();
 }
예제 #13
0
 public SyncMsgTransceiver(ModuleGlobalInfo globalInfo) : base(globalInfo)
 {
     _engineMessageQueue = new LocalMessageQueue <MessageBase>(Constants.DefaultEventsQueueSize);
     _statusMessageQueue = new LocalMessageQueue <MessageBase>(Constants.DefaultEventsQueueSize);
 }