A STOMP protocol message
        /// <summary>
        ///   Sends a 'SEND' message
        /// </summary>
        public static void Send(this IStompClient client, string message, string destination)
        {
            var stompMsg = new StompMessage("SEND", message);
            stompMsg["destination"] = destination;

            client.Send(stompMsg);
        }
        /// <summary>
        ///   Sends an 'UNSUBSCRIBE' message
        /// </summary>
        public static void UnSubscribe(this IStompClient client, string destination)
        {
            var stompMsg = new StompMessage("UNSUBSCRIBE");
            stompMsg["destination"] = destination;

            client.Send(stompMsg);
        }
Example #3
0
        /// <summary>
        ///   Sends an 'UNSUBSCRIBE' message
        /// </summary>
        public static void UnSubscribe(this IStompClient client, string destination)
        {
            var stompMsg = new StompMessage("UNSUBSCRIBE");

            stompMsg["destination"] = destination;

            client.Send(stompMsg);
        }
Example #4
0
        /// <summary>
        /// Handles the SEND message
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="message">The message.</param>
        private void OnStompSend(IStompClient client, StompMessage message)
        {
            var destination = message["destination"];

            var queue = _queues.FirstOrDefault(s => s.Address == destination) ?? AddNewQueue(destination);

            queue.Publish(message.Body);
        }
Example #5
0
        /// <summary>
        ///   Sends a 'SEND' message
        /// </summary>
        public static void Send(this IStompClient client, string message, string destination)
        {
            var stompMsg = new StompMessage("SEND", message);

            stompMsg["destination"] = destination;

            client.Send(stompMsg);
        }
Example #6
0
        /// <summary>
        /// Handles the SUBSCRIBE message
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="message">The message.</param>
        private void OnStompSubscribe(IStompClient client, StompMessage message)
        {
            string destination = message["destination"];

            var queue = _queues.FirstOrDefault(s => s.Address == destination) ?? AddNewQueue(destination);

            queue.AddClient(client, message["id"]);
        }
Example #7
0
        /// <summary>
        /// Handles the CONNECT message
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="message">The message.</param>
        private static void OnStompConnect(IStompClient client, StompMessage message)
        {
            var result = new StompMessage("CONNECTED");

            result["session-id"] = client.SessionId.ToString();

            client.Send(result);
        }
Example #8
0
        private void SendMessage(IStompClient client, string body, Guid messageId, string subscriptionId)
        {
            Log.Info(string.Format("Sending message to {0}", client.SessionId));
            Log.Debug(string.Format("message {0}", body));

            var stompMessage = new StompMessage("MESSAGE", body);

            stompMessage["message-id"]  = messageId.ToString();
            stompMessage["destination"] = Address;

            if (!string.IsNullOrEmpty(subscriptionId))
            {
                stompMessage["subscription"] = subscriptionId;
            }

            client.Send(stompMessage);
        }
Example #9
0
        /// <summary>
        /// Handles the UNSUBSCRIBE message
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="message">The message.</param>
        private void OnStompUnsubscribe(IStompClient client, StompMessage message)
        {
            string destination = message["destination"];

            if (string.IsNullOrEmpty(destination))
            {
                return;
            }
            var queue = _queues.FirstOrDefault(q => q.Address == destination);

            if (queue == null || queue.Clients.Contains(client) == false)
            {
                client.Send(new StompMessage("ERROR", "You are not subscribed to queue '" + destination + "'"));
                return;
            }

            queue.RemoveClient(client);
        }
Example #10
0
        /// <summary>
        ///   Serializes the specified message.
        /// </summary>
        /// <param name = "message">The message.</param>
        /// <returns>A serialized version of the given <see cref="StompMessage"/></returns>
        public string Serialize(StompMessage message)
        {
            var buffer = new StringBuilder();

            buffer.Append(message.Command + "\n");

            if (message.Headers != null)
            {
                foreach (var header in message.Headers)
                {
                    buffer.Append(header.Key + ":" + header.Value + "\n");
                }
            }

            buffer.Append("\n");
            buffer.Append(message.Body);
            buffer.Append('\0');

            return(buffer.ToString());
        }
        /// <summary>
        ///   Serializes the specified message.
        /// </summary>
        /// <param name = "message">The message.</param>
        /// <returns>A serialized version of the given <see cref="StompMessage"/></returns>
        public string Serialize(StompMessage message)
        {
            var buffer = new StringBuilder();

            buffer.Append(message.Command + "\n");

            if (message.Headers != null)
            {
                foreach (var header in message.Headers)
                {
                    buffer.Append(header.Key + ":" + header.Value + "\n");
                }
            }

            buffer.Append("\n");
            buffer.Append(message.Body);
            buffer.Append('\0');

            return buffer.ToString();
        }
Example #12
0
        /// <summary>
        ///   Excutes the action assigned to the message command
        /// </summary>
        /// <param name = "client"></param>
        /// <param name = "message"></param>
        private void OnClientMessage(IStompClient client, StompMessage message)
        {
            if (message == null || message.Command == null)
            {
                return;
            }

            if (client.SessionId == Guid.Empty)
            {
                client.SessionId = Guid.NewGuid();
            }

            Log.Info(string.Format("Processing command: {0} from client {1}", message.Command, client.SessionId));

            if (!_actions.ContainsKey(message.Command))
            {
                Log.Warn(string.Format("Client {0} sended an unknown command: {1}", client.SessionId, message.Command));
                return;
            }

            if (message.Command != "CONNECT" && client.IsConnected() == false)
            {
                Log.Info(string.Format("Client {0} was not connected before sending command: {1}", client.SessionId, message.Command));

                client.Send(new StompMessage("ERROR", "Please connect before sending '" + message.Command + "'"));
                return;
            }

            _actions[message.Command](client, message);

            // when a receipt is request, we send a receipt frame
            if (message.Command == "CONNECT" || message["receipt"] == string.Empty)
            {
                return;
            }
            var response = new StompMessage("RECEIPT");

            response["receipt-id"] = message["receipt"];
            client.Send(response);
        }
        /// <summary>
        ///   Sends a 'CONNECT' message
        /// </summary>
        public static void Connect(this IStompClient client)
        {
            var stompMsg = new StompMessage("CONNECT");

            client.Send(stompMsg);
        }
Example #14
0
        /// <summary>
        ///   Sends a 'CONNECT' message
        /// </summary>
        public static void Connect(this IStompClient client)
        {
            var stompMsg = new StompMessage("CONNECT");

            client.Send(stompMsg);
        }
Example #15
0
        /// <summary>
        /// Handles the DISCONNECT message
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="message">The message.</param>
        public void OnStompDisconnect(IStompClient client, StompMessage message)
        {
            var stompQueues = _queues.Where(q => q.Clients.Contains(client)).ToList();

            stompQueues.ForEach(q => q.RemoveClient(client));
        }
Example #16
0
        private void SendMessage(IStompClient client, string body, Guid messageId, string subscriptionId)
        {
            Log.Info(string.Format("Sending message to {0}", client.SessionId));
            Log.Debug(string.Format("message {0}", body));

            var stompMessage = new StompMessage("MESSAGE", body);
            stompMessage["message-id"] = messageId.ToString();
            stompMessage["destination"] = Address;

            if (!string.IsNullOrEmpty(subscriptionId))
            {
                stompMessage["subscription"] = subscriptionId;
            }

            client.Send(stompMessage);
        }