-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemoteCodeServer.groovy
More file actions
72 lines (68 loc) · 2.11 KB
/
Copy pathRemoteCodeServer.groovy
File metadata and controls
72 lines (68 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import javax.script.ScriptEngineManager
import javax.script.ScriptException
import javax.script.SimpleScriptContext
final PORT = 6666
class ServerException extends Exception {
public ServerException(String message) {
super(message)
}
}
boolean handleClient(Socket connection) throws IOException {
println "Connection accepted"
def input = new BufferedReader(new InputStreamReader(connection.inputStream))
def output = new PrintStream(connection.outputStream)
try {
output.println "WELCOME"
String header = input.readLine()
if (header == null)
throw new ServerException("NO BODY RECIEVED")
if (header.equals("KILL")) {
output.println "GOODBYE"
return false
}
if (!header.matches("RUN \\d+"))
throw new ServerException("INVALID MESSAGE HEADER")
def numLines = Integer.parseInt(header.substring(4))
def code = new StringBuilder()
for (int i = 0; i < numLines; i++) {
String line = input.readLine()
if (line == null)
throw new ServerException("NOT ENOUGH LINES SENT")
code.append(line)
code.append("\n")
}
output.println "RECIEVED"
def engine = new ScriptEngineManager().getEngineByExtension("groovy")
def context = new SimpleScriptContext()
def codeOutput = new StringWriter()
context.setWriter(codeOutput)
context.setErrorWriter(codeOutput)
Object result = engine.eval(code.toString(), context)
if (result != null)
codeOutput.append("RESULT: ${result.toString()}")
def outputLines = codeOutput.toString().split("\n").length
output.println "SUCCESS ${outputLines} LINES"
output.println codeOutput.toString()
println "Successfully sent response"
} catch (ServerException e) {
output.println "BAD-REQUEST : ${e.message}"
println "Invalid client request"
} catch (IOException e) {
println "Transmission error"
} catch (ScriptException e) {
output.println "EXECUTION-ERROR : ${e.message}"
println "Executed code caused exception"
} finally {
output.flush()
connection.close()
}
return true
}
println "Starting socket"
def server = new ServerSocket(PORT)
try {
while (handleClient(server.accept())) {}
} finally {
println "Shutting down socket"
server.close()
}