Ejemplo n.º 1
0
        /// <summary>
        /// Called by radar whenever a device enters the space.
        /// </summary>
        /// <param name="device"></param>
        public void DeviceEntered(NetworkDevice device)
        {
            if (device == null)
                return;

            // verify if device entered is the current device
            string deviceHost = Util.GetHost(device.networkDeviceName);
            foreach (UpNetworkInterface networkInterface in this.currentDevice.networks)
            {
                string currentDeviceHost = Util.GetHost(networkInterface.networkAddress);
                if (deviceHost != null && deviceHost.Equals(currentDeviceHost))
                {
                    logger.Log("Host of device entered is the same of current device:" + device.networkDeviceName);
                    return;
                }
            }

            // verify if already know this device.
            UpDevice upDevice = RetrieveDevice(deviceHost, device.networkDeviceType);

            if (upDevice == null)
                // Does handshake on a new thread...
                new Thread(new ThreadStart(delegate()
                {
                    upDevice = DoHandshake(device);
                    if (upDevice != null)
                        DoDriversRegistry(device, upDevice);
                })).Start();
            else
                logger.Log("Already known device " + device.networkDeviceName);
        }
Ejemplo n.º 2
0
        public Response HandleServiceCall(Call serviceCall, CallContext messageContext)
        {
            NetworkDevice netDevice = messageContext.callerNetworkDevice;

            if (netDevice != null)
            {
                if (netDevice is LoopbackDevice)
                {
                    messageContext.callerDevice = currentDevice;
                }
                else
                {
                    string addr = Util.GetHost(netDevice.networkDeviceName);
                    string type = netDevice.networkDeviceType;
                    messageContext.callerDevice = deviceManager.RetrieveDevice(addr, type);
                }
            }

            if (IsApplicationCall(serviceCall))
            {
                if (app == null)
                {
                    throw new System.InvalidOperationException("No valid app instance set.");
                }
                return(reflectionServiceCaller.CallService(app, serviceCall, messageContext));
            }
            else
            {
                return(driverManager.HandleServiceCall(serviceCall, messageContext));
            }
        }
Ejemplo n.º 3
0
        public NetworkDevice GetAvailableNetworkDevice()
        {
            NetworkDevice device = passiveDevices[passiveIndex];

            passiveIndex = (passiveIndex + 1) % passiveDevices.Count;
            return(device);
        }
Ejemplo n.º 4
0
            public ServerThreadData(GatewayServer gatewayServer, NetworkDevice device)
            {
                this.gatewayServer = gatewayServer;
                this.device        = device;

                thread = new Thread(new ThreadStart(ConnectionThread));
                thread.Start();
            }
Ejemplo n.º 5
0
            public StreamConnectionThreadData(UnityGateway gateway, CallContext msgContext, NetworkDevice networkDevice)
            {
                this.gateway = gateway;

                this.thread        = new Thread(new ThreadStart(Run));
                this.msgContext    = msgContext;
                this.networkDevice = networkDevice;
            }
Ejemplo n.º 6
0
        private UpDevice DoHandshake(NetworkDevice device)
        {
            try
            {
                // Create a Dummy device just for calling it
                logger.Log("Trying to handshake with device : " + device.networkDeviceName);

                UpDevice dummyDevice = new UpDevice(device.networkDeviceName);
                dummyDevice.AddNetworkInterface(device.networkDeviceName, device.networkDeviceType);

                Call call = new Call(DEVICE_DRIVER_NAME, "handshake", null);
                call.AddParameter("device", Json.Serialize(currentDevice.ToJSON()));

                Response response = gateway.CallService(dummyDevice, call);
                if ((response != null) && string.IsNullOrEmpty(response.error))
                {
                    // in case of a success greeting process, register the device in the neighborhood database
                    object responseDevice = response.GetResponseData("device");
                    if (responseDevice != null)
                    {
                        UpDevice remoteDevice;
                        if (responseDevice is string)
                        {
                            remoteDevice = UpDevice.FromJSON(Json.Deserialize(responseDevice as string));
                        }
                        else
                        {
                            remoteDevice = UpDevice.FromJSON(responseDevice);
                        }

                        RegisterDevice(remoteDevice);
                        logger.Log("Registered device " + remoteDevice.name);

                        return(remoteDevice);
                    }
                    else
                    {
                        logger.LogError(
                            "Not possible complete handshake with device '" + device.networkDeviceName +
                            "' for no device on the handshake response.");
                    }
                }
                else
                {
                    logger.LogError(
                        "Not possible to handshake with device '" +
                        device.networkDeviceName +
                        (response == null ? ": No Response received." : "': Cause : " + response.error));
                }
            }
            catch (System.Exception e)
            {
                logger.Log(e.StackTrace);
                logger.LogError("Not possible to handshake with device '" + device.networkDeviceName + "'. " + e.Message);
            }

            return(null);
        }
Ejemplo n.º 7
0
        private void HandleStreamCall(Call serviceCall, CallContext messageContext)
        {
            if (serviceCall.serviceType == ServiceType.STREAM)
            {
                NetworkDevice networkDevice = messageContext.callerNetworkDevice;

                string host = Util.GetHost(networkDevice.networkDeviceName);
                for (int i = 0; i < serviceCall.channels; i++)
                {
                    ClientConnection con = gateway.OpenActiveConnection(host + ":" + serviceCall.channelIDs[i], serviceCall.channelType);
                    messageContext.AddConnection(con);
                }
            }
        }
Ejemplo n.º 8
0
        private void HandleNotify(string message, NetworkDevice clientDevice)
        {
            try
            {
                Notify   notify = Notify.FromJSON(Json.Deserialize(message));
                UpDevice device = gateway.RetrieveDevice(
                    Util.GetHost(clientDevice.networkDeviceName),
                    clientDevice.networkDeviceType);

                gateway.HandleNotify(notify, device);
            }
            catch (System.Exception e)
            {
                PushLog("Internal Failure. Notify cannot be handled. ", e);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Called by radar whenever a device enters the space.
        /// </summary>
        /// <param name="device"></param>
        public void DeviceEntered(NetworkDevice device)
        {
            if (device == null)
            {
                return;
            }

            // verify if device entered is the current device
            string deviceHost = Util.GetHost(device.networkDeviceName);

            foreach (UpNetworkInterface networkInterface in this.currentDevice.networks)
            {
                string currentDeviceHost = Util.GetHost(networkInterface.networkAddress);
                if (deviceHost != null && deviceHost.Equals(currentDeviceHost))
                {
                    logger.Log("Host of device entered is the same of current device:" + device.networkDeviceName);
                    return;
                }
            }

            // verify if already know this device.
            UpDevice upDevice = RetrieveDevice(deviceHost, device.networkDeviceType);

            if (upDevice == null)
            {
                // Does handshake on a new thread...
                new Thread(new ThreadStart(delegate()
                {
                    upDevice = DoHandshake(device);
                    if (upDevice != null)
                    {
                        DoDriversRegistry(device, upDevice);
                    }
                })).Start();
            }
            else
            {
                logger.Log("Already known device " + device.networkDeviceName);
            }
        }
Ejemplo n.º 10
0
        private void DoDriversRegistry(NetworkDevice device, UpDevice upDevice)
        {
            try
            {
                Response response = gateway.CallService(upDevice, new Call(DEVICE_DRIVER_NAME, "listDrivers"));
                if ((response != null) && (response.responseData != null) && (response.GetResponseData("driverList") != null))
                {
                    try
                    {
                        IDictionary <string, object> driversListMap = null;
                        object temp = response.GetResponseData("driverList");
                        if (temp is IDictionary <string, object> )
                        {
                            driversListMap = temp as IDictionary <string, object>; //TODO: Not tested. Why?
                        }
                        else
                        {
                            driversListMap = Json.Deserialize(temp.ToString()) as IDictionary <string, object>;
                        }

                        List <string> ids = new List <string>(driversListMap.Keys);
                        RegisterRemoteDriverInstances(upDevice, driversListMap, ids.ToArray());
                    }
                    catch (System.Exception e)
                    {
                        logger.LogError(
                            "Problems occurred while registering drivers from device '" + upDevice.name + "' . " + e.Message);
                    }
                }
            }
            catch (System.Exception)
            {
                logger.LogError(
                    "Not possible to discover services from device '" + device.networkDeviceName +
                    "'. Possibly not a uOS Device.");
            }
        }
Ejemplo n.º 11
0
        private StreamConnectionThreadData[] OpenStreamChannel(UpDevice device, Call serviceCall, CallContext messageContext)
        {
            StreamConnectionThreadData[] data = null;

            //Channel type decision
            string netType = null;

            if (serviceCall.channelType != null)
            {
                netType = serviceCall.channelType;
            }
            else
            {
                UpNetworkInterface network = GetAppropriateInterface(device);
                netType = network.netType;
            }

            int channels = serviceCall.channels;

            data = new StreamConnectionThreadData[channels];
            string[] channelIDs = new string[channels];

            for (int i = 0; i < channels; i++)
            {
                NetworkDevice networkDevice = GetAvailableNetworkDevice(netType);
                channelIDs[i] = Util.GetPort(networkDevice.networkDeviceName);
                StreamConnectionThreadData thread = new StreamConnectionThreadData(this, messageContext, networkDevice);
                thread.thread.Start();
                data[i] = thread;
            }

            serviceCall.channelIDs  = channelIDs;
            serviceCall.channelType = netType;

            return(data);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Called by radar whenever a device leaves the space.
        /// </summary>
        /// <param name="device"></param>
        public void DeviceLeft(NetworkDevice device)
        {
            if (device == null || device.networkDeviceName == null || device.networkDeviceType == null)
            {
                return;
            }

            // Remove what services this device has.
            logger.Log("Device " + device.networkDeviceName + " of type " + device.networkDeviceType + " leaving.");

            string          host = Util.GetHost(device.networkDeviceName);
            List <UpDevice> devices;

            lock (_devicedao_lock) { devices = deviceDao.List(host, device.networkDeviceType); }

            if ((devices != null) && (devices.Count > 0))
            {
                UpDevice           upDevice        = devices[0];
                List <DriverModel> returnedDrivers = driverManager.List(null, upDevice.name);
                if ((returnedDrivers != null) && (returnedDrivers.Count > 0))
                {
                    foreach (DriverModel rdd in returnedDrivers)
                    {
                        driverManager.Delete(rdd.id, rdd.device);
                    }
                }

                lock (_devicedao_lock) { deviceDao.Delete(upDevice.name); };

                logger.Log("Device '" + upDevice.name + "' left");
            }
            else
            {
                logger.Log("Device not found in database.");
            }
        }
Ejemplo n.º 13
0
        private void DoDriversRegistry(NetworkDevice device, UpDevice upDevice)
        {
            try
            {
                Response response = gateway.CallService(upDevice, new Call(DEVICE_DRIVER_NAME, "listDrivers"));
                if ((response != null) && (response.responseData != null) && (response.GetResponseData("driverList") != null))
                {
                    try
                    {
                        IDictionary<string, object> driversListMap = null;
                        object temp = response.GetResponseData("driverList");
                        if (temp is IDictionary<string, object>)
                            driversListMap = temp as IDictionary<string, object>; //TODO: Not tested. Why?
                        else
                            driversListMap = Json.Deserialize(temp.ToString()) as IDictionary<string, object>;

                        List<string> ids = new List<string>(driversListMap.Keys);
                        RegisterRemoteDriverInstances(upDevice, driversListMap, ids.ToArray());
                    }
                    catch (System.Exception e)
                    {
                        logger.LogError(
                            "Problems occurred while registering drivers from device '" + upDevice.name + "' . " + e.Message);
                    }
                }
            }
            catch (System.Exception)
            {
                logger.LogError(
                    "Not possible to discover services from device '" + device.networkDeviceName +
                    "'. Possibly not a uOS Device.");
            }
        }
Ejemplo n.º 14
0
            public ServerThreadData(GatewayServer gatewayServer, NetworkDevice device)
            {
                this.gatewayServer = gatewayServer;
                this.device = device;

                thread = new Thread(new ThreadStart(ConnectionThread));
                thread.Start();
            }
Ejemplo n.º 15
0
        private void HandleNotify(string message, NetworkDevice clientDevice)
        {
            try
            {
                Notify notify = Notify.FromJSON(Json.Deserialize(message));
                UpDevice device = gateway.RetrieveDevice(
                    Util.GetHost(clientDevice.networkDeviceName),
                    clientDevice.networkDeviceType);

                gateway.HandleNotify(notify, device);
            }
            catch (System.Exception e)
            {
                PushLog("Internal Failure. Notify cannot be handled. ", e);
            }
        }
Ejemplo n.º 16
0
            public StreamConnectionThreadData(UnityGateway gateway, CallContext msgContext, NetworkDevice networkDevice)
            {
                this.gateway = gateway;

                this.thread = new Thread(new ThreadStart(Run));
                this.msgContext = msgContext;
                this.networkDevice = networkDevice;
            }
Ejemplo n.º 17
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);
                }
            }
        }
Ejemplo n.º 18
0
        private UpDevice DoHandshake(NetworkDevice device)
        {
            try
            {
                // Create a Dummy device just for calling it
                logger.Log("Trying to handshake with device : " + device.networkDeviceName);

                UpDevice dummyDevice = new UpDevice(device.networkDeviceName);
                dummyDevice.AddNetworkInterface(device.networkDeviceName, device.networkDeviceType);

                Call call = new Call(DEVICE_DRIVER_NAME, "handshake", null);
                call.AddParameter("device", Json.Serialize(currentDevice.ToJSON()));

                Response response = gateway.CallService(dummyDevice, call);
                if ((response != null) && string.IsNullOrEmpty(response.error))
                {
                    // in case of a success greeting process, register the device in the neighborhood database
                    object responseDevice = response.GetResponseData("device");
                    if (responseDevice != null)
                    {
                        UpDevice remoteDevice;
                        if (responseDevice is string)
                            remoteDevice = UpDevice.FromJSON(Json.Deserialize(responseDevice as string));
                        else
                            remoteDevice = UpDevice.FromJSON(responseDevice);

                        RegisterDevice(remoteDevice);
                        logger.Log("Registered device " + remoteDevice.name);

                        return remoteDevice;
                    }
                    else
                        logger.LogError(
                            "Not possible complete handshake with device '" + device.networkDeviceName +
                            "' for no device on the handshake response.");
                }
                else
                {
                    logger.LogError(
                        "Not possible to handshake with device '" +
                        device.networkDeviceName +
                        (response == null ? ": No Response received." : "': Cause : " + response.error));
                }
            }
            catch (System.Exception e)
            {
                logger.Log(e.StackTrace);
                logger.LogError("Not possible to handshake with device '" + device.networkDeviceName + "'. " + e.Message);
            }

            return null;
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Called by radar whenever a device leaves the space.
        /// </summary>
        /// <param name="device"></param>
        public void DeviceLeft(NetworkDevice device)
        {
            if (device == null || device.networkDeviceName == null || device.networkDeviceType == null)
                return;

            // Remove what services this device has.
            logger.Log("Device " + device.networkDeviceName + " of type " + device.networkDeviceType + " leaving.");

            string host = Util.GetHost(device.networkDeviceName);
            List<UpDevice> devices;
            lock (_devicedao_lock) { devices = deviceDao.List(host, device.networkDeviceType); }

            if ((devices != null) && (devices.Count > 0))
            {
                UpDevice upDevice = devices[0];
                List<DriverModel> returnedDrivers = driverManager.List(null, upDevice.name);
                if ((returnedDrivers != null) && (returnedDrivers.Count > 0))
                {
                    foreach (DriverModel rdd in returnedDrivers)
                        driverManager.Delete(rdd.id, rdd.device);
                }

                lock (_devicedao_lock) { deviceDao.Delete(upDevice.name); };

                logger.Log("Device '" + upDevice.name + "' left");
            }
            else
                logger.Log("Device not found in database.");
        }