/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2010, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.server.manager;

import org.jboss.as.model.Standalone;

/**
 * A client proxy for communication between a ServerManager and a managed server.
 * 
 * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
 */
public final class Server {
//    private static final ThreadFactory FACTORY = Executors.defaultThreadFactory();
    
    private final ServerCommunicationHandler communicationHandler;
    
//    public Server(final InputStream errorStream, final InputStream inputStream, final OutputStream outputStream) {
//        this.processManagerSlave = null;
//        final Thread thread = FACTORY.newThread(new Runnable() {
//            public void run() {
//                try {
//                    final InputStreamReader reader = new InputStreamReader(errorStream);
//                    final BufferedReader bufferedReader = new BufferedReader(reader);
//                    String line;
//                    try {
//                        while ((line = bufferedReader.readLine()) != null) {
//                            System.err.println("Server reported error: " + line.trim());
//                        }
//                    } catch (IOException e) {
//                        // todo log it
//                    }
//                } finally {
//                    try {
//                        errorStream.close();
//                    } catch (IOException e) {
//                        // todo log
//                    }
//                }
//            }
//        });
//        thread.start();
//        FACTORY.newThread(new Runnable() {
//            public void run() {
//                String cmd;
//                try {
//                    while ((cmd = readCommand(inputStream)) != null) {
//                        System.out.println("Got msg: " + cmd);
//                    }
//                } catch (IOException e) {
//                    // todo log it
//                } finally {
//                    try {
//                        inputStream.close();
//                    } catch (IOException e) {
//                        // todo log
//                    }
//                }
//            }
//
//        });
//    }
    
    public Server(ServerCommunicationHandler communicationHandler) {
        if (communicationHandler == null) {
            throw new IllegalArgumentException("communicationHandler is null");
        }
        this.communicationHandler = communicationHandler;
    }

    public void start(Standalone serverConf) {
        // FIXME implement start
        throw new UnsupportedOperationException("implement me");        
    }

    public void stop() {
        // TODO anything to do here?   
    }

//    private static String readCommand(final InputStream in) throws IOException {
//        final StringBuilder b = new StringBuilder();
//        int c;
//        while ((c = in.read()) != -1 && c != '\n') {
//            b.append((char) (c & 0xff));
//        }
//        if (b.length() == 0 && c == -1) {
//            return null;
//        }
//        return b.toString();
//    }
}

@@ -1055,16 +1055,246 @@
 nager;%0A%0A
+import java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.ObjectOutputStream;%0Aimport java.util.Arrays;%0Aimport java.util.zip.Adler32;%0Aimport java.util.zip.CheckedOutputStream;%0Aimport java.util.zip.Checksum;%0A%0A
 import o
@@ -3781,115 +3781,583 @@
 nf) 
-%7B%0A        // FIXME implement start%0A        throw new UnsupportedOperationException(%22implement me%22
+throws IOException %7B%0A        %0A        ServerCommand command = new ServerCommand(%22START%22, new Object%5B%5D %7BserverConf%7D, new Class%3C?%3E%5B%5D%7BStandalone.class%7D);%0A        sendCommand(command);%0A    %7D%0A%0A    public void stop() throws IOException %7B%0A        %0A        sendCommand(new ServerCommand(%22STOP%22)); %0A    %7D%0A    %0A    private void sendCommand(ServerCommand command) throws IOException %7B%0A        %0A        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);%0A        Checksum chksum = new Adler32();%0A        CheckedOutputStream cos = new CheckedOutputStream(baos, chksum
 );
+%0A
         
 %0A   
@@ -4356,77 +4356,480 @@
     
-%0A    %7D%0A%0A    public void stop() %7B%0A        // TODO anything to do here?
+ObjectOutputStream oos = null;%0A        try %7B%0A            oos = new ObjectOutputStream(cos);%0A            oos.writeObject(command);%0A            oos.close();%0A            oos = null;%0A            communicationHandler.sendMessage(baos.toByteArray(), chksum.getValue());%0A        %7D%0A        finally %7B%0A            if (oos != null) %7B%0A                try %7B%0A                    oos.close();%0A                %7D%0A                catch (IOException ignored) %7B%7D%0A            %7D%0A        %7D%0A     
    %0A

Use binary communication to send Standalone to servers