gt22

Untitled

Jan 16th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.12 KB | None | 0 0
  1. package com.projectbronze.botlauncher.nogui;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import com.projectbronze.botlauncher.Core;
  11. import com.projectbronze.botlauncher.api.IBot;
  12. import com.projectbronze.botlauncher.json.BotJson;
  13. import com.projectbronze.botlauncher.launcher.BotLauncher;
  14.  
  15. import net.dv8tion.jda.JDA.Status;
  16.  
  17. public class NoGuiManager extends Thread {
  18.  
  19.     public static final String BOT_MSG = "BOT_MSG", MSG_END = "MSG_END", OK = "OK";
  20.     public static final String UNABLE_TO_PARSE = "UNABLE_TO_PARSE", UNKNOWN_COMMAND = "UNKNOW_COMMAND", NOT_ENOUGH_ARGS = "NOT_ENOUGH_ARGS";
  21.     public static final String DIR_NOT_EXITST = "NOT_EXIST", NOT_DIR = "NOT_DIR", BOT_NOT_FOUND = "NOT_FOUND", BOT_DIR_NOT_SETED = "DIR_NOT_SETED";
  22.     public static final String BOT_ALREADY_STARTED = "ALREADY_STARTED", BOT_NOT_STARTED = "NOT_STARTED";
  23.     public static final String START_BOT = "START", STOP_BOT = "STOP", RESTART_BOT = "RESTART", GET_BOT_STATE = "GET_STATE", SET_BOT_DIR = "SET_DIR", GET_BOT_DIR = "GET_DIR", GET_BOT_JSON = "GET_JSON", STOP_MANAGER = "STOP_MANAGER", GET_USED_MEMORY = "GET_USED_MEMORY";
  24.  
  25.     private final INoGuiHandler handler;
  26.     private IBot bot;
  27.     private File botDir;
  28.     private BotJson json;
  29.     private HandlerPrintStream ps;
  30.     private BotThread thread;
  31.     private volatile BotState state = BotState.NOT_STARTED;
  32.  
  33.     public NoGuiManager(INoGuiHandler handler) {
  34.         this.handler = handler;
  35.         ps = new HandlerPrintStream();
  36.         thread = new BotThread();
  37.     }
  38.  
  39.     @Override
  40.     public void run() {
  41.         try {
  42.             loop: while (true) {
  43.                 List<String> cmd = readCommand();
  44.                 if (cmd.isEmpty()) {
  45.                     continue;
  46.                 }
  47.                 String command = cmd.get(0);
  48.                 synchronized (handler) {
  49.                     switch (command) {
  50.                         case SET_BOT_DIR: {
  51.                             if (checkArgs(2, cmd)) {
  52.                                 String dir = cmd.get(1);
  53.                                 if (dir.equals("null")) {
  54.                                     dir = null;
  55.                                     json = null;
  56.                                     handler.writeLine(OK);
  57.                                 }
  58.                                 File d = new File(dir);
  59.                                 if (!d.exists()) {
  60.                                     handler.writeLine(DIR_NOT_EXITST);
  61.                                     handler.writeLine(d.getAbsolutePath());
  62.                                 } else if (!d.isDirectory()) {
  63.                                     handler.writeLine(NOT_DIR);
  64.                                     handler.writeLine(d.getAbsolutePath());
  65.                                 } else if (d.list((di, name) -> di.equals(d) && name.equals("bot.json")).length == 0) {
  66.                                     handler.writeLine(BOT_NOT_FOUND);
  67.                                     handler.writeLine(d.getAbsolutePath());
  68.                                 } else {
  69.                                     botDir = d;
  70.                                     json = new BotJson(new File(d, "bot.json"));
  71.                                     handler.writeLine(OK);
  72.                                 }
  73.                             }
  74.                             break;
  75.                         }
  76.                         case GET_BOT_DIR: {
  77.                             handler.writeLine(botDir == null ? BOT_DIR_NOT_SETED : botDir.getAbsolutePath());
  78.                             break;
  79.                         }
  80.                         case GET_BOT_JSON: {
  81.                             handler.writeLine(json == null ? BOT_DIR_NOT_SETED : json.getJson().toString());
  82.                             break;
  83.                         }
  84.                         case START_BOT: {
  85.                             if (json == null) {
  86.                                 handler.writeLine(BOT_DIR_NOT_SETED);
  87.                             } else if (bot != null) {
  88.                                 handler.writeLine(BOT_ALREADY_STARTED);
  89.                             } else {
  90.                                 state = BotState.STARTING;
  91.                                 if (!thread.isAlive()) {
  92.                                     thread.start();
  93.                                 }
  94.                                 handler.writeLine(OK);
  95.                             }
  96.                             break;
  97.                         }
  98.                         case RESTART_BOT: {
  99.                             if (bot == null) {
  100.                                 handler.writeLine(BOT_NOT_STARTED);
  101.                             } else {
  102.                                 state = BotState.RESTARTING;
  103.                                 handler.writeLine(OK);
  104.                             }
  105.                             break;
  106.                         }
  107.                         case STOP_BOT: {
  108.                             if (checkArgs(2, cmd)) {
  109.                                 boolean free;
  110.                                 String freeS = cmd.get(1);
  111.                                 if (freeS.equals("true")) {
  112.                                     free = true;
  113.                                 } else if (freeS.equals("false")) {
  114.                                     free = false;
  115.                                 } else {
  116.                                     handler.writeLine(UNABLE_TO_PARSE);
  117.                                     handler.writeLine(freeS);
  118.                                     break;
  119.                                 }
  120.                                 state = BotState.STOPPING;
  121.                                 handler.writeLine(OK);
  122.                             }
  123.                             break;
  124.                         }
  125.                         case STOP_MANAGER: {
  126.                             state = BotState.FINISED;
  127.                             handler.writeLine(OK);
  128.                             handler.writeLine(MSG_END);
  129.                             break loop;
  130.                         }
  131.                         case GET_BOT_STATE:
  132.                         {
  133.                             handler.writeLine(state.name());
  134.                             break;
  135.                         }
  136.                         case GET_USED_MEMORY:
  137.                         {
  138.                             Runtime r = Runtime.getRuntime();
  139.                             handler.writeLine(r.totalMemory() - r.freeMemory() / 1048567 + "");
  140.                             break;
  141.                         }
  142.                         default: {
  143.                             handler.writeLine(UNKNOWN_COMMAND);
  144.                             break;
  145.                         }
  146.                     }
  147.                     handler.writeLine(MSG_END);
  148.                 }
  149.             }
  150.         } catch (Throwable t) {
  151.             Core.err.error("NoGui manager %s has an uncaught excepiton", handler);
  152.             t.printStackTrace(Core.err);
  153.             try {
  154.                 handler.writeLine(t.getLocalizedMessage());
  155.             } catch (Throwable t1) {
  156.                 Core.err.println("Unable to send exception message");
  157.                 t1.printStackTrace(Core.err);
  158.             }
  159.         }
  160.     }
  161.  
  162.     private List<String> readCommand() throws Exception {
  163.         List<String> cmd = new ArrayList<>();
  164.         for (String line = handler.readLine(); !line.equals(MSG_END); line = handler.readLine()) {
  165.             cmd.add(line);
  166.         }
  167.         return cmd;
  168.     }
  169.  
  170.     private boolean checkArgs(int minArgs, List<String> cmd) throws Exception {
  171.         if (cmd.size() < minArgs) {
  172.             notEnoughArgs(minArgs);
  173.             return false;
  174.         } else {
  175.             return true;
  176.         }
  177.     }
  178.  
  179.     private void notEnoughArgs(int minArgs) throws Exception {
  180.         handler.writeLine(NOT_ENOUGH_ARGS);
  181.         handler.writeLine(minArgs + "");
  182.     }
  183.  
  184.     private class HandlerPrintStream extends PrintStream {
  185.         public HandlerPrintStream() {
  186.             super(System.out);
  187.         }
  188.  
  189.         @Override
  190.         public void print(String s) {
  191.             super.print(s);
  192.             synchronized (handler) {
  193.                 try {
  194.                     handler.writeLine(BOT_MSG);
  195.                     handler.writeLine(s);
  196.                     handler.writeLine(MSG_END);
  197.                 } catch (Exception e) {
  198.                     e.printStackTrace();
  199.                 }
  200.             }
  201.         }
  202.     }
  203.  
  204.     private static enum BotState {
  205.         NOT_STARTED,
  206.         STOPPING,
  207.         STARTING,
  208.         WORKING,
  209.         RESTARTING,
  210.         FINISED;
  211.     }
  212.  
  213.     private class BotThread extends Thread {
  214.  
  215.         @Override
  216.         public void run() {
  217.             loop: while (true) {
  218.                 BotState state = NoGuiManager.this.state;
  219.                 switch (state) {
  220.                     case NOT_STARTED:
  221.                     case WORKING: {
  222.                         break;
  223.                     }
  224.                     case STOPPING: {
  225.                         if (bot != null && bot.getBot().getStatus() == Status.CONNECTED) {
  226.                             bot.stop(false);
  227.                         }
  228.                         bot = null;
  229.                         NoGuiManager.this.state = BotState.NOT_STARTED;
  230.                         break;
  231.                     }
  232.                     case RESTARTING: {
  233.                         bot.restart(ps);
  234.                         NoGuiManager.this.state = BotState.WORKING;
  235.                         break;
  236.                     }
  237.                     case STARTING: {
  238.                         if (bot == null) {
  239.                             try {
  240.                                 bot = BotLauncher.launchBot(botDir, ps);
  241.                             } catch (ClassNotFoundException | InstantiationException | IllegalArgumentException | NoSuchMethodException | SecurityException | InvocationTargetException | IOException e) {
  242.                                 throw new RuntimeException(e);
  243.                             }
  244.                             NoGuiManager.this.state = BotState.WORKING;
  245.                         }
  246.                         break;
  247.                     }
  248.                     case FINISED: {
  249.                         if (bot != null) {
  250.                             bot.stop(true);
  251.                             bot = null;
  252.                            
  253.                         }
  254.                         break loop;
  255.                     }
  256.                 }
  257.             }
  258.         }
  259.     }
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment