summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/dnbd3/DNBD3Server.java
blob: 1ac2e730b547535110b2288349d2fb37d41cfd08 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                          
                                     


                            
                       
                           

                      



                                              


                          







                                                   
                                                               
                                             

                                                           


                     
                                                                 
                                             

                                                         


                     




                                                               
                                             

                                                                   
                            
                     

     
                                                     
                                             

                                                         
                                                                            
                     
                                

     
                                                       
                                             

                                                         
                                                                            
                     
                                 



                                                                                                                        
                                                                                 
                                                 






























                                                                                       
     

 
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.List;

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 {

    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, DNBD3Exception {
        Socket sock = new Socket(HOST, PORT);
        sendHeader(sock, new DNBD3Header(CMDRELOAD, 0, 0));
        receiveHeader(sock);
        sock.close();
    }

    public void doShutdown() throws IOException, DNBD3Exception {
        Socket sock = new Socket(HOST, PORT);
        sendHeader(sock, new DNBD3Header(CMDSTOP, 0, 0));
        receiveHeader(sock);
        sock.close();
    }

    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();
    }

    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();
    }

    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();
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    private void sendHeader(Socket sock, DNBD3Header header) throws IOException {
        OutputStream os = sock.getOutputStream();
        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...");
        }
    }

}