package uk.me.graphe.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import uk.me.graphe.server.ot.GraphProcessor;

public class GraphemeServer extends Thread {

    public static final int GAPHEME_PORT = 6689;
    private static GraphemeServer sInstance = null;

    public static GraphemeServer getInstance() {
        if (sInstance == null) sInstance = new GraphemeServer();
        return sInstance;
    }

    private ClientMessageHandler mClientMessageHandler;
    
    private ClientMessageSender mClientMessageSender;    

    private boolean mRunning = false;

    private ServerSocketChannel mServerSocketChannel;
    
    private ClientManager mClientManager = ClientManager.getInstance();

    private GraphemeServer() {
        try {
            // sets up a server socket listening on the grapheme port
            mServerSocketChannel = ServerSocketChannel.open();
            mServerSocketChannel.socket().bind(new InetSocketAddress(GAPHEME_PORT));
            // let's make sure for certain we're up and running
            assert mServerSocketChannel.isOpen();

            // start a new client message handler: it's going to accept incoming
            // data from clients
            mClientMessageHandler = ClientMessageHandler.getInstance();
            mClientMessageSender = ClientMessageSender.getInstance();
            GraphProcessor.getInstance().start();
            
            mClientMessageHandler.start();
            mClientMessageSender.start();
        } catch (IOException e) {
            throw new Error("couldn't start server", e);
        }

    }

    public boolean isShutDown() {
        return !mRunning;
    }

    @Override
    public void run() {
        while (mRunning) {
            try {
                // accept incoming connections and let the client message
                // handler know about them
                SocketChannel clientSock = mServerSocketChannel.accept();
                Client client = new Client(clientSock);
                mClientManager.addClient(client);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    public void shutDown() {
        mRunning = false;
    }

    @Override
    public synchronized void start() {
        mRunning = true;
        super.start();
    }

}

@@ -571,20 +571,16 @@
 andler;%0A
-    
 %0A    pri
@@ -625,20 +625,16 @@
 eSender;
-    
 %0A%0A    pr
@@ -720,20 +720,16 @@
 hannel;%0A
-    
 %0A    pri
@@ -1019,16 +1019,37 @@
 ().bind(
+%0A                    
 new Inet
@@ -1499,28 +1499,16 @@
 tart();%0A
-            
 %0A       
@@ -2304,16 +2304,174 @@
  false;%0A
+        mClientMessageHandler.shutDown();%0A        mClientMessageSender.shutDown();%0A        GraphProcessor.getInstance().shutDown();%0A        this.interrupt();%0A
     %7D%0A%0A 
@@ -2518,24 +2518,58 @@
 d start() %7B%0A
+        if (!this.mRunning) %7B%0A    
         mRun
@@ -2589,16 +2589,20 @@
         
+ 
+   
 super.st
@@ -2604,21 +2604,548 @@
 er.start();%0A
+        %7D%0A    %7D%0A%0A    public void waitTornDown() %7B%0A        while (mClientMessageHandler.isAlive()%0A                %7C%7C mClientMessageSender.isAlive()%0A                %7C%7C GraphProcessor.getInstance().isAlive() %7C%7C this.isAlive()) %7B%0A            System.err.println(%22cmh: %22 + mClientMessageHandler.isAlive());%0A            System.err.println(%22cms: %22 + mClientMessageSender.isAlive());%0A            System.err.println(%22gp: %22 + GraphProcessor.getInstance().isAlive());%0A            System.err.println(%22this: %22 + this.isAlive());%0A%0A        %7D%0A%0A
     %7D%0A%0A%7D%0A

add shutdown to grapheme server