Exemplo n.º 1
0
        public override List <ClientId> GetAllClientsIds()
        {
            List <ClientId> result = new List <ClientId>();

            foreach (MessageBusClient client in this._clientsHotSwap)
            {
                if (client == null)
                {// This is normal since items are never removed from list, only set to null.
                    continue;
                }

                ClientId id = client.Id;
                if (id != null)
                {
                    result.Add(id);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Actually supply the item to client.
        /// </summary>
        /// <param name="senderId"></param>
        /// <param name="receiverId"></param>
        /// <param name="envelope"></param>
        /// <param name="requestConfirm">In local mode we have receival confirmation, so this value is ignored here (as result is always assured true).</param>
        /// <returns></returns>
        protected virtual SendToClientResultEnum DoSendToClient(ClientId senderId, ClientId receiverId,
                                                                Envelope envelope, TimeSpan?requestConfirmTimeout)
        {
            if (receiverId.MessageBus != this)
            {
                //// Maybe this is a "lost" id, try to see if it is one of ours.
                //if (receiverId.MessageBus == null && _guidToIndexHotSwap.ContainsKey(receiverId.Guid))
                //{// Yes!
                //    receiverId.MessageBus = this;
                //}
                return(SendToClientResultEnum.ClientNotFound);
            }

            MessageBusClient client = GetLocalClientByIndex(receiverId.LocalMessageBusIndex);

            if (client == null)
            {
                return(SendToClientResultEnum.ClientNotFound);
            }

            ISerializer serializer = _serializer;

            if (serializer == null)
            {
                return(SendToClientResultEnum.Failure);
            }

            // Duplicate what (if anything) as according to envelope duplication model.
            envelope = envelope.Duplicate(serializer);
            envelope.History.PushStamp(new EnvelopeStamp(PendingStampId, receiverId, senderId));

            if (client.Receive(envelope))
            {
                return(SendToClientResultEnum.Success);
            }
            else
            {
                return(SendToClientResultEnum.Failure);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Needs to consume both client and id, since it may be used with client == null.
        /// </summary>
        /// <returns>The index of the newly registered client, or InvalidClientIndex on failure.</returns>
        protected int DoAddClient(MessageBusClient client, ClientId clientId)
        {
            int index = 0;

            if (client != null && client.Id.Equals(clientId) == false)
            {
#if Matrix_Diagnostics
                InstanceMonitor.Error("Client id mismatch.");
#endif
                return(ClientId.InvalidMessageBusClientIndex);
            }

            lock (_syncRoot)
            {
                if (clientId.LocalMessageBusIndex != ClientId.InvalidMessageBusClientIndex)
                {// Client already has an Index assigned, must reuse it.
                    MessageBusClient existingInstance = null;
                    if (_clientsHotSwap.TryGetValue(clientId.LocalMessageBusIndex, ref existingInstance))
                    {// Successfully acquired existing value for client.
                        // Check if we are OK to assign to this position.
                        if (existingInstance != null && existingInstance != client)
                        {// There is something else at that position.
#if Matrix_Diagnostics
                            InstanceMonitor.Error("Client id mismatch.");
#endif
                            return(ClientId.InvalidMessageBusClientIndex);
                        }
                    }
                    else
                    {// Failed to acquire value with this message bus index.
#if Matrix_Diagnostics
                        InstanceMonitor.Error("Client with this message bus index can not be assigned.");
#endif
                        return(ClientId.InvalidMessageBusClientIndex);
                    }

                    // Assign the client to its former spot.
                    _clientsHotSwap[clientId.LocalMessageBusIndex] = client;
                    index = clientId.LocalMessageBusIndex;
                }
                else
                {
                    if (GetClientIndexByGuid(clientId.Guid) >= 0)
                    {// Already added.
#if Matrix_Diagnostics
                        InstanceMonitor.Error("Message bus client [" + clientId.ToString() + "] added more than once.");
#endif
                        return(ClientId.InvalidMessageBusClientIndex);
                    }

                    // Add the client to a new spot.
                    _clientsHotSwap.Add(client);
                    index = _clientsHotSwap.Count - 1;
                }


                // This type of assignment will also work with multiple entries.
                // This performs an internal hotswap.
                _guidToIndexHotSwap[clientId.Guid] = index;
            }

            if (client != null &&
                client.AssignMessageBus(this, index) == false)
            {
#if Matrix_Diagnostics
                InstanceMonitor.OperationError("A client has denied adding to Message bus.");
#endif
                RemoveClient(client, true);
                return(ClientId.InvalidMessageBusClientIndex);
            }

            client.UpdateEvent += new MessageBusClient.ClientUpdateDelegate(client_UpdateEvent);

            RaiseClientAddedEvent(clientId);

            return(index);
        }
Exemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 public override bool ContainsClient(ClientId clientId)
 {
     return(_guidToIndexHotSwap.ContainsKey(clientId.Guid));
 }
Exemplo n.º 5
0
 protected abstract Outcomes DoSend(ClientId senderIndex, IEnumerable <ClientId> receiversIndeces, Envelope envelope, TimeSpan?requestConfirmTimeout, bool showErrorsDiagnostics);
Exemplo n.º 6
0
 /// <summary>
 /// Send an item to a single repicient.
 /// </summary>
 /// <returns></returns>
 public Outcomes Send(ClientId senderId, ClientId receiverId, Envelope envelope, TimeSpan?requestConfirmTimeout, bool showErrorsDiagnostics)
 {
     return(Send(senderId, new ClientId[] { receiverId }, envelope, requestConfirmTimeout, showErrorsDiagnostics));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Send an item to multiple recipients.
 /// </summary>
 public Outcomes Send(ClientId senderId, IEnumerable <ClientId> receiversIds, Envelope envelope, TimeSpan?requestConfirmTimeout, bool showErrorsDiagnostics)
 {
     return(DoSend(senderId, receiversIds, envelope, requestConfirmTimeout, showErrorsDiagnostics));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Obtain an instance of the client, in case it is a local client
 /// and the instance is accessible. This will not work for remote
 /// (network) associated clients.
 /// </summary>
 public abstract MessageBusClient GetLocalClientInstance(ClientId clientId);
Exemplo n.º 9
0
 public abstract List <string> GetClientSourceTypes(ClientId clientId);
Exemplo n.º 10
0
 public abstract Type GetClientType(ClientId clientId);
Exemplo n.º 11
0
 public abstract bool ContainsClient(ClientId client);
Exemplo n.º 12
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public EnvelopeStamp(long stampId, ClientId receiverId, ClientId senderId)
 {
     _stampId    = stampId;
     _receiverId = receiverId;
     _senderId   = senderId;
 }