summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/openslx/dnbd3/DNBD3Header.java7
-rw-r--r--src/main/java/org/openslx/dnbd3/DNBD3Server.java89
-rw-r--r--src/main/java/org/openslx/dnbd3/Test.java10
3 files changed, 68 insertions, 38 deletions
diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Header.java b/src/main/java/org/openslx/dnbd3/DNBD3Header.java
index 75c1358..ec22406 100644
--- a/src/main/java/org/openslx/dnbd3/DNBD3Header.java
+++ b/src/main/java/org/openslx/dnbd3/DNBD3Header.java
@@ -4,9 +4,12 @@ import java.nio.ByteBuffer;
public class DNBD3Header {
- public static final int ERROR_IMAGE_NOT_FOUND = 1;
+ public static final int ERROR_FILE_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_IMAGE_NOT_FOUND = 4;
+ public static final int ERROR_RID = 5;
+ public static final int ERROR_IMAGE_IN_USE = 6;
public static final int ERROR_UNKNOWN = 10;
private int cmd;
@@ -29,7 +32,7 @@ public class DNBD3Header {
public DNBD3Header() {
}
-
+
public byte[] toByteArray() {
ByteBuffer bb = ByteBuffer.allocate(12);
bb.putInt(cmd);
diff --git a/src/main/java/org/openslx/dnbd3/DNBD3Server.java b/src/main/java/org/openslx/dnbd3/DNBD3Server.java
index 1ac2e73..4830010 100644
--- a/src/main/java/org/openslx/dnbd3/DNBD3Server.java
+++ b/src/main/java/org/openslx/dnbd3/DNBD3Server.java
@@ -18,63 +18,76 @@ public class DNBD3Server {
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 int CMDADDIMG = 3;
+ private static final int CMDDELIMG = 4;
private static final String HOST = "127.0.0.1";
private static final int PORT = 5004;
public void doReload() throws IOException, DNBD3Exception {
- Socket sock = new Socket(HOST, PORT);
- sendHeader(sock, new DNBD3Header(CMDRELOAD, 0, 0));
- receiveHeader(sock);
- sock.close();
+ sndCommand(CMDRELOAD);
}
public void doShutdown() throws IOException, DNBD3Exception {
- Socket sock = new Socket(HOST, PORT);
- sendHeader(sock, new DNBD3Header(CMDSTOP, 0, 0));
- receiveHeader(sock);
- sock.close();
+ sndCommand(CMDSTOP);
}
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);
- sendHeader(sock, new DNBD3Header(CMDCONFIG, os.size(), 0));
- sock.getOutputStream().write(os.toByteArray());
- receiveHeader(sock);
- sock.close();
+ sndData(CMDADDIMG, info);
+ doReload();
+ }
+
+ public void delImage(Image image) throws Exception {
+ Info info = new Info();
+ info.getImages().add(image);
+ sndData(CMDDELIMG, info);
+ doReload();
}
public List<Image> getImages() throws Exception {
- Socket sock = new Socket(HOST, PORT);
- sendHeader(sock, new DNBD3Header(CMDINFO, 0, 0));
- receiveHeader(sock);
- Info info = new Persister().read(Info.class, sock.getInputStream());
- sock.close();
- return info.getImages();
+ return rcvData().getImages();
}
public List<Client> getClients() throws Exception {
- Socket sock = new Socket(HOST, PORT);
- sendHeader(sock, new DNBD3Header(CMDINFO, 0, 0));
- receiveHeader(sock);
- Info info = new Persister().read(Info.class, sock.getInputStream());
- sock.close();
- return info.getClients();
+ return rcvData().getClients();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private void sendHeader(Socket sock, DNBD3Header header) throws IOException {
+ private void sndCommand(int cmd) throws IOException, DNBD3Exception {
+ Socket sock = new Socket(HOST, PORT);
+ sndHeader(sock, new DNBD3Header(cmd, 0, 0));
+ rcvHeader(sock);
+ sock.close();
+ }
+
+ private void sndData(int cmd, Info info) throws Exception {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ new Persister().write(info, os);
+ Socket sock = new Socket(HOST, PORT);
+ sndHeader(sock, new DNBD3Header(cmd, os.size(), 0));
+ sock.getOutputStream().write(os.toByteArray());
+ rcvHeader(sock);
+ sock.close();
+ }
+
+ private Info rcvData() throws Exception {
+ Socket sock = new Socket(HOST, PORT);
+ sndHeader(sock, new DNBD3Header(CMDINFO, 0, 0));
+ rcvHeader(sock);
+ Info info = new Persister().read(Info.class, sock.getInputStream());
+ sock.close();
+ return info;
+ }
+
+ private void sndHeader(Socket sock, DNBD3Header header) throws IOException {
OutputStream os = sock.getOutputStream();
os.write(header.toByteArray());
}
- private DNBD3Header receiveHeader(Socket sock) throws IOException, DNBD3Exception {
+ private DNBD3Header rcvHeader(Socket sock) throws IOException, DNBD3Exception {
DNBD3Header header = new DNBD3Header();
InputStream is = sock.getInputStream();
byte[] bytes = ByteBuffer.allocate(12).array();
@@ -86,8 +99,8 @@ public class DNBD3Server {
case 0:
return header;
- case DNBD3Header.ERROR_IMAGE_NOT_FOUND:
- throw new DNBD3Exception(DNBD3Header.ERROR_IMAGE_NOT_FOUND,
+ case DNBD3Header.ERROR_FILE_NOT_FOUND:
+ throw new DNBD3Exception(DNBD3Header.ERROR_FILE_NOT_FOUND,
"Image file does not exist or is not readable");
case DNBD3Header.ERROR_IMAGE_ALREADY_EXISTS:
@@ -97,6 +110,18 @@ public class DNBD3Server {
case DNBD3Header.ERROR_CONFIG_FILE_PERMISSIONS:
throw new DNBD3Exception(DNBD3Header.ERROR_CONFIG_FILE_PERMISSIONS,
"Config file is not writable");
+
+ case DNBD3Header.ERROR_IMAGE_NOT_FOUND:
+ throw new DNBD3Exception(DNBD3Header.ERROR_IMAGE_NOT_FOUND,
+ "Image does not exist");
+
+ case DNBD3Header.ERROR_RID:
+ throw new DNBD3Exception(DNBD3Header.ERROR_RID,
+ "Delete with rid=0 is not allowed");
+
+ case DNBD3Header.ERROR_IMAGE_IN_USE:
+ throw new DNBD3Exception(DNBD3Header.ERROR_IMAGE_IN_USE,
+ "Image is in use");
default:
throw new DNBD3Exception(DNBD3Header.ERROR_UNKNOWN,
diff --git a/src/main/java/org/openslx/dnbd3/Test.java b/src/main/java/org/openslx/dnbd3/Test.java
index c94348c..35ec2a7 100644
--- a/src/main/java/org/openslx/dnbd3/Test.java
+++ b/src/main/java/org/openslx/dnbd3/Test.java
@@ -11,18 +11,17 @@ public class Test {
try {
DNBD3Server server = new DNBD3Server();
-
+
String group = "Test 11.11";
int vid = 10;
- int rid = 0;
+ int rid = 1;
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";
String cache = "/tmp/image.iso.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<Image> images = server.getImages();
@@ -52,6 +51,9 @@ public class Test {
System.out.println("Number clients: " + clients.size());
System.out.println();
+ server.delImage(new Image("", vid, rid, "", ""));
+
+ // server.doReload();
// server.doShutdown();
} catch (DNBD3Exception e) {