コード例 #1
0
 bool Dispatch(string routingKey, IDictionary <string, object> headers, byte[] body, ulong deliveryTag)
 {
     if (routingKey != null && routingKey.Length > 0)
     {
         string MID = MessageRoutingKey.ParseMID(routingKey);
         if (MID == _MID && body != null && body.Length > 0)
         {
             if (MessageRoutingKey.IsDeviceCommand(routingKey))
             {
                 string  json    = Encoding.UTF8.GetString(body);
                 Command command = JsonConvert.DeserializeObject <Command>(json);
                 CommandSubscription <Command> cs = _commandSubscriptions.Find(s => s.CommandId == command.Id);
                 if (cs != null)
                 {
                     cs.CommandHandler(MID, command);
                     lock (_modelLock)
                     {
                         _model.BasicAck(deliveryTag, false);
                     }
                     return(true);
                 }
             }
             else if (MessageRoutingKey.IsApplicationPulse(routingKey))
             {
                 string            json  = Encoding.UTF8.GetString(body);
                 Pulse             pulse = JsonConvert.DeserializeObject <Pulse>(json);
                 PulseSubscription ps    = _pulseSubscriptions.Find(s => s.PulseId == pulse.PulseId);
                 if (ps != null)
                 {
                     ps.PulseHandler(MID, pulse.PulseId, pulse);
                     lock (_modelLock)
                     {
                         _model.BasicAck(deliveryTag, false);
                     }
                     return(true);
                 }
             }
         }
         lock (_modelLock)
         {
             _model.BasicNack(deliveryTag, false, false);
         }
     }
     return(false);
 }
コード例 #2
0
        /// <summary>
        /// Registers a new callback method that is called when pulse from a specified pulse identifier is received.
        /// </summary>
        /// <param name="pulseId">Pulse identifier.</param>
        /// <param name="pulseHandler">Callback method with argument Pulse (e.g. "void MyCallback(string MID, int pulseId, Pulse pulse) { ... }"</param>
        public void RegisterPulseHandler(int pulseId, Action <string, int, Pulse> pulseHandler)
        {
            PulseSubscription pulseSubscription = new PulseSubscription(_MID, pulseId, pulseHandler);

            _pulseSubscriptions.Add(pulseSubscription);
        }