Main Page   Packages   Class Hierarchy   Compound List   File List   Compound Members  

Node.java

Go to the documentation of this file.
00001 /* Cyclops
00002  * FER - SPVP
00003  * Fakultet elektrotehnike i racunarstva (http://www.fer.hr/)
00004  * Unska 3, 10000 Zagreb, Hrvatska
00005  * (c) 2001 FER, Zagreb.
00006  */
00007 
00008 package hr.fer.zesoi.cyclops;
00009 
00010 import java.util.*;
00011 import java.awt.*;
00012 
00013 
00018 public class Node
00019 {
00020 
00022     public RIPTable routing_table;
00023 
00025     protected String name;
00026     
00028     protected int x_position = 20;
00029 
00031     protected int y_position = 20;
00032 
00034     protected NodeQueue receive_buffer = new NodeQueue();
00035 
00037     protected Vector connecting_lines = new Vector();
00038 
00040     protected Vector connecting_nodes = new Vector();
00041 
00042 
00043 
00047     public Node ()
00048     {
00049         name = "Izolirani cvor";
00050         x_position = (int)(50 + 200 * Math.random());
00051         y_position = (int)(50 + 200 * Math.random());
00052         this.routing_table = new RIPTable(this);
00053     }
00054     /* Node */
00055 
00061     public Node (String name)
00062     {
00063         x_position = (int)(50 + 200 * Math.random());
00064         y_position = (int)(50 + 200 * Math.random());
00065         this.name = name;
00066         this.routing_table = new RIPTable(this);
00067     }
00068     /* Node */
00069 
00074     public void receive (Object packet)
00075     {
00076         receive_buffer.put(packet);
00077     }
00078     /* receive */
00079 
00083     public void updateRoutingTable ()
00084     {
00085         int i;
00086         Node other_node;
00087 
00088         for (i=0; i<connecting_nodes.size(); i++)
00089             {
00090                 other_node = (Node)connecting_nodes.elementAt(i);
00091                 routing_table.updateTable(other_node);
00092             }
00093         routing_table.garbageCollect();
00094     }
00095     /* updateRoutingTable */
00096 
00100     public void transmitAll ()
00101     {
00102         int i;
00103         Packet packet;
00104         Node destination;
00105         Node next_node;
00106         LineEnd line;
00107 
00108         while ( !receive_buffer.isEmpty() )
00109             {
00110                 packet = (Packet)receive_buffer.get();
00111                 destination = packet.getDestinationNode();
00112                 if (destination == this)
00113                     {
00114                         /* Discard packet. */
00115                     }
00116                 else if (packet.hop() != 0)
00117                     {
00118                         next_node = routing_table.findNextNodeForRouteTo(destination);
00119                         if (next_node == null)
00120                             {
00121                                 i = (int)(Math.random()*connecting_lines.size());
00122                                 if (!connecting_lines.isEmpty())
00123                                     {
00124                                         line = (LineEnd)connecting_lines.elementAt(i);
00125                                         line.receive(packet);
00126                                     }
00127                             }
00128                         else
00129                             {
00130                                 i = connecting_nodes.indexOf(next_node);
00131                                 if (i >= 0)
00132                                     {
00133                                         line = (LineEnd)connecting_lines.elementAt(i);
00134                                         line.receive(packet);
00135                                     }
00136                             }
00137                         /* else */
00138                     }
00139                 /* else if */
00140             }
00141         /* while */
00142     }
00143     /* transmitAll */
00144 
00150     public void attach (LineEnd line_end, Node other_node)
00151     {
00152         connecting_lines.addElement(line_end);
00153         connecting_nodes.addElement(other_node);
00154         routing_table.addElement(other_node, other_node, 1);
00155     }
00156     /* attach */
00157 
00162     public void detach (LineEnd line_end)
00163     {
00164         int index;
00165         Node node;
00166 
00167         index = connecting_lines.indexOf(line_end);
00168         if (0 <= index)
00169             {
00170                 connecting_lines.removeElementAt(index);
00171                 node = (Node)connecting_nodes.elementAt(index);
00172                 connecting_nodes.removeElementAt(index);
00173                 routing_table.setCostToInfinity(node);
00174             }
00175     }
00176     /* detach */
00177 
00181     public void delete ()
00182     {
00183         int index;
00184 
00185         while (!connecting_lines.isEmpty())
00186             {
00187                 LineEnd line = (LineEnd)connecting_lines.lastElement();
00188                 line.delete();
00189             }
00190     }
00191     /* delete */
00192 
00197     public void detach (Node other_node)
00198     {
00199         int index;
00200 
00201         index = connecting_nodes.indexOf(other_node);
00202         if (0 <= index)
00203             {
00204                 connecting_lines.removeElementAt(index);
00205                 connecting_nodes.removeElementAt(index);
00206                 routing_table.setCostToInfinity(other_node);
00207             }
00208     }
00209     /* detach */
00210 
00215     public RIPTable getRIPTable ()
00216     {
00217         return routing_table;
00218     }
00219     /* getRIPTable */
00220 
00226     public void setCoordinates (int x, int y)
00227     {
00228         x_position = x;
00229         y_position = y;
00230     }
00231     /* setCoordinates */
00232 
00237     public String getName ()
00238     {
00239         return this.name;
00240     }
00241     /* nodeName */
00242 
00247     public int getXCoordinate ()
00248     {
00249         return x_position;
00250     }
00251     /* getXCoordinate */
00252 
00257     public int getYCoordinate ()
00258     {
00259         return y_position;
00260     }
00261     /* getYCoordinate */
00262 
00267     public void setXCoordinate (int x)
00268     {
00269         x_position = x;
00270     }
00271     /* setXCoordinate */
00272 
00277     public void getYCoordinate (int y)
00278     {
00279         y_position = y;
00280     }
00281     /* setYCoordinate */
00282 
00289     public void paint (Graphics g, FontMetrics metrics, Color fill_color)
00290     {
00291         int w = metrics.stringWidth(this.name) + 10;
00292         int h = metrics.getHeight() + 4;
00293         g.setColor(fill_color);
00294         g.fillRect(x_position - w/2, y_position - h/2, w, h);
00295         g.setColor(Color.black);
00296         g.drawRect(x_position - w/2, y_position - h/2, w - 1, h - 1);
00297         g.drawString(this.name,
00298                      x_position - (w - 10) / 2,
00299                      y_position - (h - 4) / 2 + metrics.getAscent()
00300                      );
00301     }
00302     /* paint */
00303 
00304 }
00305 /* Node */
00306 
00307 
00311 class NodeQueue
00312 {
00313 
00315     private Stack storage_buffer = new Stack();
00316 
00320     public NodeQueue ()
00321     {
00322     }
00323     /* NodeQueue */
00324 
00329     public boolean isEmpty ()
00330     {
00331         return storage_buffer.isEmpty();
00332     }
00333     /* isEmpty */
00334 
00339     public void put (Object packet)
00340     {
00341         storage_buffer.push(packet);
00342     }
00343     /* put */
00344 
00350     public Object get ()
00351     {
00352         Object packet;
00353 
00354         packet = storage_buffer.pop();
00355         return packet;
00356     }
00357     /* get */
00358 
00363     public Object peek ()
00364     {
00365         return storage_buffer.peek();
00366     }
00367     /* peek */
00368 
00369 }
00370 /* NodeQueue */

Generated at Thu Jun 28 03:04:21 2001 for Cyclops Network Simulator by doxygen1.2.7 written by Dimitri van Heesch, © 1997-2001