diff --git a/tests/serveur_compilation/server_compilation/src/main/java/app.java b/tests/serveur_compilation/server_compilation/src/main/java/app.java index 9044c3b60d7231ad6f561632f6ef350b2b5d6241..3f62482815c7e71089053f471fbefe71940e6ed4 100644 --- a/tests/serveur_compilation/server_compilation/src/main/java/app.java +++ b/tests/serveur_compilation/server_compilation/src/main/java/app.java @@ -1,81 +1,14 @@ import io.javalin.Javalin; import org.json.JSONObject; -import java.io.*; -import java.util.ArrayList; -import java.util.HashMap; - -import org.apache.commons.lang.StringEscapeUtils; - - public class app { public static void main(String[] args) { Javalin app = Javalin.create().enableCorsForAllOrigins().start(6999); app.post("/", ctx -> { - - JSONObject input = new JSONObject(ctx.body()); - - PrintWriter writer = null; - File file = null; - - String filename = ""; - String cmd = ""; - - if (input.get("language").equals("python")) { - // Create a PrintWriter object targeting sample.py - - filename = "sample.py"; - writer = new PrintWriter(filename, "UTF-8"); - cmd = "python " + filename; - - } else if (input.get("language").equals("java")) { - // Create a PrintWriter object targeting sample.py - filename = "app.java"; - writer = new PrintWriter(filename, "UTF-8"); - cmd = "java " + filename; - } - - // For delete purpose, create a File object also targeting sample.py - file = new File(filename); - - // Complex file with tabulation structure for test purpose - writer.println(input.get("stream")); - - // Close the writer, the file has been filled and is ready to be executed - writer.close(); - - long start = System.currentTimeMillis(); - Process cmdProc = Runtime.getRuntime().exec(cmd); - cmdProc.waitFor(); - long elapsed = System.currentTimeMillis() - start; - - // Create Array lists that will contain output and error values of the executed program - /**************************/ - - String line, output = "", error = ""; - BufferedReader stdoutReaderl = new BufferedReader(new InputStreamReader(cmdProc.getInputStream())); - while ((line = stdoutReader.readLine()) != null) - output += line + "\n"; - - BufferedReader stderrReader = new BufferedReader(new InputStreamReader(cmdProc.getErrorStream())); - while ((line = stderrReader.readLine()) != null) - error += line + "\n"; - - /**************************/ - - HashMap<String, Object> json = new HashMap<>(); - - //exit value of the ran program - json.put("exit", cmdProc.exitValue()); - json.put("output", output); - json.put("error", error); - json.put("time", elapsed); - - ctx.json(json); - - file.delete(); + compilation cpl = new compilation(); + ctx.json(cpl.execute_kata(new JSONObject(ctx.body()))); }); } } diff --git a/tests/serveur_compilation/server_compilation/src/main/java/compilation.java b/tests/serveur_compilation/server_compilation/src/main/java/compilation.java new file mode 100644 index 0000000000000000000000000000000000000000..c16e7ca61be9ab718f04fe52979a02703d190f59 --- /dev/null +++ b/tests/serveur_compilation/server_compilation/src/main/java/compilation.java @@ -0,0 +1,81 @@ +import org.json.JSONObject; + +import java.io.*; +import java.util.HashMap; + +public class compilation { + public HashMap<String, Object> execute_kata(JSONObject input) { + + + String filename = "", cmd = "", line, output = "", error = ""; + + switch (input.get("language").toString()) { + case "python": + filename = "sample.py"; + cmd = "python " + filename; + break; + case "java": + filename = "app.java"; + cmd = "java " + filename; + break; + } + + try (PrintWriter writer = new PrintWriter(filename, "UTF-8")) { + writer.println(input.get("stream")); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + + long start,elapsed = 0; + Process cmdProc = null; + + try { + start = System.currentTimeMillis(); + cmdProc = Runtime.getRuntime().exec(cmd); + cmdProc.waitFor(); + + // Compute the execute time + elapsed = System.currentTimeMillis() - start; + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + // Create Array lists that will contain output and error values of the executed program + /**************************/ + + try (BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(cmdProc.getInputStream()))) { + while ((line = stdoutReader.readLine()) != null) + output += line + "\n"; + } catch (IOException e) { + e.printStackTrace(); + } + + try (BufferedReader stderrReader = new BufferedReader(new InputStreamReader(cmdProc.getErrorStream()))) { + while ((line = stderrReader.readLine()) != null) + error += line + "\n"; + } catch (IOException e) { + e.printStackTrace(); + } + + /**************************/ + + HashMap<String, Object> json = new HashMap<>(); + + //exit value of the ran program + json.put("exit", cmdProc.exitValue()); + json.put("output", output); + json.put("error", error); + json.put("time", elapsed); + + // For delete purpose, create a File object also targeting sample.py + File file = new File(filename); + file.delete(); + + return json; + } +} diff --git a/tests/serveur_compilation/server_compilation/target/classes/app.class b/tests/serveur_compilation/server_compilation/target/classes/app.class index 13ac41ac2ff2bb50fa991e923c14965d0632abde..1bb05bb1f1ab76afd2e609b14706fe1f02fa8b93 100644 Binary files a/tests/serveur_compilation/server_compilation/target/classes/app.class and b/tests/serveur_compilation/server_compilation/target/classes/app.class differ diff --git a/tests/serveur_compilation/server_compilation/target/classes/compilation.class b/tests/serveur_compilation/server_compilation/target/classes/compilation.class new file mode 100644 index 0000000000000000000000000000000000000000..c940bcb0b792f79f564b9a8b0ccb5b52e4b1c5a0 Binary files /dev/null and b/tests/serveur_compilation/server_compilation/target/classes/compilation.class differ diff --git a/tests/serveur_compilation/server_rest/src/main/java/app.java b/tests/serveur_compilation/server_rest/src/main/java/app.java index 83b57af8b36ca4cfa5e333d838dd7d2462f596d5..14dee561bfbfb3d6a1c7e313cab5376dd6d3c3ea 100644 --- a/tests/serveur_compilation/server_rest/src/main/java/app.java +++ b/tests/serveur_compilation/server_rest/src/main/java/app.java @@ -7,7 +7,7 @@ import org.apache.commons.lang.StringEscapeUtils; public class app { - public static void main(String[] args) { + public static void main(String[] args) { Javalin app = Javalin.create().enableCorsForAllOrigins().start(7000); @@ -26,7 +26,7 @@ public class app { String code = StringEscapeUtils.escapeJava(ctx.queryParams("code").get(0)); wr.writeBytes("{\"language\":\""+type+"\",\"stream\":\""+code+"\"}"); - //wr.writeBytes("{\"language\":\"python\",\"stream\":\"def avg(marks):\\n assert len(marks) != 0\\n return sum(marks)/len(marks)\\nmark1 = []\\nprint('Average of mark1:',avg(mark1))\"}"); + wr.close(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));