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