annotate src/fdl/test/debug/MetaProtocolEngine.java @ 89:b658ff1eb90f

MetaLinda.java sync() bug fix
author one
date Thu, 11 Feb 2010 16:26:04 +0900
parents 5d1189e9e420
children 9cdc24bae625
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
84
c0575f877591 add debug engine
one
parents:
diff changeset
63 // Callback class
c0575f877591 add debug engine
one
parents:
diff changeset
64 class AcceptXMLCallback implements PSXCallback {
c0575f877591 add debug engine
one
parents:
diff changeset
65 int tid;
c0575f877591 add debug engine
one
parents:
diff changeset
66
c0575f877591 add debug engine
one
parents:
diff changeset
67 private DocumentBuilderFactory dbFactory = null;
c0575f877591 add debug engine
one
parents:
diff changeset
68 private DocumentBuilder docBuilder = null;
c0575f877591 add debug engine
one
parents:
diff changeset
69 protected Document document;
c0575f877591 add debug engine
one
parents:
diff changeset
70
c0575f877591 add debug engine
one
parents:
diff changeset
71 public AcceptXMLCallback(int tid) {
c0575f877591 add debug engine
one
parents:
diff changeset
72 this.tid = tid;
c0575f877591 add debug engine
one
parents:
diff changeset
73 dbFactory = DocumentBuilderFactory.newInstance();
c0575f877591 add debug engine
one
parents:
diff changeset
74 try {
c0575f877591 add debug engine
one
parents:
diff changeset
75 docBuilder = dbFactory.newDocumentBuilder();
c0575f877591 add debug engine
one
parents:
diff changeset
76 } catch (ParserConfigurationException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
77 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
78 }
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 public void callback(ByteBuffer reply) {
c0575f877591 add debug engine
one
parents:
diff changeset
82 String xml = new String(reply.array());
c0575f877591 add debug engine
one
parents:
diff changeset
83 print(xml);
c0575f877591 add debug engine
one
parents:
diff changeset
84 parseXML(xml);
c0575f877591 add debug engine
one
parents:
diff changeset
85
c0575f877591 add debug engine
one
parents:
diff changeset
86 ml.in(tid, this);
c0575f877591 add debug engine
one
parents:
diff changeset
87 }
c0575f877591 add debug engine
one
parents:
diff changeset
88 @SuppressWarnings("deprecation")
c0575f877591 add debug engine
one
parents:
diff changeset
89 protected void parseXML(String xml) {
c0575f877591 add debug engine
one
parents:
diff changeset
90 Document doc = null;
c0575f877591 add debug engine
one
parents:
diff changeset
91 try {
c0575f877591 add debug engine
one
parents:
diff changeset
92 doc = docBuilder.parse(new StringBufferInputStream(xml));
c0575f877591 add debug engine
one
parents:
diff changeset
93 } catch (SAXException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
94 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
95 } catch (IOException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
96 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
97 }
c0575f877591 add debug engine
one
parents:
diff changeset
98
c0575f877591 add debug engine
one
parents:
diff changeset
99 Element root = doc.getDocumentElement();
c0575f877591 add debug engine
one
parents:
diff changeset
100 if(root.getTagName().equals("connections")) {
c0575f877591 add debug engine
one
parents:
diff changeset
101 nodeId = new Integer(root.getAttribute("id")).intValue();
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
102 if (nodeId == 0) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
103 ml.in(START, new StartCallback());
8931a3e90c2a ring implements
one
parents: 86
diff changeset
104 ml.in(DEBUGSTART, new DebugStartCallback());
8931a3e90c2a ring implements
one
parents: 86
diff changeset
105 }
84
c0575f877591 add debug engine
one
parents:
diff changeset
106 NodeList connections = root.getElementsByTagName("connection");
c0575f877591 add debug engine
one
parents:
diff changeset
107 for (int i = 0; i < connections.getLength(); i++) {
c0575f877591 add debug engine
one
parents:
diff changeset
108 Element connection = (Element)connections.item(i);
c0575f877591 add debug engine
one
parents:
diff changeset
109 Element host = (Element)connection.getElementsByTagName("host").item(0);
c0575f877591 add debug engine
one
parents:
diff changeset
110 Element port = (Element)connection.getElementsByTagName("port").item(0);
c0575f877591 add debug engine
one
parents:
diff changeset
111 Element t = (Element)connection.getElementsByTagName("tid").item(0);
c0575f877591 add debug engine
one
parents:
diff changeset
112 int srcId = new Integer(connection.getAttribute("id")).intValue();
c0575f877591 add debug engine
one
parents:
diff changeset
113 String dstHostName = host.getTextContent();
c0575f877591 add debug engine
one
parents:
diff changeset
114 int dstPort = new Integer(port.getAttribute("id")).intValue();
c0575f877591 add debug engine
one
parents:
diff changeset
115 int dstId = new Integer(t.getAttribute("id")).intValue();
c0575f877591 add debug engine
one
parents:
diff changeset
116 try {
c0575f877591 add debug engine
one
parents:
diff changeset
117 PSXLindaImpl linda = (PSXLindaImpl) ml.open(dstHostName, dstPort);
c0575f877591 add debug engine
one
parents:
diff changeset
118 Routing r = new Routing(linda, dstId);
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
119 nodes.put(new Integer(srcId), r);
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
120 ml.in(srcId, new RoutingCallback(srcId, r));
84
c0575f877591 add debug engine
one
parents:
diff changeset
121 } catch (IOException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
122 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
123 }
c0575f877591 add debug engine
one
parents:
diff changeset
124 }
c0575f877591 add debug engine
one
parents:
diff changeset
125 } else if (root.getTagName().equals("routing")) {
c0575f877591 add debug engine
one
parents:
diff changeset
126 print("Routing xml received!");
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
127
84
c0575f877591 add debug engine
one
parents:
diff changeset
128 NodeList routing = root.getElementsByTagName("source");
c0575f877591 add debug engine
one
parents:
diff changeset
129 for (int i = 0; i < routing.getLength(); i++) {
c0575f877591 add debug engine
one
parents:
diff changeset
130 Element src = (Element) routing.item(i);
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
131 Integer srcId = new Integer(src.getAttribute("id"));
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
132 Routing r = nodes.get(srcId);
84
c0575f877591 add debug engine
one
parents:
diff changeset
133 NodeList dest = src.getElementsByTagName("dest");
85
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
134 for (int j = 0; j < dest.getLength(); j++) {
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
135 Element dst = (Element) dest.item(j);
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
136 Integer dstId = new Integer(dst.getAttribute("id"));
d0d8aeaebccf add routing table
one
parents: 84
diff changeset
137 r.route.add(dstId);
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());
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
165
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");
8931a3e90c2a ring implements
one
parents: 86
diff changeset
170 } else if (nodeId == 0 && tid == RINGLEFT) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
171 relayCounter++;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
172 print(new Integer(relayCounter).toString() + " relay");
8931a3e90c2a ring implements
one
parents: 86
diff changeset
173 if (relayCounter >= relayNum) {
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
174 sendLocalHostName();
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
175 ml.in(tid, this);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
176 return;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
177 }
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
178 } else if (str.equals("shutdown")) {
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
179 if (tid == RINGLEFT) {
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
180 Routing r = nodes.get(new Integer(RINGRIGHT));
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
181 r.linda.out(r.dstId, ByteBuffer.wrap("shutdown".getBytes()));
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
182 try {
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
183 r.linda.sync(1);
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
184 } catch (IOException e) {
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
185 e.printStackTrace();
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
186 }
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
187 running = false;
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
188 return;
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
189 } else if (tid == RINGRIGHT) {
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
190 Routing r = nodes.get(new Integer(RINGLEFT));
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
191 r.linda.out(r.dstId, ByteBuffer.wrap("shutdown".getBytes()));
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
192 try {
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
193 r.linda.sync(1);
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
194 } catch (IOException e) {
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
195 e.printStackTrace();
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
196 }
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
197 running = false;
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
198 return;
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
199 }
87
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
8931a3e90c2a ring implements
one
parents: 86
diff changeset
215 public void callback(ByteBuffer reply) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
216 Routing r;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
217
8931a3e90c2a ring implements
one
parents: 86
diff changeset
218 // 子があるならば、子にタプルを伝搬
8931a3e90c2a ring implements
one
parents: 86
diff changeset
219 if (nodes.containsKey(new Integer(TREERIGHT))) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
220 r = nodes.get(new Integer(TREERIGHT));
8931a3e90c2a ring implements
one
parents: 86
diff changeset
221 r.linda.out(r.dstId, reply);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
222 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
223 if (nodes.containsKey(new Integer(TREELEFT))) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
224 r = nodes.get(new Integer(TREELEFT));
8931a3e90c2a ring implements
one
parents: 86
diff changeset
225 r.linda.out(r.dstId, reply);
8931a3e90c2a ring implements
one
parents: 86
diff changeset
226 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
227 ml.in(START, this);
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
8931a3e90c2a ring implements
one
parents: 86
diff changeset
232 private class DebugStartCallback implements PSXCallback {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
233 public void callback(ByteBuffer reply) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
234 String[] commands = new String(reply.array()).split(",");
8931a3e90c2a ring implements
one
parents: 86
diff changeset
235 String command = commands[0];
8931a3e90c2a ring implements
one
parents: 86
diff changeset
236 if (command.equals("relay")) {
8931a3e90c2a ring implements
one
parents: 86
diff changeset
237 relayNum = new Integer(commands[1]).intValue();
8931a3e90c2a ring implements
one
parents: 86
diff changeset
238 relaySize = new Integer(commands[2]).intValue();
8931a3e90c2a ring implements
one
parents: 86
diff changeset
239 relayCounter = 0;
8931a3e90c2a ring implements
one
parents: 86
diff changeset
240
8931a3e90c2a ring implements
one
parents: 86
diff changeset
241 print("relay num=" + new Integer(relayNum).toString() + " size=" + new Integer(relaySize).toString());
8931a3e90c2a ring implements
one
parents: 86
diff changeset
242
8931a3e90c2a ring implements
one
parents: 86
diff changeset
243 Routing r = nodes.get(new Integer(RINGRIGHT));
8931a3e90c2a ring implements
one
parents: 86
diff changeset
244 r.linda.out(r.dstId, ByteBuffer.wrap(new byte[relaySize]));
8931a3e90c2a ring implements
one
parents: 86
diff changeset
245 ml.in(DEBUGSTART, this);
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
246 } else if (command.equals("shutdown")) {
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
247 Routing r = nodes.get(new Integer(RINGRIGHT));
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
248 r.linda.out(r.dstId, ByteBuffer.wrap("shutdown".getBytes()));
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
249 running = false;
87
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 }
8931a3e90c2a ring implements
one
parents: 86
diff changeset
253
84
c0575f877591 add debug engine
one
parents:
diff changeset
254 // Constructor
c0575f877591 add debug engine
one
parents:
diff changeset
255 public MetaProtocolEngine(int port, MetaLinda ml, String managerHostName, int managerPort) {
c0575f877591 add debug engine
one
parents:
diff changeset
256 this.ml = ml;
c0575f877591 add debug engine
one
parents:
diff changeset
257 this.localPort = port;
c0575f877591 add debug engine
one
parents:
diff changeset
258 this.managerHostName = managerHostName;
c0575f877591 add debug engine
one
parents:
diff changeset
259 this.managerPort = managerPort;
c0575f877591 add debug engine
one
parents:
diff changeset
260 this.nodes = new HashMap<Integer, Routing>();
c0575f877591 add debug engine
one
parents:
diff changeset
261 try {
c0575f877591 add debug engine
one
parents:
diff changeset
262 this.localHostName = InetAddress.getLocalHost().getHostName();
c0575f877591 add debug engine
one
parents:
diff changeset
263 } catch (UnknownHostException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
264 e.printStackTrace();
c0575f877591 add debug engine
one
parents:
diff changeset
265 }
87
8931a3e90c2a ring implements
one
parents: 86
diff changeset
266 manager = connectServer(this.managerHostName, this.managerPort);
84
c0575f877591 add debug engine
one
parents:
diff changeset
267 sendLocalHostName();
c0575f877591 add debug engine
one
parents:
diff changeset
268 }
c0575f877591 add debug engine
one
parents:
diff changeset
269
c0575f877591 add debug engine
one
parents:
diff changeset
270 public void mainLoop() {
c0575f877591 add debug engine
one
parents:
diff changeset
271 initPoller();
c0575f877591 add debug engine
one
parents:
diff changeset
272 while (running) {
88
5d1189e9e420 MetaLinda.sync() fix
one
parents: 87
diff changeset
273 ml.sync();
84
c0575f877591 add debug engine
one
parents:
diff changeset
274 }
c0575f877591 add debug engine
one
parents:
diff changeset
275 }
c0575f877591 add debug engine
one
parents:
diff changeset
276
c0575f877591 add debug engine
one
parents:
diff changeset
277 protected void initPoller() {
c0575f877591 add debug engine
one
parents:
diff changeset
278 ml.in(MANAGE, new AcceptXMLCallback(MANAGE));
c0575f877591 add debug engine
one
parents:
diff changeset
279 ml.in(DEBUG, new AcceptXMLCallback(DEBUG));
c0575f877591 add debug engine
one
parents:
diff changeset
280 }
c0575f877591 add debug engine
one
parents:
diff changeset
281
c0575f877591 add debug engine
one
parents:
diff changeset
282 protected void sendLocalHostName() {
c0575f877591 add debug engine
one
parents:
diff changeset
283 // TopologyManager に自分のホストネームを送信して、起動を伝える
c0575f877591 add debug engine
one
parents:
diff changeset
284 ByteBuffer local;
c0575f877591 add debug engine
one
parents:
diff changeset
285 local = ByteBuffer.wrap((localHostName + ":" + new Integer(localPort).toString()).getBytes());
c0575f877591 add debug engine
one
parents:
diff changeset
286 manager.out(MANAGE, local);
c0575f877591 add debug engine
one
parents:
diff changeset
287 }
c0575f877591 add debug engine
one
parents:
diff changeset
288
c0575f877591 add debug engine
one
parents:
diff changeset
289 protected PSXLinda connectServer(String hostName, int port) {
c0575f877591 add debug engine
one
parents:
diff changeset
290 PSXLinda linda = null;
c0575f877591 add debug engine
one
parents:
diff changeset
291 boolean connectPSX = true;
c0575f877591 add debug engine
one
parents:
diff changeset
292 while (connectPSX) {
c0575f877591 add debug engine
one
parents:
diff changeset
293 try {
c0575f877591 add debug engine
one
parents:
diff changeset
294 linda = ml.open(hostName, port);
c0575f877591 add debug engine
one
parents:
diff changeset
295 connectPSX = false;
c0575f877591 add debug engine
one
parents:
diff changeset
296 } catch (IOException e) {
c0575f877591 add debug engine
one
parents:
diff changeset
297 try {
c0575f877591 add debug engine
one
parents:
diff changeset
298 Thread.sleep(40);
c0575f877591 add debug engine
one
parents:
diff changeset
299 } catch (InterruptedException e1) {
c0575f877591 add debug engine
one
parents:
diff changeset
300 }
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 print("Connect to " + hostName);
c0575f877591 add debug engine
one
parents:
diff changeset
304 return linda;
c0575f877591 add debug engine
one
parents:
diff changeset
305 }
c0575f877591 add debug engine
one
parents:
diff changeset
306
c0575f877591 add debug engine
one
parents:
diff changeset
307 void print(String str) {
c0575f877591 add debug engine
one
parents:
diff changeset
308 System.err.println("[DEBUG] " + localHostName + ": " + str);
89
b658ff1eb90f MetaLinda.java sync() bug fix
one
parents: 88
diff changeset
309 System.err.flush();
84
c0575f877591 add debug engine
one
parents:
diff changeset
310 }
c0575f877591 add debug engine
one
parents:
diff changeset
311
c0575f877591 add debug engine
one
parents:
diff changeset
312 }