Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- public class RouterNode {
- private int myID;
- private GuiTextArea myGUI;
- private RouterSimulator sim;
- private int [] Neighbours = new int[RouterSimulator.NUM_NODES];
- private int[][] UpdatedConnectcosts = new int[RouterSimulator.NUM_NODES][RouterSimulator.NUM_NODES];
- private int[] costs = new int[RouterSimulator.NUM_NODES];
- private int[] ByNode = new int[RouterSimulator.NUM_NODES];
- private boolean PoisonedReverse = false;
- //--------------------------------------------------
- public RouterNode(int ID, RouterSimulator sim, int[] costs) {
- myID = ID;
- this.sim = sim;
- myGUI =new GuiTextArea(" Output window for Router #"+ ID + " ");
- //Added
- System.arraycopy(costs, 0, this.costs, 0, RouterSimulator.NUM_NODES);
- System.arraycopy(costs, 0, this.Neighbours, 0, RouterSimulator.NUM_NODES);
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- if (i == this.myID)
- {
- //costs[this.myID] = 0;
- System.arraycopy(costs, 0, this.UpdatedConnectcosts[i], 0, RouterSimulator.NUM_NODES);
- }
- else
- {
- for (int j = 0; j < RouterSimulator.NUM_NODES; j++)
- {
- this.UpdatedConnectcosts[i][j] = RouterSimulator.INFINITY;
- }
- }
- }
- sendCosts();
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- if (costs[i] != RouterSimulator.INFINITY){
- ByNode[i] = i;
- }
- else{
- ByNode[i] = -1;
- }
- }
- }
- //--------------------------------------------------
- private void sendCosts()
- {
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- if ( i != this.myID && !PoisonedReverse && Neighbours[i] != RouterSimulator.INFINITY)
- {
- //Make a Router_Packet and send it.
- RouterPacket pkt = new RouterPacket(this.myID, i, this.costs);
- sendUpdate(pkt);
- }
- else
- {
- //PoisonedReverse
- }
- }
- }
- //--------------------------------------------------
- public void recvUpdate(RouterPacket pkt) {
- System.arraycopy(pkt.mincost,0,this.UpdatedConnectcosts[pkt.sourceid],0,RouterSimulator.NUM_NODES);
- myGUI.println ("Update received from pkt: " + pkt.destid) ;
- // If the Bellman function modify something
- if( Bellman() )
- {
- // Send the modification to neighbors
- sendCosts();
- }
- }
- //--------------------------------------------------
- private void sendUpdate(RouterPacket pkt) {
- sim.toLayer2(pkt);
- }
- //--------------------------------------------------
- public void printDistanceTable() {
- myGUI.println("Current table for " + myID +
- " at time " + sim.getClocktime());
- myGUI.println("Distancetable:");
- myGUI.print(" dst |");
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- myGUI.print(" " + i);
- }
- myGUI.println();
- myGUI.print("---------");
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- myGUI.print("-------");
- }
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- // If it's a direct neighbor
- // if(Neighbours[i] != RouterSimulator.INFINITY && i != myID)
- //{
- myGUI.println();
- myGUI.print(F.format(" nbr", 6));
- myGUI.print(i + " |");
- for(int j = 0; j < RouterSimulator.NUM_NODES; j++)
- {
- myGUI.print(" " + F.format(UpdatedConnectcosts[i][j], 3));
- }
- // }
- }
- myGUI.println();
- myGUI.println();
- myGUI.println("Our distance vector and routes:");
- myGUI.print(" dst |");
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- myGUI.print(F.format(i,4));
- }
- myGUI.println();
- myGUI.print("---------");
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- myGUI.print("-------");
- }
- myGUI.println();
- myGUI.print(" cost |");
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- myGUI.print(" " + F.format(costs[i], 3));
- }
- myGUI.println();
- myGUI.print(F.format(" route |",8));
- for (int i = 0; i < RouterSimulator.NUM_NODES; i++)
- {
- if (ByNode[i] != -1)
- {
- myGUI.print(" " + F.format(ByNode[i], 3));
- }
- else
- {
- myGUI.print(" " + F.format('-', 3));
- }
- }
- myGUI.println();
- myGUI.println();
- }
- //--------------------------------------------------
- private boolean Bellman()
- {
- boolean changed = false;
- for(int dest = 0; dest < RouterSimulator.NUM_NODES; dest++)
- {
- int firstHop = RouterSimulator.INFINITY;
- int min_cost = RouterSimulator.INFINITY;
- if(dest==this.myID)
- {
- costs[dest] = 0;
- }
- else
- {
- for (int neig = 0; neig < RouterSimulator.NUM_NODES; neig++)
- {
- if ((Neighbours[neig] != RouterSimulator.INFINITY) && (Neighbours[neig] != 0))
- {
- if (Neighbours[neig] + UpdatedConnectcosts[neig][dest] < min_cost)
- {
- min_cost = Neighbours[neig] + UpdatedConnectcosts[neig][dest];
- firstHop = neig;
- }
- }
- }
- if ((min_cost != this.costs[dest]) && (min_cost != RouterSimulator.INFINITY))
- {
- ByNode[dest] = firstHop;
- this.costs[dest] = min_cost;
- changed = true;
- }
- }
- }
- if (changed){
- UpdatedConnectcosts[this.myID] = this.costs;
- }
- return changed;
- }
- //--------------------------------------------------
- public void updateLinkCost(int dest, int newcost) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement