// Thoms Dorn // Ethernet Visualisierung // WS 1996/97 // E881 8625093 //+ "Folding Information" // For all, who have an editor, which is able to fold: my folding-keys are //+ and //- //- import java.applet.*; import java.awt.*; import java.lang.Math; import java.util.Random; import java.lang.*; import java.awt.Graphics; import java.util.Date; interface Const{ public static final int MAXPC = 6; // Max-PC = 8 public static final byte T = 0; // T-Wedge public static final byte C = 1; // Cable - public static final byte L = 2; // Terminator, End Left public static final byte R = 3; // Terminator, End Right public static final int WHITE = 9; // Weisse Farbe (Leeres Kabel) public static final int BLACK = 0; // Schwarze Farbe (Jam) public static final int TWOCOL = 10; // Grau public static final int MAXSLICE = 10; // Maximale Pakete public static final int MINSIZE = MAXPC*3+4; // Minimale Paketlänge public static final int BIGSIZE = (MAXPC*3+4)*2; // Große Paketlänge public static final int SLOW = 1000; // For speed public static final int MED = 500; public static final int FAST = 30; // colortable 6 PCs with its colors (0, 1-8, 9) public static final Color pc_col[]={Color.black, Color.blue,Color.cyan,Color.green,Color.magenta,Color.orange,Color.pink, Color.red, Color.yellow, Color.white, Color.gray}; } public class Ether extends Applet implements Runnable { static final String message = "Thomas Dorn (tdorn@vip.at)"; int speed = Const.SLOW; int gh = 0; // Height for JavaApplikat! int zaehler=0; // For statistics int slice = 0; // Time-Slice from 0 to max_slice; boolean autoplay = true; // else single-step boolean forward = true; // else backward // Variables for memory (ff/bw) int packetI[][] = new int [Const.MAXSLICE][Const.MAXPC]; int destI[][] = new int [Const.MAXSLICE][Const.MAXPC]; Thread animThread = null; //+ "Initialisierung der Buttons, Labels und Panels" // Needed Buttons Button b_stepB; Button f_stepB; Button b_playB; Button f_playB; Button stopB; Button newB; Choice speedB; Choice packetB[][] = new Choice [Const.MAXSLICE][Const.MAXPC]; Choice destB[][] = new Choice [Const.MAXSLICE][Const.MAXPC]; Checkbox connectB[] = new Checkbox [Const.MAXPC]; // Needed Labels Label overL; Label timeL; Label sliceL; Label stepL; Label playL; Label speedL; Label statusL[] = new Label[Const.MAXPC]; // Panels Panel mainP; Panel motionP; Panel dataP; Panel barsP; Panel timeP; Panel stepP; Panel playP; Panel speedP; Panel spcP[] = new Panel[Const.MAXPC]; Panel hpcP[] = new Panel[Const.MAXPC]; Panel pcP[] = new Panel[Const.MAXPC]; Panel progP[] = new Panel[Const.MAXPC]; Panel hstepP; Panel hplayP; barCanvas progC[] = new barCanvas[Const.MAXPC]; Panel animP; pipeCanvas pipeC[] = new pipeCanvas[4+(Const.MAXPC*3)]; // for the animation: a cable 0 and 21 are terminators ... nrCanvas nrC[] = new nrCanvas[2]; Panel nrP; //- // First Initialization (init Data) public void init() { //+ " Buttons" System.out.println("Making a nice Layout ..."); setLayout(new BorderLayout()); // Initialize the global Layout overL = new Label("Ethernetvisualisierung", Label.CENTER); add("North",overL); // Make the Label of Motion Panel timeL = new Label("Time Slice", Label.CENTER); sliceL = new Label("0:0", Label.CENTER); stepL = new Label("Step", Label.CENTER); playL = new Label("Play", Label.CENTER); speedL = new Label("Speed", Label.CENTER); // Make the Buttons of Motion Panel b_stepB = new Button("<"); f_stepB = new Button(">"); b_playB = new Button("<"); f_playB = new Button(">"); stopB = new Button("Stop"); newB = new Button("New"); speedB = new Choice(); speedB.addItem("slow"); speedB.addItem("medium"); speedB.addItem("high"); //- //+ " Motion Panel" // ############### motion Panel ###################################### motionP = new Panel(); motionP.setLayout(new FlowLayout()); // Motion-Panel timeP = new Panel(); timeP.setLayout(new BorderLayout()); // Time-Panel hstepP = new Panel(); hstepP.setLayout(new FlowLayout()); // Step-Panel Help hplayP = new Panel(); hplayP.setLayout(new FlowLayout()); // Play-Panel Help stepP = new Panel(); stepP.setLayout(new BorderLayout()); // Step-Panel playP = new Panel(); playP.setLayout(new BorderLayout()); // Play-Panel speedP = new Panel(); speedP.setLayout(new BorderLayout()); // Speed-Panel timeP.add ("North", timeL); timeP.add ("South", sliceL); hstepP.add (b_stepB); hstepP.add (f_stepB); stepP.add ("North", stepL); stepP.add ("South", hstepP); hplayP.add (b_playB); hplayP.add (f_playB); hplayP.add (stopB); hplayP.add (newB); playP.add ("North", playL); playP.add ("South", hplayP); speedP.add ("North", speedL); speedP.add ("South", speedB); motionP.add(timeP); motionP.add(stepP); motionP.add(playP); motionP.add(speedP); //- //+ " Data Panel (PCs)" // ############### Data Panel ######################################### dataP = new Panel(); dataP.setLayout(new GridLayout(1,Const.MAXPC)); // Data-Panel for (int i=0;i 0) { j = true; if (packetI[slice][i] == 1) // Kleines Paket pipeC[i*3+3].inject(Const.MINSIZE); else // Großes Paket pipeC[i*3+3].inject(Const.BIGSIZE); } } nrC[0].setH(gh); nrC[1].setH(gh); nrC[0].setAct(slice); nrC[1].setAct(slice); // Solange, bis kein Paket und nichts auf der Leitung while (j) { int k=0; boolean test = true; zaehler ++; sliceL.setText(slice+":"+zaehler); // System.out.print("*"); while (k 0) || (pipeC[k*3+3].getWait() > 0) ) { k = Const.MAXPC; test = true; } else { k++; test = false; } } if (test == false) // Nur, wenn sonst nichts mehr da ist { int a = 0; for (int i=0;i 0) // Es ist noch was am Kabel test = true; } j = test; // Kabel durchchecken pipeC[0].setColor(pipeC[1].getColor()); // Left Terminator eats Pakets richtungN[0] = richtung[1]; // Left Terminator eats Pakets for (int i=1; i < Const.MINSIZE-1; i++) // Maximum Cable peaces { if (( (richtung[i-1] == 1) || (richtung[i-1] == 0) ) && ((richtung[i+1] == 2) || (richtung[i+1] == 0) ) ) { // Zeigt auseinander pipeC[i].setColor(Const.WHITE); richtungN[i] = 0; } else if (((richtung[i-1] == 1) || (richtung[i-1] == 0) ) && ( (richtung[i+1] == 1) || (richtung[i+1] == 3)) ) // Get Color from Right neighbour { pipeC[i].setColor(pipeC[i+1].getColor()); richtungN[i] = 1; } else if (((richtung[i-1] == 2) || (richtung[i-1] == 3)) && ((richtung[i+1] == 2) || (richtung[i+1] == 0)) ) // Get Color from Left neighbour { pipeC[i].setColor(pipeC[i-1].getColor()); richtungN[i] = 2; } else if (((richtung[i-1] == 2) || (richtung[i-1] == 3)) && ((richtung[i+1] == 1) || (richtung[i+1] == 3) )) // Collision { pipeC[i].setColor(Const.BLACK); richtungN[i] = 3; // In both directions } else { System.out.println("Impossible State. Please tell tdorn@vip.at that this happens!"); } } pipeC[Const.MINSIZE-1].setColor(pipeC[Const.MINSIZE-2].getColor()); // rechter Terminator frißt Pakete richtungN[Const.MINSIZE-1] = richtung[Const.MINSIZE-2]; // rechter Terminator frißt Pakete for (int i=0; i < Const.MAXPC; i++) // Maximum T-Peaces { progC[i].setZaehler(zaehler); statusL[i].setText("Listening"); pipeC[i*3+3].decWait(); // Dec Waiting-time for sending if ( (pipeC[i*3+3].getInject() > 0) && (pipeC[i*3+3].getWait() == 0) ) // ready for sending { if (pipeC[i*3+3].getInject() == pipeC[i*3+3].getOInject()) // Nothing sent befor { if (richtungN[i*3+3] == 0) // Free Cell, start sending { pipeC[i*3+3].setColor(pipeC[i*3+3].getOColor()); richtungN[i*3+3] = 3; pipeC[i*3+3].decInject(); statusL[i].setText("Sending"); progC[i].setTransmit(); } else // I have to wait till cable is free { } } else // sending in progress { if (richtungN[i*3+3] == 0) // Free Cell, sending { pipeC[i*3+3].setColor(pipeC[i*3+3].getOColor()); richtungN[i*3+3] = 3; pipeC[i*3+3].decInject(); statusL[i].setText("Sending"); progC[i].setTransmit(); } else // Collision! { pipeC[i*3+3].inject(pipeC[i*3+3].getOInject()); // wieder alte Paketanzahl Random zufall = new Random(); // Wartezeit für nächsten Versuch pipeC[i*3+3].setWait( (Math.abs(zufall.nextInt())%6 * Const.MINSIZE)); System.out.println("Collision Dedect: Wait "+pipeC[i*3+3].getWait() ); statusL[i].setText("Collision"); progC[i].setCollision(); } } } else // Nothing to send, maybe something for receiving { if ( (pipeC[i*3+3].getColor() < Const.WHITE) && (pipeC[i*3+3].getColor() > Const.BLACK) ) { // System.out.println("R i="+(i+1)+" slice="+ slice + "Color="+pipeC[i*3+3].getColor() + ":="+destI[slice][pipeC[i*3+3].getColor()-1] ); if ( (i+1) == destI[slice][pipeC[i*3+3].getColor()-1] ) { statusL[i].setText("Receiving"); progC[i].setReceive(); } } } } for (int i=0; i < Const.MINSIZE; i++) // update whole Cable { richtung[i] = richtungN[i]; // update direction pipeC[i].update(); } try // Wait a little bit { Thread.sleep(speed); } catch (InterruptedException e){}; } if (autoplay == true) { if (forward == true) { slice ++; if (slice >= Const.MAXSLICE) slice = 0; } else { slice --; if (slice < 0) slice = Const.MAXSLICE-1; } } } animThread = null; System.out.println("Thread fertig"); } } //- //+ " public boolean action(Event ev, Object arg) " public boolean action(Event ev, Object arg) { // System.out.println("Hab was "+ (String)arg); if (ev.id == Event.WINDOW_DESTROY) // Fenster sollte zu machen { System.exit(0); } if (ev.target instanceof Button) // Is a Button { if (ev.target == stopB) // Stop { if (animThread != null) animThread.stop(); animThread = null; } else if (ev.target == newB) // New { zaehler = 0; slice = 0; sliceL.setText(slice+":"+zaehler); for (int i=0;i= Const.MAXSLICE) slice = 0; sliceL.setText(slice+":"+zaehler); animThread = new Thread(this, "Anim-Thread"); animThread.start(); } else System.out.println("Unknown Button "+ (String)arg); return true; } if (ev.target instanceof Choice) // Is Choice { String label = (String)arg; // Contents of Result if (ev.target == speedB) // Speed-Choice { if (label.equals("slow")) speed = Const.SLOW; else if (label.equals("medium")) speed = Const.MED; else speed = Const.FAST; // System.out.println("New Speed: "+speed); } else // Ich geh mal davon aus, dass es einer meiner Choices ist! { // System.out.println("New Choice"); for (int i=0;i 0) this.wait--; } public void setWait(int wait) { this.wait = wait; } } //- //+ "class nrCanvas extends Canvas" class nrCanvas extends Canvas { int act; int height; int oh=0; public void paint(Graphics g) { int w = size().width; int h = size().height; g.setColor(Color.lightGray); g.fillRect(0, 0, w , h); g.setColor(Color.black); g.fillRect(0, (height+oh)*act+6, 10, 10); } public void setH(int h) { this.oh = h; } public void setAct(int t) { this.act = t; repaint(); } //If we don't specify this, the canvas might not show up at all //(depending on the layout manager). public Dimension minimumSize() { Graphics g = getGraphics(); Font f = g.getFont(); FontMetrics fm = g.getFontMetrics(); this.height = fm.getHeight() +13; // System.out.println("FontHeight: "+height); return new Dimension(15,Const.MAXSLICE * (height+oh)); } //If we don't specify this, the canvas might not show up at all //(depending on the layout manager). public Dimension preferredSize() { return minimumSize(); } } //- /* class FramedArea extends Panel { public FramedArea(Ether controller) { ...//Set the layout manager. //Add any Components this Panel contains... } //Ensure that no Component is placed on top of the frame. //The inset values were determined by trail and error. public Insets insets() { return new Insets(4,4,5,5); } //Draw the frame at this Panel's edges. public void paint(Graphics g) { Dimension d = size(); Color bg = getBackground(); g.setColor(bg); g.draw3DRect(0, 0, d.width - 1, d.height - 1, true); g.draw3DRect(3, 3, d.width - 7, d.height - 7, false); } } */