WebServer.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.acdp.transceivr;
  2. import com.eclipsesource.json.JsonArray;
  3. import info.faljse.SDNotify.SDNotify;
  4. import org.slf4j.LoggerFactory;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9. import java.util.concurrent.ConcurrentHashMap;
  10. import static spark.Spark.*;
  11. public class WebServer {
  12. private final static org.slf4j.Logger logger = LoggerFactory.getLogger(WebServer.class);
  13. private final Params params;
  14. private int nextID = 1;
  15. private ConcurrentHashMap<Integer, Transfer> uploads = new ConcurrentHashMap<>();
  16. private Timer t=new Timer("cleanup",true);
  17. public WebServer(Params params) {
  18. this.params = params;
  19. }
  20. public void start() {
  21. port(params.port);
  22. if(!params.webroot.isEmpty())
  23. staticFileLocation(params.webroot);
  24. else
  25. staticFiles.location("/webroot");
  26. get("/zero", (req, res) -> {
  27. var os = res.raw().getOutputStream();
  28. var ba = new byte[1024];
  29. while (true)
  30. os.write(ba);
  31. });
  32. post("/null", (req, res) -> {
  33. var is = req.raw().getInputStream();
  34. var ba = new byte[1024];
  35. while (is.read(ba)!=-1);
  36. res.status(200);
  37. return "";
  38. });
  39. post("/cancel", (req, res) -> {
  40. int id = Integer.parseInt(req.queryParams("id"));
  41. var t = uploads.get(id);
  42. t.canceled = true;
  43. t.call.cancel();
  44. res.status(200);
  45. return "OK";
  46. });
  47. get("/status", (req, res) -> {
  48. JsonArray ja = new JsonArray();
  49. for (Transfer t : uploads.values())
  50. ja.add(t.toJSON());
  51. res.header("Content-Type", "application/json");
  52. return ja.toString();
  53. });
  54. post("/xfer", (req, res) -> {
  55. try {
  56. Transfer t = new Transfer(nextID++);
  57. t.from = req.queryParams("from");
  58. t.to = req.queryParams("to");
  59. t.cpeeCallback = req.headers("CPEE-CALLBACK");
  60. t.cpeeCallbackId = req.headers("CPEE-CALLBACK-ID");
  61. t.cpeeInstanceURL = req.headers("CPEE-INSTANCE-URL");
  62. if (Boolean.valueOf(req.queryParams("callback"))) {
  63. res.header("CPEE-CALLBACK", "true");
  64. t.doCpeeCallback = true;
  65. }
  66. LoadTools.startTransfer(t);
  67. uploads.put(t.id, t);
  68. res.status(200);
  69. return "OK: " + t.id;
  70. } catch (Exception e) {
  71. return "FAILED: " + e.toString();
  72. }
  73. });
  74. awaitInitialization();
  75. t.scheduleAtFixedRate(new TimerTask() {
  76. @Override
  77. public void run() {
  78. for (var i = uploads.entrySet().iterator(); i.hasNext();) {
  79. var e = i.next();
  80. if(e.getValue().finished&&(e.getValue().finishedAT+30000<System.currentTimeMillis())){
  81. i.remove();
  82. logger.info("Removed finished transfer {}", e.getValue().id);
  83. }
  84. }
  85. }
  86. },1000,1000);
  87. SDNotify.sendNotify(); //notify: ready
  88. logger.info("Running");
  89. }
  90. }