コード例 #1
0
 public Subscription(Guid id, WebSocketConnectionBase connection, TKey[] keys, object data = null)
 {
     Id         = id;
     Connection = connection;
     Keys       = keys;
     Data       = data;
 }
コード例 #2
0
        private void Notify(WebSocketConnectionBase connection, Guid subscriptionId, DeviceCommand command, Device device,
                            bool isInitialCommand = false)
        {
            if (!isInitialCommand)
            {
                var initialCommandList = GetInitialCommandList(connection, subscriptionId);
                lock (initialCommandList) // wait until all initial commands are sent
                {
                    if (initialCommandList.Contains(command.ID))
                    {
                        return;
                    }
                }

                if (!IsDeviceAccessible(connection, device, "GetDeviceCommand"))
                {
                    return;
                }
            }

            connection.SendResponse("command/insert",
                                    new JProperty("subscriptionId", subscriptionId),
                                    new JProperty("deviceGuid", device.GUID),
                                    new JProperty("command", GetMapper <DeviceCommand>().Map(command)));
        }
コード例 #3
0
        private void Notify(WebSocketConnectionBase connection, Guid subscriptionId, DeviceNotification notification, Device device,
                            bool isInitialNotification = false)
        {
            if (!isInitialNotification)
            {
                var initialNotificationList = GetInitialNotificationList(connection, subscriptionId);
                lock (initialNotificationList) // wait until all initial notifications are sent
                {
                    if (initialNotificationList.Contains(notification.ID))
                    {
                        return;
                    }
                }

                if (!IsDeviceAccessible(connection, device, "GetDeviceNotification"))
                {
                    return;
                }
            }

            connection.SendResponse("notification/insert",
                                    new JProperty("subscriptionId", subscriptionId),
                                    new JProperty("deviceGuid", device.GUID),
                                    new JProperty("notification", GetMapper <DeviceNotification>().Map(notification)));
        }
コード例 #4
0
 public void NotifyConnectionOpened(WebSocketConnectionBase connection)
 {
     lock (_lock)
     {
         _connections.Add(connection);
         SendMessage(new ConnectionOpenedMessage(connection));
     }
 }
コード例 #5
0
ファイル: Router.cs プロジェクト: oryol/devicehive-.net
        private ControllerBase GetController(WebSocketConnectionBase connection)
        {
            Type controllerType;
            if (!_controllersMapping.TryGetValue(connection.Path, out controllerType))
                throw new InvalidOperationException("Can't accept connections on invalid path: " + connection.Path);

            return (ControllerBase) CreateController(controllerType);
        }
コード例 #6
0
 protected void RegisterConnection(WebSocketConnectionBase connection)
 {
     lock (_connections)
     {
         _connections[connection.Identity] = connection;
         OnConnectionOpened(new WebSocketConnectionEventArgs(connection));
     }
 }
コード例 #7
0
 public ActionContext(WebSocketConnectionBase connection, ControllerBase controller, string action, JObject request)
 {
     Connection = connection;
     Controller = controller;
     Action     = action;
     Request    = request;
     Parameters = new Dictionary <string, object>();
 }
コード例 #8
0
        public override void CleanupConnection(WebSocketConnectionBase connection)
        {
            base.CleanupConnection(connection);

            _deviceSubscriptionManagerForNotifications.Cleanup(connection);
            _deviceSubscriptionManagerForCommands.Cleanup(connection);
            _commandSubscriptionManager.Cleanup(connection);
        }
コード例 #9
0
        public override void CleanupConnection(WebSocketConnectionBase connection)
        {
            base.CleanupConnection(connection);

            _deviceSubscriptionManagerForNotifications.Cleanup(connection);
            _deviceSubscriptionManagerForCommands.Cleanup(connection);
            _commandSubscriptionManager.Cleanup(connection);
        }
コード例 #10
0
        public void HandleNewConnection(WebSocketConnectionBase connection)
        {
            var controller = GetController(connection, allowNullResult: true);

            if (controller == null)
            {
                connection.Close();
            }
        }
コード例 #11
0
        public void CleanupConnection(WebSocketConnectionBase connection)
        {
            var controller = GetController(connection, allowNullResult: true);

            if (controller != null)
            {
                controller.CleanupConnection(connection);
            }
        }
コード例 #12
0
        public void Cleanup(WebSocketConnectionBase connection)
        {
            var deviceGuids = GetSubscriptions(connection).Select(s => s.Key).Distinct().ToArray();

            foreach (var deviceGuid in deviceGuids)
            {
                var subscriptionList = _subscriptionCollection.GetSubscriptionList(deviceGuid);
                subscriptionList.RemoveAll(s => s.Connection == connection);
            }
        }
コード例 #13
0
        public void Unsubscribe(WebSocketConnectionBase connection, TKey key)
        {
            var connectionSubscriptions = GetSubscriptions(connection);

            connectionSubscriptions.RemoveAll(s => object.Equals(s.Key, key));

            var subscriptionList = _subscriptionCollection.GetSubscriptionList(key);

            subscriptionList.RemoveAll(s => s.Connection == connection);
        }
コード例 #14
0
ファイル: Router.cs プロジェクト: oryol/devicehive-.net
        private ControllerBase GetController(WebSocketConnectionBase connection)
        {
            Type controllerType;

            if (!_controllersMapping.TryGetValue(connection.Path, out controllerType))
            {
                throw new InvalidOperationException("Can't accept connections on invalid path: " + connection.Path);
            }

            return((ControllerBase)CreateController(controllerType));
        }
コード例 #15
0
        public void Subscribe(WebSocketConnectionBase connection, TKey key)
        {
            var subscription = new Subscription <TKey>(key, connection);

            var connectionSubscriptions = GetSubscriptions(connection);

            connectionSubscriptions.Add(subscription);

            var subscriptionList = _subscriptionCollection.GetSubscriptionList(key);

            subscriptionList.Add(subscription);
        }
コード例 #16
0
        private ControllerBase GetController(WebSocketConnectionBase connection, bool allowNullResult = false)
        {
            Type controllerType;
            if (!_controllersMapping.TryGetValue(connection.Path, out controllerType))
            {
                if (allowNullResult)
                    return null;

                throw new InvalidOperationException("Can't accept connections on invalid path: " + connection.Path);
            }

            return (ControllerBase) CreateController(controllerType);
        }
コード例 #17
0
        private void Notify(WebSocketConnectionBase connection, DeviceNotification notification, Device device)
        {
            var user = (User)connection.Session["user"];

            if (user == null || !IsNetworkAccessible(device.NetworkID, user))
            {
                return;
            }

            connection.SendResponse("notification/insert",
                                    new JProperty("deviceGuid", device.GUID),
                                    new JProperty("notification", NotificationMapper.Map(notification)));
        }
コード例 #18
0
        public void RoutePing(WebSocketConnectionBase connection)
        {
            try
            {
                var controller = GetController(connection);

                var actionContext = new ActionContext(connection, controller, null, null);
                controller.InvokePingAction(actionContext);
            }
            catch (Exception e)
            {
                LogManager.GetLogger(typeof(Router)).Error("WebSocket ping error", e);
            }
        }
コード例 #19
0
 public void RoutePing(WebSocketConnectionBase connection)
 {
     try
     {
         var controller = GetController(connection);
         
         var actionContext = new ActionContext(connection, controller, null, null);
         controller.InvokePingAction(actionContext);
     }
     catch (Exception e)
     {
         LogManager.GetLogger(typeof(Router)).Error("WebSocket ping error", e);
     }
 }
コード例 #20
0
ファイル: Router.cs プロジェクト: oryol/devicehive-.net
        public void RouteRequest(WebSocketConnectionBase connection, string message)
        {
            try
            {
                var controller = GetController(connection);

                var request = JObject.Parse(message);
                var action = (string) request["action"];

                controller.InvokeAction(connection, action, request);
            }
            catch (Exception e)
            {
                LogManager.GetLogger(typeof(Router)).Error("WebSocket request error", e);
            }
        }
コード例 #21
0
        public static void SendResponse(this WebSocketConnectionBase connection,
                                        string action, params JProperty[] properties)
        {
            var mainProperties = new List <JProperty>()
            {
                new JProperty("action", action),
            };

            var responseProperties = mainProperties.Concat(properties).Cast <object>().ToArray();
            var responseObj        = new JObject(responseProperties);

            connection.Send(responseObj.ToString(Formatting.None,
                                                 new IsoDateTimeConverter {
                DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.ffffff"
            }));
        }
コード例 #22
0
ファイル: Router.cs プロジェクト: oryol/devicehive-.net
        public void RouteRequest(WebSocketConnectionBase connection, string message)
        {
            try
            {
                var controller = GetController(connection);

                var request = JObject.Parse(message);
                var action  = (string)request["action"];

                controller.InvokeAction(connection, action, request);
            }
            catch (Exception e)
            {
                LogManager.GetLogger(typeof(Router)).Error("WebSocket request error", e);
            }
        }
コード例 #23
0
        private ControllerBase GetController(WebSocketConnectionBase connection, bool allowNullResult = false)
        {
            Type controllerType;

            if (!_controllersMapping.TryGetValue(connection.Path, out controllerType))
            {
                if (allowNullResult)
                {
                    return(null);
                }

                throw new InvalidOperationException("Can't accept connections on invalid path: " + connection.Path);
            }

            return((ControllerBase)CreateController(controllerType));
        }
コード例 #24
0
        public void NotifyConnectionClosed(WebSocketConnectionBase connection)
        {
            if (!_handleConnectionClose)
            {
                return;
            }

            lock (_lock)
            {
                _connections.Remove(connection);
                if (_connections.Count == 0)
                {
                    _inactiveStartTime = DateTime.Now;
                }

                SendMessage(new ConnectionClosedMessage(connection.Identity));
            }
        }
コード例 #25
0
        /// <summary>
        /// Notifies the device about new command.
        /// </summary>
        /// <action>command/insert</action>
        /// <response>
        ///     <parameter name="deviceGuid" type="string">Device unique identifier.</parameter>
        ///     <parameter name="command" cref="DeviceCommand">A <see cref="DeviceCommand"/> resource representing the command.</parameter>
        /// </response>
        private void Notify(WebSocketConnectionBase connection, Device device, DeviceCommand command,
                            bool isInitialCommand = false)
        {
            if (!isInitialCommand)
            {
                var initialCommandList = GetInitialCommandList(connection);
                lock (initialCommandList) // wait until all initial commands are sent
                {
                    if (initialCommandList.Contains(command.ID))
                    {
                        return;
                    }
                }
            }

            connection.SendResponse("command/insert",
                                    new JProperty("deviceGuid", device.GUID),
                                    new JProperty("command", MapDeviceCommand(command)));
        }
コード例 #26
0
        public Subscription <TKey> Subscribe(Guid subscriptionId, WebSocketConnectionBase connection, TKey[] keys, object data)
        {
            var subscription = new Subscription <TKey>(subscriptionId, connection, keys, data);

            // add into connection session
            var connectionSubscriptions = GetConnectionSubscriptions(connection);

            connectionSubscriptions.Add(subscription);

            // add into lookup by key
            foreach (var key in subscription.Keys)
            {
                var subscriptionList = _lookupByKey.GetOrAdd(key, new List <Subscription <TKey> >());
                subscriptionList.Add(subscription);
            }

            // add into lookup by id
            _lookupById[subscription.Id] = subscription;
            return(subscription);
        }
コード例 #27
0
        public void InvokeAction(WebSocketConnectionBase connection, string action, JObject args)
        {
            Connection = connection;
            ActionName = action;
            ActionArgs = args;

            try
            {
                BeforeActionInvoke();
                _actionInvoker.InvokeAction(this, action);
                AfterActionInvoke();
            }
            catch (WebSocketRequestException e)
            {
                SendErrorResponse(e.Message);
            }
            catch (Exception)
            {
                SendErrorResponse("Server error");
                throw;
            }
        }
コード例 #28
0
        public void Cleanup(WebSocketConnectionBase connection)
        {
            // get subscription from session
            var connectionSubscriptions = GetConnectionSubscriptions(connection);

            foreach (var subscription in connectionSubscriptions)
            {
                // remove from lookup by id
                Subscription <TKey> s;
                _lookupById.TryRemove(subscription.Id, out s);

                // remove from subscription list
                List <Subscription <TKey> > subscriptionList;
                foreach (var key in subscription.Keys)
                {
                    if (_lookupByKey.TryGetValue(key, out subscriptionList))
                    {
                        subscriptionList.Remove(subscription);
                    }
                }
            }
        }
コード例 #29
0
        private bool IsDeviceAccessible(WebSocketConnectionBase connection, Device device, string accessKeyAction)
        {
            var user = (User)connection.Session["User"];

            if (user == null)
            {
                return(false);
            }

            if (user.Role != (int)UserRole.Administrator)
            {
                if (device.NetworkID == null)
                {
                    return(false);
                }

                var userNetworks = (List <UserNetwork>)connection.Session["UserNetworks"];
                if (userNetworks == null)
                {
                    userNetworks = DataContext.UserNetwork.GetByUser(user.ID);
                    connection.Session["UserNetworks"] = userNetworks;
                }

                if (!userNetworks.Any(un => un.NetworkID == device.NetworkID))
                {
                    return(false);
                }
            }

            // check if access key permissions are sufficient
            var accessKey = (AccessKey)connection.Session["AccessKey"];

            return(accessKey == null || accessKey.Permissions.Any(p =>
                                                                  p.IsActionAllowed(accessKeyAction) && p.IsAddressAllowed(connection.Host) &&
                                                                  p.IsNetworkAllowed(device.NetworkID) && p.IsDeviceAllowed(device.GUID)));
        }
コード例 #30
0
        public void Unsubscribe(WebSocketConnectionBase connection, Guid subscriptionId)
        {
            // remove from lookup by id
            Subscription <TKey> subscription;

            _lookupById.TryRemove(subscriptionId, out subscription);

            if (subscription != null)
            {
                // remove from connection session
                var connectionSubscriptions = GetConnectionSubscriptions(connection);
                connectionSubscriptions.Remove(subscription);

                // remove from subscription list
                List <Subscription <TKey> > subscriptionList;
                foreach (var key in subscription.Keys)
                {
                    if (_lookupByKey.TryGetValue(key, out subscriptionList))
                    {
                        subscriptionList.Remove(subscription);
                    }
                }
            }
        }
コード例 #31
0
ファイル: Router.cs プロジェクト: oryol/devicehive-.net
 public void CleanupConnection(WebSocketConnectionBase connection)
 {
     var controller = GetController(connection);
     controller.CleanupConnection(connection);
 }
コード例 #32
0
 private ISet<int> GetInitialCommandList(WebSocketConnectionBase connection, Guid subscriptionId)
 {
     return (ISet<int>)connection.Session.GetOrAdd("InitialCommands_" + subscriptionId, () => new HashSet<int>());
 }
コード例 #33
0
        private void Notify(WebSocketConnectionBase connection, Guid subscriptionId, DeviceCommand command, Device device,
            bool isInitialCommand = false)
        {
            if (!isInitialCommand)
            {
                var initialCommandList = GetInitialCommandList(connection, subscriptionId);
                lock (initialCommandList) // wait until all initial commands are sent
                {
                    if (initialCommandList.Contains(command.ID))
                        return;
                }

                if (!IsDeviceAccessible(connection, device, "GetDeviceCommand"))
                    return;
            }

            connection.SendResponse("command/insert",
                new JProperty("subscriptionId", subscriptionId),
                new JProperty("deviceGuid", device.GUID),
                new JProperty("command", GetMapper<DeviceCommand>().Map(command)));
        }
コード例 #34
0
 public override void CleanupConnection(WebSocketConnectionBase connection)
 {
     base.CleanupConnection(connection);
     _subscriptionManager.Cleanup(connection);
 }
コード例 #35
0
 private ISet<int> GetInitialCommandList(WebSocketConnectionBase connection)
 {
     return (ISet<int>) connection.Session.GetOrAdd("InitialCommands", () => new HashSet<int>());
 }
コード例 #36
0
 public override void CleanupConnection(WebSocketConnectionBase connection)
 {
     base.CleanupConnection(connection);
     _subscriptionManager.Cleanup(connection);
 }
コード例 #37
0
 public void HandleNewConnection(WebSocketConnectionBase connection)
 {
     var controller = GetController(connection, allowNullResult: true);
     if (controller == null)
         connection.Close();
 }
コード例 #38
0
 protected void RegisterConnection(WebSocketConnectionBase connection)
 {
     _connections[connection.Identity] = connection;
 }
コード例 #39
0
 public WebSocketConnectionEventArgs(WebSocketConnectionBase connection)
 {
     Connection = connection;
 }
コード例 #40
0
 private void CleanupNotifications(WebSocketConnectionBase connection)
 {
     _subscriptionManager.Cleanup(connection);
 }
コード例 #41
0
 public override void CleanupConnection(WebSocketConnectionBase connection)
 {
     base.CleanupConnection(connection);
     CleanupNotifications(connection);
 }
コード例 #42
0
        private void Notify(WebSocketConnectionBase connection, DeviceNotification notification, Device device)
        {
            var user = (User) connection.Session["user"];
            if (user == null || !IsNetworkAccessible(device.NetworkID, user))
                return;

            connection.SendResponse("notification/insert",
                new JProperty("deviceGuid", device.GUID),
                new JProperty("notification", NotificationMapper.Map(notification)));
        }
コード例 #43
0
 public void CleanupConnection(WebSocketConnectionBase connection)
 {
     var controller = GetController(connection, allowNullResult: true);
     if (controller != null)
         controller.CleanupConnection(connection);
 }
コード例 #44
0
 private ISet <int> GetInitialCommandList(WebSocketConnectionBase connection, Guid subscriptionId)
 {
     return((ISet <int>)connection.Session.GetOrAdd("InitialCommands_" + subscriptionId, () => new HashSet <int>()));
 }
コード例 #45
0
 private void Notify(WebSocketConnectionBase connection, Device device, DeviceCommand command)
 {
     connection.SendResponse("command/insert",
         new JProperty("deviceGuid", device.GUID),
         new JProperty("command", CommandMapper.Map(command)));
 }
コード例 #46
0
 public ConnectionOpenedMessage(WebSocketConnectionBase connection)
 {
     ConnectionIdentity = connection.Identity;
     Host = connection.Host;
     Path = connection.Path;
 }
コード例 #47
0
 private void Notify(WebSocketConnectionBase connection, Device device, DeviceCommand command)
 {
     connection.SendResponse("command/insert",
                             new JProperty("deviceGuid", device.GUID),
                             new JProperty("command", CommandMapper.Map(command)));
 }
コード例 #48
0
 private ISet <int> GetInitialCommandList(WebSocketConnectionBase connection)
 {
     return((ISet <int>)connection.Session.GetOrAdd("InitialCommands", () => new HashSet <int>()));
 }
コード例 #49
0
 public override void CleanupConnection(WebSocketConnectionBase connection)
 {
     base.CleanupConnection(connection);
     CleanupNotifications(connection);
 }
コード例 #50
0
        private void Notify(WebSocketConnectionBase connection, Guid subscriptionId, DeviceNotification notification, Device device,
            bool isInitialNotification = false)
        {
            if (!isInitialNotification)
            {
                var initialNotificationList = GetInitialNotificationList(connection, subscriptionId);
                lock (initialNotificationList) // wait until all initial notifications are sent
                {
                    if (initialNotificationList.Contains(notification.ID))
                        return;
                }

                if (!IsDeviceAccessible(connection, device, "GetDeviceNotification"))
                    return;
            }

            connection.SendResponse("notification/insert",
                new JProperty("subscriptionId", subscriptionId),
                new JProperty("deviceGuid", device.GUID),
                new JProperty("notification", GetMapper<DeviceNotification>().Map(notification)));
        }
コード例 #51
0
 public virtual void CleanupConnection(WebSocketConnectionBase connection)
 {
 }
コード例 #52
0
        private bool IsDeviceAccessible(WebSocketConnectionBase connection, Device device, string accessKeyAction)
        {
            var user = (User)connection.Session["User"];
            if (user == null)
                return false;

            if (user.Role != (int)UserRole.Administrator)
            {
                if (device.NetworkID == null)
                    return false;

                var userNetworks = (List<UserNetwork>)connection.Session["UserNetworks"];
                if (userNetworks == null)
                {
                    userNetworks = DataContext.UserNetwork.GetByUser(user.ID);
                    connection.Session["UserNetworks"] = userNetworks;
                }

                if (!userNetworks.Any(un => un.NetworkID == device.NetworkID))
                    return false;
            }

            // check if access key permissions are sufficient
            var accessKey = (AccessKey)connection.Session["AccessKey"];
            return accessKey == null || accessKey.Permissions.Any(p =>
                p.IsActionAllowed(accessKeyAction) && p.IsAddressAllowed(connection.Host) &&
                p.IsNetworkAllowed(device.NetworkID) && p.IsDeviceAllowed(device.GUID.ToString()));
        }
コード例 #53
0
ファイル: Subscription.cs プロジェクト: oryol/devicehive-.net
 public Subscription(TKey key, WebSocketConnectionBase connection)
 {
     Key        = key;
     Connection = connection;
 }
コード例 #54
0
        /// <summary>
        /// Notifies the device about new command.
        /// </summary>
        /// <action>command/insert</action>
        /// <response>
        ///     <parameter name="deviceGuid" type="string">Device unique identifier.</parameter>
        ///     <parameter name="command" cref="DeviceCommand">A <see cref="DeviceCommand"/> resource representing the command.</parameter>
        /// </response>
        private void Notify(WebSocketConnectionBase connection, Device device, DeviceCommand command,
            bool isInitialCommand = false)
        {
            if (!isInitialCommand)
            {
                var initialCommandList = GetInitialCommandList(connection);
                lock (initialCommandList) // wait until all initial commands are sent
                {
                    if (initialCommandList.Contains(command.ID))
                        return;
                }
            }

            connection.SendResponse("command/insert",
                new JProperty("deviceGuid", device.GUID),
                new JProperty("command", MapDeviceCommand(command)));
        }
コード例 #55
0
 public WebSocketMessageEventArgs(WebSocketConnectionBase connection, string message) :
     base(connection)
 {
     Message = message;
 }