/// <summary> /// Procesa los mensajes tipo Safe que no han sido confirmados /// </summary> public void proccessNotConfirmedMessages() { Message[] messages = notConfirmedMessageList.messageListToArray(); int nMessages = messages.Length; if (nMessages > netHandler.NetData.MaxMessagesProcess) { nMessages = netHandler.NetData.MaxMessagesProcess; } for (int i = 0; i < nMessages; i++) { SafeUnicastMessage safeMessage = (SafeUnicastMessage)messages[i]; if (safeMessage.WaitTimeOut <= 0) { //statics nMessagesReplayed++; notConfirmedMessageList.remove(safeMessage.Id); queueMessageToSend(safeMessage); } else { safeMessage.WaitTimeOut--; } } }
/// <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; } } } }
/// <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); }
/// <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); } } }