Exemple #1
0
        private void OnUnicastMessage(object sender, UnicastMessage e)
        {
            e.Username = Users.FindByClient((sender as ClientOperator)?.TcpClient);
            var user = Users.Where(kv => e.UserReciever == kv.Key || e.Username == kv.Key).Select(kv => kv.Key).ToList();

            user.Add(e.Username);
            UniCast(e, user);
        }
Exemple #2
0
        public void SendUnicastMessage(string sendMessage, string userReciever)
        {
            var command = new UnicastMessage()
            {
                UserReciever = userReciever,
                Message      = sendMessage
            };

            Write(command);
        }
Exemple #3
0
        private void _client_UnicastMessageReceived(object sender, UnicastMessage e)
        {
            var localuser = Settings.Username + Ip;
            var message   = new MessageViewModel()
            {
                Username   = e.Username,
                DateTime   = e.DateTime,
                Message    = e.Message,
                TargetInfo = localuser == e.Username ? e.UserReciever : e.Username
            };

            _messagesRevieved.Add(message);
            MessageWpfShow?.Invoke(message, "Recieved");
        }
Exemple #4
0
        public Task <IEnumerable <UnicastMessage <MailMessage> > > GetMailMessages(Signature signature, ExchangePrivateKey exchangePrivateKey)
        {
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }
            if (exchangePrivateKey == null)
            {
                throw new ArgumentNullException(nameof(exchangePrivateKey));
            }

            return(Task.Run(() =>
            {
                var results = new List <UnicastMessage <MailMessage> >();

                var trusts = new List <UnicastMetadata>();

                foreach (var unicastMetadata in _coreManager.GetUnicastMetadatas(signature, "MailMessage"))
                {
                    if (_searchSignatures.Contains(unicastMetadata.Certificate.GetSignature()))
                    {
                        trusts.Add(unicastMetadata);
                    }
                }

                trusts.Sort((x, y) => y.CreationTime.CompareTo(x.CreationTime));

                foreach (var unicastMetadata in trusts.Take(1024))
                {
                    var dic = _cache_MailMessages.GetOrAdd(unicastMetadata.Signature, (_) => new LockedHashDictionary <UnicastMetadata, UnicastMessage <MailMessage> >());

                    // Cache
                    {
                        if (dic.TryGetValue(unicastMetadata, out var result))
                        {
                            results.Add(result);

                            continue;
                        }
                    }

                    {
                        var stream = _coreManager.VolatileGetStream(unicastMetadata.Metadata, 1024 * 1024 * 1);
                        if (stream == null)
                        {
                            continue;
                        }

                        var result = new UnicastMessage <MailMessage>(
                            unicastMetadata.Signature,
                            unicastMetadata.Certificate.GetSignature(),
                            unicastMetadata.CreationTime,
                            ContentConverter.FromCryptoStream <MailMessage>(stream, exchangePrivateKey));

                        if (result.Value == null)
                        {
                            continue;
                        }

                        dic[unicastMetadata] = result;

                        results.Add(result);
                    }
                }

                return (IEnumerable <UnicastMessage <MailMessage> >)results.ToArray();
            }));
        }
Exemple #5
0
        /// <summary>
        /// Procesa los mensajes de tipo Unicast que no han sido enviados con exito
        /// </summary>
        public void proccessFailedMessages()
        {
            int nMessages = failedMessageQueue.size();

            if (nMessages > netHandler.NetData.MaxMessagesProcess)
            {
                nMessages = netHandler.NetData.MaxMessagesProcess;
            }
            for (int i = 0; i < nMessages; i++)
            {
                Message message = failedMessageQueue.draw();
                switch (message.FailReason)
                {
                case MessageFailReason.TCPFAIL:
                {
                    queueMessageToSend(message);
                    break;
                }

                case MessageFailReason.NOTROUTEBUTHOSTONNET:
                {
                    queueMessageToSend(message);
                    break;
                }

                case MessageFailReason.NOTROUTETOHOST:
                {
                    if (message.SenderNetUser.Id.Equals(netUser.Id))
                    {
                        if (message.MetaType == MessageMetaType.MULTICAST)
                        {
                            messageError(message);
                        }
                        else if (message.MetaType == MessageMetaType.SAFEMULTICAST)
                        {
                            messageError(message);
                        }
                        else if (message.MetaType == MessageMetaType.UNICAST)
                        {
                            UnicastMessage unicastMessage = (UnicastMessage)message;
                            messageError(message);
                        }
                        else if (message.MetaType == MessageMetaType.SAFEUNICAST)
                        {
                            //statics
                            nMessagesDroped++;
                            SafeUnicastMessage safeUnicastMessage = (SafeUnicastMessage)message;
                            messageError(message);
                        }
                    }
                    break;
                }

                case MessageFailReason.DESTROY:
                {
                    if (message.SenderNetUser.Id.Equals(netUser.Id))
                    {
                        if (message.MetaType == MessageMetaType.MULTICAST)
                        {
                            messageError(message);
                        }
                        else if (message.MetaType == MessageMetaType.SAFEMULTICAST)
                        {
                            messageError(message);
                        }
                        else if (message.MetaType == MessageMetaType.UNICAST)
                        {
                            UnicastMessage unicastMessage = (UnicastMessage)message;
                            messageError(message);
                        }
                        else if (message.MetaType == MessageMetaType.SAFEUNICAST)
                        {
                            //statics
                            nMessagesDroped++;
                            SafeUnicastMessage safeUnicastMessage = (SafeUnicastMessage)message;
                            messageError(message);
                        }
                    }
                    else
                    {
                        if (message.GetType().BaseType.Equals(typeof(SafeUnicastMessage)))
                        {
                            //statics
                            nMessagesDestroyed++;
                        }
                    }
                    break;
                }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Identifica un mensaje de bajo nivel recibido y lo retorna como mensaje de alto nivel, y/o lo rutea segun corresponde
        /// </summary>
        /// <param name="netMessage">El mensaje recibido de bajo nivel</param>
        /// <returns>el mensaje de alto nivel recibido, o null si se ruteó hacia otra parte</returns>
        public Message attendMessage(NetMessage netMessage)
        {
            Int32 messageMetaType = messageFactory.getMessageMetaType(netMessage.Body);

            if (messageMetaType == MessageMetaType.MULTICAST)
            {
                Guid messageId = messageFactory.getMessageId(netMessage.Body);
                if (!messageIdCollection.contains(messageId))
                {
                    Message message = messageFactory.makeMessage(netMessage.Body);
                    if (message != null)
                    {
                        message.Jumps++;
                        if (message.Jumps > 1)
                        {
                            queueMessageToSend(message);
                        }
                        else
                        {
                            if (rand.NextDouble() < 0.5)
                            {
                                queueMessageToSend(message);
                            }
                            else
                            {
                                messageIdCollection.add(message.Id);
                                nMessagesCounted++;
                            }
                        }
                        return(message);
                    }
                }
            }
            else if (messageMetaType == MessageMetaType.SAFEMULTICAST)
            {
                Guid messageId = messageFactory.getMessageId(netMessage.Body);
                if (!messageIdCollection.contains(messageId))
                {
                    Message message = messageFactory.makeMessage(netMessage.Body);
                    if (message != null)
                    {
                        message.Jumps++;
                        queueMessageToSend(message);
                        return(message);
                    }
                }
            }
            else if (messageMetaType == MessageMetaType.UNICAST)
            {
                Message message = messageFactory.makeMessage(netMessage.Body);
                if (message != null)
                {
                    message.Jumps++;
                    UnicastMessage unicastMessage = (UnicastMessage)message;
                    if (unicastMessage.TargetNetUser.Id.Equals(netUser.Id))
                    {
                        return(message);
                    }
                    else
                    {
                        //Parametrizar
                        if (notSentMessageQueue.size() > 50 || failedMessageQueue.size() > 50)
                        {
                            nMessagesFailed++;
                            unicastMessage.FailReason = MessageFailReason.DESTROY;
                            failedMessageQueue.put(unicastMessage);
                        }
                        else
                        {
                            nMessagesRouted++;
                            queueMessageToSend(unicastMessage);
                        }
                    }
                }
            }
            else if (messageMetaType == MessageMetaType.SAFEUNICAST)
            {
                //Enviamos el ack primero que todo
                NetUser targetNetUser = messageFactory.getTargetNetUser(netMessage.Body);
                if (netUser.Id.Equals(targetNetUser.Id))
                {
                    Guid    messageId     = messageFactory.getMessageId(netMessage.Body);
                    NetUser senderNetUser = messageFactory.getSenderNetUser(netMessage.Body);
                    if (senderNetUser != null)
                    {
                        AckMessage ackMessage = new AckMessage(senderNetUser, messageId);
                        ackMessage.SenderNetUser = netUser;
                        queueMessageToSend(ackMessage);
                    }
                }
                //PRocesamos en mensaje
                Message message = messageFactory.makeMessage(netMessage.Body);
                if (message != null)
                {
                    message.Jumps++;
                    SafeUnicastMessage safeMessage = (SafeUnicastMessage)message;
                    if (safeMessage.TargetNetUser.Id.Equals(netUser.Id))
                    {
                        if (!messageIdCollection.contains(safeMessage.Id))
                        {
                            //statics
                            nMessagesReceived++;
                            messageIdCollection.add(safeMessage.Id);
                            nMessagesCounted++;
                            return(message);
                        }
                    }
                    else
                    {
                        //Parametrizar
                        if (notSentMessageQueue.size() > 50 || failedMessageQueue.size() > 50)
                        {
                            nMessagesFailed++;
                            safeMessage.FailReason = MessageFailReason.DESTROY;
                            failedMessageQueue.put(safeMessage);
                        }
                        else
                        {
                            nMessagesRouted++;
                            queueMessageToSend(safeMessage);
                        }
                    }
                }
            }
            else if (messageMetaType == MessageMetaType.FASTUNICAST)
            {
                Message message = messageFactory.makeMessage(netMessage.Body);
                if (message != null)
                {
                    message.Jumps++;
                    FastUnicastMessage fastUnicastMessage = (FastUnicastMessage)message;
                    if (fastUnicastMessage.TargetNetUser.Id.Equals(netUser.Id))
                    {
                        return(message);
                    }
                    else
                    {
                        nMessagesRouted++;
                        queueMessageToSend(fastUnicastMessage);
                    }
                }
            }
            return(null);
        }
Exemple #7
0
 /// <summary>
 /// Identifica el tipo de mensaje a enviar y lo envia mediante un netHandler
 /// </summary>
 /// <param name="message">El mensaje e enviar</param>
 private void send(Message message)
 {
     if (message.MetaType == MessageMetaType.MULTICAST)
     {
         MulticastMessage multicastMessage = (MulticastMessage)message;
         multicastMessage.send(netHandler);
     }
     else if (message.MetaType == MessageMetaType.SAFEMULTICAST)
     {
         SafeMulticastMessage safeMulticastMessage = (SafeMulticastMessage)message;
         safeMulticastMessage.send(netHandler);
     }
     else if (message.MetaType == MessageMetaType.UNICAST)
     {
         UnicastMessage unicastMessage = (UnicastMessage)message;
         IPAddress      ip             = pathNextIp(unicastMessage.TargetNetUser);
         NetUser        listedNetUSer  = netUserList.getUser(unicastMessage.TargetNetUser.Ip);
         if (ip != null)
         {
             if (!unicastMessage.send(netHandler, ip))
             {
                 unicastMessage.FailReason = MessageFailReason.TCPFAIL;
                 failedMessageQueue.put(unicastMessage);
             }
         }
         else if (listedNetUSer != null && listedNetUSer.Id.Equals(unicastMessage.TargetNetUser.Id))
         {
             unicastMessage.FailReason = MessageFailReason.NOTROUTEBUTHOSTONNET;
             failedMessageQueue.put(unicastMessage);
         }
         else
         {
             unicastMessage.FailReason = MessageFailReason.NOTROUTETOHOST;
             failedMessageQueue.put(unicastMessage);
         }
     }
     else if (message.MetaType == MessageMetaType.SAFEUNICAST)
     {
         SafeUnicastMessage safeMessage   = (SafeUnicastMessage)message;
         IPAddress          ip            = pathNextIp(safeMessage.TargetNetUser);
         NetUser            listedNetUser = netUserList.getUser(safeMessage.TargetNetUser.Ip);
         if (ip != null)
         {
             if (safeMessage.send(netHandler, ip))
             {
                 //statics
                 nMessagesSent++;
                 //si el mensaje enviado fue de este usuario se espera confirmación
                 if (safeMessage.SenderNetUser.Id.Equals(netUser.Id))
                 {
                     safeMessage.WaitTimeOut = waitForAck + 2 * safeMessage.TargetNetUser.JumpsAway;
                     notConfirmedMessageList.add(safeMessage);
                 }
             }
             else
             {
                 //statics
                 nMessagesFailed++;
                 safeMessage.FailReason = MessageFailReason.TCPFAIL;
                 failedMessageQueue.put(safeMessage);
             }
         }
         else if (listedNetUser != null && listedNetUser.Id.Equals(safeMessage.TargetNetUser.Id))
         {
             safeMessage.FailReason = MessageFailReason.NOTROUTEBUTHOSTONNET;
             failedMessageQueue.put(safeMessage);
         }
         else
         {
             safeMessage.FailReason = MessageFailReason.DESTROY;
             failedMessageQueue.put(safeMessage);
         }
     }
     else if (message.MetaType == MessageMetaType.FASTUNICAST)
     {
         FastUnicastMessage fastUnicastMessage = (FastUnicastMessage)message;
         IPAddress          ip            = pathNextIp(fastUnicastMessage.TargetNetUser);
         NetUser            listedNetUSer = netUserList.getUser(fastUnicastMessage.TargetNetUser.Ip);
         if (ip != null)
         {
             fastUnicastMessage.send(netHandler, ip);
         }
     }
 }