/*
 *  Copyright 2013 JMX Daemon contributors
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.hibnet.jmxdaemon;

import java.net.SocketAddress;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

public class JmxDaemonClient {

    private ClientBootstrap bootstrap;

    private Channel ch;

    private JmxDaemonClientHandler handler;

    private static class JmxDaemonClientHandler extends SimpleChannelUpstreamHandler {

        private LinkedBlockingQueue<String> responses = new LinkedBlockingQueue<>();

        @Override
        public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
            String response = (String) e.getMessage();
            responses.add(response);
            super.messageReceived(ctx, e);
        }
    }

    public JmxDaemonClient(SocketAddress serverAdd) {
        Executor bossPool = Executors.newCachedThreadPool();
        Executor workerPool = Executors.newCachedThreadPool();
        bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(bossPool, workerPool));
        handler = new JmxDaemonClientHandler();
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            public ChannelPipeline getPipeline() throws Exception {
                return Channels.pipeline(new StringDecoder(), new StringEncoder(), handler);
            };
        });
        ChannelFuture cf = bootstrap.connect(serverAdd);
        cf.awaitUninterruptibly();
        ch = cf.getChannel();
    }

    public String send(String cmd) {
        ch.write(cmd);
        try {
            return handler.responses.take();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public void close() {
        ch.close();
        bootstrap.releaseExternalResources();
    }
}

@@ -1508,33 +1508,8 @@
 p;%0A%0A
-    private Channel ch;%0A%0A
     
@@ -1549,16 +1549,54 @@
 ndler;%0A%0A
+    private SocketAddress serverAdd;%0A%0A
     priv
@@ -2079,24 +2079,60 @@
 erverAdd) %7B%0A
+        this.serverAdd = serverAdd;%0A
         Exec
@@ -2645,16 +2645,60 @@
     %7D);%0A
+    %7D%0A%0A    public String send(String cmd) %7B%0A
         
@@ -2789,16 +2789,24 @@
         
+Channel 
 ch = cf.
@@ -2823,52 +2823,8 @@
 ();%0A
-    %7D%0A%0A    public String send(String cmd) %7B%0A
     
@@ -2839,16 +2839,23 @@
 rite(cmd
+ + %22%5Cn%22
 );%0A     
@@ -3041,28 +3041,8 @@
 ) %7B%0A
-        ch.close();%0A
     

fix tests