/* Distributed Systems: Practical Exercise 2 This class represents the updated version of the network constructed on the canvas. It contains two versions of the network nodes. One is the general Node objects and one is the operational "Node objects", i.e. BullyNode or RingNode depending on which algorithm is running. You should not change anything in this file. This template file (C) The University of Edinburgh, 2002 Author: Yussuf Abu Shaaban */ import java.util.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class Network extends Component{ private static int nodeCount = 1; private static int channelCount = 1; private Simulator simulator; public Vector generalNodes = new Vector(); private Vector nodes = new Vector(); private Vector channels = new Vector(); private Vector messages = new Vector(); public Network(Simulator s){ simulator = s; } public void updateInterface(){ simulator.repaint(); } public void paint(Graphics g){ if(!generalNodes.isEmpty()){ for(int i = 0; i < generalNodes.size(); i++){ Node n = (Node) generalNodes.elementAt(i); n.paint(g); } } if(!channels.isEmpty()){ for(int i = 0; i < channels.size(); i++){ Channel c = (Channel) channels.elementAt(i); c.paint(g); } } if(!messages.isEmpty()){ for(int i = 0; i < messages.size(); i++){ Message m = (Message) messages.elementAt(i); m.paint(g); } } } public Vector getNodes(){ return nodes; } public Vector getChannels(){ return channels; } public Vector getMessages(){ return messages; } public void addMessage(Message msg){ messages.addElement(msg); } public void deleteMessage(Message msg){ for(int i = 0; i < messages.size(); i++){ Message m = (Message) messages.elementAt(i); if(m.getId() == msg.getId()){ messages.removeElementAt(i); } } updateInterface(); } public void addNode(Point p){ boolean intersect = false; Rectangle2D potentialNode = (new Ellipse2D.Double(p.x, p.y, 30, 30)).getBounds2D(); for(int i = 0; i < generalNodes.size(); i++){ Node temp = (Node) generalNodes.elementAt(i); Rectangle2D recTemp = temp.graphicalRep.getBounds2D(); if(recTemp.intersects(potentialNode)){ intersect = true; } } if(!intersect){ generalNodes.addElement(new Node(p, nodeCount++, this)); } } public Node getNode(Node n){ return (Node) nodes.elementAt(generalNodes.indexOf(n)); } public void addChannel(Point p1, Point p2){ Node found1 = null, found2 = null; for(int i = 0; i < generalNodes.size(); i++){ Node n = (Node) generalNodes.elementAt(i); if(n.graphicalRep.contains(p1.x, p1.y)) found1 = n; if(n.graphicalRep.contains(p2.x, p2.y)) found2 = n; } try{ if(found1 != null & found2 != null & found1.getChannel(found2) == null & found1.compareTo(found2) != 0){ channels.addElement(new Channel(found1, found2, channelCount++)); } } catch(NullPointerException ex){ ; } } public Node nodeSelected(MouseEvent e){ for(int i = 0; i < generalNodes.size(); i++){ Node n = (Node) generalNodes.elementAt(i); if(n.graphicalRep.contains(e.getX(), e.getY())) return n; } return null; } public void setFailed(Node n){ try{ n.setFailed(); for(int i = 0; i < nodes.size(); i++){ if(((Node)nodes.elementAt(i)).compareTo(n) == 0){ ((Node)nodes.elementAt(i)).setFailed(); } } } catch(NullPointerException ex){ ; } } public void resetNetwork(){ for(int i = 0; i < generalNodes.size(); i++){ Node n = (Node) generalNodes.elementAt(i); n.color = Color.blue; } messages.removeAllElements(); updateInterface(); } public void updateNodeGraphically(Node n){ getGeneralNode(n).color = Color.red; updateInterface(); } private Node getGeneralNode(Node n){ return (Node) generalNodes.elementAt(nodes.indexOf(n)); } }