public Subscription(Guid id, WebSocketConnectionBase connection, TKey[] keys, object data = null) { Id = id; Connection = connection; Keys = keys; Data = data; }
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))); }
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))); }
public void NotifyConnectionOpened(WebSocketConnectionBase connection) { lock (_lock) { _connections.Add(connection); SendMessage(new ConnectionOpenedMessage(connection)); } }
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); }
protected void RegisterConnection(WebSocketConnectionBase connection) { lock (_connections) { _connections[connection.Identity] = connection; OnConnectionOpened(new WebSocketConnectionEventArgs(connection)); } }
public ActionContext(WebSocketConnectionBase connection, ControllerBase controller, string action, JObject request) { Connection = connection; Controller = controller; Action = action; Request = request; Parameters = new Dictionary <string, object>(); }
public override void CleanupConnection(WebSocketConnectionBase connection) { base.CleanupConnection(connection); _deviceSubscriptionManagerForNotifications.Cleanup(connection); _deviceSubscriptionManagerForCommands.Cleanup(connection); _commandSubscriptionManager.Cleanup(connection); }
public void HandleNewConnection(WebSocketConnectionBase connection) { var controller = GetController(connection, allowNullResult: true); if (controller == null) { connection.Close(); } }
public void CleanupConnection(WebSocketConnectionBase connection) { var controller = GetController(connection, allowNullResult: true); if (controller != null) { controller.CleanupConnection(connection); } }
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); } }
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); }
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)); }
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); }
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); }
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))); }
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); } }
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); } }
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" })); }
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); } }
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)); }
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)); } }
/// <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))); }
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); }
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; } }
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); } } } }
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))); }
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); } } } }
public void CleanupConnection(WebSocketConnectionBase connection) { var controller = GetController(connection); controller.CleanupConnection(connection); }
private ISet<int> GetInitialCommandList(WebSocketConnectionBase connection, Guid subscriptionId) { return (ISet<int>)connection.Session.GetOrAdd("InitialCommands_" + subscriptionId, () => new HashSet<int>()); }
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))); }
public override void CleanupConnection(WebSocketConnectionBase connection) { base.CleanupConnection(connection); _subscriptionManager.Cleanup(connection); }
private ISet<int> GetInitialCommandList(WebSocketConnectionBase connection) { return (ISet<int>) connection.Session.GetOrAdd("InitialCommands", () => new HashSet<int>()); }
public void HandleNewConnection(WebSocketConnectionBase connection) { var controller = GetController(connection, allowNullResult: true); if (controller == null) connection.Close(); }
protected void RegisterConnection(WebSocketConnectionBase connection) { _connections[connection.Identity] = connection; }
public WebSocketConnectionEventArgs(WebSocketConnectionBase connection) { Connection = connection; }
private void CleanupNotifications(WebSocketConnectionBase connection) { _subscriptionManager.Cleanup(connection); }
public override void CleanupConnection(WebSocketConnectionBase connection) { base.CleanupConnection(connection); CleanupNotifications(connection); }
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))); }
public void CleanupConnection(WebSocketConnectionBase connection) { var controller = GetController(connection, allowNullResult: true); if (controller != null) controller.CleanupConnection(connection); }
private ISet <int> GetInitialCommandList(WebSocketConnectionBase connection, Guid subscriptionId) { return((ISet <int>)connection.Session.GetOrAdd("InitialCommands_" + subscriptionId, () => new HashSet <int>())); }
private void Notify(WebSocketConnectionBase connection, Device device, DeviceCommand command) { connection.SendResponse("command/insert", new JProperty("deviceGuid", device.GUID), new JProperty("command", CommandMapper.Map(command))); }
public ConnectionOpenedMessage(WebSocketConnectionBase connection) { ConnectionIdentity = connection.Identity; Host = connection.Host; Path = connection.Path; }
private ISet <int> GetInitialCommandList(WebSocketConnectionBase connection) { return((ISet <int>)connection.Session.GetOrAdd("InitialCommands", () => new HashSet <int>())); }
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))); }
public virtual void CleanupConnection(WebSocketConnectionBase connection) { }
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())); }
public Subscription(TKey key, WebSocketConnectionBase connection) { Key = key; Connection = connection; }
/// <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))); }
public WebSocketMessageEventArgs(WebSocketConnectionBase connection, string message) : base(connection) { Message = message; }