/// <summary> /// Register new object /// </summary> /// <param name="newObject">Object to be registered</param> public void RegisterObject(IIdentifiedObject newObject) { if (keyToIdDictionary.ContainsKey(newObject.Key)) { return; } if (AssignIds) { var id = idManager.GetId(); idToObjectDictionary.Add(id, newObject); keyToIdDictionary.Add(newObject.Key, id); idRegistrationTimestamp.Add(id, DateTime.UtcNow); BroadcastMessage(GetCommandMessage(IdsRegisterCommandType.BindIdAndKey, newObject)); ObjectBoundToId?.Invoke(newObject, id); return; } lock (unboundObjects) unboundObjects.Add(newObject); if (!awaitingKeyIdBinds.TryGetValue(newObject.Key, out var bindId)) { return; } awaitingKeyIdBinds.Remove(newObject.Key); TryBindReceiver(newObject.Key, bindId); }
public EquipmentNodeItem(Type itemType, IIdentifiedObject item, bool hidden = false, bool visited = false) { Type = itemType; Item = item; Hidden = hidden; Visited = visited; }
public SwitchModel(IIdentifiedObject item) : this() { Description = item.Description; GID = item.GID; MRID = item.MRID; Name = item.Name; }
public BaseSchemaModel(IIdentifiedObject identifiedObject = null) { Description = identifiedObject.Description; GID = identifiedObject.GID; MRID = identifiedObject.MRID; Name = identifiedObject.Name; }
/// <summary> /// Unregister identified object /// </summary> /// <param name="unregisteredObject">Object to be unregistered</param> public void UnregisterObject(IIdentifiedObject unregisteredObject) { if (unregisteredObject == null) { return; } if (!keyToIdDictionary.ContainsKey(unregisteredObject.Key)) { lock (unboundObjects) unboundObjects.Remove(unregisteredObject); return; } var id = ResolveId(unregisteredObject); if (id != null) { if (AssignIds) { BroadcastMessage( GetCommandMessage( IdsRegisterCommandType.UnbindIdAndKey, unregisteredObject)); } idRegistrationTimestamp.Remove(id.Value); idToObjectDictionary.Remove(id.Value); idManager.ReturnId(id.Value); } keyToIdDictionary.Remove(unregisteredObject.Key); }
/// <summary> /// Register identified object in the manager /// </summary> /// <param name="identifiedObject">Identified object to be registered</param> public void RegisterObject(IIdentifiedObject identifiedObject) { if (identifiedObject is IMessageSender sender) { senders.Add(sender); } idsRegister.RegisterObject(identifiedObject); }
public SQLCollectionChangedArgs(IIdentifiedObject item /*, int index*/, string property, string newValue) { Action = SQLCollectionChangedAction.Edit; Item = item; //Index = index; Property = property; NewValue = newValue; }
/// <summary> /// Unregister identified object from the manager /// </summary> /// <param name="identifiedObject">Identified object to be unregistered</param> public void UnregisterObject(IIdentifiedObject identifiedObject) { if (identifiedObject is IMessageSender sender) { senders.Remove(sender); } idsRegister.UnregisterObject(identifiedObject); }
public static ISchemaModel Map(IIdentifiedObject item, Type itemType) { if (itemType != null && modelFactoryByType.TryGetValue(itemType, out var factory)) { return(factory(item)); } return(DefaultMap(item)); }
public void CreateConnection(IIdentifiedObject source, IIdentifiedObject target, ConnectionType connectionType = ConnectionType.OneWay) { var sObj = NodeList.FirstOrDefault(v => v.TargetObjectID == source.ObjectID); var tObj = NodeList.FirstOrDefault(v => v.TargetObjectID == target.ObjectID); if (sObj == null || tObj == null) { return; } CreateConnection(sObj, tObj, connectionType); }
/// <summary> /// Pushes the identifier bound to passed object into this message /// </summary> /// <param name="distributedMessage">Message where identifier will be pushed</param> /// <param name="identifiedObject">Identified object which identifier will be pushed</param> /// <exception cref="ArgumentException">Cannot resolve identifier for this address key</exception> public void PushId(DistributedMessage distributedMessage, IIdentifiedObject identifiedObject) { var id = ResolveId(identifiedObject); if (!id.HasValue) { Log.Error($"Cannot push identifier of an unregistered object {identifiedObject} with key {identifiedObject.Key}."); return; } distributedMessage.Content.PushInt(id.Value, BytesPerId); }
public static string GetNextId(string id, IIdentifiedObject obj, string group) { string suggestedId = id; int count = 1; while (UniqueIDCache.IsIdInUse(suggestedId, obj, group)) { suggestedId = id + "-" + count.ToString(); count++; } return(suggestedId); }
public void CreateNode(IIdentifiedObject targetObject, UserControl control) { var node = new Node() { TargetObjectID = targetObject.ObjectID }; node.Left = Canvas.GetLeft(control); node.Top = Canvas.GetTop(control); node.ZIndex = Canvas.GetZIndex(control); node.Width = control.ActualWidth; node.Height = control.ActualHeight; NodeList.Add(node); }
/// <summary> /// Gets ids register command for the identified object in the bytes stack message /// </summary> /// <param name="commandType">Command type</param> /// <param name="identifiedObject">Identified object which is command target</param> /// <returns>Register command in bytes stack</returns> private Message GetCommandMessage(IdsRegisterCommandType commandType, IIdentifiedObject identifiedObject) { var bytesStack = new BytesStack(); var id = ResolveId(identifiedObject); if (id == null) { throw new ArgumentException("Usage of the unregistered identified object."); } bytesStack.PushInt(id.Value, BytesPerId); bytesStack.PushString(identifiedObject.Key); bytesStack.PushInt((int)commandType, BytesPerCommandType); return(new Message(Key, bytesStack, MessageType.ReliableOrdered)); }
public static bool IsIdInUse(string id, IIdentifiedObject obj, string group) { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (group == null) { throw new ArgumentNullException(nameof(@group)); } HashSet <IIdentifiedObject> itemGroup = UniqueIDCache.GetGroupCollection(group); return(itemGroup.Any(t => t.ID == id && t != obj)); }
public static void RemoveItem(IIdentifiedObject item, string group) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (group == null) { throw new ArgumentNullException(nameof(@group)); } HashSet <IIdentifiedObject> itemGroup = UniqueIDCache.GetGroupCollection(group); if (itemGroup.Contains(item)) { itemGroup.Remove(item); } }
/// <summary> /// Gets ids register command for the identified object in the bytes stack message /// </summary> /// <param name="commandType">Command type</param> /// <param name="identifiedObject">Identified object which is command target</param> /// <returns>Register command in bytes stack</returns> private DistributedMessage GetCommandMessage( IdsRegisterCommandType commandType, IIdentifiedObject identifiedObject) { var id = ResolveId(identifiedObject); if (id == null) { throw new ArgumentException("Usage of the unregistered identified object."); } var message = MessagesPool.Instance.GetMessage( BytesPerId + BytesStack.GetMaxByteCount(identifiedObject.Key) + BytesPerCommandType); message.AddressKey = Key; message.Content.PushInt(id.Value, BytesPerId); message.Content.PushString(identifiedObject.Key); message.Content.PushInt((int)commandType, BytesPerCommandType); message.Type = DistributedMessageType.ReliableOrdered; return(message); }
public static void AddItem(IIdentifiedObject item, string group) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (group == null) { throw new ArgumentNullException(nameof(@group)); } HashSet <IIdentifiedObject> itemGroup = UniqueIDCache.GetGroupCollection(group); if (itemGroup.Any(t => t.ID == item.ID)) { throw new ArgumentException(string.Format("The specified id '{0}' is already in use in group {1}", item.ID, group)); } else { itemGroup.Add(item); } }
public static string SetID(IIdentifiedObject obj, string proposedValue, string groupName, bool canRename) { string newID = proposedValue; string oldID = obj.ID; if (proposedValue != null) { if (obj.ID == null) { if (UniqueIDCache.IsIdInUse(proposedValue, obj, groupName)) { if (canRename) { newID = UniqueIDCache.GetNextId(proposedValue, obj, groupName); } else { throw new DuplicateIdentifierException(); } } else { newID = proposedValue; } } else if (UniqueIDCache.CanChangeId(groupName, obj, proposedValue)) { newID = proposedValue; } else { throw new DuplicateIdentifierException(); } } return(newID); }
private void OnSQLUpdatesItem(IIdentifiedObject item, string property, string value) { SQLUpdatesItem(new SQLCollectionChangedArgs(item, property, value)); }
public EquipmentNodeItem(Type itemType, IIdentifiedObject item, IEnumerable <long> connectedTo, bool hidden = false, bool visited = false) : this(itemType, item, hidden, visited) { ConnectedTo = connectedTo; }
private static ISchemaModel MapToSwitchModel(IIdentifiedObject item) { return(new SwitchModel(item)); }
private static ISchemaModel CastToSchemaModel(IIdentifiedObject item) { return(item as ISchemaModel); }
public static bool HasObject(string group, IIdentifiedObject item) { return(UniqueIDCache.GetGroupCollection(group).Contains(item)); }
/// <summary> /// Checks if object is already bound to any identifier /// </summary> /// <param name="identifiedObject">Identifier object to check</param> /// <returns></returns> public bool IsObjectBoundToId(IIdentifiedObject identifiedObject) { return(IsKeyBoundToId(identifiedObject.Key)); }
private static ISchemaModel DefaultMap(IIdentifiedObject item) { return(new BaseSchemaModel(item)); }
/// <summary> /// Returns identifier of the identified object if it is already bound /// </summary> /// <param name="identifiedObject">Identified object to check</param> /// <returns>Identifier of the identified object, null if it's not bound</returns> public int?ResolveId(IIdentifiedObject identifiedObject) { return(ResolveId(identifiedObject.Key)); }
public static bool CanChangeId(string group, IIdentifiedObject item, string newId) { HashSet <IIdentifiedObject> itemGroup = UniqueIDCache.GetGroupCollection(group); return(!itemGroup.Any(t => t.ID == newId && t != item)); }
public SQLCollectionChangedArgs(SQLCollectionChangedAction action, IIdentifiedObject item) { Action = action; Item = item; //Index = index; }
/// <summary> /// Method called when new identified object is bound with id /// </summary> /// <param name="identifiedObject">Bound identified object</param> /// <param name="id">Bound identifier</param> private void IdsRegisterOnObjectBoundToId(IIdentifiedObject identifiedObject, int id) { //Send to the object all awaiting incoming messages addressed to this identifier if (identifiedObject is IMessageReceiver receiver) { if (awaitingIncomingMessages.TryGetValue(id, out var awaitingMessages)) { try { //Pass still valid messages foreach (var awaitingMessage in awaitingMessages) { //Ignore messages with outdated assigned identifiers var registrationTimestamp = idsRegister.GetRegistrationTimestamp(id); if (registrationTimestamp == null) { Log.Error( $"Registration event called without the timestamp set for the object with id {id}."); break; } if (awaitingMessage.DistributedMessage.ServerTimestamp < registrationTimestamp) { continue; } awaitingMessage.DistributedMessage.AddressKey = identifiedObject.Key; receiver.ReceiveMessage(connectionManager.GetConnectedPeerManager(awaitingMessage.EndPoint), awaitingMessage.DistributedMessage); awaitingMessage.DistributedMessage.Release(); } } finally { awaitingIncomingMessages.Remove(id); } } } //Send all awaiting outgoing messages tried to be send with the same address key if (identifiedObject is IMessageSender sender) { if (awaitingOutgoingMessages.TryGetValue(sender.Key, out var awaitingMessages)) { try { foreach (var awaitingMessage in awaitingMessages) { if (Equals(awaitingMessage.EndPoint.Address, IPAddress.Broadcast)) { BroadcastMessage(awaitingMessage.DistributedMessage); } else { UnicastMessage(awaitingMessage.EndPoint, awaitingMessage.DistributedMessage); } } } finally { awaitingOutgoingMessages.Remove(sender.Key); } } } }