summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohann Latocha2012-07-30 19:06:23 +0200
committerJohann Latocha2012-07-30 19:06:23 +0200
commita86d51668249038fc09d436dbb3290ce882e7861 (patch)
treea9fa8c02b630afb3dfcba6316cabb7408a8a8b22
parentAdd new constructor (diff)
downloaddnbd3-mgr-a86d51668249038fc09d436dbb3290ce882e7861.tar.gz
dnbd3-mgr-a86d51668249038fc09d436dbb3290ce882e7861.tar.xz
dnbd3-mgr-a86d51668249038fc09d436dbb3290ce882e7861.zip
Add IPCHeader
-rw-r--r--src/main/java/org/openslx/Test.java21
-rw-r--r--src/main/java/org/openslx/dnbd3/DNBD3Exception.java18
-rw-r--r--src/main/java/org/openslx/dnbd3/DNBD3Header.java72
-rw-r--r--src/main/java/org/openslx/dnbd3/DNBD3Server.java68
4 files changed, 153 insertions, 26 deletions
diff --git a/src/main/java/org/openslx/Test.java b/src/main/java/org/openslx/Test.java
index 56afc7f..111e9f2 100644
--- a/src/main/java/org/openslx/Test.java
+++ b/src/main/java/org/openslx/Test.java
@@ -1,27 +1,29 @@
package org.openslx;
+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;
public class Test {
- public static void main(String[] args) {
+ public static void main(String[] args) throws IOException {
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";
+ 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 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<DNBD3Image> images = server.getImages();
@@ -51,11 +53,14 @@ public class Test {
System.out.println("Number clients: " + clients.size());
System.out.println();
- // server.doReload();
+
// server.doShutdown();
+ // server.doReload();
+
+ } catch (DNBD3Exception e) {
+ System.out.println(e);
- } catch (Exception e) {
- e.printStackTrace();
}
}
+
}
diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Exception.java b/src/main/java/org/openslx/dnbd3/DNBD3Exception.java
new file mode 100644
index 0000000..1188067
--- /dev/null
+++ b/src/main/java/org/openslx/dnbd3/DNBD3Exception.java
@@ -0,0 +1,18 @@
+package org.openslx.dnbd3;
+
+public class DNBD3Exception extends Exception {
+
+ private int error;
+
+ private static final long serialVersionUID = -4538453729966276434L;
+
+ public DNBD3Exception(int error, String message) {
+ super(message);
+ this.error = error;
+ }
+
+ public int getError() {
+ return error;
+ }
+
+}
diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Header.java b/src/main/java/org/openslx/dnbd3/DNBD3Header.java
new file mode 100644
index 0000000..75c1358
--- /dev/null
+++ b/src/main/java/org/openslx/dnbd3/DNBD3Header.java
@@ -0,0 +1,72 @@
+package org.openslx.dnbd3;
+
+import java.nio.ByteBuffer;
+
+public class DNBD3Header {
+
+ public static final int ERROR_IMAGE_NOT_FOUND = 1;
+ public static final int ERROR_IMAGE_ALREADY_EXISTS = 2;
+ public static final int ERROR_CONFIG_FILE_PERMISSIONS = 3;
+ public static final int ERROR_UNKNOWN = 10;
+
+ private int cmd;
+ private int size;
+ private int error;
+
+ public DNBD3Header(int cmd, int size, int error) {
+ this.cmd = cmd;
+ this.size = size;
+ this.error = error;
+ }
+
+ public DNBD3Header(byte[] bytes) {
+ ByteBuffer bb = ByteBuffer.wrap(bytes);
+ this.cmd = bb.getInt();
+ this.size = bb.getInt();
+ this.error = bb.getInt();
+ }
+
+ public DNBD3Header() {
+
+ }
+
+ public byte[] toByteArray() {
+ ByteBuffer bb = ByteBuffer.allocate(12);
+ bb.putInt(cmd);
+ bb.putInt(size);
+ bb.putInt(error);
+ return bb.array();
+ }
+
+ public void fromByteArray(byte[] bytes) {
+ ByteBuffer bb = ByteBuffer.wrap(bytes);
+ this.cmd = bb.getInt();
+ this.size = bb.getInt();
+ this.error = bb.getInt();
+ }
+
+ public int getCmd() {
+ return cmd;
+ }
+
+ public void setCmd(int cmd) {
+ this.cmd = cmd;
+ }
+
+ public int getSize() {
+ return size;
+ }
+
+ public void setSize(int size) {
+ this.size = size;
+ }
+
+ public int getError() {
+ return error;
+ }
+
+ public void setError(int error) {
+ this.error = error;
+ }
+
+}
diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Server.java b/src/main/java/org/openslx/dnbd3/DNBD3Server.java
index 2b871f0..36646b5 100644
--- a/src/main/java/org/openslx/dnbd3/DNBD3Server.java
+++ b/src/main/java/org/openslx/dnbd3/DNBD3Server.java
@@ -1,10 +1,10 @@
package org.openslx.dnbd3;
-import java.io.DataOutputStream;
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;
@@ -29,22 +29,21 @@ public class DNBD3Server {
private static final String HOST = "127.0.0.1";
private static final int PORT = 5004;
- public void doReload() throws IOException {
+ public void doReload() throws IOException, DNBD3Exception {
Socket sock = new Socket(HOST, PORT);
- sendCommand(sock, CMDRELOAD);
+ sendHeader(sock, new DNBD3Header(CMDRELOAD, 0, 0));
+ receiveHeader(sock);
sock.close();
}
- public void doShutdown() throws IOException {
+ public void doShutdown() throws IOException, DNBD3Exception {
Socket sock = new Socket(HOST, PORT);
- sendCommand(sock, CMDSTOP);
+ sendHeader(sock, new DNBD3Header(CMDSTOP, 0, 0));
+ receiveHeader(sock);
sock.close();
}
- public void addImage(DNBD3Image image) throws IOException {
- Socket sock = new Socket(HOST, PORT);
- sendCommand(sock, CMDCONFIG);
- OutputStream os = sock.getOutputStream();
+ public void addImage(DNBD3Image image) throws IOException, DNBD3Exception {
Element rootNode = new Element("dnbd3-server");
Element imageNode = new Element("image");
@@ -57,17 +56,20 @@ public class DNBD3Server {
imageNode.setAttribute(new Attribute("cache_file", image.getCache()));
rootNode.addContent(imageNode);
Document doc = new Document(rootNode);
+ 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);
-
+ receiveHeader(sock);
sock.close();
-
}
- public List<DNBD3Image> getImages() throws IOException {
+ public List<DNBD3Image> getImages() throws IOException, DNBD3Exception {
List<DNBD3Image> images = new ArrayList<DNBD3Image>();
Socket sock = new Socket(HOST, PORT);
- sendCommand(sock, CMDINFO);
+ sendHeader(sock, new DNBD3Header(CMDINFO, 0, 0));
+ receiveHeader(sock);
try {
String group, atime, path, servers, cache;
@@ -97,10 +99,11 @@ public class DNBD3Server {
return images;
}
- public List<DNBD3Client> getClients() throws IOException {
+ public List<DNBD3Client> getClients() throws IOException, DNBD3Exception {
List<DNBD3Client> clients = new ArrayList<DNBD3Client>();
Socket sock = new Socket(HOST, PORT);
- sendCommand(sock, CMDINFO);
+ sendHeader(sock, new DNBD3Header(CMDINFO, 0, 0));
+ receiveHeader(sock);
try {
String ip, image;
@@ -126,10 +129,39 @@ public class DNBD3Server {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private void sendCommand(Socket sock, int cmd) throws IOException {
+ private void sendHeader(Socket sock, DNBD3Header header) throws IOException {
OutputStream os = sock.getOutputStream();
- DataOutputStream dos = new DataOutputStream(os);
- dos.writeInt(cmd);
+ os.write(header.toByteArray());
+ }
+
+ private DNBD3Header receiveHeader(Socket sock) throws IOException, DNBD3Exception {
+ DNBD3Header header = new DNBD3Header();
+ InputStream is = sock.getInputStream();
+ byte[] bytes = ByteBuffer.allocate(12).array();
+ is.read(bytes, 0, 12);
+ header.fromByteArray(bytes);
+
+ switch (header.getError()) {
+
+ case 0:
+ return header;
+
+ case DNBD3Header.ERROR_IMAGE_NOT_FOUND:
+ throw new DNBD3Exception(DNBD3Header.ERROR_IMAGE_NOT_FOUND,
+ "Image file does not exist or is not readable");
+
+ case DNBD3Header.ERROR_IMAGE_ALREADY_EXISTS:
+ throw new DNBD3Exception(DNBD3Header.ERROR_IMAGE_ALREADY_EXISTS,
+ "Image with given vid and rid already exists");
+
+ case DNBD3Header.ERROR_CONFIG_FILE_PERMISSIONS:
+ throw new DNBD3Exception(DNBD3Header.ERROR_CONFIG_FILE_PERMISSIONS,
+ "Config file is not writable");
+
+ default:
+ throw new DNBD3Exception(DNBD3Header.ERROR_UNKNOWN,
+ "Unknown error, something went horribly wrong...");
+ }
}
}