diff options
-rw-r--r-- | pom.xml | 31 | ||||
-rw-r--r-- | src/main/java/org/openslx/dnbd3/DNBD3Client.java | 29 | ||||
-rw-r--r-- | src/main/java/org/openslx/dnbd3/DNBD3Image.java | 91 | ||||
-rw-r--r-- | src/main/java/org/openslx/dnbd3/DNBD3Server.java | 96 | ||||
-rw-r--r-- | src/main/java/org/openslx/dnbd3/Test.java (renamed from src/main/java/org/openslx/Test.java) | 35 | ||||
-rw-r--r-- | src/main/java/org/openslx/dnbd3/xml/Client.java | 33 | ||||
-rw-r--r-- | src/main/java/org/openslx/dnbd3/xml/Image.java | 111 | ||||
-rw-r--r-- | src/main/java/org/openslx/dnbd3/xml/Info.java | 34 |
8 files changed, 215 insertions, 245 deletions
@@ -23,34 +23,9 @@ </dependency> <!-- xml --> <dependency> - <groupId>org.jdom</groupId> - <artifactId>jdom</artifactId> - <version>2.0.2</version> - </dependency> - <dependency> - <groupId>jaxen</groupId> - <artifactId>jaxen</artifactId> - <version>1.1.3</version> - <exclusions> - <exclusion> - <artifactId>maven-cobertura-plugin</artifactId> - <groupId>maven-plugins</groupId> - </exclusion> - <exclusion> - <artifactId>maven-findbugs-plugin</artifactId> - <groupId>maven-plugins</groupId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>xalan</groupId> - <artifactId>xalan</artifactId> - <version>2.7.1</version> - </dependency> - <dependency> - <groupId>xerces</groupId> - <artifactId>xercesImpl</artifactId> - <version>2.10.0</version> + <groupId>org.simpleframework</groupId> + <artifactId>simple-xml</artifactId> + <version>2.6.6</version> </dependency> </dependencies> diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Client.java b/src/main/java/org/openslx/dnbd3/DNBD3Client.java deleted file mode 100644 index 6608bc8..0000000 --- a/src/main/java/org/openslx/dnbd3/DNBD3Client.java +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index 98d8fc8..0000000 --- a/src/main/java/org/openslx/dnbd3/DNBD3Image.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.openslx.dnbd3; - -public class DNBD3Image { - - private String group; - private String atime; - private int vid; - private int rid; - private String path; - private String servers; - private String cache; - - public DNBD3Image(String group, String atime, int vid, int rid, String path, String servers, String cache) { - this.group = group; - this.atime = atime; - this.vid = vid; - this.rid = rid; - this.path = path; - this.servers = servers; - this.cache = cache; - } - - public DNBD3Image(String group, int vid, int rid, String path, String servers, String cache) { - this(group, "01.01.70 01:00:00", vid, rid, path, servers, cache); - } - - public DNBD3Image(String group, int vid, int rid, String path, String servers) { - this(group, "01.01.70 01:00:00", vid, rid, path, servers, ""); - } - - public DNBD3Image(String group, int vid, String path, String servers) { - this(group, "01.01.70 01:00:00", vid, 0, path, servers, ""); - } - - public String getGroup() { - return group; - } - - public void setGroup(String group) { - this.group = group; - } - - public String getAtime() { - return atime; - } - - public void setAtime(String atime) { - this.atime = atime; - } - - public int getVid() { - return vid; - } - - public void setVid(int vid) { - this.vid = vid; - } - - public int getRid() { - return rid; - } - - public void setRid(int rid) { - this.rid = rid; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public String getServers() { - return servers; - } - - public void setServers(String servers) { - this.servers = servers; - } - - public String getCache() { - return cache; - } - - public void setCache(String cache) { - this.cache = cache; - } - -} diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Server.java b/src/main/java/org/openslx/dnbd3/DNBD3Server.java index 36646b5..1ac2e73 100644 --- a/src/main/java/org/openslx/dnbd3/DNBD3Server.java +++ b/src/main/java/org/openslx/dnbd3/DNBD3Server.java @@ -1,23 +1,17 @@ package org.openslx.dnbd3; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.nio.ByteBuffer; -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; +import org.openslx.dnbd3.xml.Client; +import org.openslx.dnbd3.xml.Image; +import org.openslx.dnbd3.xml.Info; +import org.simpleframework.xml.core.Persister; public class DNBD3Server { @@ -43,88 +37,34 @@ public class DNBD3Server { sock.close(); } - public void addImage(DNBD3Image image) throws IOException, DNBD3Exception { - - 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); + public void addImage(Image image) throws Exception { + Info info = new Info(); + info.getImages().add(image); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + new Persister().write(info, os); Socket sock = new Socket(HOST, PORT); - OutputStream os = sock.getOutputStream(); - XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); - sendHeader(sock, new DNBD3Header(CMDCONFIG, outputter.outputString(doc).length(), 0)); - outputter.output(doc, os); + sendHeader(sock, new DNBD3Header(CMDCONFIG, os.size(), 0)); + sock.getOutputStream().write(os.toByteArray()); receiveHeader(sock); sock.close(); } - public List<DNBD3Image> getImages() throws IOException, DNBD3Exception { - List<DNBD3Image> images = new ArrayList<DNBD3Image>(); + public List<Image> getImages() throws Exception { Socket sock = new Socket(HOST, PORT); sendHeader(sock, new DNBD3Header(CMDINFO, 0, 0)); receiveHeader(sock); - - 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<Element> 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(); - } - + Info info = new Persister().read(Info.class, sock.getInputStream()); sock.close(); - return images; + return info.getImages(); } - public List<DNBD3Client> getClients() throws IOException, DNBD3Exception { - List<DNBD3Client> clients = new ArrayList<DNBD3Client>(); + public List<Client> getClients() throws Exception { Socket sock = new Socket(HOST, PORT); sendHeader(sock, new DNBD3Header(CMDINFO, 0, 0)); receiveHeader(sock); - - try { - String ip, image; - InputStream is = sock.getInputStream(); - SAXBuilder builder = new SAXBuilder(); - Document document = (Document) builder.build(is); - XPathFactory xpfac = XPathFactory.instance(); - XPathExpression<Element> 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(); - } - + Info info = new Persister().read(Info.class, sock.getInputStream()); sock.close(); - return clients; + return info.getClients(); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/main/java/org/openslx/Test.java b/src/main/java/org/openslx/dnbd3/Test.java index 111e9f2..c94348c 100644 --- a/src/main/java/org/openslx/Test.java +++ b/src/main/java/org/openslx/dnbd3/Test.java @@ -1,38 +1,37 @@ -package org.openslx; +package org.openslx.dnbd3; -import java.io.IOException; import java.util.List; -import org.openslx.dnbd3.DNBD3Client; -import org.openslx.dnbd3.DNBD3Exception; -import org.openslx.dnbd3.DNBD3Image; -import org.openslx.dnbd3.DNBD3Server; +import org.openslx.dnbd3.xml.Client; +import org.openslx.dnbd3.xml.Image; public class Test { - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws Exception { try { DNBD3Server server = new DNBD3Server(); - String group = "Test"; + String group = "Test 11.11"; int vid = 10; int rid = 0; String path = "/home/jjl/Data/ISOs/Ubuntu/ubuntu-10.04.3-desktop-i386.iso"; - String servers = "132.230.4.29;132.230.4.220;10.1.1.1;10.1.1.2"; + String servers = "132.230.4.29;132.230.4.220;10.1.1.1"; String cache = "/tmp/image.iso.cache"; - DNBD3Image image = new DNBD3Image(group, vid, rid, path, servers, cache); + Image image = new Image(group, vid, rid, path, servers, cache); server.addImage(image); - + + server.doReload(); + System.out.println("Exported images (group, atime, vid, rid, path, servers, cache):"); System.out.println("==============================================================="); - List<DNBD3Image> images = server.getImages(); - for (DNBD3Image i : images) { + List<Image> images = server.getImages(); + for (Image i : images) { System.out.print(i.getGroup() + "\t"); System.out.print(i.getAtime() + "\t"); System.out.print(i.getVid() + "\t"); System.out.print(i.getRid() + "\t"); - System.out.print(i.getPath() + "\t"); + System.out.print(i.getFile() + "\t"); System.out.print(i.getServers() + "\t"); System.out.print(i.getCache()); System.out.println(); @@ -43,19 +42,17 @@ public class Test { System.out.println("Connected clients (ip, file)"); System.out.println("============================"); - List<DNBD3Client> clients = server.getClients(); - for (DNBD3Client client : clients) { + List<Client> clients = server.getClients(); + for (Client client : clients) { System.out.print(client.getIp() + "\t"); - System.out.print(client.getImage()); + System.out.print(client.getFile()); System.out.println(); } System.out.println(); System.out.println("Number clients: " + clients.size()); System.out.println(); - // server.doShutdown(); - // server.doReload(); } catch (DNBD3Exception e) { System.out.println(e); diff --git a/src/main/java/org/openslx/dnbd3/xml/Client.java b/src/main/java/org/openslx/dnbd3/xml/Client.java new file mode 100644 index 0000000..8cc7879 --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/xml/Client.java @@ -0,0 +1,33 @@ +package org.openslx.dnbd3.xml; + +import org.simpleframework.xml.Attribute; + +public class Client { + + @Attribute + private String ip; + @Attribute + private String file; + + public Client(@Attribute(name="ip") String ip, @Attribute(name="file") String file) { + this.ip = ip; + this.file = file; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + +} diff --git a/src/main/java/org/openslx/dnbd3/xml/Image.java b/src/main/java/org/openslx/dnbd3/xml/Image.java new file mode 100644 index 0000000..84ad9d9 --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/xml/Image.java @@ -0,0 +1,111 @@ +package org.openslx.dnbd3.xml; + +import org.simpleframework.xml.Attribute; + +public class Image { + + @Attribute + private String group; + @Attribute + private String atime; + @Attribute + private int vid; + @Attribute + private int rid; + @Attribute + private String file; + @Attribute + private String servers; + @Attribute + private String cache; + + public Image(@Attribute(name = "group") String group, @Attribute(name = "atime") String atime, + @Attribute(name = "vid") int vid, @Attribute(name = "rid") int rid, @Attribute(name = "file") String file, + @Attribute(name = "servers") String servers, @Attribute(name = "cache") String cache) { + + this.group = group; + this.atime = atime; + this.vid = vid; + this.rid = rid; + this.file = file; + this.servers = servers; + this.cache = cache; + } + + public Image(@Attribute(name = "group") String group, @Attribute(name = "vid") int vid, + @Attribute(name = "rid") int rid, @Attribute(name = "file") String file, + @Attribute(name = "servers") String servers, @Attribute(name = "cache") String cache) { + + this(group, "01.01.70 01:00:00", vid, rid, file, servers, cache); + } + + public Image(@Attribute(name = "group") String group, @Attribute(name = "vid") int vid, + @Attribute(name = "rid") int rid, @Attribute(name = "file") String file, + @Attribute(name = "servers") String servers) { + + this(group, "01.01.70 01:00:00", vid, rid, file, servers, ""); + } + + public Image(@Attribute(name = "group") String group, @Attribute(name = "vid") int vid, + @Attribute(name = "file") String file, @Attribute(name = "servers") String servers) { + + this(group, "01.01.70 01:00:00", vid, 0, file, servers, ""); + } + + public String getGroup() { + return group; + } + + public void setGroup(String group) { + this.group = group; + } + + public String getAtime() { + return atime; + } + + public void setAtime(String atime) { + this.atime = atime; + } + + public int getVid() { + return vid; + } + + public void setVid(int vid) { + this.vid = vid; + } + + public int getRid() { + return rid; + } + + public void setRid(int rid) { + this.rid = rid; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + + public String getServers() { + return servers; + } + + public void setServers(String servers) { + this.servers = servers; + } + + public String getCache() { + return cache; + } + + public void setCache(String cache) { + this.cache = cache; + } + +} diff --git a/src/main/java/org/openslx/dnbd3/xml/Info.java b/src/main/java/org/openslx/dnbd3/xml/Info.java new file mode 100644 index 0000000..7b817ac --- /dev/null +++ b/src/main/java/org/openslx/dnbd3/xml/Info.java @@ -0,0 +1,34 @@ +package org.openslx.dnbd3.xml; + +import java.util.ArrayList; +import java.util.List; + +import org.simpleframework.xml.ElementList; +import org.simpleframework.xml.Root; + +@Root +public class Info { + + @ElementList + private List<Image> images = new ArrayList<Image>(); + + @ElementList + private List<Client> clients = new ArrayList<Client>(); + + public List<Image> getImages() { + return images; + } + + public void setImages(List<Image> images) { + this.images = images; + } + + public List<Client> getClients() { + return clients; + } + + public void setClients(List<Client> clients) { + this.clients = clients; + } + +} |