00001
00002
00003
00004
00005
00006
00007
00008
00009 package hr.fer.zesoi.cyclops;
00010
00011 import java.util.*;
00012 import java.awt.*;
00013
00014
00021 public class CyclopsSimulator extends Thread
00022 {
00023
00025 protected Vector node_list = new Vector();
00026
00028 protected Vector line_list = new Vector();
00029
00031 protected Node start_node = null;
00032
00034 protected String start_node_name = null;
00035
00037 protected Node end_node = null;
00038
00040 protected String end_node_name = null;
00041
00043 protected boolean trace_flag = false;
00044
00046 protected boolean running = false;
00047
00049 protected boolean destroy = false;
00050
00054 public void run()
00055 {
00056 while (true)
00057 {
00058 if (running)
00059 {
00060 simulate();
00061 if (trace_flag)
00062 {
00063 sendPacket();
00064 }
00065 try
00066 {
00067 Thread.sleep(100);
00068 }
00069 catch (InterruptedException exception)
00070 {
00071 }
00072 }
00073 else
00074 {
00075 try
00076 {
00077 Thread.sleep(100);
00078 }
00079 catch (InterruptedException exception)
00080 {
00081 }
00082 }
00083 if (destroy)
00084 {
00085 return;
00086 }
00087 }
00088 }
00089
00090
00094 public CyclopsSimulator ()
00095 {
00096 }
00097
00098
00105 public synchronized void simulate ()
00106 {
00107 int i;
00108 Line current_line;
00109 Node current_node;
00110
00111 for (i=0; i<line_list.size(); i++)
00112 {
00113 current_line = (Line)line_list.elementAt(i);
00114 current_line.transmitAll();
00115 }
00116 for (i=0; i<node_list.size(); i++)
00117 {
00118 current_node = (Node)node_list.elementAt(i);
00119 current_node.transmitAll();
00120 }
00121 for (i=0; i<node_list.size(); i++)
00122 {
00123 current_node = (Node)node_list.elementAt(i);
00124 current_node.updateRoutingTable();
00125 }
00126 }
00127
00128
00132 public synchronized void topologyUpdate ()
00133 {
00134 int i;
00135 Node current_node;
00136
00137 for (i=0; i<node_list.size(); i++)
00138 {
00139 current_node = (Node)node_list.elementAt(i);
00140 current_node.updateRoutingTable();
00141 }
00142 }
00143
00144
00148 public void startSimulation ()
00149 {
00150 running = true;
00151 }
00152
00153
00159 public void tracePackets (String start_name, String end_name)
00160 {
00161 Node start_temp;
00162 Node end_temp;
00163
00164 start_temp = findNode(start_name);
00165 if (start_temp == null)
00166 {
00167 return;
00168 }
00169 end_temp = findNode(end_name);
00170 if (end_temp == null)
00171 {
00172 return;
00173 }
00174 start_node = start_temp;
00175 start_node_name = start_name;
00176 end_node = end_temp;
00177 end_node_name = end_name;
00178 trace_flag = true;
00179 }
00180
00181
00186 public void sendPacket()
00187 {
00188 Node temp;
00189
00190 temp = findNode(start_node_name);
00191 if (temp != null)
00192 {
00193 temp = findNode(end_node_name);
00194 if (temp != null)
00195 {
00196 Packet traced = new Packet(120, start_node, end_node, 14);
00197 traced.setDrawingFlag(true);
00198 start_node.receive(traced);
00199 }
00200 }
00201 }
00202
00203
00210 public void addDummyPackets(int number, int time_to_live)
00211 {
00212 int i;
00213 int j;
00214 Node current_node;
00215 Packet packet;
00216 boolean running_flag;
00217
00218 running_flag = running;
00219 running = false;
00220
00221 for (i=0, j=0; i<number; i++)
00222 {
00223 packet = new Packet();
00224 current_node = (Node)node_list.elementAt(j);
00225 j++;
00226 if (j >= node_list.size())
00227 {
00228 j = 0;
00229 }
00230 packet.setTimeToLive(time_to_live);
00231 current_node.receive(packet);
00232 }
00233 running = running_flag;
00234 }
00235
00236
00240 public void stopSimulation ()
00241 {
00242 running = false;
00243 }
00244
00245
00249 public void endSimulation ()
00250 {
00251 destroy = true;
00252 }
00253
00254
00259 public void addNode (Node new_node)
00260 {
00261 node_list.addElement(new_node);
00262 }
00263
00264
00270 public Node findNode (String node_name)
00271 {
00272 int i;
00273 String current_name;
00274 Node current_node;
00275
00276 for (i=0; i!=node_list.size(); i++)
00277 {
00278 current_node = (Node)node_list.elementAt(i);
00279 current_name = current_node.getName();
00280 if (current_name.equals(node_name))
00281 {
00282 return current_node;
00283 }
00284 }
00285 return null;
00286 }
00287
00288
00294 public Node deleteNode (String node_name)
00295 {
00296 int i;
00297 String current_name;
00298 Node current_node = null;
00299 Line current_line;
00300
00301 for (i=0; i!=node_list.size(); i++)
00302 {
00303 current_node = (Node)node_list.elementAt(i);
00304 current_name = current_node.getName();
00305 if (current_name.equals(node_name))
00306 {
00307 current_node.delete();
00308 node_list.removeElementAt(i);
00309 break;
00310 }
00311 }
00312 for (i=0; i!=line_list.size(); i++)
00313 {
00314 current_line = (Line)line_list.elementAt(i);
00315 if (current_line.isDisconnected())
00316 {
00317 line_list.removeElementAt(i);
00318
00319 i--;
00320 }
00321 }
00322 return current_node;
00323 }
00324
00325
00330 public int numberOfNodes ()
00331 {
00332 return node_list.size();
00333 }
00334
00335
00342 public Node nodeAt(int index)
00343 {
00344 return (Node)node_list.elementAt(index);
00345 }
00346
00347
00354 public Line lineAt(int index)
00355 {
00356 return (Line)line_list.elementAt(index);
00357 }
00358
00359
00364 public int numberOfLines ()
00365 {
00366 return line_list.size();
00367 }
00368
00369
00374 public void addLine (Line new_line)
00375 {
00376 line_list.addElement(new_line);
00377 }
00378
00379
00386 public Line deleteLine (String first_node, String second_node)
00387 {
00388 Node first;
00389 Node second;
00390 Line current_line;
00391 int i;
00392
00393 first = findNode(first_node);
00394 second = findNode(second_node);
00395 if (null == first) return null;
00396 if (null == second) return null;
00397 current_line = null;
00398 for (i=0; i!=line_list.size(); i++)
00399 {
00400 current_line = (Line)line_list.elementAt(i);
00401 if (current_line.areEndingNodes(first, second))
00402 {
00403 current_line.delete();
00404 line_list.removeElementAt(i);
00405 break;
00406 }
00407 }
00408 return current_line;
00409 }
00410
00411
00424 public synchronized void paint (Graphics g, FontMetrics metrics,
00425 Color node_color, Color line_color,
00426 Color left_packet_color, Color right_packet_color,
00427 Color traced_packet_color,
00428 boolean show_line_capacity, boolean show_all_packets
00429 )
00430 {
00431 int i;
00432 Line current_line;
00433 Node current_node;
00434
00435 for (i=0; i!=line_list.size(); i++)
00436 {
00437 current_line = (Line)line_list.elementAt(i);
00438 current_line.paint(g, metrics,
00439 line_color, left_packet_color, right_packet_color,
00440 traced_packet_color,
00441 show_line_capacity, show_all_packets);
00442 }
00443 for (i=0; i!=node_list.size(); i++)
00444 {
00445 current_node = (Node)node_list.elementAt(i);
00446 current_node.paint(g, metrics, node_color);
00447 }
00448
00449 }
00450
00451
00452 }
00453