Esempio n. 1
0
        /// <summary>
        /// Occurs when a message is received.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMessageReceived(object sender, Messages.MqttMsgPublishEventArgs e)
        {
            try
            {
                // Did we receive a keygen response?
                if (e.Topic == "emitter/keygen/")
                {
                    // Deserialize the response
                    var response = KeygenResponse.FromBinary(e.Message);
                    if (response == null || response.Status != 200)
                    {
                        this.InvokeError(response.Status);
                        return;
                    }

                    // Call the response handler we have registered previously
                    if (this.KeygenHandlers.ContainsKey(response.Channel))
                    {
                        ((KeygenHandler)this.KeygenHandlers[response.Channel])(response);
                    }
                    return;
                }
            }
            catch (Exception ex)
            {
                this.InvokeError(ex);
            }

            // Invoke every handler matching the channel
            foreach (MessageHandler handler in this.Trie.Match(e.Topic))
            {
                handler(e.Topic, e.Message);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Append the key.
        /// </summary>
        static void WriteKey( KeygenResponse k)
        {
            // Format .NET friendly name
            var keyName = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(
                k.Channel.Replace("-", " ")
                ).Replace(" ", "");

            Builder.AppendFormat("\t\tpublic static readonly string {0} = \"{1}\";", keyName, k.Key);
            Builder.AppendLine();
            Console.WriteLine("{0} = {1}", keyName, k.Key);
        }
Esempio n. 3
0
        /// <summary>
        /// Occurs when a message is received.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private Task OnMessageReceived(MqttApplicationMessageReceivedEventArgs e)
        {
            return(Task.Run(() =>
            {
                try
                {
                    if (!e.ApplicationMessage.Topic.StartsWith("emitter"))
                    {
                        var handlers = this.Trie.Match(e.ApplicationMessage.Topic);
                        // Invoke every handler matching the channel
                        foreach (MessageHandler handler in handlers)
                        {
                            handler(e.ApplicationMessage.Topic, e.ApplicationMessage.Payload);
                        }

                        if (handlers.Count == 0)
                        {
                            DefaultMessageHandler?.Invoke(e.ApplicationMessage.Topic, e.ApplicationMessage.Payload);
                        }
                        return;
                    }

                    // Did we receive a keygen response?
                    if (e.ApplicationMessage.Topic == "emitter/keygen/")
                    {
                        // Deserialize the response
                        var response = KeygenResponse.FromBinary(e.ApplicationMessage.Payload);
                        if (response == null || response.Status != 200)
                        {
                            this.InvokeError(response.Status);
                            return;
                        }

                        // Call the response handler we have registered previously
                        // TODO: get rid of the handler afterwards, or refac keygen
                        if (this.KeygenHandlers.ContainsKey(response.Channel))
                        {
                            ((KeygenHandler)this.KeygenHandlers[response.Channel])(response);
                        }
                        return;
                    }

                    if (e.ApplicationMessage.Topic == "emitter/presence/")
                    {
                        var presenceEvent = PresenceEvent.FromBinary(e.ApplicationMessage.Payload);

                        var handlers = this.PresenceTrie.Match(presenceEvent.Channel);
                        // Invoke every handler matching the channel
                        foreach (PresenceHandler handler in handlers)
                        {
                            handler(presenceEvent);
                        }

                        if (handlers.Count == 0)
                        {
                            DefaultPresenceHandler?.Invoke(presenceEvent);
                        }

                        return;
                    }

                    if (e.ApplicationMessage.Topic == "emitter/error/")
                    {
                        var errorEvent = ErrorEvent.FromBinary(e.ApplicationMessage.Payload);
                        var emitterException = new EmitterException((EmitterEventCode)errorEvent.Status, errorEvent.Message);

                        InvokeError(emitterException);
                        return;
                    }

                    if (e.ApplicationMessage.Topic == "emitter/me/")
                    {
                        var meResponse = MeResponse.FromBinary(e.ApplicationMessage.Payload);
                        Me?.Invoke(meResponse);

                        return;
                    }
                    return;
                }
                catch (Exception ex)
                {
                    this.InvokeError(ex);
                    return;
                }
            }));
        }