From afa2c21ca7d05b70c174c18dbcf7665efade168d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Fern=C3=A1ndez?= Date: Thu, 31 Mar 2011 23:00:05 +0200 Subject: [PATCH] Added a way to run the program. Now the clients obtain a "session" object (which would allow to do authentication but is not implemented yet), through which they can call 'compile' and 'run'. --- .gitignore | 2 +- client.js | 21 ++++++++++++++++----- server.js | 46 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 7c95747..795d6bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -a.out +a.out* *~ jquery.js diff --git a/client.js b/client.js index 3960d2b..95a6ae8 100644 --- a/client.js +++ b/client.js @@ -1,6 +1,6 @@ var editor; -var remote; +var session; function clear_errors() { $("#errors").html(''); @@ -54,12 +54,16 @@ function showErrors(errs) { function compileProgram() { var code = editor.getSession().getDocument().getValue(); - remote.compile(code, showErrors); + session.compile(code, showErrors); +} + +function showOutput(linesOfOutput) { + alert(linesOfOutput); } function runProgram() { if (!$(this).button("option", "disabled")) { - alert("Now the program is run..."); + session.run("", showOutput); } } @@ -68,8 +72,12 @@ function openSettings() { return false; } -function setup(_remote) { - remote = _remote; +function sessionStart(_session) { + session = _session; + $("#compileButton").button("enable"); +} + +function setup(remote) { var EditSession = require("ace/edit_session").EditSession; var CppMode = require("ace/mode/c_cpp").Mode; @@ -94,6 +102,7 @@ function setup(_remote) { $("#compileButton") .button({ icons: { primary: 'ui-icon-gear' } }) + .button("disable") .click(compileProgram); $("#runButton") @@ -110,6 +119,8 @@ function setup(_remote) { alert(y); $("#editor").css({ bottom: y }); }); + + remote.startSession(sessionStart); } $(document).ready(function () { diff --git a/server.js b/server.js index a729a0e..4f24550 100644 --- a/server.js +++ b/server.js @@ -26,9 +26,32 @@ function split_lines(text) { var compiler = "gcc"; -var server = dnode({ - compile: function (text, _callback) { - var cmd = compiler + ' -Wall -x c++ - -lstdc++'; +var server = dnode(function (client, connection) { + this.startSession = function (_callback) { + // AquĆ­ se puede mirar un login+password + _callback(new Session({ + client: client, + connection: connection, + })); + } +}); + +var global_id = 1; + +var Session = function (params) { + var conn = params.connection; + var client = params.client; + var id = global_id; + global_id += 1; + + console.log("Session start: " + id + ' ' + client + ' ' + conn); + + conn.addListener('end', function() { + console.log('Session end:' + id + ' ' + client + ' ' + conn); + }); + + this.compile = function (text, _callback) { + var cmd = compiler + ' -o a.out.' + id + ' -Wall -x c++ - -lstdc++'; var cc = spawn('/bin/sh', ['-c', cmd]); var stderr = ''; cc.stdin.end(text); @@ -40,5 +63,20 @@ var server = dnode({ _callback(lines); }); } -}); + + this.run = function (text, _callback) { + var cmd = './a.out.' + id; + var exe = spawn('/bin/sh', ['-c', cmd]); + var stdout = ''; + exe.stdin.end(text); + exe.stdout.addListener('data', function (chunk) { + stdout += chunk; + }); + exe.addListener('exit', function (code, signal) { + var outputLines = stdout.split('\n'); + _callback(outputLines); + }); + } +} + server.listen(app); \ No newline at end of file