From 8b19992144120cb1dbbb1cf97aa2951dd05c8ea4 Mon Sep 17 00:00:00 2001 From: Johann Latocha Date: Thu, 26 Jul 2012 20:49:12 +0200 Subject: Add image --- src/main/java/org/openslx/dnbd3/DNBD3Server.java | 195 +++++++++++++---------- 1 file changed, 113 insertions(+), 82 deletions(-) (limited to 'src/main/java/org/openslx/dnbd3/DNBD3Server.java') diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Server.java b/src/main/java/org/openslx/dnbd3/DNBD3Server.java index 5cbaa27..2b871f0 100644 --- a/src/main/java/org/openslx/dnbd3/DNBD3Server.java +++ b/src/main/java/org/openslx/dnbd3/DNBD3Server.java @@ -8,97 +8,128 @@ import java.net.Socket; import java.util.ArrayList; import java.util.List; +import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.filter.Filters; import org.jdom2.input.SAXBuilder; +import org.jdom2.output.Format; +import org.jdom2.output.XMLOutputter; import org.jdom2.xpath.XPathExpression; import org.jdom2.xpath.XPathFactory; public class DNBD3Server { - private static final int CMDSTOP = 0; - private static final int CMDRELOAD = 1; - private static final int CMDINFO = 2; - - private static final String HOST = "127.0.0.1"; - private static final int PORT = 5004; - - public void doReload() throws IOException { - Socket sock = new Socket(HOST, PORT); - sendCommand(sock, CMDRELOAD); - sock.close(); - } - - public void doShutdown() throws IOException { - Socket sock = new Socket(HOST, PORT); - sendCommand(sock, CMDSTOP); - sock.close(); - } - - public List getImages() throws IOException { - List images = new ArrayList(); - Socket sock = new Socket(HOST, PORT); - sendCommand(sock, CMDINFO); - - try { - String atime, vid, rid, file; - InputStream is = sock.getInputStream(); - SAXBuilder builder = new SAXBuilder(); - Document document = (Document) builder.build(is); - XPathFactory xpfac = XPathFactory.instance(); - XPathExpression xp; - xp = xpfac.compile("//dnbd3-server/images/image", Filters.element()); - for (Element e : xp.evaluate(document)) { - atime = e.getAttributeValue("atime"); - vid = e.getAttributeValue("vid"); - rid = e.getAttributeValue("rid"); - file = e.getAttributeValue("file"); - images.add(new DNBD3Image(atime, vid, rid, file)); - } - - } catch (JDOMException ex) { - ex.printStackTrace(); - } - - sock.close(); - return images; - } - - public List getClients() throws IOException { - List clients = new ArrayList(); - Socket sock = new Socket(HOST, PORT); - sendCommand(sock, CMDINFO); - - try { - String ip, image; - InputStream is = sock.getInputStream(); - SAXBuilder builder = new SAXBuilder(); - Document document = (Document) builder.build(is); - XPathFactory xpfac = XPathFactory.instance(); - XPathExpression xp; - xp = xpfac.compile("//dnbd3-server/clients/client", Filters.element()); - for (Element e : xp.evaluate(document)) { - ip = e.getAttributeValue("ip"); - image = e.getAttributeValue("file"); - clients.add(new DNBD3Client(ip, image)); - } - - } catch (JDOMException ex) { - ex.printStackTrace(); - } - - sock.close(); - return clients; - } - -//////////////////////////////////////////////////////////////////////////////// - - private void sendCommand(Socket sock, int cmd) throws IOException { - OutputStream os = sock.getOutputStream(); - DataOutputStream dos = new DataOutputStream(os); - dos.writeInt(cmd); - } + private static final int CMDSTOP = 0; + private static final int CMDRELOAD = 1; + private static final int CMDINFO = 2; + private static final int CMDCONFIG = 3; + + private static final String HOST = "127.0.0.1"; + private static final int PORT = 5004; + + public void doReload() throws IOException { + Socket sock = new Socket(HOST, PORT); + sendCommand(sock, CMDRELOAD); + sock.close(); + } + + public void doShutdown() throws IOException { + Socket sock = new Socket(HOST, PORT); + sendCommand(sock, CMDSTOP); + sock.close(); + } + + public void addImage(DNBD3Image image) throws IOException { + Socket sock = new Socket(HOST, PORT); + sendCommand(sock, CMDCONFIG); + OutputStream os = sock.getOutputStream(); + + Element rootNode = new Element("dnbd3-server"); + Element imageNode = new Element("image"); + imageNode.setAttribute(new Attribute("group", image.getGroup())); + imageNode.setAttribute(new Attribute("atime", image.getAtime())); + imageNode.setAttribute(new Attribute("vid", String.valueOf(image.getVid()))); + imageNode.setAttribute(new Attribute("rid", String.valueOf(image.getRid()))); + imageNode.setAttribute(new Attribute("file", image.getPath())); + imageNode.setAttribute(new Attribute("servers", image.getServers())); + imageNode.setAttribute(new Attribute("cache_file", image.getCache())); + rootNode.addContent(imageNode); + Document doc = new Document(rootNode); + XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); + outputter.output(doc, os); + + sock.close(); + + } + + public List getImages() throws IOException { + List images = new ArrayList(); + Socket sock = new Socket(HOST, PORT); + sendCommand(sock, CMDINFO); + + try { + String group, atime, path, servers, cache; + int vid, rid; + InputStream is = sock.getInputStream(); + SAXBuilder builder = new SAXBuilder(); + Document document = (Document) builder.build(is); + XPathFactory xpfac = XPathFactory.instance(); + XPathExpression xp; + xp = xpfac.compile("//dnbd3-server/images/image", Filters.element()); + for (Element e : xp.evaluate(document)) { + group = e.getAttributeValue("group"); + atime = e.getAttributeValue("atime"); + vid = Integer.parseInt(e.getAttributeValue("vid")); + rid = Integer.parseInt(e.getAttributeValue("rid")); + path = e.getAttributeValue("file"); + servers = e.getAttributeValue("servers"); + cache = e.getAttributeValue("cache_file"); + images.add(new DNBD3Image(group, atime, vid, rid, path, servers, cache)); + } + + } catch (JDOMException ex) { + ex.printStackTrace(); + } + + sock.close(); + return images; + } + + public List getClients() throws IOException { + List clients = new ArrayList(); + Socket sock = new Socket(HOST, PORT); + sendCommand(sock, CMDINFO); + + try { + String ip, image; + InputStream is = sock.getInputStream(); + SAXBuilder builder = new SAXBuilder(); + Document document = (Document) builder.build(is); + XPathFactory xpfac = XPathFactory.instance(); + XPathExpression xp; + xp = xpfac.compile("//dnbd3-server/clients/client", Filters.element()); + for (Element e : xp.evaluate(document)) { + ip = e.getAttributeValue("ip"); + image = e.getAttributeValue("file"); + clients.add(new DNBD3Client(ip, image)); + } + + } catch (JDOMException ex) { + ex.printStackTrace(); + } + + sock.close(); + return clients; + } + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + private void sendCommand(Socket sock, int cmd) throws IOException { + OutputStream os = sock.getOutputStream(); + DataOutputStream dos = new DataOutputStream(os); + dos.writeInt(cmd); + } } -- cgit v1.2.3-55-g7522