001package com.ganteater.ae;
002
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.InputStreamReader;
006import java.io.OutputStream;
007import java.net.ServerSocket;
008import java.net.Socket;
009
010import org.apache.commons.lang.StringUtils;
011
012public class CommandServer {
013
014        private static final String HOST = "127.0.0.1";
015
016        private static final String STATUS = "/status";
017        protected static final String STOP = "/stop";
018        protected static final String RESTART = "/restart";
019
020        public static void sendCommand(int commandPort, String[] commands) throws IOException {
021                try (Socket socket = new Socket(HOST, commandPort)) {
022                        BufferedReader stream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
023                        OutputStream outputStream = socket.getOutputStream();
024                        for (String command : commands) {
025                                outputStream.write(command.getBytes());
026                                socket.getOutputStream().write('\n');
027                                socket.getOutputStream().flush();
028                                String line = stream.readLine();
029
030                                System.out.println("Returns: " + line);
031                        }
032                        socket.close();
033                }
034        }
035
036        public static void openCommandPort(final AEWorkspace workspace, final int commandPort) throws IOException {
037                Thread thread = new Thread() {
038                        public void run() {
039                                try (final ServerSocket serverSocket = new ServerSocket(commandPort)) {
040                                        Socket accept;
041                                        while (true) {
042                                                String result = "";
043
044                                                accept = serverSocket.accept();
045                                                BufferedReader stream = new BufferedReader(new InputStreamReader(accept.getInputStream()));
046                                                boolean isStopCommand = false;
047                                                try {
048                                                        while (true) {
049                                                                String line = stream.readLine();
050                                                                if (line == null) {
051                                                                        break;
052                                                                }
053
054                                                                switch (line.toLowerCase()) {
055                                                                case STATUS:
056                                                                        result = StringUtils.join(
057                                                                                        workspace.getRunners().stream().map(item -> item.getTitle()).toArray(),
058                                                                                        "\n");
059                                                                        isStopCommand = false;
060                                                                        break;
061
062                                                                case RESTART:
063                                                                        String configurationName = workspace.getConfigurationName();
064                                                                        workspace.stopAllRunners();
065                                                                        workspace.resetConfiguration();
066                                                                        workspace.loadConfiguration(configurationName, true);
067                                                                        try {
068                                                                                workspace.runSetupNodes();
069                                                                        } catch (Exception e) {
070                                                                                e.printStackTrace();
071                                                                                result = "";
072                                                                        }
073                                                                        result = "Restart completed successfully";
074                                                                        isStopCommand = false;
075                                                                        break;
076
077                                                                case STOP:
078                                                                        isStopCommand = true;
079                                                                        workspace.stopAllRunners();
080                                                                        result = "Stop completed successfully";
081                                                                        break;
082
083                                                                default:
084                                                                        String testPath = workspace.getTestPath(line);
085                                                                        if (testPath != null) {
086                                                                                workspace.runTask(line, true);
087                                                                                isStopCommand = false;
088                                                                                result = "Recipe: [" + line + "] started successfully";
089                                                                        } else {
090                                                                                result = "Recipe: [" + line + "] not found";
091                                                                        }
092                                                                }
093
094                                                                accept.getOutputStream().write(result.getBytes());
095                                                                accept.getOutputStream().write('\n');
096                                                                accept.getOutputStream().flush();
097                                                        }
098
099                                                        accept.close();
100
101                                                        if (isStopCommand) {
102                                                                System.out.println("Anteater on port: " + commandPort + " stopped.");
103                                                                System.exit(3);
104                                                        }
105
106                                                } catch (IOException | CommandException e) {
107                                                        e.printStackTrace();
108
109                                                }
110                                        }
111                                } catch (IOException e1) {
112                                        e1.printStackTrace();
113                                }
114                        };
115                };
116                thread.start();
117        }
118}