コード例 #1
0
ファイル: RoutingTable.cs プロジェクト: umbarger/RouterRIP
        public RoutingTable( Node n, HashSet<Node> netGraph )
        {
            this.currentNode = n;

            HashSet<Edge> neighborsEdges = n.GetEdges();

            foreach( Edge e in neighborsEdges )
            {
                Node x = e.GetX();
                Node y = e.GetY();

                Node neighbor;

                if (currentNode.Equals(x))
                    neighbor = y;
                else
                    neighbor = x;

                neighborNodes.Add(neighbor);
            }

            foreach( Node r in netGraph )
            {
                String curIP = r.GetAddress();

                if( !curIP.Equals( currentNode.GetAddress()) && !neighborNodes.Contains( r ) )
                {
                    destinationIP.Add(curIP);
                    subnetMask.Add(n.GetMask());
                    distanceMetric.Add(infinity);
                    nextHopIP.Add("0.0.0.0");
                }
                else if ( neighborNodes.Contains(r) )
                {
                    int weight = 0;

                    foreach( Edge e in neighborsEdges )
                    {
                        Node x = e.GetX();
                        Node y = e.GetY();

                        if( x.Equals(r) || y.Equals(r) )
                        {
                            weight = e.GetWeight();
                        }
                    }

                    destinationIP.Add(curIP);
                    subnetMask.Add(n.GetMask());
                    distanceMetric.Add(weight);
                    nextHopIP.Add(n.GetAddress());
                    destinationTimes.Add(0);
                }
            }
        }
コード例 #2
0
ファイル: NetworkGraph.cs プロジェクト: umbarger/RouterRIP
        public NetworkGraph( int numNodePairs )
        {
            HashSet<Edge> initialEdges = new HashSet<Edge>();
            String nodeType1;
            String nodeType2;

            for (int i = 0; i < numNodePairs; i++)
            {
                String address1 = this.GenerateIPAddress();
                String address2 = this.GenerateIPAddress();

                if (uniqueAddresses.Contains(address1) || uniqueAddresses.Contains(address2))
                    continue;

                do
                {
                    nodeType1 = GenerateNodeType();
                    nodeType2 = GenerateNodeType();
                } while (!ValidateNodeConnection(nodeType1, nodeType2));

                Node x = new Node(nodeType1, address1);
                Node y = new Node(nodeType2, address2);

                Edge edge = new Edge(x, y, GenerateWeight());

                x.AddEdge(edge);
                y.AddEdge(edge);

                initialEdges.Add(edge);
                networkGraph.Add(x);
                networkGraph.Add(y);

                uniqueAddresses.Add(address1);
                uniqueAddresses.Add(address2);
            }

            using (IEnumerator<Edge> it = initialEdges.GetEnumerator())
            {

                it.MoveNext();

                Edge curEdge = it.Current;

                while (it.MoveNext())
                {
                    Edge nextEdge = it.Current;

                    Node curX = curEdge.GetX();
                    Node curY = curEdge.GetY();

                    Node nextX = nextEdge.GetX();
                    Node nextY = nextEdge.GetY();

                    if (ValidateNodeConnection(curX.GetType().ToString(), nextX.GetType().ToString()))
                    {
                        Edge newXX = new Edge(curX, nextX, GenerateWeight());

                        curX.AddEdge(newXX);
                        nextX.AddEdge(newXX);
                    }
                    else if (ValidateNodeConnection(curX.GetType().ToString(), nextY.GetType().ToString()))
                    {
                        Edge newXY = new Edge(curX, nextY, GenerateWeight());

                        curX.AddEdge(newXY);
                        nextY.AddEdge(newXY);
                    }
                    else if (ValidateNodeConnection(curY.GetType().ToString(), nextX.GetType().ToString()))
                    {
                        Edge newYX = new Edge(curY, nextX, GenerateWeight());

                        curX.AddEdge(newYX);
                        nextY.AddEdge(newYX);
                    }
                    else if (ValidateNodeConnection(curY.GetType().ToString(), nextY.GetType().ToString()))
                    {
                        Edge newYY = new Edge(curY, nextY, GenerateWeight());

                        curX.AddEdge(newYY);
                        nextY.AddEdge(newYY);
                    }
                    curEdge = nextEdge;
                }
            }
        }
コード例 #3
0
ファイル: Edge.cs プロジェクト: umbarger/RouterRIP
 public Edge( Node a, Node b, int w)
 {
     this.x = a;
     this.y = b;
     this.weight = w;
 }
コード例 #4
0
ファイル: Node.cs プロジェクト: umbarger/RouterRIP
        private void Receive( Node neighbor, RoutingTable neighborTable )
        {
            try
            {
                String neighborAddress = neighbor.GetAddress();

                List<String> neighborDestinations = neighborTable.GetDestinations();
                List<String> myDestinations = table.GetDestinations();

                // Compare the neighbor's routing table with your own... Update better routes
                foreach (String nDest in neighborDestinations)
                {
                    foreach (String myDest in myDestinations)
                    {
                        if (myDest.Equals(nDest))
                        {
                            int myMetric = table.GetDistanceMetric(myDest);
                            int nMetric = neighborTable.GetDistanceMetric(nDest);

                            if (nMetric < myMetric)
                            {
                                int newMetric;
                                int curNodeToNeighborDistance = table.GetDistanceMetric(nDest);

                                if (curNodeToNeighborDistance == (int)Int32.MaxValue)
                                    newMetric = nMetric;
                                else
                                    newMetric = nMetric + curNodeToNeighborDistance;

                                table.UpdateMetric(myDest, newMetric);
                                table.UpdateNextHop(myDest, table.GetDestination(neighborAddress));

                                table.ResetTimer(myDest);
                                table.ResetTimer(nDest);
                            }
                        }
                    }
                }

                table.ResetTimer(neighborAddress);
            }
            catch (NullReferenceException e) { }
        }