Example #1
0
        private void LookFor(string search, string sender)
        {
            if (LookTable.ContainsKey(search))
            {
                return;
            }

            LookTable.Add(search, new KeyValuePair <string, int>(sender, TIMEOUT));

            lock (NeighborsTable)
            {
                foreach (string neigborAddress in NeighborsTable)
                {
                    if (neigborAddress != sender)
                    {
                        Node neigbor = Program.NodeList.First <Node>(x => x.Address == neigborAddress);

                        RFMessage routeAnswer = new RFMessage();
                        routeAnswer.From   = Address;
                        routeAnswer.To     = neigbor.Address;
                        routeAnswer.Header = 0x40; //01000000 ROUTE ANSWER
                        routeAnswer.Data   = search;
                        neigbor.SendMessage(routeAnswer);
                    }
                }
            }
            mainLoopTimer.Enabled = true;
        }
Example #2
0
        private void transmitRF(RFMessage message)
        {
            message.Data += "\n" + Address + " -> ";
            Node Dest = Program.NodeList.First <Node>(x => x.Address == RouteTable[message.To].Key);

            Dest.SendMessage(message);
        }
Example #3
0
        public void Transmit(string message, string to)
        {
            RFMessage rfMsg = new RFMessage();

            rfMsg.Root   = Address;
            rfMsg.From   = Address;
            rfMsg.To     = to;
            rfMsg.Header = 0x80;//DATA HEADER
            rfMsg.Data   = message;


            if (RouteTable.ContainsKey(to))
            {
                transmitRF(rfMsg);
            }
            else
            {
                LookFor(to, null);
                outputBuffer.Add(rfMsg, false);
            }
        }
Example #4
0
        void mainLoopTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (this)
            {
                mainLoopTimer.Enabled = false;
                RefreshNeigbors();
                TimeSpan iterationTime = new TimeSpan(DateTime.Now.Ticks);
                int      diff          = (int)(iterationTime - lastIterationTime).TotalMilliseconds;
                lastIterationTime = iterationTime;

                if (LookTable.Count > 0) //Check pending searches
                {
                    List <string> items = LookTable.Keys.ToList();
                    for (int i = 0; i < items.Count; i++)
                    {
                        string search = items[i];
                        var    aux    = LookTable[search];
                        if (aux.Value > 0)
                        {
                            int newCount = aux.Value - diff;

                            LookTable[search] = new KeyValuePair <string, int>(aux.Key, newCount);
                        }
                        else
                        {
                            LookTable.Remove(search);
                        }
                    }
                }

                if (outputBuffer.Count > 0)//Messages to send
                {
                    List <RFMessage> items = outputBuffer.Keys.ToList();
                    //List<RFMessage> removeList = new List<RFMessage>();

                    foreach (var outmsg in items)
                    {
                        if (!LookTable.ContainsKey(outmsg.To))//Look finished
                        {
                            transmitRF(outmsg);
                            //removeList.Add(outmsg);
                            outputBuffer.Remove(outmsg);
                        }
                        else if (LookTable[outmsg.To].Value <= 0)
                        {
                            Program.MessageBoxCustom("Timeout superado", "MESSAGE FROM " + outmsg.Root + " TO " + outmsg.To + "           ");
                            //removeList.Add(outmsg);
                            outputBuffer.Remove(outmsg);
                            LookTable.Remove(outmsg.To);
                        }
                    }
                    //outputBuffer.RemoveAll(x => removeList.Contains(x));
                }

                if (inputBuffer.Count > 0)//Messages to read
                {
                    RFMessage message    = inputBuffer[0];
                    byte      maskHeader = (byte)(message.Header & 0xC0);

                    if (maskHeader == 0x40)//Is RouteAnswer?
                    {
                        if (RouteTable.ContainsKey(message.Data))
                        {
                            RFMessage routeResponse = new RFMessage();
                            routeResponse.To     = message.From;
                            routeResponse.From   = Address;
                            routeResponse.Header = (byte)(0x20 | RouteTable[message.Data].Value); //RouteResponse OK y longitud almacenada en la routeTable
                            routeResponse.Data   = message.Data;                                  //Search Node

                            Node Dest = Program.NodeList.First <Node>(x => x.Address == message.From);
                            Dest.SendMessage(routeResponse);
                        }
                        else
                        {
                            LookFor(message.Data, message.From);
                        }
                    }
                    else if (maskHeader == 0x00)                     //Is RouteResponse?
                    {
                        bool isOK = (message.Header & 0xE0) == 0x20; //is OK?
                        if (isOK)
                        {
                            byte distance = (byte)(message.Header & 0x1F);
                            if (distance == 0x1F)
                            {
                                throw new IndexOutOfRangeException(); //Distance Overflow
                            }
                            if (RouteTable.ContainsKey(message.Data))
                            {
                                if ((distance + 1) < RouteTable[message.Data].Value) //Short way
                                {
                                    RouteTable[message.Data] = new KeyValuePair <string, int>(message.From, distance + 1);
                                }
                            }
                            else
                            {
                                RouteTable.Add(message.Data, new KeyValuePair <string, int>(message.From, distance + 1));
                            }
                        }
                        else if (RouteTable.ContainsKey(message.Data) && RouteTable[message.Data].Key == message.From)
                        {
                            RouteTable.Remove(message.Data);
                            //Avisar a mis vecinos de que ya no puedo llegar al destinatario del mensaje
                            foreach (var nodeAddress in NeighborsTable)
                            {
                                if (message.From != nodeAddress)
                                {
                                    RFMessage routeResponse = new RFMessage();
                                    routeResponse.To     = nodeAddress;
                                    routeResponse.From   = Address;
                                    routeResponse.Data   = message.Data; //Search Node
                                    routeResponse.Header = 0x00;         //RouteResponse FAIL
                                    routeResponse.Root   = message.Root; //TRAMPA

                                    Node Dest = Program.NodeList.First <Node>(x => x.Address == routeResponse.To);
                                    Dest.SendMessage(routeResponse);
                                }
                            }

                            //TODO: Si existen mensajes pendientes para este nodo, se reintenta para trazar una nueva ruta
                            if (message.Root == Address)//outputBuffer.Exists(x => x.To == message.Data))
                            {
                                //RFMessage retry = outputBuffer.First(x => x.To == message.Data);
                                //outputBuffer.Remove(retry);
                                //transmitRF(retry);
                                Program.MessageBoxCustom("Ha cambiado la topologia de la red. Reenviar", "MESSAGE FROM " + Address + " TO " + message.Data + "           ");
                            }
                        }

                        if (LookTable.ContainsKey(message.Data) && LookTable[message.Data].Key != null)
                        {
                            RFMessage routeResponse = new RFMessage();
                            routeResponse.To   = LookTable[message.Data].Key;
                            routeResponse.From = Address;
                            routeResponse.Data = message.Data; //Search Node

                            if (isOK)
                            {
                                routeResponse.Header = (byte)(0x20 | (byte)(message.Header & 0x1F) + 1); //RouteResponse OK y longitud recibida + 1
                            }
                            else
                            {
                                routeResponse.Header = 0x00; //RouteResponse FAIL
                            }
                            Node Dest = Program.NodeList.First <Node>(x => x.Address == routeResponse.To);
                            Dest.SendMessage(routeResponse);
                        }

                        LookTable.Remove(message.Data);
                    }
                    else //Is a message
                    {
                        if (message.To == Address) //it's to me?
                        {
                            message.Data += Address;
                            Program.MessageBoxCustom(message.Data, "MESSAGE FROM " + message.Root + " TO " + message.To + "           ");
                        }
                        else if (RouteTable.ContainsKey(message.To))
                        {
                            message.From  = Address;
                            message.Data += Address + " -> ";
                            Node Dest = Program.NodeList.First <Node>(x => x.Address == RouteTable[message.To].Key);
                            Dest.SendMessage(message);
                        }
                        else
                        {
                            RFMessage routeResponse = new RFMessage();
                            routeResponse.To     = message.From;
                            routeResponse.From   = Address;
                            routeResponse.Data   = message.To;   //Search Node
                            routeResponse.Header = 0x00;         //RouteResponse FAIL
                            routeResponse.Root   = message.Root; //TRAMPA

                            Node Dest = Program.NodeList.First <Node>(x => x.Address == routeResponse.To);
                            Dest.SendMessage(routeResponse);
                        }
                    }

                    inputBuffer.Remove(message);
                }
            }
        }
Example #5
0
 public void SendMessage(RFMessage message)
 {
     inputBuffer.Add(message);
 }
Example #6
0
 private void transmitRF(RFMessage message)
 {
     message.Data += "\n" + Address + " -> ";
     Node Dest = Program.NodeList.First<Node>(x => x.Address == RouteTable[message.To].Key);
     Dest.SendMessage(message);
 }
Example #7
0
        void mainLoopTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (this)
            {
                mainLoopTimer.Enabled = false;
                RefreshNeigbors();
                TimeSpan iterationTime = new TimeSpan(DateTime.Now.Ticks);
                int diff = (int)(iterationTime - lastIterationTime).TotalMilliseconds;
                lastIterationTime = iterationTime;

                if (LookTable.Count > 0) //Check pending searches
                {
                    List<string> items = LookTable.Keys.ToList();
                    for (int i = 0; i < items.Count; i++)
                    {
                        string search = items[i];
                        var aux = LookTable[search];
                        if (aux.Value > 0)
                        {
                            int newCount = aux.Value - diff;

                            LookTable[search] = new KeyValuePair<string, int>(aux.Key, newCount);
                        }
                        else
                        {
                            LookTable.Remove(search);
                        }
                    }
                }

                if (outputBuffer.Count > 0)//Messages to send
                {
                    List<RFMessage> items = outputBuffer.Keys.ToList();
                    //List<RFMessage> removeList = new List<RFMessage>();

                    foreach (var outmsg in items)
                    {
                        if (!LookTable.ContainsKey(outmsg.To))//Look finished
                        {
                            transmitRF(outmsg);
                            //removeList.Add(outmsg);
                            outputBuffer.Remove(outmsg);
                        }
                        else if (LookTable[outmsg.To].Value <= 0)
                        {
                            Program.MessageBoxCustom("Timeout superado", "MESSAGE FROM " + outmsg.Root + " TO " + outmsg.To + "           ");
                            //removeList.Add(outmsg);
                            outputBuffer.Remove(outmsg);
                            LookTable.Remove(outmsg.To);
                        }
                    }
                    //outputBuffer.RemoveAll(x => removeList.Contains(x));
                }

                if (inputBuffer.Count > 0)//Messages to read
                {
                    RFMessage message = inputBuffer[0];
                    byte maskHeader = (byte)(message.Header & 0xC0);

                    if (maskHeader == 0x40)//Is RouteAnswer?
                    {
                        if (RouteTable.ContainsKey(message.Data))
                        {
                            RFMessage routeResponse = new RFMessage();
                            routeResponse.To = message.From;
                            routeResponse.From = Address;
                            routeResponse.Header = (byte)(0x20 | RouteTable[message.Data].Value); //RouteResponse OK y longitud almacenada en la routeTable
                            routeResponse.Data = message.Data; //Search Node

                            Node Dest = Program.NodeList.First<Node>(x => x.Address == message.From);
                            Dest.SendMessage(routeResponse);
                        }
                        else
                        {
                            LookFor(message.Data, message.From);
                        }
                    }
                    else if (maskHeader == 0x00)//Is RouteResponse?
                    {
                        bool isOK = (message.Header & 0xE0) == 0x20; //is OK?
                        if (isOK)
                        {
                            byte distance = (byte)(message.Header & 0x1F);
                            if (distance == 0x1F)
                                throw new IndexOutOfRangeException(); //Distance Overflow

                            if (RouteTable.ContainsKey(message.Data))
                            {
                                if ((distance + 1) < RouteTable[message.Data].Value) //Short way
                                {
                                    RouteTable[message.Data] = new KeyValuePair<string, int>(message.From, distance + 1);
                                }
                            }
                            else
                            {
                                RouteTable.Add(message.Data, new KeyValuePair<string, int>(message.From, distance + 1));
                            }
                        }
                        else if (RouteTable.ContainsKey(message.Data) && RouteTable[message.Data].Key == message.From)
                        {
                            RouteTable.Remove(message.Data);
                            //Avisar a mis vecinos de que ya no puedo llegar al destinatario del mensaje
                            foreach (var nodeAddress in NeighborsTable)
                            {
                                if (message.From != nodeAddress)
                                {
                                    RFMessage routeResponse = new RFMessage();
                                    routeResponse.To = nodeAddress;
                                    routeResponse.From = Address;
                                    routeResponse.Data = message.Data; //Search Node
                                    routeResponse.Header = 0x00; //RouteResponse FAIL
                                    routeResponse.Root = message.Root;//TRAMPA

                                    Node Dest = Program.NodeList.First<Node>(x => x.Address == routeResponse.To);
                                    Dest.SendMessage(routeResponse);
                                }
                            }

                            //TODO: Si existen mensajes pendientes para este nodo, se reintenta para trazar una nueva ruta
                            if (message.Root == Address)//outputBuffer.Exists(x => x.To == message.Data))
                            {
                                //RFMessage retry = outputBuffer.First(x => x.To == message.Data);
                                //outputBuffer.Remove(retry);
                                //transmitRF(retry);
                                Program.MessageBoxCustom("Ha cambiado la topologia de la red. Reenviar", "MESSAGE FROM " + Address + " TO " + message.Data + "           ");
                            }
                        }

                        if (LookTable.ContainsKey(message.Data) && LookTable[message.Data].Key != null)
                        {
                            RFMessage routeResponse = new RFMessage();
                            routeResponse.To = LookTable[message.Data].Key;
                            routeResponse.From = Address;
                            routeResponse.Data = message.Data; //Search Node

                            if (isOK)
                                routeResponse.Header = (byte)(0x20 | (byte)(message.Header & 0x1F) + 1); //RouteResponse OK y longitud recibida + 1
                            else
                                routeResponse.Header = 0x00; //RouteResponse FAIL

                            Node Dest = Program.NodeList.First<Node>(x => x.Address == routeResponse.To);
                            Dest.SendMessage(routeResponse);
                        }

                        LookTable.Remove(message.Data);
                    }
                    else //Is a message
                    {
                        if (message.To == Address) //it's to me?
                        {
                            message.Data += Address;
                            Program.MessageBoxCustom(message.Data, "MESSAGE FROM " + message.Root + " TO " + message.To + "           ");
                        }
                        else if (RouteTable.ContainsKey(message.To))
                        {
                            message.From = Address;
                            message.Data += Address + " -> ";
                            Node Dest = Program.NodeList.First<Node>(x => x.Address == RouteTable[message.To].Key);
                            Dest.SendMessage(message);
                        }
                        else
                        {
                            RFMessage routeResponse = new RFMessage();
                            routeResponse.To = message.From;
                            routeResponse.From = Address;
                            routeResponse.Data = message.To; //Search Node
                            routeResponse.Header = 0x00; //RouteResponse FAIL
                            routeResponse.Root = message.Root;//TRAMPA

                            Node Dest = Program.NodeList.First<Node>(x => x.Address == routeResponse.To);
                            Dest.SendMessage(routeResponse);
                        }

                    }

                    inputBuffer.Remove(message);
                }
            }
        }
Example #8
0
        private void LookFor(string search, string sender)
        {
            if (LookTable.ContainsKey(search))
                return;

            LookTable.Add(search, new KeyValuePair<string, int>(sender, TIMEOUT));

            lock (NeighborsTable)
            {
                foreach (string neigborAddress in NeighborsTable)
                {
                    if (neigborAddress != sender)
                    {
                        Node neigbor = Program.NodeList.First<Node>(x => x.Address == neigborAddress);

                        RFMessage routeAnswer = new RFMessage();
                        routeAnswer.From = Address;
                        routeAnswer.To = neigbor.Address;
                        routeAnswer.Header = 0x40; //01000000 ROUTE ANSWER
                        routeAnswer.Data = search;
                        neigbor.SendMessage(routeAnswer);
                    }
                }
            }
            mainLoopTimer.Enabled = true;
        }
Example #9
0
        public void Transmit(string message, string to)
        {
            RFMessage rfMsg = new RFMessage();
            rfMsg.Root = Address;
            rfMsg.From = Address;
            rfMsg.To = to;
            rfMsg.Header = 0x80;//DATA HEADER
            rfMsg.Data = message;

            if (RouteTable.ContainsKey(to))
            {
                transmitRF(rfMsg);
            }
            else
            {
                LookFor(to, null);
                outputBuffer.Add(rfMsg, false);
            }
        }
Example #10
0
 public void SendMessage(RFMessage message)
 {
     inputBuffer.Add(message);
 }