114
|
1 package fdl.test.debug;
|
92
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.io.StringReader;
|
|
5 import java.nio.ByteBuffer;
|
|
6
|
|
7 import javax.xml.parsers.DocumentBuilder;
|
|
8 import javax.xml.parsers.DocumentBuilderFactory;
|
|
9 import javax.xml.parsers.ParserConfigurationException;
|
|
10
|
|
11 import org.w3c.dom.Document;
|
|
12 import org.w3c.dom.Element;
|
|
13 import org.w3c.dom.NodeList;
|
|
14 import org.xml.sax.InputSource;
|
|
15 import org.xml.sax.SAXException;
|
|
16
|
|
17 import fdl.PSXCallback;
|
|
18 import fdl.PSXLindaImpl;
|
|
19
|
|
20 class AcceptXMLCallback implements PSXCallback {
|
|
21 TupleId tid;
|
|
22 NodeProperty np;
|
|
23 TreeProperty tp;
|
|
24 DebugProperty dp;
|
|
25
|
|
26 private DocumentBuilderFactory dbFactory = null;
|
|
27 private DocumentBuilder docBuilder = null;
|
|
28
|
|
29 public AcceptXMLCallback(TupleId tid, NodeProperty np) {
|
|
30 this.tid = tid;
|
|
31 this.np = np;
|
|
32 dbFactory = DocumentBuilderFactory.newInstance();
|
|
33 try {
|
|
34 docBuilder = dbFactory.newDocumentBuilder();
|
|
35 } catch (ParserConfigurationException e) {
|
|
36 e.printStackTrace();
|
|
37 }
|
|
38
|
|
39 }
|
|
40 public void callback(ByteBuffer reply) {
|
|
41 String xml = new String(reply.array());
|
|
42 Debug.print(xml);
|
|
43 parseXML(xml);
|
|
44
|
|
45 np.ml.in(tid.id, this);
|
|
46 }
|
|
47 protected void parseXML(String xml) {
|
|
48 Document doc = null;
|
|
49 try {
|
|
50 doc = docBuilder.parse(new InputSource(new StringReader(xml)));
|
|
51 } catch (SAXException e) {
|
|
52 e.printStackTrace();
|
|
53 } catch (IOException e) {
|
|
54 e.printStackTrace();
|
|
55 }
|
|
56
|
|
57 Element root = doc.getDocumentElement();
|
|
58 if(root.getTagName().equals("connections")) {
|
|
59 np.nodeId = Integer.parseInt(root.getAttribute("id"));
|
|
60 if (np.nodeId == 0) {
|
|
61 np.ml.in(TupleId.START.id, new StartCallback(np, tp = new TreeProperty(np)));
|
|
62 np.ml.in(TupleId.DEBUGSTART.id, new DebugStartCallback(np, dp = new DebugProperty()));
|
|
63 }
|
|
64 NodeList connections = root.getElementsByTagName("connection");
|
|
65 makeConnections(connections);
|
|
66 } else if (root.getTagName().equals("routing")) {
|
|
67 Debug.print("Routing xml received!");
|
|
68
|
|
69 NodeList routing = root.getElementsByTagName("source");
|
|
70 for (int i = 0; i < routing.getLength(); i++) {
|
|
71 Element src = (Element) routing.item(i);
|
|
72 Integer srcId = Integer.parseInt(src.getAttribute("id"));
|
|
73 Routing r = np.nodes.get(TupleId.getTupleIdFromId(srcId));
|
|
74 NodeList dest = src.getElementsByTagName("dest");
|
|
75 for (int j = 0; j < dest.getLength(); j++) {
|
|
76 Element dst = (Element) dest.item(j);
|
|
77 r.route.add(Integer.parseInt(dst.getAttribute("id")));
|
|
78 }
|
|
79 }
|
|
80
|
|
81 }
|
|
82 switch (tid) {
|
|
83 case MANAGE:
|
|
84 np.connected = true;
|
|
85 break;
|
|
86 case DEBUG:
|
|
87 np.debugConnected = true;
|
|
88 break;
|
122
|
89 default:
|
|
90 break;
|
92
|
91 }
|
|
92 if (np.connected && np.debugConnected) {
|
|
93 np.sendLocalHostName();
|
|
94 Debug.print("Send local host name");
|
|
95 np.connected = np.debugConnected = false;
|
|
96 }
|
|
97 }
|
|
98 private void makeConnections(NodeList connections) {
|
|
99 for (int i = 0; i < connections.getLength(); i++) {
|
|
100 Element connection = (Element)connections.item(i);
|
|
101 Element host = (Element)connection.getElementsByTagName("host").item(0);
|
|
102 Element port = (Element)connection.getElementsByTagName("port").item(0);
|
|
103 Element t = (Element)connection.getElementsByTagName("tid").item(0);
|
|
104 int srcId = Integer.parseInt(connection.getAttribute("id"));
|
|
105 String dstHostName = host.getTextContent();
|
|
106 int dstPort = Integer.parseInt(port.getAttribute("id"));
|
|
107 int dstId = Integer.parseInt(t.getAttribute("id"));
|
|
108 try {
|
|
109 PSXLindaImpl linda = (PSXLindaImpl) np.ml.open(dstHostName, dstPort);
|
|
110 Routing r = new Routing(linda, dstId);
|
|
111 np.nodes.put(TupleId.getTupleIdFromId(srcId), r);
|
|
112 np.ml.in(srcId, new RoutingCallback(TupleId.getTupleIdFromId(srcId), r, np, tp, dp));
|
|
113 } catch (IOException e) {
|
|
114 e.printStackTrace();
|
|
115 }
|
|
116 }
|
|
117 }
|
|
118
|
|
119 }
|