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/Test.java | 88 +++++----- src/main/java/org/openslx/dnbd3/DNBD3Client.java | 36 ++--- src/main/java/org/openslx/dnbd3/DNBD3Image.java | 124 +++++++++----- src/main/java/org/openslx/dnbd3/DNBD3Server.java | 195 +++++++++++++---------- 4 files changed, 262 insertions(+), 181 deletions(-) diff --git a/src/main/java/org/openslx/Test.java b/src/main/java/org/openslx/Test.java index f473df5..56afc7f 100644 --- a/src/main/java/org/openslx/Test.java +++ b/src/main/java/org/openslx/Test.java @@ -8,42 +8,54 @@ import org.openslx.dnbd3.DNBD3Server; public class Test { - public static void main(String[] args) { - - try { - DNBD3Server server = new DNBD3Server(); - - // server.doReload(); - // server.doShutdown(); - - 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(); - } - } + public static void main(String[] args) { + + try { + DNBD3Server server = new DNBD3Server(); + + String group = "Test"; + int vid = 10; + int rid = 1; + String path = "/image.iso"; + 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); + server.addImage(image); + + System.out.println("Exported images (group, atime, vid, rid, path, servers, cache):"); + System.out.println("==============================================================="); + List images = server.getImages(); + for (DNBD3Image 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.getServers() + "\t"); + System.out.print(i.getCache()); + 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(); + + // server.doReload(); + // server.doShutdown(); + + } 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 index 1f0ced5..6608bc8 100644 --- a/src/main/java/org/openslx/dnbd3/DNBD3Client.java +++ b/src/main/java/org/openslx/dnbd3/DNBD3Client.java @@ -2,28 +2,28 @@ package org.openslx.dnbd3; public class DNBD3Client { - private String ip; - private String image; + private String ip; + private String image; - public DNBD3Client(String ip, String image) { - this.ip = ip; - this.image = image; - } + public DNBD3Client(String ip, String image) { + this.ip = ip; + this.image = image; + } - public String getIp() { - return ip; - } + public String getIp() { + return ip; + } - public void setIp(String ip) { - this.ip = ip; - } + public void setIp(String ip) { + this.ip = ip; + } - public String getImage() { - return image; - } + public String getImage() { + return image; + } - public void setImage(String image) { - this.image = 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 index e1b3429..5047f2a 100644 --- a/src/main/java/org/openslx/dnbd3/DNBD3Image.java +++ b/src/main/java/org/openslx/dnbd3/DNBD3Image.java @@ -2,48 +2,86 @@ 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; - } + 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 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 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