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 {
        TTransport transport = new TSocket("localhost", 8877);
        TProtocol protocol = new TBinaryProtocol(transport);
        ExampleService.Client client = new ExampleService.Client(protocol);
        transport.open();
        String rev = client.echo("Ah asdljasldj");
        System.out.println(rev);
        transport.close();

        transport.open();
        File file = new File(ExampleClient.class.getResource("/holololo.jpg").toURI());
        UploadInfo uploadInfo = new UploadInfo();
        uploadInfo.msg = UploadMessage.BEGIN_UPLOAD;
        uploadInfo.fileName = file.getName();
        uploadInfo.length = file.length();

        if (client.upload(uploadInfo)) {
            FileInputStream f = new FileInputStream(file);
            FileChannel fileChannel = f.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024 * 10);
            boolean sendAll = true;
            uploadInfo.msg = UploadMessage.PROGRESS_UPLOAD;
            while ((uploadInfo.length = fileChannel.read(buffer)) > 0) {
                buffer.flip();
                uploadInfo.data = buffer;
                if (!client.upload(uploadInfo)) {
                    sendAll = false;
                    break;
                }
                buffer.clear();
            }
            uploadInfo.msg = UploadMessage.END_UPLOAD;
            if (!client.upload(uploadInfo)) {
                sendAll = false;
            }
            f.close();
        }
        transport.close();
    }
}

@@ -966,18 +966,19 @@
 %22Ah 
-asdljasldj
+lololololo.
 %22);%0A
@@ -1042,34 +1042,8 @@
 );%0A%0A
-        transport.open();%0A
     
@@ -1315,24 +1315,82 @@
 .length();%0A%0A
+        boolean success = true;%0A        transport.open();%0A
         if (
@@ -1447,16 +1447,18 @@
 Stream f
+is
  = new F
@@ -1518,16 +1518,18 @@
 nnel = f
+is
 .getChan
@@ -1603,43 +1603,8 @@
 0);%0A
-            boolean sendAll = true;
 %0A   
@@ -1652,32 +1652,70 @@
 ROGRESS_UPLOAD;%0A
+            uploadInfo.data = buffer;%0A
             whil
@@ -1806,50 +1806,8 @@
 ();%0A
-                uploadInfo.data = buffer;%0A
     
@@ -1865,38 +1865,38 @@
                s
-endAll
+uccess
  = false;%0A      
@@ -1972,32 +1972,58 @@
 ;%0A            %7D%0A
+            fis.close();%0A%0A
             uplo
@@ -2053,32 +2053,68 @@
 age.END_UPLOAD;%0A
+            uploadInfo.data = null;%0A
             if (
@@ -2164,14 +2164,14 @@
    s
-endAll
+uccess
  = f
@@ -2198,21 +2198,35 @@
         
+%7D%0A
     
-f
+    transport
 .close()
@@ -2227,24 +2227,25 @@
 lose();%0A
+%0A
         
 %7D%0A      
@@ -2240,34 +2240,116 @@
     
-%7D%0A        transport.close(
+String alertMsg = success ? %22Success to upload.%22 : %22Failure to upload.%22;%0A        System.out.println(alertMsg
 );%0A 

make to be pretty.