Ejemplo n.º 1
0
        public bool IsNeigbor(NodeuC node)
        {
            float dx = node.Position.X - Position.X;
            float dy = node.Position.Y - Position.Y;

            return(((dx * dx) + (dy * dy) < Range * Range) && node.NodeAddress != this.NodeAddress);
        }
Ejemplo n.º 2
0
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible        = true;

            Random ran = new Random();

            int[] locations = { 100, 200,
                                175, 200,
                                250, 200,
                                325, 200,
                                400, 200,
                                475, 200 };

            int height = Window.ClientBounds.Height - 30;
            int width  = Window.ClientBounds.Width - 30;

            int numberOfNodes = 10;//MAX = 255
            int size          = (int)MathHelper.Clamp(29f - 0.040f * (float)numberOfNodes, 20, 40);

            for (int i = 0; i < numberOfNodes; i++)
            {
                NodeuC node   = new NodeuC(size);
                int    margin = node.Size / 2;

                if (locations.Length > 2 * i)
                {
                    node.Position = new Vector2(locations[2 * i], locations[(2 * i) + 1]);
                }
                else
                {
                    node.Position = new Vector2(ran.Next(margin, width - margin), ran.Next(margin, height - node.Size * 5));
                }

                node.NodeAddress = (byte)(i + 1);
                Program.NodeListuC.Add(node);
            }
            graphics.PreferredBackBufferWidth = Window.ClientBounds.Width + rightMargin;
            Window.AllowUserResizing          = true;
        }
Ejemplo n.º 3
0
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;

            Random ran = new Random();
            int[] locations = { 100, 200,
                                175, 200,
                                250, 200,
                                325, 200,
                                400, 200,
                                475, 200
                              };

            int height = Window.ClientBounds.Height - 30;
            int width = Window.ClientBounds.Width - 30;

            int numberOfNodes = 10;//MAX = 255
            int size = (int)MathHelper.Clamp(29f-0.040f*(float)numberOfNodes, 20, 40);

            for (int i = 0; i < numberOfNodes; i++)
            {
                NodeuC node = new NodeuC(size);
                int margin = node.Size / 2;

                if (locations.Length > 2 * i)
                    node.Position = new Vector2(locations[2 * i], locations[(2 * i) + 1]);
                else
                    node.Position = new Vector2(ran.Next(margin, width - margin), ran.Next(margin, height - node.Size * 5));

                node.NodeAddress = (byte)(i + 1);
                Program.NodeListuC.Add(node);
            }
            graphics.PreferredBackBufferWidth = Window.ClientBounds.Width + rightMargin;
            Window.AllowUserResizing = true;
        }
Ejemplo n.º 4
0
        private void LookFor(byte reference, byte neighborInterested, byte rootInterested, byte distance) //RootInterested represent the first interesed, who start the interrogation
        {
            //This occur when many neighbors ask for the same node
            if (LookTable.ContainsKey(reference))
            {
                return;
            }

            //This check prevents a node with no neighbors store a search in lookTable
            if (neighborInterested == 0 || NeighborsTable.Count > 1)
            {
                LookTable.Add(reference, new byte[] { neighborInterested, 0, (byte)(distance - 1) });

                //Send RouteAnswer to everyone in NeighborsTable != neighborInterested
                lock (NeighborsTable)
                {
                    foreach (byte neigborAddress in NeighborsTable)
                    {
                        if (neigborAddress != neighborInterested)
                        {
                            NodeuC neigbor = Program.GetNode(neigborAddress);

                            ROUTE_MSG routeAnswer = new ROUTE_MSG();
                            routeAnswer.header_distance = distance;
                            routeAnswer.header_restype  = 1; //ANSWER
                            routeAnswer.header_type     = 0; //ROUTE
                            routeAnswer.from            = rootInterested;
                            routeAnswer.to        = neigborAddress;
                            routeAnswer.parent    = NodeAddress;
                            routeAnswer.reference = reference;

                            neigbor.SendMessage(routeAnswer);
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        protected override void Update(GameTime gameTime)
        {
            MouseState mouse = Mouse.GetState();
            KeyboardState currentKeyboardState = Keyboard.GetState();

            if (CheckKeyReleased(Keys.P, currentKeyboardState, previousKeyboardState))
                pauseSim = !pauseSim;

            //Restore color nodes
            if (CheckKeyReleased(Keys.C, currentKeyboardState, previousKeyboardState))
            {
                foreach (NodeuC node in Program.NodeListuC)
                    node.Color = Color.DarkBlue;
            }

            //Reset route tables
            if (CheckKeyReleased(Keys.R, currentKeyboardState, previousKeyboardState))
            {
                foreach (NodeuC node in Program.NodeListuC)
                    node.RouteTable.Clear();
            }

            if (CheckKeyReleased(Keys.Add, currentKeyboardState, previousKeyboardState))
                Program.SleepDelay = Math.Min(1000, Program.SleepDelay + 100);
            else if (CheckKeyReleased(Keys.Subtract, currentKeyboardState, previousKeyboardState))
                Program.SleepDelay = Math.Max(100, Program.SleepDelay - 100);

            Window.Title = "Simulation delay: " + Program.SleepDelay + "ms";

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            bool leftPressed = mouse.LeftButton == ButtonState.Pressed;
            bool rightClick = mouse.RightButton == ButtonState.Pressed && mouseLast.RightButton == ButtonState.Released;
            Vector2 mousePosition = new Vector2(mouse.X, mouse.Y);
            bool selected = false;

            foreach (var node in Program.NodeListuC)
            {
                node.ShowAddress = node.ContainsPoint(mousePosition);
                node.Paused = pauseSim;

                if (!selected && leftPressed && (node.ContainsPoint(mousePosition) || node.IsSelected))
                {
                    if(mouse.X < Window.ClientBounds.Width - rightMargin)
                        node.Position = mousePosition;
                    node.IsSelected = true;
                    selected = true;
                }
                else
                {
                    node.IsSelected = false;
                }

                if (rightClick && node.ContainsPoint(mousePosition))
                {
                    if (selectedNode == null) //First click
                    {
                        selectedNode = node;
                        node.Color = Color.Pink;
                    }
                    else //Second click
                    {
                        if (node != selectedNode)
                        {
                            selectedNode.Color = Color.DarkBlue;
                            Program.init = DateTime.Now;
                            selectedNode.Transmit(node.NodeAddress, "HOLA");
                        }
                        selectedNode = null;
                    }
                }

                node.Update();
            }

            mouseLast = mouse;
            previousKeyboardState = currentKeyboardState;

            // TODO: Add your update logic here

            base.Update(gameTime);
        }
Ejemplo n.º 6
0
        private void loop()
        {
            TimeSpan iterationTime = new TimeSpan(DateTime.Now.Ticks);

            refreshNeigbors();

            if (routeBuffer.Count > 0) //check the outstanding route messages
            {
                ROUTE_MSG message = routeBuffer[0];

                if (message.header_restype == 1) //Route Answer
                {
                    bool isNeighbor = NeighborsTable.Contains(message.reference);

                    //Check if the first variable returns true, because in this case the second search is not needed
                    if (isNeighbor || RouteTable.ContainsKey(message.reference))
                    {
                        ROUTE_MSG routeResponse = new ROUTE_MSG();
                        if (isNeighbor)
                        {
                            routeResponse.header_distance = 1;
                        }
                        else
                        {
                            routeResponse.header_distance = (byte)(RouteTable[message.reference][1] + 1);
                        }
                        routeResponse.header_restype = 0; //RESPONSE
                        routeResponse.header_type    = 0; //ROUTE
                        routeResponse.header_ok      = 1;

                        routeResponse.from      = NodeAddress;
                        routeResponse.to        = message.from;
                        routeResponse.parent    = NodeAddress;
                        routeResponse.reference = message.reference;

                        NodeuC Dest = Program.GetNode(message.parent);
                        Dest.SendMessage(routeResponse);

                        //TODO: Notify the node sought to avoid a possible subsequent search in the opposite
                        routeResponse.header_distance = (byte)(message.header_distance + 1);
                        routeResponse.from            = NodeAddress;
                        routeResponse.to        = message.reference;
                        routeResponse.parent    = NodeAddress;
                        routeResponse.reference = message.from;
                        Dest = Program.GetNode(isNeighbor ? message.reference : RouteTable[message.reference][0]);
                        Dest.SendMessage(routeResponse);

                        addToRouteTable(message.from, message.parent, message.header_distance);
                    }
                    else
                    {
                        LookFor(message.reference, message.parent, message.from, (byte)(message.header_distance + 1));
                    }
                    Color = Color.Yellow;
                }
                else //Route Response
                {
                    if (message.header_ok > 0)
                    {
                        addToRouteTable(message.reference, message.parent, message.header_distance);

                        if (LookTable.ContainsKey(message.reference))
                        {
                            byte interested = LookTable[message.reference][0];
                            if (interested != 0)
                            {
                                ROUTE_MSG routeResponse = new ROUTE_MSG();
                                routeResponse.header_distance = (byte)(message.header_distance + 1);
                                routeResponse.header_restype  = 0; //RESPONSE
                                routeResponse.header_type     = 0; //ROUTE
                                routeResponse.header_ok       = 1;

                                routeResponse.from      = message.from;
                                routeResponse.to        = message.to;
                                routeResponse.parent    = NodeAddress;
                                routeResponse.reference = message.reference;

                                NodeuC Dest = Program.GetNode(interested);
                                Dest.SendMessage(routeResponse);

                                //Store the way back if distance > 0 (message.to is not a neighbor)
                                byte distance = LookTable[message.reference][2];
                                if (distance > 0)
                                {
                                    addToRouteTable(message.to, interested, distance);
                                }
                            }
                            LookTable.Remove(message.reference);
                            Color = Color.Purple;
                        }
                    }
                    //This case is given when it tried to route a message from a node that is no longer able to reach the recipient.
                    else if (RouteTable.ContainsKey(message.reference) && RouteTable[message.reference][0] == message.parent)
                    {
                        RouteTable.Remove(message.reference);

                        //Notify the neighbors that I can't reach the intended recipient
                        foreach (var neighborAddress in NeighborsTable)
                        {
                            if (message.parent != neighborAddress)
                            {
                                ROUTE_MSG routeResponse = new ROUTE_MSG();
                                routeResponse.header_restype  = 0; //RESPONSE
                                routeResponse.header_type     = 0; //ROUTE
                                routeResponse.header_ok       = 0; //FAIL
                                routeResponse.header_distance = message.header_distance;
                                routeResponse.from            = NodeAddress;
                                routeResponse.to        = message.to;
                                routeResponse.parent    = NodeAddress;
                                routeResponse.reference = message.reference;

                                NodeuC Dest = Program.GetNode(neighborAddress);
                                Dest.SendMessage(routeResponse);
                            }
                        }

                        //TODO: If there are pending messages for this node, try to trace a new route
                        if (message.to == NodeAddress)
                        {
                            Program.MessageBoxCustom("Ha cambiado la topología de la red. Reenviar", "MESSAGE " + message.header_distance +
                                                     " FROM " + NodeAddress + " TO " + message.reference + "           ");
                        }
                    }
                }

                routeBuffer.Remove(message);
            }

            if (dataInputBuffer.Count > 0)//Data messages to read
            {
                Color = Color.YellowGreen;
                DATA_MSG message = dataInputBuffer[0];
                bool     isNeighbor;
                if (message.to == NodeAddress) //it's to me?
                {
                    Color         = Color.Magenta;
                    message.data += NodeAddress;
                    Program.MessageBoxCustom(message.data, "MESSAGE " + message.header_id +
                                             " FROM " + message.from + " TO " + message.to + " BY " + message.parent + "           ");
                }
                else if ((isNeighbor = NeighborsTable.Contains(message.to)) || RouteTable.ContainsKey(message.to))
                {
                    message.parent = NodeAddress;
                    message.data  += NodeAddress + " -> ";
                    NodeuC Dest = Program.GetNode(isNeighbor ? message.to : RouteTable[message.to][0]);
                    Dest.SendMessage(message);
                }
                else
                {
                    ROUTE_MSG routeResponse = new ROUTE_MSG();
                    routeResponse.header_restype  = 0;                 //RESPONSE
                    routeResponse.header_type     = 0;                 //ROUTE
                    routeResponse.header_ok       = 0;                 //FAIL
                    routeResponse.header_distance = message.header_id; //Use the length field to store the id of the failed message
                    routeResponse.from            = NodeAddress;
                    routeResponse.to        = message.from;
                    routeResponse.parent    = NodeAddress;
                    routeResponse.reference = message.to;

                    NodeuC Dest = Program.GetNode(message.parent);
                    Dest.SendMessage(routeResponse);
                }
                dataInputBuffer.Remove(message);
            }

            if (dataOutputBuffer.Count > 0)//Messages to send
            {
                List <DATA_MSG> items = dataOutputBuffer.ToList();

                foreach (var message in items)
                {
                    if (!LookTable.ContainsKey(message.to))//Look finished
                    {
                        transmitRF(message);
                        dataOutputBuffer.Remove(message);
                    }
                    else if (LookTable[message.to][1] >= TIMEOUT)
                    {
                        Program.MessageBoxCustom("Timeout superado", "MESSAGE FROM " + message.from + " TO " + message.to + "           ");
                        dataOutputBuffer.Remove(message);
                        LookTable.Remove(message.to);
                    }
                }
            }

            int diff = (int)(iterationTime - lastLookTableUpdate).TotalMilliseconds;

            if (diff > 1000 && LookTable.Count > 0) //Check the pending searches every second
            {
                foreach (var item in LookTable.ToList())
                {
                    byte reference = item.Key;
                    byte counter   = item.Value[1];

                    if (counter < TIMEOUT)
                    {
                        LookTable[reference][1]++;
                    }
                    else
                    {
                        LookTable.Remove(reference); //TIMEOUT limit exceeded
                    }
                }

                lastLookTableUpdate = iterationTime;
            }
        }
Ejemplo n.º 7
0
        public bool IsNeigbor(NodeuC node)
        {
            float dx = node.Position.X - Position.X;
            float dy = node.Position.Y - Position.Y;

            return ((dx * dx) + (dy * dy) < Range * Range) && node.NodeAddress != this.NodeAddress;
        }
Ejemplo n.º 8
0
        protected override void Update(GameTime gameTime)
        {
            MouseState    mouse = Mouse.GetState();
            KeyboardState currentKeyboardState = Keyboard.GetState();

            if (CheckKeyReleased(Keys.P, currentKeyboardState, previousKeyboardState))
            {
                pauseSim = !pauseSim;
            }

            //Restore color nodes
            if (CheckKeyReleased(Keys.C, currentKeyboardState, previousKeyboardState))
            {
                foreach (NodeuC node in Program.NodeListuC)
                {
                    node.Color = Color.DarkBlue;
                }
            }

            //Reset route tables
            if (CheckKeyReleased(Keys.R, currentKeyboardState, previousKeyboardState))
            {
                foreach (NodeuC node in Program.NodeListuC)
                {
                    node.RouteTable.Clear();
                }
            }

            if (CheckKeyReleased(Keys.Add, currentKeyboardState, previousKeyboardState))
            {
                Program.SleepDelay = Math.Min(1000, Program.SleepDelay + 100);
            }
            else if (CheckKeyReleased(Keys.Subtract, currentKeyboardState, previousKeyboardState))
            {
                Program.SleepDelay = Math.Max(100, Program.SleepDelay - 100);
            }

            Window.Title = "Simulation delay: " + Program.SleepDelay + "ms";

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            bool    leftPressed   = mouse.LeftButton == ButtonState.Pressed;
            bool    rightClick    = mouse.RightButton == ButtonState.Pressed && mouseLast.RightButton == ButtonState.Released;
            Vector2 mousePosition = new Vector2(mouse.X, mouse.Y);
            bool    selected      = false;

            foreach (var node in Program.NodeListuC)
            {
                node.ShowAddress = node.ContainsPoint(mousePosition);
                node.Paused      = pauseSim;

                if (!selected && leftPressed && (node.ContainsPoint(mousePosition) || node.IsSelected))
                {
                    if (mouse.X < Window.ClientBounds.Width - rightMargin)
                    {
                        node.Position = mousePosition;
                    }
                    node.IsSelected = true;
                    selected        = true;
                }
                else
                {
                    node.IsSelected = false;
                }

                if (rightClick && node.ContainsPoint(mousePosition))
                {
                    if (selectedNode == null) //First click
                    {
                        selectedNode = node;
                        node.Color   = Color.Pink;
                    }
                    else //Second click
                    {
                        if (node != selectedNode)
                        {
                            selectedNode.Color = Color.DarkBlue;
                            Program.init       = DateTime.Now;
                            selectedNode.Transmit(node.NodeAddress, "HOLA");
                        }
                        selectedNode = null;
                    }
                }

                node.Update();
            }

            mouseLast             = mouse;
            previousKeyboardState = currentKeyboardState;

            // TODO: Add your update logic here

            base.Update(gameTime);
        }