// Path: PrintClient/app/src/main/java/com/tinkerlog/printclient/model/Command.java
// public class Command {
// 
//     protected String request;
//     protected String response;
//     public ResponseHandler handler;
// 
//     public Command(String request, ResponseHandler handler) {
//         this.request = request;
//         this.handler = handler;
//     }
// 
//     public void setResponse(String response) {
//         this.response = response;
//     }
// 
//     public String getResponse() {
//         return response;
//     }
// 
//     public String getRequest() {
//         return request;
//     }
// 
//     public byte[] getPayload() {
//         return request.getBytes();
//     }
// }
// 
// Path: PrintClient/app/src/main/java/com/tinkerlog/printclient/util/ResponseHandler.java
// public class ResponseHandler extends Handler {
// 
//     private static final String TAG = "ResponseHandler";
// 
//     public static final int OK = 1;
//     public static final int ERROR = 2;
// 
//     public ResponseHandler() {
//         super();
//     }
// 
//     public ResponseHandler(Callback callback) {
//         super(callback);
//     }
// 
//     public ResponseHandler(Looper looper) {
//         super(looper);
//     }
// 
//     public ResponseHandler(Looper looper, Callback callback) {
//         super(looper, callback);
//     }
// 
//     @Override
//     public void handleMessage(Message msg) {
//         switch (msg.what) {
//             case OK:
//                 handleSuccess((Command)msg.obj);
//                 break;
//             case ERROR:
//                 handleFailed((Command)msg.obj);
//                 break;
//         }
//     }
// 
//     public void handleFailed(Command cmd) {
//         Log.e(TAG, "failed: " + cmd.getRequest());
//     }
// 
//     public void handleSuccess(Command cmd) {
//         Log.d(TAG, "success: " + cmd.getResponse());
//     }
// 
// }


import android.os.Message;
import android.util.Log;
import com.tinkerlog.printclient.model.Command;
import com.tinkerlog.printclient.util.ResponseHandler;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
            socket.setSoTimeout(MAX_SOCKET_TIMEOUT);
            socket.connect(new InetSocketAddress(SERVER_IP, SERVER_PORT), 1000);
            out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
            in = new BufferedInputStream(socket.getInputStream());

            Log.d(TAG, "socket timeout: " + socket.getSoTimeout());
            while (running) {

                Command cmd = outQueue.poll(2000, TimeUnit.MILLISECONDS);
                if (!running) {
                    break;
                }

                if (cmd == null) {
                    Log.d(TAG, "ping ...");
                    cmd = new Command(MSG_PING, null);
                    // continue;
                }
                else {
                    Log.d(TAG, "sending: " + cmd.getRequest());
                }
                out.write(cmd.getRequest());
                out.println();
                out.flush();
                Thread.sleep(1);

                int bytesRead = readBuffer(in, inBuffer);
                if (bytesRead == -1) {
                    Log.d(TAG, "no response, socket closed!");
                    if (cmd.handler != null) {
// Path: PrintClient/app/src/main/java/com/tinkerlog/printclient/model/Command.java
// public class Command {
// 
//     protected String request;
//     protected String response;
//     public ResponseHandler handler;
// 
//     public Command(String request, ResponseHandler handler) {
//         this.request = request;
//         this.handler = handler;
//     }
// 
//     public void setResponse(String response) {
//         this.response = response;
//     }
// 
//     public String getResponse() {
//         return response;
//     }
// 
//     public String getRequest() {
//         return request;
//     }
// 
//     public byte[] getPayload() {
//         return request.getBytes();
//     }
// }
// 
// Path: PrintClient/app/src/main/java/com/tinkerlog/printclient/util/ResponseHandler.java
// public class ResponseHandler extends Handler {
// 
//     private static final String TAG = "ResponseHandler";
// 
//     public static final int OK = 1;
//     public static final int ERROR = 2;
// 
//     public ResponseHandler() {
//         super();
//     }
// 
//     public ResponseHandler(Callback callback) {
//         super(callback);
//     }
// 
//     public ResponseHandler(Looper looper) {
//         super(looper);
//     }
// 
//     public ResponseHandler(Looper looper, Callback callback) {
//         super(looper, callback);
//     }
// 
//     @Override
//     public void handleMessage(Message msg) {
//         switch (msg.what) {
//             case OK:
//                 handleSuccess((Command)msg.obj);
//                 break;
//             case ERROR:
//                 handleFailed((Command)msg.obj);
//                 break;
//         }
//     }
// 
//     public void handleFailed(Command cmd) {
//         Log.e(TAG, "failed: " + cmd.getRequest());
//     }
// 
//     public void handleSuccess(Command cmd) {
//         Log.d(TAG, "success: " + cmd.getResponse());
//     }
// 
// }


// Path: PrintClient/app/src/main/java/com/tinkerlog/printclient/network/ComThread.java
import android.os.Message;
import android.util.Log;
import com.tinkerlog.printclient.model.Command;
import com.tinkerlog.printclient.util.ResponseHandler;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

            socket.setSoTimeout(MAX_SOCKET_TIMEOUT);
            socket.connect(new InetSocketAddress(SERVER_IP, SERVER_PORT), 1000);
            out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
            in = new BufferedInputStream(socket.getInputStream());

            Log.d(TAG, "socket timeout: " + socket.getSoTimeout());
            while (running) {

                Command cmd = outQueue.poll(2000, TimeUnit.MILLISECONDS);
                if (!running) {
                    break;
                }

                if (cmd == null) {
                    Log.d(TAG, "ping ...");
                    cmd = new Command(MSG_PING, null);
                    // continue;
                }
                else {
                    Log.d(TAG, "sending: " + cmd.getRequest());
                }
                out.write(cmd.getRequest());
                out.println();
                out.flush();
                Thread.sleep(1);

                int bytesRead = readBuffer(in, inBuffer);
                if (bytesRead == -1) {
                    Log.d(TAG, "no response, socket closed!");
                    if (cmd.handler != null) {
                        cmd.handler.sendMessage(Message.obtain(cmd.handler, ResponseHandler.ERROR, cmd));