package com.hazelcast.remotecontroller;

import org.apache.logging.log4j.LogManager;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.ServerContext;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServerEventHandler;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransport;

public class Main {

    public static org.apache.logging.log4j.Logger LOG = LogManager.getLogger(Main.class);

    public static int PORT = 9701;

    public static RemoteControllerHandler handler;

    public static RemoteController.Processor processor;

    public static void main(String[] args) {
        try {
            handler = new RemoteControllerHandler();
            processor = new RemoteController.Processor(handler);

            Runnable simple = () -> {
                try {
                    TNonblockingServerSocket socket = new TNonblockingServerSocket(PORT);

                    TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(socket);
                    tnbArgs.processor(processor);

                    tnbArgs.transportFactory(new TFramedTransport.Factory(Integer.MAX_VALUE));
                    tnbArgs.protocolFactory(new TBinaryProtocol.Factory());

                    TServer server = new TNonblockingServer(tnbArgs);
                    server.setServerEventHandler(new ServerEventHandler(handler));

                    LOG.info("Starting Remote Controller Server on port:" + PORT);

                    server.serve();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };

            new Thread(simple).start();
        } catch (Exception x) {
            x.printStackTrace();
        }
    }

    private static class ServerEventHandler implements TServerEventHandler {

        private RemoteControllerHandler handler;

        public ServerEventHandler(RemoteControllerHandler handler) {

            this.handler = handler;
        }

        public void preServe() {
            LOG.info("TServerEventHandler.preServe - server starts accepting connections");
        }

        public ServerContext createContext(TProtocol input, TProtocol output) {
            LOG.info("TServerEventHandler.createContext - new client context created");
            return null;
        }

        public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
            try {
                this.handler.clean();
            } catch (TException e) {
                LOG.error(e.getMessage());
            }
            LOG.info("TServerEventHandler.deleteContext client");
        }

        public void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport) {
        }

    }

}

@@ -455,59 +455,8 @@
 er;%0A
-import org.apache.thrift.server.TThreadPoolServer;%0A
 impo
@@ -651,22 +651,16 @@
 nsport.T
-Server
 Transpor
@@ -707,16 +707,25 @@
 ransport
+Exception
 ;%0A%0Apubli
@@ -736,24 +736,104 @@
 ass Main %7B%0A%0A
+    private static final String USE_SIMPLE_SERVER_ARG = %22--use-simple-server%22;%0A%0A
     public s
@@ -1319,78 +1319,23 @@
    T
-NonblockingServerSocket socket = new TNonblockingServerSocket(PORT)
+Server server
 ;%0A
-%0A
     
@@ -1354,78 +1354,42 @@
     
-TNonblockingServer.Args tnbArgs = new TNonblocking
+if (shouldUseSimple
 Server
-.Args(socket);
+(args)) %7B
 %0A   
@@ -1409,24 +1409,38 @@
     
-tnbArgs.processo
+    server = createSimpleServe
 r(pr
@@ -1445,25 +1445,24 @@
 processor);%0A
-%0A
             
@@ -1473,160 +1473,86 @@
     
-tnbArgs.transportFactory(new TFramedTransport.Factory(Integer.MAX_VALUE));%0A                    tnbArgs.protocolFactory(new TBinaryProtocol.Factory()
+%7D else %7B%0A                        server = createNonblockingServer(processor
 );%0A
-%0A
     
@@ -1571,57 +1571,10 @@
     
-TServer server = new TNonblockingServer(tnbArgs);
+%7D%0A
 %0A   
@@ -2007,24 +2007,1032 @@
    %7D%0A    %7D%0A%0A
+    private static boolean shouldUseSimpleServer(String%5B%5D args) %7B%0A        return args != null && args.length %3E 0 && USE_SIMPLE_SERVER_ARG.equals(args%5B0%5D);%0A    %7D%0A%0A    private static TServer createNonblockingServer(RemoteController.Processor processor) throws TTransportException %7B%0A        TNonblockingServerSocket socket = new TNonblockingServerSocket(PORT);%0A%0A        TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(socket);%0A        tnbArgs.processor(processor);%0A%0A        tnbArgs.transportFactory(new TFramedTransport.Factory(Integer.MAX_VALUE));%0A        tnbArgs.protocolFactory(new TBinaryProtocol.Factory());%0A%0A        return new TNonblockingServer(tnbArgs);%0A    %7D%0A%0A    private static TServer createSimpleServer(RemoteController.Processor processor) throws TTransportException %7B%0A        TServerSocket socket = new TServerSocket(PORT);%0A%0A        TSimpleServer.Args tssArgs = new TSimpleServer.Args(socket);%0A        tssArgs.processor(processor);%0A%0A        return new TSimpleServer(tssArgs);%0A    %7D%0A%0A
     private 

Add ability to use simple server with the buffered transport (#23)