package com.dglogik.mobile;

import com.dglogik.api.server.AbstractTunnelClient;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.WebSocket;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import ext.javax.servlet.ServletContext;

public class AndroidTunnelClient extends AbstractTunnelClient {
    private WebSocket socket;
    private ServletContext servlet;

    public AndroidTunnelClient(ServletContext cx) {
        super(cx);

        servlet = cx;
    }

    public AndroidTunnelClient setUri(String uri) {
        return this;
    }

    @Override
    public void stop() {
        super.stop();
        DGMobileContext.log("Android Tunnel Client Stopping");
        if (socket != null) socket.close();
    }

    @Override
    protected void connect() throws Exception {
        Future<WebSocket> future = AsyncHttpClient.getDefaultInstance().websocket(servlet.getInitParameter("tunnel"), null, new AsyncHttpClient.WebSocketConnectCallback() {
            @Override
            public void onCompleted(Exception e, WebSocket webSocket) {
                if (e != null) {
                    e.printStackTrace();
                    try { Thread.sleep(1000); } catch (Exception ignored) {}
                }
            }
        });

        socket = future.get();
        init();
        sendPing();
    }

    private void init() {
        DGMobileContext.log("Android Tunnel Client Connected");

        socket.setStringCallback(new WebSocket.StringCallback() {
            @Override
            public void onStringAvailable(String s) {
                DGMobileContext.log("Android Tunnel Client Received: " + s);
                try {
                    StringReader reader = new StringReader(s);
                    processRequest(reader);
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        socket.setDataCallback(new DataCallback() {
            @Override
            public void onDataAvailable(DataEmitter emitter, ByteBufferList byteBufferList) {
                DGMobileContext.log("Android Tunnel Client Got Data");
                byteBufferList.recycle();
            }
        });

        socket.setPongCallback(new WebSocket.PongCallback() {
            @Override
            public void onPongReceived(String s) {
                DGMobileContext.log("Android Tunnel Client Received a Pong");
                pingOk();
            }
        });

        socket.setClosedCallback(new CompletedCallback() {
            @Override
            public void onCompleted(Exception e) {
                //Thread.dumpStack();
                if (e != null) {
                    e.printStackTrace();
                }
                DGMobileContext.log("Android Tunnel Client Closed");
                disconnected();
                if (socket.isOpen()) {
                    socket.close();
                }
                socket = null;
            }
        });
    }

    @Override
    protected Writer responseBegin() throws IOException {
        return new StringWriter();
    }

    @Override
    protected void responseEnd(Writer out) throws IOException {
        String str = out.toString();
        socket.send(str);
        DGMobileContext.log("Android Tunnel Client Sent: " + str);
    }

    @Override
    protected void sendPing() throws Exception {
        DGMobileContext.log("Android Tunnel Client Sending Ping");
        socket.ping("b00b1e5555");
    }
}

@@ -943,32 +943,34 @@
 stop();%0A        
+//
 DGMobileContext.
@@ -1004,24 +1004,24 @@
 Stopping%22);%0A
-
         if (
@@ -1684,32 +1684,34 @@
 nit() %7B%0A        
+//
 DGMobileContext.
@@ -1901,32 +1901,34 @@
                 
+//
 DGMobileContext.
@@ -2441,32 +2441,34 @@
                 
+//
 DGMobileContext.
@@ -2718,32 +2718,34 @@
                 
+//
 DGMobileContext.
@@ -3113,32 +3113,34 @@
                 
+//
 DGMobileContext.
@@ -3620,32 +3620,34 @@
 d(str);%0A        
+//
 DGMobileContext.
@@ -3759,24 +3759,24 @@
 ption %7B%0A
-
         
 DGMobile
@@ -3767,16 +3767,18 @@
         
+//
 DGMobile

Comment out the stuff that spams the console.