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/Test.java | 46 +++++++++ src/main/java/org/openslx/dnbd3/DNBD3Client.java | 29 ++++++ src/main/java/org/openslx/dnbd3/DNBD3Image.java | 49 ++++++++++ src/main/java/org/openslx/dnbd3/DNBD3Server.java | 116 +++++++++++++++++++++++ src/test/java/org/openslx/AppTest.java | 38 ++++++++ 5 files changed, 278 insertions(+) create mode 100644 src/main/java/org/openslx/Test.java create mode 100644 src/main/java/org/openslx/dnbd3/DNBD3Client.java create mode 100644 src/main/java/org/openslx/dnbd3/DNBD3Image.java create mode 100644 src/main/java/org/openslx/dnbd3/DNBD3Server.java create mode 100644 src/test/java/org/openslx/AppTest.java (limited to 'src') diff --git a/src/main/java/org/openslx/Test.java b/src/main/java/org/openslx/Test.java new file mode 100644 index 0000000..4e44552 --- /dev/null +++ b/src/main/java/org/openslx/Test.java @@ -0,0 +1,46 @@ +package org.openslx; + +import java.util.List; + +import org.openslx.dnbd3.DNBD3Client; +import org.openslx.dnbd3.DNBD3Image; +import org.openslx.dnbd3.DNBD3Server; + +public class Test { + + public static void main(String[] args) { + + try { + DNBD3Server server = new DNBD3Server(); + + System.out.println("Exported images (atime, vid, rid, file):"); + System.out.println("========================================"); + List images = server.getImages(); + for (DNBD3Image image : images) { + System.out.print(image.getAtime() + "\t"); + System.out.print(image.getVid() + "\t"); + System.out.print(image.getRid() + "\t"); + System.out.print(image.getPath()); + System.out.println(); + } + System.out.println(); + System.out.println("Number images: " + images.size()); + System.out.println(); + + System.out.println("Connected clients (ip, file)"); + System.out.println("============================"); + List clients = server.getClients(); + for (DNBD3Client client : clients) { + System.out.print(client.getIp() + "\t"); + System.out.print(client.getImage()); + System.out.println(); + } + System.out.println(); + System.out.println("Number clients: " + clients.size()); + System.out.println(); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Client.java b/src/main/java/org/openslx/dnbd3/DNBD3Client.java new file mode 100644 index 0000000..1f0ced5 --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/DNBD3Client.java @@ -0,0 +1,29 @@ +package org.openslx.dnbd3; + +public class DNBD3Client { + + private String ip; + private String image; + + public DNBD3Client(String ip, String image) { + this.ip = ip; + this.image = image; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getImage() { + return image; + } + + public void setImage(String image) { + this.image = image; + } + +} diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Image.java b/src/main/java/org/openslx/dnbd3/DNBD3Image.java new file mode 100644 index 0000000..e1b3429 --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/DNBD3Image.java @@ -0,0 +1,49 @@ +package org.openslx.dnbd3; + +public class DNBD3Image { + + private String atime; + private String vid; + private String rid; + private String path; + + public DNBD3Image(String atime, String vid, String rid, String path) { + this.atime = atime; + this.vid = vid; + this.rid = rid; + this.path = path; + } + + public String getAtime() { + return atime; + } + + public void setAtime(String atime) { + this.atime = atime; + } + + public String getVid() { + return vid; + } + + public void setVid(String vid) { + this.vid = vid; + } + + public String getRid() { + return rid; + } + + public void setRid(String rid) { + this.rid = rid; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + +} 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(); + } + +} diff --git a/src/test/java/org/openslx/AppTest.java b/src/test/java/org/openslx/AppTest.java new file mode 100644 index 0000000..ff6e16e --- /dev/null +++ b/src/test/java/org/openslx/AppTest.java @@ -0,0 +1,38 @@ +package org.openslx; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} -- cgit v1.2.3-55-g7522