Skip to content

Commit

Permalink
Added a way to run the program.
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
pauek committed Mar 31, 2011
1 parent 96600cc commit afa2c21
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
a.out
a.out*
*~
jquery.js
21 changes: 16 additions & 5 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var editor;
var remote;
var session;

function clear_errors() {
$("#errors").html('');
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;

Expand All @@ -94,6 +102,7 @@ function setup(_remote) {

$("#compileButton")
.button({ icons: { primary: 'ui-icon-gear' } })
.button("disable")
.click(compileProgram);

$("#runButton")
Expand All @@ -110,6 +119,8 @@ function setup(_remote) {
alert(y);
$("#editor").css({ bottom: y });
});

remote.startSession(sessionStart);
}

$(document).ready(function () {
Expand Down
46 changes: 42 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

0 comments on commit afa2c21

Please sign in to comment.