From a37d70fd44fe8eb5519a729f40880268964db00f Mon Sep 17 00:00:00 2001 From: Johann Latocha Date: Wed, 25 Jul 2012 15:39:59 +0200 Subject: Initial commit --- src/main/java/org/openslx/dnbd3/DNBD3Server.java | 116 +++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/main/java/org/openslx/dnbd3/DNBD3Server.java (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 new file mode 100644 index 0000000..ce575dd --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/DNBD3Server.java @@ -0,0 +1,116 @@ +package org.openslx.dnbd3; + +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + +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.xpath.XPathExpression; +import org.jdom2.xpath.XPathFactory; +import org.newsclub.net.unix.AFUNIXSocket; +import org.newsclub.net.unix.AFUNIXSocketAddress; + +public class DNBD3Server { + + private final String UNIXSOCK = "/run/dnbd3-server.sock"; + + private static final int CMDSTOP = 0; + private static final int CMDRELOAD = 1; + private static final int CMDINFO = 2; + + public void doReload() throws IOException { + AFUNIXSocket sock = connect(); + sendCommand(sock, CMDRELOAD); + sock.close(); + } + + public void doShutdown() throws IOException { + AFUNIXSocket sock = connect(); + sendCommand(sock, CMDSTOP); + sock.close(); + } + + public List getImages() throws IOException { + List images = new ArrayList(); + AFUNIXSocket sock = connect(); + 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)); + } + is.close(); + + } catch (JDOMException ex) { + ex.printStackTrace(); + } + + sock.close(); + return images; + } + + public List getClients() throws IOException { + List clients = new ArrayList(); + AFUNIXSocket sock = connect(); + 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)); + } + is.close(); + + } catch (JDOMException ex) { + ex.printStackTrace(); + } + + sock.close(); + return clients; + } + +//////////////////////////////////////////////////////////////////////////////// + + private AFUNIXSocket connect() throws IOException { + AFUNIXSocket sock = null; + File socketFile = new File(UNIXSOCK); + sock = AFUNIXSocket.newInstance(); + sock.connect(new AFUNIXSocketAddress(socketFile)); + return sock; + } + + private void sendCommand(AFUNIXSocket sock, int cmd) throws IOException { + OutputStream os = sock.getOutputStream(); + DataOutputStream dos = new DataOutputStream(os); + dos.writeInt(cmd); + os.close(); + } + +} -- cgit v1.2.3-55-g7522