예제 #1
0
        private string SendMessage(string msg, UpDevice target, bool waitForResponse = true)
        {
            UpNetworkInterface netInt         = GetAppropriateInterface(target);
            string             networkAddress = netInt.networkAddress;
            string             networkType    = netInt.netType;

            ClientConnection con = OpenActiveConnection(networkAddress, networkType);

            if (con == null)
            {
                logger.LogWarning("Not possible to stablish a connection with '" + networkAddress + "' of type '" + networkType + "'.");
                return(null);
            }

            try
            {
                byte[] data = Encoding.UTF8.GetBytes(msg);
                con.Write(data, 0, data.Length);


                // Gets response.
                string response = null;
                if (waitForResponse)
                {
                    data = con.Read();
                    if (data != null)
                    {
                        response = Encoding.UTF8.GetString(data);
                        if (response.Trim().Length == 0)
                        {
                            response = null;
                        }
                    }
                }
                con.Close();
                return(response);
            }
            catch (System.Exception)
            {
                if (con.connected)
                {
                    con.Close();
                }
                throw;
            }
        }
예제 #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);
                }
            }
        }
예제 #3
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);
                }
            }
        }