Beispiel #1
0
        public static Notify FromJSON(object jsonObj)
        {
            Notify e = new Notify();
            Message.FromJSON(e, jsonObj);

            IDictionary<string, object> json = jsonObj as IDictionary<string, object>;

            e.eventKey = Util.JsonOptString(json, "eventKey");
            e.driver = Util.JsonOptString(json, "driver");
            e.instanceId = Util.JsonOptString(json, "instanceId");
            e.parameters = Util.JsonOptField(json, "parameters") as IDictionary<string, object>;

            return e;
        }
Beispiel #2
0
        private void HandleMessage(string message, ClientConnection connection)
        {
            if ((message == null) || ((message = message.Trim()).Length == 0) ||
                (connection == null) || (!connection.connected))
                return;

            NetworkDevice clientDevice = connection.clientDevice;
            Message response = null;
            try
            {
                logger.Log("Handling incoming message:\n" + message);
                object json = Json.Deserialize(message);
                string type = Util.JsonOptString(json as IDictionary<string, object>, "type");
                if (type != null)
                {
                    Message.Type messageType = (Message.Type)System.Enum.Parse(typeof(Message.Type), type, true);
                    switch (messageType)
                    {
                        case Message.Type.SERVICE_CALL_REQUEST:
                            logger.Log("Incoming Service Call");
                            CallContext messageContext = new CallContext();
                            messageContext.callerNetworkDevice = clientDevice;
                            response = HandleServiceCall(message, messageContext);
                            break;

                        case Message.Type.NOTIFY:
                            logger.Log("Incoming Notify");
                            HandleNotify(message, clientDevice);
                            break;

                        default:
                            break;
                    }
                }
            }
            catch (System.Exception ex)
            {
                PushLog("Failure to handle the incoming message. ", ex);

                response = new Notify();
                response.error = "Failure to handle the incoming message. ";
            }

            if (response != null)
            {
                string msg = Json.Serialize(response.ToJSON()) + "\n";
                byte[] bytes = Encoding.UTF8.GetBytes(msg);
                try
                {
                    connection.Write(bytes, 0, bytes.Length);
                    PushLog("Responded successfully.");
                }
                catch (System.Exception e)
                {
                    PushLog("Error while responding. ", e);
                }
            }
        }
Beispiel #3
0
        private void RemoteNotify(Notify notify, UpDevice device)
        {
            if (
                    device == null || notify == null ||
                    string.IsNullOrEmpty(notify.driver) ||
                    string.IsNullOrEmpty(notify.eventKey))
                throw new System.ArgumentException("Either the device or notification is invalid.");

            string message = Json.Serialize(notify.ToJSON());
            SendMessage(message, device, false);
        }
Beispiel #4
0
        private void HandleNotify(Notify notify, List<ListenerInfo> listeners, string eventIdentifier)
        {
            if ((listeners == null) || (listeners.Count == 0))
            {
                logger.Log("No listeners waiting for notify events for the key '" + eventIdentifier + "'.");
                return;
            }

            foreach (ListenerInfo li in listeners)
            {
                if (li.listener != null)
                    li.listener.HandleEvent(notify);
            }
        }
Beispiel #5
0
 public void Notify(Notify notify, UpDevice device)
 {
     if (IsLocalCall(device))
         HandleNotify(notify, device);
     else
         RemoteNotify(notify, device);
 }
Beispiel #6
0
        public void HandleNotify(Notify notify, UpDevice device)
        {
            if ((notify == null) || (notify.eventKey == null) || (notify.eventKey.Length == 0))
                logger.Log("No information in notify to handle.");

            if ((listenerMap == null) || (listenerMap.Count == 0))
            {
                logger.Log("No listeners waiting for notify events.");
                return;
            }

            //Notifying listeners from more specific to more general entries
            string eventIdentifier;
            List<ListenerInfo> listeners;

            // First full entries (device, driver, event, intanceId)
            eventIdentifier = GetEventIdentifier(device, notify.driver, notify.instanceId, notify.eventKey);
            if (listenerMap.TryGetValue(eventIdentifier, out listeners))
                HandleNotify(notify, listeners, eventIdentifier);

            // After less general entries (device, driver, event)
            eventIdentifier = GetEventIdentifier(device, notify.driver, null, notify.eventKey);
            if (listenerMap.TryGetValue(eventIdentifier, out listeners))
                HandleNotify(notify, listeners, eventIdentifier);

            // An then the least general entries (driver, event)
            eventIdentifier = GetEventIdentifier(null, notify.driver, null, notify.eventKey);
            if (listenerMap.TryGetValue(eventIdentifier, out listeners))
                HandleNotify(notify, listeners, eventIdentifier);
        }
Beispiel #7
0
        private void HandleMessage(string message, ClientConnection connection)
        {
            if ((message == null) || ((message = message.Trim()).Length == 0) ||
                (connection == null) || (!connection.connected))
            {
                return;
            }

            NetworkDevice clientDevice = connection.clientDevice;
            Message       response     = null;

            try
            {
                logger.Log("Handling incoming message:\n" + message);
                object json = Json.Deserialize(message);
                string type = Util.JsonOptString(json as IDictionary <string, object>, "type");
                if (type != null)
                {
                    Message.Type messageType = (Message.Type)System.Enum.Parse(typeof(Message.Type), type, true);
                    switch (messageType)
                    {
                    case Message.Type.SERVICE_CALL_REQUEST:
                        logger.Log("Incoming Service Call");
                        CallContext messageContext = new CallContext();
                        messageContext.callerNetworkDevice = clientDevice;
                        response = HandleServiceCall(message, messageContext);
                        break;

                    case Message.Type.NOTIFY:
                        logger.Log("Incoming Notify");
                        HandleNotify(message, clientDevice);
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (System.Exception ex)
            {
                PushLog("Failure to handle the incoming message. ", ex);

                response       = new Notify();
                response.error = "Failure to handle the incoming message. ";
            }

            if (response != null)
            {
                string msg   = Json.Serialize(response.ToJSON()) + "\n";
                byte[] bytes = Encoding.UTF8.GetBytes(msg);
                try
                {
                    connection.Write(bytes, 0, bytes.Length);
                    PushLog("Responded successfully.");
                }
                catch (System.Exception e)
                {
                    PushLog("Error while responding. ", e);
                }
            }
        }