package com.example.client;

import com.example.ExampleService;
import com.example.UploadInfo;
import com.example.UploadMessage;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by silver on 14. 6. 2.
 */
public class ExampleClient {

    public static void main(String[] args) throws TException, IOException, URISyntaxException {
        if (args.length != 2) {
            throw new TException("invalid arguments");
        }

        String address = "localhost";
        int port = 10004;
        if (System.getProperty("address") != null) {
            address = System.getProperty("address");
        }
        if (Integer.getInteger("port") != null) {
            port = Integer.getInteger("port");
        }

        TTransport transport = new TSocket(address, port);
        TProtocol protocol = new TBinaryProtocol(transport);
        ExampleService.Client client = new ExampleService.Client(protocol);
        transport.open();

        if ("echo".equals(args[0])) {
            echo(client, args[1]);
        } else if ("upload".equals(args[0])) {
            upload(client, args[1]);
        }

        transport.close();
    }

    private static void echo(ExampleService.Client client, String msg) throws TException {
        String rev = client.echo(msg);
        System.out.println(rev);
    }

    private static void upload(ExampleService.Client client, String path) throws URISyntaxException, TException, IOException {
        File file = new File(path);
        UploadInfo uploadInfo = new UploadInfo();
        uploadInfo.msg = UploadMessage.BEGIN_UPLOAD;
        uploadInfo.fileName = file.getName();
        uploadInfo.length = file.length();

        boolean success = true;
        if (client.upload(uploadInfo)) {
            FileInputStream fis = new FileInputStream(file);
            FileChannel fileChannel = fis.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024 * 10);

            uploadInfo.msg = UploadMessage.PROGRESS_UPLOAD;
            uploadInfo.data = buffer;
            while ((uploadInfo.length = fileChannel.read(buffer)) > 0) {
                buffer.flip();
                if (!client.upload(uploadInfo)) {
                    success = false;
                    break;
                }
                buffer.clear();
            }
            fis.close();

            uploadInfo.msg = UploadMessage.END_UPLOAD;
            uploadInfo.data = null;
            if (!client.upload(uploadInfo)) {
                success = false;
            }
        }

        String alertMsg = success ? "Success to upload." : "Failure to upload.";
        System.out.println(alertMsg);
    }
}

@@ -400,16 +400,54 @@
 Stream;%0A
+import java.io.FileNotFoundException;%0A
 import j
@@ -567,16 +567,42 @@
 Channel;
+%0Aimport java.util.Scanner;
 %0A%0A/**%0A *
@@ -767,106 +767,8 @@
 n %7B%0A
-        if (args.length != 2) %7B%0A            throw new TException(%22invalid arguments%22);%0A        %7D%0A%0A
     
@@ -1247,24 +1247,25 @@
 tocol);%0A
+%0A
         
 transpor
@@ -1260,27 +1260,136 @@
     
-transport.open();%0A%0A
+Scanner prompt = new Scanner(System.in);%0A        do %7B%0A            printMenus();%0A            String arg = prompt.nextLine();%0A    
     
@@ -1397,20 +1397,17 @@
    if (%22
-echo
+1
 %22.equals
@@ -1406,33 +1406,134 @@
 %22.equals(arg
-s%5B0%5D
 )) %7B%0A
+                System.out.print(%22Input message: %22);%0A                String msg = prompt.nextLine();%0A    
             
@@ -1541,34 +1541,34 @@
 cho(client, 
-args%5B1%5D);%0A
+msg);%0A    
         %7D el
@@ -1575,22 +1575,17 @@
 se if (%22
-upload
+2
 %22.equals
@@ -1592,12 +1592,8 @@
 (arg
-s%5B0%5D
 )) %7B
@@ -1609,68 +1609,417 @@
     
-upload(client, args%5B1%5D);%0A        %7D%0A
+    System.out.print(%22Input file path: %22);%0A                String path = prompt.nextLine();%0A                upload(client, path);%0A            %7D else if (%223%22.equals(arg)) %7B%0A                System.out.println(%22Bye~%22);%0A                break;%0A            %7D%0A        %7D while (true);
 %0A    
+%7D%0A%0A
     
-transport.close(
+private static void printMenus() %7B%0A        System.out.print(%22Select a number%5Cn1) Echo%5Cn2) File upload%5Cn3) Exit%5CnSelect: %22
 );%0A 
@@ -2107,32 +2107,90 @@
 ws TException %7B%0A
+        client.getOutputProtocol().getTransport().open();%0A
         String r
@@ -2245,16 +2245,75 @@
 n(rev);%0A
+        client.getOutputProtocol().getTransport().close();%0A
     %7D%0A%0A 
@@ -2470,24 +2470,116 @@
 File(path);%0A
+        if (!file.exists()) %7B%0A            throw new FileNotFoundException(path);%0A        %7D%0A%0A
         Uplo
@@ -2755,24 +2755,82 @@
 .length();%0A%0A
+        client.getOutputProtocol().getTransport().open();%0A
         bool
@@ -3663,32 +3663,91 @@
      %7D%0A        %7D
+%0A        client.getOutputProtocol().getTransport().close();
 %0A%0A        String

change to interactive.