Esempio n. 1
0
        private async void Run()
        {
            ServerSocket.Listen(200);

            while (!Exiting)
            {
                var client = await ServerSocket.AcceptAsync();

                var mqClient = new MessageQueueClient(client, OnMessage);

                Log.LogInformation("Client connected");

                Clients.Add(mqClient);

                mqClient.Start();
            }

            ServerSocket.Shutdown(SocketShutdown.Both);
            ServerSocket.Dispose();
        }
Esempio n. 2
0
        private void OnMessage(MessageQueueClient client, BaseMessage message)
        {
            if (message is ContentMessage)
            {
                if (message.QueueName.Contains("*"))
                {
                    Log.LogInformation("Message will be dispatched to wildcard queues");
                    foreach (var queue in GetQueuesByFilter(message.QueueName))
                    {
                        queue.Enqueue(new ContentMessage()
                        {
                            ConversationId = message.ConversationId,
                            Created        = message.Created,
                            Data           = ((ContentMessage)message).Data,
                            QueueName      = queue.Name,
                            SenderId       = message.SenderId
                        });
                    }
                }

                GetQueue(message.QueueName).Enqueue(message);
            }
            else if (message is SubscriptionMessage)
            {
                var subMsg = message as SubscriptionMessage;


                if (subMsg.Mode == SubscribeMode.PermanentConsumer || subMsg.Mode == SubscribeMode.TemporaryConsumer)
                {
                    Log.LogInformation($"Client subscription for queue {subMsg.QueueName} with type {subMsg.Mode}");
                    GetQueue(message.QueueName).Subscribe(message.SenderId, subMsg.Mode);
                }
                else if (subMsg.Mode == SubscribeMode.Remove)
                {
                    GetQueue(message.QueueName).Unsubscribe(message.SenderId);
                }
            }
            else if (message is DisconnectMessage)
            {
                if (Clients.Remove(client))
                {
                    foreach (var q in Queues.Where(x => !x.IsPermanentSubscriber(client.Info.Id)))
                    {
                        q.Unsubscribe(client.Info.Id);
                    }

                    client.Dispose();

                    Log.LogInformation($"Client {client.Info.Id} disconnected");
                }
            }
            else if (message is AnnounceMessage)
            {
                var announceMessage = message as AnnounceMessage;

                Log.LogInformation($@"Received announce from client; new Id {message.SenderId}.
Service : {announceMessage.ServiceName}
Node : {announceMessage.NodeName}
Client version : {announceMessage.ClientVersion}
Platform : {announceMessage.OperatingSystem}");
                client.Info.Id = message.SenderId;
            }
        }