annotate src/fdl/test/debug/MetaProtocolEngine.java @ 91:4df1d50df52a

Ring: fdl.test.debug
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Tue, 16 Feb 2010 03:58:06 +0900
parents 9cdc24bae625
children 0ea086f0e96f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84
c0575f877591 add debug engine
one
parents:
diff changeset
1 package fdl.test.debug;
c0575f877591 add debug engine
one
parents:
diff changeset
2
c0575f877591 add debug engine
one
parents:
diff changeset
3 import java.io.*;
c0575f877591 add debug engine
one
parents:
diff changeset
4 import java.net.InetAddress;
c0575f877591 add debug engine
one
parents:
diff changeset
5 import java.net.UnknownHostException;
c0575f877591 add debug engine
one
parents:
diff changeset
6
c0575f877591 add debug engine
one
parents:
diff changeset
7 import java.nio.ByteBuffer;
c0575f877591 add debug engine
one
parents:
diff changeset
8 import java.util.*;
c0575f877591 add debug engine
one
parents:
diff changeset
9
c0575f877591 add debug engine
one
parents:
diff changeset
10 import javax.xml.parsers.*;
c0575f877591 add debug engine
one
parents:
diff changeset
11
c0575f877591 add debug engine
one
parents:
diff changeset
12 import org.w3c.dom.Document;
c0575f877591 add debug engine
one
parents:
diff changeset
13 import org.w3c.dom.Element;
c0575f877591 add debug engine
one
parents:
diff changeset
14 import org.w3c.dom.NodeList;
c0575f877591 add debug engine
one
parents:
diff changeset
15 import org.xml.sax.SAXException;
c0575f877591 add debug engine
one
parents:
diff changeset
16
c0575f877591 add debug engine
one
parents:
diff changeset
17 import fdl.*;
c0575f877591 add debug engine
one
parents:
diff changeset
18
c0575f877591 add debug engine
one
parents:
diff changeset
19 /**
c0575f877591 add debug engine
one
parents:
diff changeset
20 * MetaProtocolEngine
c0575f877591 add debug engine
one
parents:
diff changeset
21 *
c0575f877591 add debug engine
one
parents:
diff changeset
22 * @author Kazuki Akamine
c0575f877591 add debug engine
one
parents:
diff changeset
23 *
c0575f877591 add debug engine
one
parents:
diff changeset
24 * 接続する機能までを実装した MetaEngine
c0575f877591 add debug engine
one
parents:
diff changeset
25 * これを継承して、具体的な処理を書く
c0575f877591 add debug engine
one
parents:
diff changeset
26 *
c0575f877591 add debug engine
one
parents:
diff changeset
27 */
c0575f877591 add debug engine
one
parents:
diff changeset
28
c0575f877591 add debug engine
one
parents:
diff changeset
29 public class MetaProtocolEngine implements MetaEngine {
c0575f877591 add debug engine
one
parents:
diff changeset
30 // Fields
c0575f877591 add debug engine
one
parents:
diff changeset
31 public static final int DEFAULTPORT = 10000;
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
32
8931a3e90c2a ring implements
one
parents: 86
diff changeset
33 public static final int BODY = 100;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
34 public static final int START = 101;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
35 public static final int FINISH = 102;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
36
84
c0575f877591 add debug engine
one
parents:
diff changeset
37 public static final int MANAGE = 60000;
c0575f877591 add debug engine
one
parents:
diff changeset
38 public static final int DEBUG = 61000;
c0575f877591 add debug engine
one
parents:
diff changeset
39
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
40 public static final int TREETOP = MANAGE + 1;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
41 public static final int TREELEFT = MANAGE + 2;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
42 public static final int TREERIGHT = MANAGE + 3;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
43
8931a3e90c2a ring implements
one
parents: 86
diff changeset
44 public static final int RINGLEFT = DEBUG + 1;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
45 public static final int RINGRIGHT = DEBUG + 2;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
46
8931a3e90c2a ring implements
one
parents: 86
diff changeset
47 public static final int DEBUGSTART = DEBUG + 1000;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
48
84
c0575f877591 add debug engine
one
parents:
diff changeset
49 private MetaLinda ml;
c0575f877591 add debug engine
one
parents:
diff changeset
50 private String localHostName;
c0575f877591 add debug engine
one
parents:
diff changeset
51 private int localPort;
c0575f877591 add debug engine
one
parents:
diff changeset
52 private PSXLinda manager;
c0575f877591 add debug engine
one
parents:
diff changeset
53 private String managerHostName;
c0575f877591 add debug engine
one
parents:
diff changeset
54 private int managerPort = DEFAULTPORT;
c0575f877591 add debug engine
one
parents:
diff changeset
55 private boolean running = true;
c0575f877591 add debug engine
one
parents:
diff changeset
56 private boolean connected = false;
c0575f877591 add debug engine
one
parents:
diff changeset
57 private boolean debugConnected = false;
c0575f877591 add debug engine
one
parents:
diff changeset
58 private int nodeId;
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
59 private HashMap<Integer, Routing> nodes;
84
c0575f877591 add debug engine
one
parents:
diff changeset
60
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
61 private int relayNum, relaySize, relayCounter;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
62
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
63 private Date startTime, endTime;
9cdc24bae625 ring test
one
parents: 89
diff changeset
64
84
c0575f877591 add debug engine
one
parents:
diff changeset
65 // Callback class
c0575f877591 add debug engine
one
parents:
diff changeset
66 class AcceptXMLCallback implements PSXCallback {
c0575f877591 add debug engine
one
parents:
diff changeset
67 int tid;
c0575f877591 add debug engine
one
parents:
diff changeset
68
c0575f877591 add debug engine
one
parents:
diff changeset
69 private DocumentBuilderFactory dbFactory = null;
c0575f877591 add debug engine
one
parents:
diff changeset
70 private DocumentBuilder docBuilder = null;
c0575f877591 add debug engine
one
parents:
diff changeset
71
c0575f877591 add debug engine
one
parents:
diff changeset
72 public AcceptXMLCallback(int tid) {
c0575f877591 add debug engine
one
parents:
diff changeset
73 this.tid = tid;
c0575f877591 add debug engine
one
parents:
diff changeset
74 dbFactory = DocumentBuilderFactory.newInstance();
c0575f877591 add debug engine
one
parents:
diff changeset
75 try {
c0575f877591 add debug engine
one
parents:
diff changeset
76 docBuilder = dbFactory.newDocumentBuilder();
c0575f877591 add debug engine
one
parents:
diff changeset
77 } catch (ParserConfigurationException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
78 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
79 }
c0575f877591 add debug engine
one
parents:
diff changeset
80
c0575f877591 add debug engine
one
parents:
diff changeset
81 }
c0575f877591 add debug engine
one
parents:
diff changeset
82 public void callback(ByteBuffer reply) {
c0575f877591 add debug engine
one
parents:
diff changeset
83 String xml = new String(reply.array());
c0575f877591 add debug engine
one
parents:
diff changeset
84 print(xml);
c0575f877591 add debug engine
one
parents:
diff changeset
85 parseXML(xml);
c0575f877591 add debug engine
one
parents:
diff changeset
86
c0575f877591 add debug engine
one
parents:
diff changeset
87 ml.in(tid, this);
c0575f877591 add debug engine
one
parents:
diff changeset
88 }
c0575f877591 add debug engine
one
parents:
diff changeset
89 @SuppressWarnings("deprecation")
c0575f877591 add debug engine
one
parents:
diff changeset
90 protected void parseXML(String xml) {
c0575f877591 add debug engine
one
parents:
diff changeset
91 Document doc = null;
c0575f877591 add debug engine
one
parents:
diff changeset
92 try {
c0575f877591 add debug engine
one
parents:
diff changeset
93 doc = docBuilder.parse(new StringBufferInputStream(xml));
c0575f877591 add debug engine
one
parents:
diff changeset
94 } catch (SAXException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
95 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
96 } catch (IOException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
97 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
98 }
c0575f877591 add debug engine
one
parents:
diff changeset
99
c0575f877591 add debug engine
one
parents:
diff changeset
100 Element root = doc.getDocumentElement();
c0575f877591 add debug engine
one
parents:
diff changeset
101 if(root.getTagName().equals("connections")) {
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
102 nodeId = Integer.parseInt(root.getAttribute("id"));
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
103 if (nodeId == 0) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
104 ml.in(START, new StartCallback());
8931a3e90c2a ring implements
one
parents: 86
diff changeset
105 ml.in(DEBUGSTART, new DebugStartCallback());
8931a3e90c2a ring implements
one
parents: 86
diff changeset
106 }
84
c0575f877591 add debug engine
one
parents:
diff changeset
107 NodeList connections = root.getElementsByTagName("connection");
c0575f877591 add debug engine
one
parents:
diff changeset
108 for (int i = 0; i < connections.getLength(); i++) {
c0575f877591 add debug engine
one
parents:
diff changeset
109 Element connection = (Element)connections.item(i);
c0575f877591 add debug engine
one
parents:
diff changeset
110 Element host = (Element)connection.getElementsByTagName("host").item(0);
c0575f877591 add debug engine
one
parents:
diff changeset
111 Element port = (Element)connection.getElementsByTagName("port").item(0);
c0575f877591 add debug engine
one
parents:
diff changeset
112 Element t = (Element)connection.getElementsByTagName("tid").item(0);
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
113 int srcId = Integer.parseInt(connection.getAttribute("id"));
84
c0575f877591 add debug engine
one
parents:
diff changeset
114 String dstHostName = host.getTextContent();
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
115 int dstPort = Integer.parseInt(port.getAttribute("id"));
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
116 int dstId = Integer.parseInt(t.getAttribute("id"));
84
c0575f877591 add debug engine
one
parents:
diff changeset
117 try {
c0575f877591 add debug engine
one
parents:
diff changeset
118 PSXLindaImpl linda = (PSXLindaImpl) ml.open(dstHostName, dstPort);
c0575f877591 add debug engine
one
parents:
diff changeset
119 Routing r = new Routing(linda, dstId);
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
120 nodes.put(srcId, r);
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
121 ml.in(srcId, new RoutingCallback(srcId, r));
84
c0575f877591 add debug engine
one
parents:
diff changeset
122 } catch (IOException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
123 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
124 }
c0575f877591 add debug engine
one
parents:
diff changeset
125 }
c0575f877591 add debug engine
one
parents:
diff changeset
126 } else if (root.getTagName().equals("routing")) {
c0575f877591 add debug engine
one
parents:
diff changeset
127 print("Routing xml received!");
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
128
84
c0575f877591 add debug engine
one
parents:
diff changeset
129 NodeList routing = root.getElementsByTagName("source");
c0575f877591 add debug engine
one
parents:
diff changeset
130 for (int i = 0; i < routing.getLength(); i++) {
c0575f877591 add debug engine
one
parents:
diff changeset
131 Element src = (Element) routing.item(i);
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
132 Integer srcId = Integer.parseInt(src.getAttribute("id"));
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
133 Routing r = nodes.get(srcId);
84
c0575f877591 add debug engine
one
parents:
diff changeset
134 NodeList dest = src.getElementsByTagName("dest");
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
135 for (int j = 0; j < dest.getLength(); j++) {
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
136 Element dst = (Element) dest.item(j);
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
137 r.route.add(Integer.parseInt(dst.getAttribute("id")));
84
c0575f877591 add debug engine
one
parents:
diff changeset
138 }
c0575f877591 add debug engine
one
parents:
diff changeset
139 }
c0575f877591 add debug engine
one
parents:
diff changeset
140
c0575f877591 add debug engine
one
parents:
diff changeset
141 }
c0575f877591 add debug engine
one
parents:
diff changeset
142 if (tid == MANAGE) connected = true;
c0575f877591 add debug engine
one
parents:
diff changeset
143 else if (tid == DEBUG) debugConnected = true;
c0575f877591 add debug engine
one
parents:
diff changeset
144 if (connected && debugConnected) {
c0575f877591 add debug engine
one
parents:
diff changeset
145 sendLocalHostName();
c0575f877591 add debug engine
one
parents:
diff changeset
146 print("Send local host name");
c0575f877591 add debug engine
one
parents:
diff changeset
147 connected = debugConnected = false;
c0575f877591 add debug engine
one
parents:
diff changeset
148 }
c0575f877591 add debug engine
one
parents:
diff changeset
149 }
c0575f877591 add debug engine
one
parents:
diff changeset
150
c0575f877591 add debug engine
one
parents:
diff changeset
151 }
c0575f877591 add debug engine
one
parents:
diff changeset
152
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
153 private class RoutingCallback implements PSXCallback {
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
154 int tid;
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
155 Routing routing;
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
156
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
157 public RoutingCallback(int tid, Routing routing) {
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
158 this.tid = tid;
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
159 this.routing = routing;
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
160 ml.out(BODY, ByteBuffer.wrap("dummy".getBytes()));
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
161 }
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
162
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
163 public void callback(ByteBuffer reply) {
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
164 String str = new String(reply.array());
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
165 print("get message");
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
166 if (tid == TREETOP || tid == TREELEFT || tid == TREERIGHT) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
167 ml.in(BODY);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
168 ml.out(BODY, reply);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
169 print("Update body");
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
170 } else if (str.equals("shutdown") && (tid == RINGLEFT || tid == RINGRIGHT)) {
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
171 print("get shutdown command id: " + nodeId);
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
172 if (nodeId != 0) {
9cdc24bae625 ring test
one
parents: 89
diff changeset
173 Routing r = null;
9cdc24bae625 ring test
one
parents: 89
diff changeset
174 if (tid == RINGLEFT) {
9cdc24bae625 ring test
one
parents: 89
diff changeset
175 r = nodes.get(new Integer(RINGRIGHT));
9cdc24bae625 ring test
one
parents: 89
diff changeset
176 } else if (tid == RINGRIGHT) {
9cdc24bae625 ring test
one
parents: 89
diff changeset
177 r = nodes.get(new Integer(RINGLEFT));
9cdc24bae625 ring test
one
parents: 89
diff changeset
178 }
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
179 r.linda.out(r.dstId, ByteBuffer.wrap("shutdown".getBytes()));
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
180 print("out");
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
181 ml.fdl.queueExec();
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
182 print("sync");
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
183 } else {
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
184 print("shutdown reaches last node!");
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
185 }
9cdc24bae625 ring test
one
parents: 89
diff changeset
186 running = false;
9cdc24bae625 ring test
one
parents: 89
diff changeset
187 return;
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
188 } else if (nodeId == 0 && tid == RINGLEFT) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
189 relayCounter++;
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
190 print("" + relayCounter + " relay");
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
191 if (relayCounter >= relayNum) {
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
192 // 実験終了
9cdc24bae625 ring test
one
parents: 89
diff changeset
193 endTime = new Date();
9cdc24bae625 ring test
one
parents: 89
diff changeset
194 Double resultTime = new Double(((endTime.getTime() - startTime.getTime()) / (double)relayNum));
9cdc24bae625 ring test
one
parents: 89
diff changeset
195 ByteBuffer data = ByteBuffer.wrap(resultTime.toString().getBytes());
9cdc24bae625 ring test
one
parents: 89
diff changeset
196 manager.out(MANAGE, data);
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
197 ml.in(tid, this);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
198 return;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
199 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
200 }
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
201
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
202 Iterator<Integer> it = routing.route.iterator();
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
203 while (it.hasNext()) {
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
204 Integer dstId = it.next();
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
205 Routing r = nodes.get(dstId);
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
206 r.linda.out(r.dstId, reply);
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
207 ml.in(tid, this);
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
208 }
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
209 }
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
210
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
211 }
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
212
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
213 private class StartCallback implements PSXCallback {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
214 public void callback(ByteBuffer reply) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
215 Routing r;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
216
8931a3e90c2a ring implements
one
parents: 86
diff changeset
217 // 子があるならば、子にタプルを伝搬
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
218 if (nodes.containsKey(TREERIGHT)) {
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
219 r = nodes.get(TREERIGHT);
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
220 r.linda.out(r.dstId, reply);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
221 }
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
222 if (nodes.containsKey(TREELEFT)) {
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
223 r = nodes.get(TREELEFT);
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
224 r.linda.out(r.dstId, reply);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
225 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
226 ml.in(START, this);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
227 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
228
8931a3e90c2a ring implements
one
parents: 86
diff changeset
229 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
230
8931a3e90c2a ring implements
one
parents: 86
diff changeset
231 private class DebugStartCallback implements PSXCallback {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
232 public void callback(ByteBuffer reply) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
233 String[] commands = new String(reply.array()).split(",");
8931a3e90c2a ring implements
one
parents: 86
diff changeset
234 String command = commands[0];
8931a3e90c2a ring implements
one
parents: 86
diff changeset
235 if (command.equals("relay")) {
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
236 relayNum = Integer.parseInt(commands[1]);
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
237 relaySize = Integer.parseInt(commands[2]);
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
238 relayCounter = 0;
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
239 print("relay num=" + relayNum + " size=" + relaySize);
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
240 Routing r = nodes.get(RINGRIGHT);
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
241 // 実験開始
9cdc24bae625 ring test
one
parents: 89
diff changeset
242 startTime = new Date();
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
243 r.linda.out(r.dstId, ByteBuffer.wrap(new byte[relaySize]));
8931a3e90c2a ring implements
one
parents: 86
diff changeset
244 ml.in(DEBUGSTART, this);
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
245 } else if (command.equals("shutdown")) {
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
246 Routing r = nodes.get(RINGRIGHT);
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
247 r.linda.out(r.dstId, ByteBuffer.wrap("shutdown".getBytes()));
90
9cdc24bae625 ring test
one
parents: 89
diff changeset
248 //running = false;
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
249 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
250 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
251 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
252
84
c0575f877591 add debug engine
one
parents:
diff changeset
253 // Constructor
c0575f877591 add debug engine
one
parents:
diff changeset
254 public MetaProtocolEngine(int port, MetaLinda ml, String managerHostName, int managerPort) {
c0575f877591 add debug engine
one
parents:
diff changeset
255 this.ml = ml;
c0575f877591 add debug engine
one
parents:
diff changeset
256 this.localPort = port;
c0575f877591 add debug engine
one
parents:
diff changeset
257 this.managerHostName = managerHostName;
c0575f877591 add debug engine
one
parents:
diff changeset
258 this.managerPort = managerPort;
c0575f877591 add debug engine
one
parents:
diff changeset
259 this.nodes = new HashMap<Integer, Routing>();
c0575f877591 add debug engine
one
parents:
diff changeset
260 try {
c0575f877591 add debug engine
one
parents:
diff changeset
261 this.localHostName = InetAddress.getLocalHost().getHostName();
c0575f877591 add debug engine
one
parents:
diff changeset
262 } catch (UnknownHostException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
263 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
264 }
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
265 manager = connectServer(this.managerHostName, this.managerPort);
84
c0575f877591 add debug engine
one
parents:
diff changeset
266 sendLocalHostName();
c0575f877591 add debug engine
one
parents:
diff changeset
267 }
c0575f877591 add debug engine
one
parents:
diff changeset
268
c0575f877591 add debug engine
one
parents:
diff changeset
269 public void mainLoop() {
c0575f877591 add debug engine
one
parents:
diff changeset
270 initPoller();
c0575f877591 add debug engine
one
parents:
diff changeset
271 while (running) {
88
5d1189e9e420 MetaLinda.sync() fix
one
parents: 87
diff changeset
272 ml.sync();
84
c0575f877591 add debug engine
one
parents:
diff changeset
273 }
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
274 print("Terminated" + nodeId
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
275 + " replies=" + ml.replies.size()
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
276 + " qsize=" + ml.fdl.qsize);
84
c0575f877591 add debug engine
one
parents:
diff changeset
277 }
c0575f877591 add debug engine
one
parents:
diff changeset
278
c0575f877591 add debug engine
one
parents:
diff changeset
279 protected void initPoller() {
c0575f877591 add debug engine
one
parents:
diff changeset
280 ml.in(MANAGE, new AcceptXMLCallback(MANAGE));
c0575f877591 add debug engine
one
parents:
diff changeset
281 ml.in(DEBUG, new AcceptXMLCallback(DEBUG));
c0575f877591 add debug engine
one
parents:
diff changeset
282 }
c0575f877591 add debug engine
one
parents:
diff changeset
283
c0575f877591 add debug engine
one
parents:
diff changeset
284 protected void sendLocalHostName() {
c0575f877591 add debug engine
one
parents:
diff changeset
285 // TopologyManager に自分のホストネームを送信して、起動を伝える
91
4df1d50df52a Ring: fdl.test.debug
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 90
diff changeset
286 ByteBuffer local = ByteBuffer.wrap((localHostName + ":" + localPort).getBytes());
84
c0575f877591 add debug engine
one
parents:
diff changeset
287 manager.out(MANAGE, local);
c0575f877591 add debug engine
one
parents:
diff changeset
288 }
c0575f877591 add debug engine
one
parents:
diff changeset
289
c0575f877591 add debug engine
one
parents:
diff changeset
290 protected PSXLinda connectServer(String hostName, int port) {
c0575f877591 add debug engine
one
parents:
diff changeset
291 PSXLinda linda = null;
c0575f877591 add debug engine
one
parents:
diff changeset
292 boolean connectPSX = true;
c0575f877591 add debug engine
one
parents:
diff changeset
293 while (connectPSX) {
c0575f877591 add debug engine
one
parents:
diff changeset
294 try {
c0575f877591 add debug engine
one
parents:
diff changeset
295 linda = ml.open(hostName, port);
c0575f877591 add debug engine
one
parents:
diff changeset
296 connectPSX = false;
c0575f877591 add debug engine
one
parents:
diff changeset
297 } catch (IOException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
298 try {
c0575f877591 add debug engine
one
parents:
diff changeset
299 Thread.sleep(40);
c0575f877591 add debug engine
one
parents:
diff changeset
300 } catch (InterruptedException e1) {
c0575f877591 add debug engine
one
parents:
diff changeset
301 }
c0575f877591 add debug engine
one
parents:
diff changeset
302 }
c0575f877591 add debug engine
one
parents:
diff changeset
303 }
c0575f877591 add debug engine
one
parents:
diff changeset
304 print("Connect to " + hostName);
c0575f877591 add debug engine
one
parents:
diff changeset
305 return linda;
c0575f877591 add debug engine
one
parents:
diff changeset
306 }
c0575f877591 add debug engine
one
parents:
diff changeset
307
c0575f877591 add debug engine
one
parents:
diff changeset
308 void print(String str) {
c0575f877591 add debug engine
one
parents:
diff changeset
309 System.err.println("[DEBUG] " + localHostName + ": " + str);
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
310 System.err.flush();
84
c0575f877591 add debug engine
one
parents:
diff changeset
311 }
c0575f877591 add debug engine
one
parents:
diff changeset
312
c0575f877591 add debug engine
one
parents:
diff changeset
313 }