Parallel Programming WS 2014/2015 - File Node.java
import java.util.Set; import java.util.HashSet; import java.util.Iterator; public class Node extends Thread { private int id; // unique number of this node private Channel input; // the input port of this node private Set<Node> neighbours; // nodes adjacent to this node // construct a node with given id number and value public Node(int id) { this.id = id; this.input = new Channel(); this.neighbours = new HashSet<Node>(); } // get the numeric unique id of this node public int getNodeID() { return id; } // get the input port of this node public Channel getInputChannel() { return input; } // how many neighbours does this node have? public int getNumberOfNeighbours() { return neighbours.size(); } // iterate over all nodes adjacent to this node // returns an iterator over Node instances public Iterator getNeighbours() { return neighbours.iterator(); } // add an undirected edge between two nodes public static void connect(Node a, Node b) { if (a.equals(b)) { return; } // no self-loops please a.neighbours.add(b); b.neighbours.add(a); } public void run() { Node sender = null; if (!isInitiator()) { sender = input.receive().getSender(); } Iterator nbs = getNeighbours(); while (nbs.hasNext()) { Node nb = (Node) nbs.next(); nb.getInputChannel().send(new Message(this)); } int outstandingMsgs = getNumberOfNeighbours() - 1; while (outstandingMsgs > 0) { input.receive(); outstandingMsgs -= 1; } if (!isInitiator()) { System.out.printf("Spanning tree edge %d --> %d\n", sender.getNodeID(), getNodeID()); } } boolean isInitiator() { return getNodeID() == Main.INITIATOR; } }
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 26.01.2015