summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/dnbd3/DNBD3Server.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/dnbd3/DNBD3Server.java')
-rw-r--r--src/main/java/org/openslx/dnbd3/DNBD3Server.java96
1 files changed, 18 insertions, 78 deletions
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();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////