// COM1204 - FILE WITH THREE JAVA SOURCE FILES // FOR A VERY SIMPLE NETWORK SIMULATION. /* * Simplest demo network simulation. * class Sim.java R P Futrelle, * 7/18/2001 */ /** * Creates the initial objects and runs * the simulation loop. * Rather than doing this all in static functions, * a single instance of Sim is created and used. */ public class Sim { Link link; Node[] nodes; public static void main(String[] args){ Sim sim = new Sim(); sim.run(); } // main() private Sim() { link = new Link(); nodes = new Node[2]; for(int i=0; i < 2; i++) nodes[i] = new Node(i,link); }// Sim() /** * Loops by running nodes */ private void run() { for(int j = 0; j < 5; j++) { nodes[0].send(); nodes[1].send(); System.out.println("0 " + nodes[0].receive()); System.out.println("1 " + nodes[1].receive()); link.clear(); } } // run() }// class Sim // ******************************************************** /* * Simplest demo network simulation. * class Link.java * R P Futrelle, 7/18/2001 */ /** * Simple passive link. * Holds one message. */ public class Link { Object msg; public static void main(String[] args){ } // main() public Link() { msg = null; }// Node() public void insert(Object m) { msg = m; } // insert() public Object retrieve () { return msg; } // retrieve() public void clear() { msg = null; } // clear() }// class Link // ******************************************************** /* * Simplest demo network simulation. * class Node.java * R P Futrelle, 7/18/2001 */ /** * A node might be a workstation or PC on the net. * It is attached to the link and sends and receives * messages */ public class Node { Link link; int nodeID; public static void main(String[] args){ } // main() public Node(int nodeID, Link ln) { link = ln; this.nodeID = nodeID; }// Node() public void send() { if(Math.random() < 0.1) link.insert("Is there life on this net?"); } // send() public Object receive() { return link.retrieve(); } // receive }// class Node