76
|
1 package fdl.test.topology;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.nio.ByteBuffer;
|
|
5 import java.util.ArrayList;
|
|
6
|
|
7 import fdl.PSXLinda;
|
|
8 import fdl.MetaLinda;
|
|
9
|
|
10 public class NodeManager {
|
|
11 public String hostName;
|
|
12 public PSXLinda linda;
|
|
13 private MetaLinda mLinda;
|
|
14 private int port;
|
|
15 private int manageId;
|
77
|
16 private ArrayList<NodeManager> waitingNodes;
|
|
17 private ArrayList<NodeManager> waitedNodes;
|
79
|
18 // private ArrayList<NodeManager> sendNodes;
|
76
|
19
|
|
20 public NodeManager(MetaLinda ml, int port, int manageId) {
|
|
21 this.port = port;
|
|
22 this.manageId = manageId;
|
|
23 this.mLinda = ml;
|
|
24 hostName = null;
|
|
25 waitingNodes = new ArrayList<NodeManager>();
|
|
26 waitedNodes = new ArrayList<NodeManager>();
|
79
|
27 // sendNodes = new ArrayList<NodeManager>();
|
76
|
28 }
|
|
29
|
|
30 public void addConnection(NodeManager node) {
|
|
31 waitingNodes.add(node);
|
|
32 node.waitedNodes.add(this);
|
|
33 }
|
|
34
|
|
35 public void finishConnection(NodeManager node) {
|
|
36 waitingNodes.remove(node);
|
|
37 node.waitedNodes.remove(this);
|
79
|
38 // sendNodes.add(node);
|
76
|
39 }
|
|
40
|
|
41 public void startUp(String hostName) {
|
|
42 // 起動
|
|
43 this.hostName = hostName;
|
|
44 try {
|
|
45 linda = mLinda.open(hostName, port);
|
|
46 } catch (IOException e) {
|
|
47 e.printStackTrace();
|
|
48 }
|
|
49 // 起動待ちのノードに通知
|
|
50 connectWaitedNodes();
|
|
51 }
|
|
52
|
|
53 public void connectWaitedNodes() {
|
|
54 for (int i = 0; i < waitedNodes.size(); i++) {
|
|
55 NodeManager node = waitedNodes.get(i);
|
|
56 if (node.isRunning()) {
|
|
57 node.connect(this);
|
|
58 node.finishConnection(this);
|
|
59 if (waitingNodes.contains(node)) {
|
|
60 connect(node);
|
|
61 finishConnection(node);
|
|
62 }
|
|
63 }
|
|
64 }
|
81
|
65 for (int i = 0; i < waitingNodes.size(); i++) {
|
|
66 NodeManager node = waitingNodes.get(i);
|
|
67 if (node.isRunning()) {
|
|
68 connect(node);
|
|
69 finishConnection(node);
|
|
70 if (node.waitingNodes.contains(this)) {
|
|
71 node.connect(this);
|
|
72 node.finishConnection(this);
|
|
73 }
|
|
74 }
|
|
75 }
|
76
|
76 }
|
|
77
|
|
78 public void connect(NodeManager node) {
|
|
79 ByteBuffer data = ByteBuffer.wrap(node.hostName.getBytes());
|
|
80 linda.out(manageId, data);
|
|
81 try {
|
|
82 linda.sync(1);
|
|
83 System.out.println("[DEBUG] SendConnectTo: " + hostName + " to " + node.hostName);
|
|
84 } catch (IOException e) {
|
|
85 e.printStackTrace();
|
|
86 }
|
|
87 }
|
|
88
|
|
89 public boolean isRunning() {
|
|
90 return hostName != null;
|
|
91 }
|
|
92
|
|
93 }
|