/* Distributed Systems: Practical Exercise 2 This class represent represents a general node in the network. Classes RingNode and BullyNode extends this class. You should not change anything in this file. This template file (C) The University of Edinburgh, 2002 Author: Yussuf Abu Shaaban */ import java.awt.*; import java.awt.geom.*; import java.util.*; public class Node extends Component implements Comparable{ private final double width = 30.0; private final double height = 30.0; public Color color = Color.blue; private static int NodeCount = 0; protected int id; protected boolean failed = false; Ellipse2D.Double graphicalRep; public Network network; public Node(){ ; } public Node(Point p, int id, Network network){ super(); this.id = id; this.network = network; graphicalRep = new Ellipse2D.Double(p.x, p.y, width, height); } public void receiveMessage(Message msg){ ; } public Vector neighbours(){ Channel tempC = null; Vector linkedNodes = new Vector(); for(int i = 0; i < network.getChannels().size(); i++){ tempC = (Channel) network.getChannels().elementAt(i); if(tempC.getNode1().compareTo(this) == 0){ linkedNodes.addElement(network.getNode(tempC.getNode2())); } } return linkedNodes; } public Vector allNodes(){ return network.getNodes(); } public int getId(){ return id; } public boolean isFailed(){ return failed; } public void changeGraphicalState(){ network.updateNodeGraphically(this); } public void discardMessage(Message msg){ network.deleteMessage(msg); } public void setFailed(){ failed = true; } public Channel getChannel(Node neighbour){ Channel channel = null; for(int i = 0; i < network.getChannels().size(); i++){ Channel tempC = (Channel) network.getChannels().elementAt(i); if(tempC.getNode1().compareTo(this) == 0 & tempC.getNode2().compareTo(neighbour) == 0){ channel = tempC; } } return channel; } public void paint(Graphics g){ Graphics2D g2 = (Graphics2D) g; g2.setPaint(color); g2.draw(graphicalRep); g2.setPaint(color); g2.fill(graphicalRep); g.setColor(Color.black); g.drawString(String.valueOf(id), (int)graphicalRep.getX(), (int) graphicalRep.getY()); if(failed){ Rectangle rn1 = graphicalRep.getBounds(); Line2D.Double line1 = new Line2D.Double(rn1.x, rn1.y, rn1.x + rn1.width, rn1.y + rn1.height); Line2D.Double line2 = new Line2D.Double(rn1.x + rn1.width, rn1.y, rn1.x, rn1.y + rn1.height); Stroke stroke = new BasicStroke(7); g2.setStroke(stroke); g2.setColor(Color.black); g2.draw(line1); g2.draw(line2); g2.setStroke(new BasicStroke()); } } public int compareTo(Object o){ Node temp = (Node) o; if(this.getId() < temp.getId()){ return -1;} else if(this.getId() == temp.getId()) { return 0;} else return 1; } }