From a86d51668249038fc09d436dbb3290ce882e7861 Mon Sep 17 00:00:00 2001 From: Johann Latocha Date: Mon, 30 Jul 2012 19:06:23 +0200 Subject: Add IPCHeader --- src/main/java/org/openslx/dnbd3/DNBD3Server.java | 68 +++++++++++++++++------- 1 file changed, 50 insertions(+), 18 deletions(-) (limited to 'src/main/java/org/openslx/dnbd3/DNBD3Server.java') 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 getImages() throws IOException { + public List getImages() throws IOException, DNBD3Exception { List images = new ArrayList(); 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 getClients() throws IOException { + public List getClients() throws IOException, DNBD3Exception { List clients = new ArrayList(); 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..."); + } } } -- cgit v1.2.3-55-g7522