Mercurial > hg > FederatedLinda
view src/fdl/test/debug2/AcceptXMLCallback.java @ 92:ea4ee892baf5
commit
author | kazz <kazz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 22 Apr 2010 16:13:03 +0900 |
parents | |
children |
line wrap: on
line source
package fdl.test.debug2; import java.io.IOException; import java.io.StringReader; import java.nio.ByteBuffer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import fdl.PSXCallback; import fdl.PSXLindaImpl; class AcceptXMLCallback implements PSXCallback { TupleId tid; NodeProperty np; TreeProperty tp; DebugProperty dp; private DocumentBuilderFactory dbFactory = null; private DocumentBuilder docBuilder = null; public AcceptXMLCallback(TupleId tid, NodeProperty np) { this.tid = tid; this.np = np; dbFactory = DocumentBuilderFactory.newInstance(); try { docBuilder = dbFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } public void callback(ByteBuffer reply) { String xml = new String(reply.array()); Debug.print(xml); parseXML(xml); np.ml.in(tid.id, this); } protected void parseXML(String xml) { Document doc = null; try { doc = docBuilder.parse(new InputSource(new StringReader(xml))); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Element root = doc.getDocumentElement(); if(root.getTagName().equals("connections")) { np.nodeId = Integer.parseInt(root.getAttribute("id")); if (np.nodeId == 0) { np.ml.in(TupleId.START.id, new StartCallback(np, tp = new TreeProperty(np))); np.ml.in(TupleId.DEBUGSTART.id, new DebugStartCallback(np, dp = new DebugProperty())); } NodeList connections = root.getElementsByTagName("connection"); makeConnections(connections); } else if (root.getTagName().equals("routing")) { Debug.print("Routing xml received!"); NodeList routing = root.getElementsByTagName("source"); for (int i = 0; i < routing.getLength(); i++) { Element src = (Element) routing.item(i); Integer srcId = Integer.parseInt(src.getAttribute("id")); Routing r = np.nodes.get(TupleId.getTupleIdFromId(srcId)); NodeList dest = src.getElementsByTagName("dest"); for (int j = 0; j < dest.getLength(); j++) { Element dst = (Element) dest.item(j); r.route.add(Integer.parseInt(dst.getAttribute("id"))); } } } switch (tid) { case MANAGE: np.connected = true; break; case DEBUG: np.debugConnected = true; break; } if (np.connected && np.debugConnected) { np.sendLocalHostName(); Debug.print("Send local host name"); np.connected = np.debugConnected = false; } } private void makeConnections(NodeList connections) { for (int i = 0; i < connections.getLength(); i++) { Element connection = (Element)connections.item(i); Element host = (Element)connection.getElementsByTagName("host").item(0); Element port = (Element)connection.getElementsByTagName("port").item(0); Element t = (Element)connection.getElementsByTagName("tid").item(0); int srcId = Integer.parseInt(connection.getAttribute("id")); String dstHostName = host.getTextContent(); int dstPort = Integer.parseInt(port.getAttribute("id")); int dstId = Integer.parseInt(t.getAttribute("id")); try { PSXLindaImpl linda = (PSXLindaImpl) np.ml.open(dstHostName, dstPort); Routing r = new Routing(linda, dstId); np.nodes.put(TupleId.getTupleIdFromId(srcId), r); np.ml.in(srcId, new RoutingCallback(TupleId.getTupleIdFromId(srcId), r, np, tp, dp)); } catch (IOException e) { e.printStackTrace(); } } } }