Example #1
0
 public void ReceivePacket(Packet packet)
 {
     if (!packets.Contains(packet.GetNumber()))
     {
         packets.Add(packet.GetNumber());
         network     = packet.GetNetwork();
         connections = Dijkstra.dijkstra(network, id);
         SendPacket(packet);
     }
 }
Example #2
0
 public Router(string id)
 {
     this.id   = id;
     neighbors = new List <Router>();
     packets   = new List <int>();
     network   = new NetworkGraph(15); //kiek routeriu norim
     network.addEdge(id);
     connections = new Dictionary <string, string>();
     connections.Add(id, id);
 }
Example #3
0
        private Packet GeneratePacket(Router router)
        {
            NetworkGraph secondNetwork = router.GetNetwork();

            foreach (string edge in secondNetwork.getEdges())
            {
                network.addEdge(edge);
                foreach (string neighbor in secondNetwork.getNeighbors(edge))
                {
                    network.addEdge(neighbor);
                    network.setLink(edge, neighbor, secondNetwork.getWeight(edge, neighbor));
                }
            }

            return(new Packet(Packet.GetCounter(), id, network));
        }
Example #4
0
 public Packet(int number, string sender, NetworkGraph network)
 {
     this.number  = number;
     this.sender  = sender;
     this.network = network;
 }
Example #5
0
        // Dijkstra's algorithm to find shortest path from s to all other nodes
        public static Dictionary <string, string> dijkstra(NetworkGraph G, string source)
        {
            string[] routers = G.getEdges();
            int[]    dist    = new int[routers.Length];
            string[] queue   = new string[routers.Length];
            Dictionary <string, string> pred = new Dictionary <string, string>();

            bool[] visited = new bool[routers.Length]; // all false initially

            for (int i = 0; i < dist.Length; i++)
            {
                dist[i]  = int.MaxValue;
                queue[i] = routers[i];
                if (routers[i].Equals(source))
                {
                    dist[i] = 0;
                }
            }

            for (int i = 0; i < dist.Length; i++)
            {
                int next = minVertex(dist, visited);
                if (next != -1)
                {
                    visited[next] = true;
                    string[] neighbors = G.getNeighbors(routers[next]);
                    for (int j = 0; j < neighbors.Length; j++)
                    {
                        int nr = 0;
                        for (int h = 0; h < routers.Length; h++)
                        {
                            if (routers[h].Equals(neighbors[j]))
                            {
                                nr = h;
                            }
                        }
                        int d = dist[next] + G.getWeight(routers[next], routers[nr]);
                        if (dist[nr] > d)
                        {
                            dist[nr]  = d;
                            queue[nr] = queue[next] + seperator + queue[nr];
                        }
                    }
                }
            }

            foreach (string str in queue)
            {
                string[] parts = str.Split(seperator);
                if (parts.Length != 1)
                {
                    pred[parts[parts.Length - 1]] = parts[1];
                }
                else
                {
                    pred[parts[0]] = parts[0];
                }
            }

            return(pred);
        }