Browse Source

transfers can be cancelled

Martin Kunz 5 years ago
parent
commit
4c9d68ee34

+ 3 - 2
src/main/java/com/acdp/transceivr/LoadTools.java

@@ -23,8 +23,9 @@ public class LoadTools {
                         .build();
             })
             .build();
-
-        client.newCall(request).enqueue(new Callback() {
+        var call=client.newCall(request);
+        t.call=call;
+        call.enqueue(new Callback() {
             @Override
             public void onFailure(Call call, IOException e) {
                 t.uploadError=e.toString();

+ 1 - 1
src/main/java/com/acdp/transceivr/Main.java

@@ -2,6 +2,6 @@ package com.acdp.transceivr;
 
 public class Main {
     public static void main(String [ ] args) {
-        new WebServer(8082,true).start();
+        new WebServer(8082).start();
     }
 }

+ 4 - 0
src/main/java/com/acdp/transceivr/Transfer.java

@@ -1,8 +1,11 @@
 package com.acdp.transceivr;
 
 import com.eclipsesource.json.JsonObject;
+import okhttp3.Call;
 
 public class Transfer {
+    public Call call;
+
     public Transfer(int id) {
         this.id = id;
         startTS=System.currentTimeMillis();
@@ -17,6 +20,7 @@ public class Transfer {
     public String uploadResponseBody;
     public boolean downloadDone = false;
     public boolean uploadDone = false;
+    public boolean canceled=false;
     public long startTS;
     public long currentTS;
 

+ 9 - 1
src/main/java/com/acdp/transceivr/WebServer.java

@@ -14,7 +14,7 @@ public class WebServer {
     private int nextID = 1;
     private ConcurrentHashMap<Integer, Transfer> uploads = new ConcurrentHashMap<>();
 
-    public WebServer(int port, boolean debug) {
+    public WebServer(int port) {
         this.port = port;
     }
 
@@ -33,6 +33,14 @@ public class WebServer {
             while (true)
                 is.read(ba);
         });
+        post("/cancel", (req, res) -> {
+            int id = Integer.parseInt(req.queryParams("id"));
+            var t=uploads.get(id);
+            t.canceled=true;
+            t.call.cancel();
+
+            return "OK";
+        });
         get("/status", (req, res) -> {
             JsonArray ja = new JsonArray();
             for (Transfer t : uploads.values())

+ 16 - 2
src/main/resources/webroot/index.html

@@ -41,7 +41,8 @@
                 </td>
             </tr>
             <tr>
-                <td></td>
+                <td>    <button @click="cancel(item.id)">cancel</button>
+                </td>
                 <td>
 <pre>id: {{ item.id}}
 from: {{ item.from}}
@@ -90,7 +91,20 @@ rate: {{ item.rate}}k/s
                     this.$data.log = [];
                     console.log(error)
                 });
-
+            },
+            cancel: function (id) {
+                let data = new URLSearchParams();
+                data.append('id', id);
+                fetch('/cancel', {
+                    method: "POST",
+                    body: data
+                }).then(response => {
+                    console.log(response);
+                })
+                .catch(error => {
+                    this.$data.log = [];
+                    console.log(error)
+                });
             },
             ts2txt: function (ts) {
                 return moment(ts).format('YYYY-MM-DD hh:mm ss.SSS ')