示例#1
0
 /// <summary>
 /// Start broker
 /// </summary>
 public void Start()
 {
     this.commLayer.Start();
     this.publisherManager.Start();
     Console.WriteLine("Broker Started");
     MqttCommon.LogMessageToFile("Broker Started");
 }
示例#2
0
        void Client_MqttMsgSubscribeReceived(object sender, MqttMsgSubscribeEventArgs e)
        {
            MqttClient client = (MqttClient)sender;
            //client.Subscribe(e.Topics, e.QoSLevels);

            int i = 0;

            //for (int i = 0; i < e.Topics.Length; i++)
            //{
            commBroker = new CommBroker();
            commBroker.connect(e.Topics[i]);
            if (!commBroker.connected)
            {
                throw new Exception("Unable to connect the broker for subscribing message");
            }
            commBroker.disable_heartbeat = true;
            MqttCommon.LogMessageToFile("Subscribed to:" + e.Topics[i].Replace("/", ".").Replace("+", "*").ToString());
            MqttCommon.LogMessageToFile("Topics Subscribed :" + e.Topics.Length.ToString());
            Subscription subscription = commBroker.new_subscription(e.Topics[i].Replace("/", ".").Replace("+", "*"), string.Empty, (message, context) =>
            {
                client.Publish(message.subject.Replace(".", "/"), Convert.FromBase64String(message.data.ToString()));   // System.Text.Encoding.UTF8.GetBytes(message.data.ToString()));
                Console.WriteLine("Message Received");
                MqttCommon.LogMessageToFile("Message Received");
                Console.WriteLine("From Topic:" + message.subject.Replace(".", "/") + ". Data:" + message.data.ToString());
                MqttCommon.LogMessageToFile("From Topic:" + message.subject.Replace(".", "/") + ". Data:" + message.data.ToString());
            }, null, commBroker, string.Empty);

            // TODO : business logic to grant QoS levels based on some conditions ?
            //        now the broker granted the QoS levels requested by client

            // subscribe client for each topic and QoS level requested
            //this.subscriberManager.Subscribe(e.Topics[i].Replace("/", ".").Replace("+", "*"), e.QoSLevels[i], client);
            //}

            try
            {
                // send SUBACK message to the client
                client.Suback(e.MessageId, e.QoSLevels);

                //for (int i = 0; i < e.Topics.Length; i++)
                //{
                //    // publish retained message on the current subscription
                //    //this.publisherManager.PublishRetaind(e.Topics[i], client.ClientId);
                //}
            }
            catch (MqttCommunicationException)
            {
                this.CloseClient(client);
            }
        }
示例#3
0
        void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            try
            {
                Console.WriteLine("Message Published");

                //DmMessage dmMessage = null;
                string ipAddress;
                commBroker = new CommBroker();

                byte[]     publishMessageByte;
                MqttClient client = (MqttClient)sender;
                ipAddress = client.RemoteEndPoint.ToString().Substring(8,
                                                                       (client.RemoteEndPoint.ToString().IndexOf(']') - 8));
                //publishMessageString = Convert.ToBase64String(e.Message) + ipAddress;
                //publishMessageByte = Encoding.UTF8.GetBytes(publishMessageString); //Encoding.ASCII.GetBytes(publishMessageString);
                DmMessage dmMessage = new DmMessage();
                dmMessage.publicIpAddress = ipAddress;
                dmMessage.data            = Convert.ToBase64String(e.Message);
                dmMessage.ToString();
                var publishMessageString = Newtonsoft.Json.JsonConvert.SerializeObject(dmMessage);
                publishMessageByte = Encoding.UTF8.GetBytes(publishMessageString.ToString());

                CommMessage message = new CommMessage()
                {
                    subject        = e.Topic.Replace("/", "."),
                    data           = publishMessageString,
                    data_length    = publishMessageString.Length,
                    message_type   = "string",
                    correlation_id = "",
                    reply_to       = ""
                };

                // create PUBLISH message to publish
                // [v3.1.1] DUP flag from an incoming PUBLISH message is not propagated to subscribers
                //          It should be set in the outgoing PUBLISH message based on transmission for each subscriber
                //MqttMsgPublish publish = new MqttMsgPublish(e.Topic, publishMessageByte, false, e.QosLevel, e.Retain);

                // publish message through publisher manager
                //this.publisherManager.Publish(publish);
                commBroker.connect(e.Topic);
                MqttCommon.LogMessageToFile("Connected to RabbitMQ");
                commBroker.publish(message, false);
                MqttCommon.LogMessageToFile("Topic Name:" + message.subject.ToString());
                MqttCommon.LogMessageToFile("Message Published:" + publishMessageString.ToString());
                MqttCommon.LogMessageToFile("Message Published");
                commBroker.disconnect();
            }
            catch (Exception ex) { MqttCommon.LogMessageToFile("Error:Message Publish - Failed"); }
        }
示例#4
0
        /// <summary>
        /// Stop broker
        /// </summary>
        public void Stop()
        {
            this.commLayer.Stop();
            this.publisherManager.Stop();

            // close connection with all clients
            lock (clients)
            {
                foreach (MqttClient client in this.clients)
                {
                    client.Close();
                }
            }
            Console.WriteLine("Broker Stopped");
            MqttCommon.LogMessageToFile("Broker Stopped");
        }
示例#5
0
        void commLayer_ClientConnected(object sender, MqttClientConnectedEventArgs e)
        {
            // register event handlers from client
            e.Client.MqttMsgDisconnected        += Client_MqttMsgDisconnected;
            e.Client.MqttMsgPublishReceived     += Client_MqttMsgPublishReceived;
            e.Client.MqttMsgConnected           += Client_MqttMsgConnected;
            e.Client.MqttMsgSubscribeReceived   += Client_MqttMsgSubscribeReceived;
            e.Client.MqttMsgUnsubscribeReceived += Client_MqttMsgUnsubscribeReceived;
            e.Client.ConnectionClosed           += Client_ConnectionClosed;

            lock (clients)
            {
                // add client to the collection
                this.clients.Add(e.Client);
            }

            // start client threads
            e.Client.Open();
            MqttCommon.LogMessageToFile("Broker - Client Connected");
        }