Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rubenfonseca/map_crowd_reduce
Browse files Browse the repository at this point in the history
Conflicts:
	views/compute.haml
  • Loading branch information
rubenfonseca committed Nov 20, 2010
2 parents 3211f1c + 600c5fb commit 12aae9b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 28 deletions.
47 changes: 37 additions & 10 deletions app.js
Expand Up @@ -10,6 +10,8 @@ var app = module.exports = express.createServer();
var fs = require('fs');
var uuid = require('uuid');

//var Sandbox = require('sandbox');

// Configuration

app.configure(function(){
Expand Down Expand Up @@ -44,14 +46,18 @@ socket.on('connection', function(client) {
};

if(data.message == "next_job") {
console.log("Client " + client.sessionId + " asked for next job");
console.log("Client " + client.sessionId + " asked for next job for computation " + data.uuid);

if(state[data.uuid]['m_jobs'].length > 0) {
var job = state[data.uuid]['m_jobs'].shift();

console.log("Client " + client.sessionId + " received a new job " + data.uuid + " | " + job.id);
client.send({message: 'process', job:job});
state[data.uuid]['m_jobs'].push(job);

// Store which clients are working on this computation
state[data.uuid]['clients'].push(client.sessionId);

return;
} else {
console.log("No job available for " + client.sessionId);
Expand All @@ -62,9 +68,10 @@ socket.on('connection', function(client) {

console.log("Client " + client.sessionId + " finished a job! " + u + " | " + id);

for(var c in socket.clients) {
socket.clients[c].send({message:"status", uuid:u, percentage:((state[u]['total_jobs'] - state[u]['m_jobs'].length) * 100) / state[u]['total_jobs']});
}
// send current computation status to clients
var job_done_percent = ((state[u]['total_jobs'] - state[u]['m_jobs'].length) * 100) / state[u]['total_jobs'];
var broadcast_message = {message:"status", uuid:u, percentage: job_done_percent };
socket.broadcast( broadcast_message );

for(var i=0; i<state[u]['m_jobs'].length; i++) {
if(state[u]['m_jobs'][i].id == id) {
Expand All @@ -75,9 +82,7 @@ socket.on('connection', function(client) {
if(id == "r_job") {
console.log("Reduce job finished :D");

for(var c in socket.clients) {
socket.clients[c].send({message:"result", data:data.data, uuid:u});
}
socket.broadcast({message:"result", data:data.data, uuid:u});

delete state[u];
} else {
Expand All @@ -87,9 +92,7 @@ socket.on('connection', function(client) {
console.log("Map phase finished :D");

console.log("Sending terminate message to all clients");
for(var c in socket.clients) {
socket.clients[c].send({message:"stop", uuid:u});
}
socket.broadcast({message:"stop", uuid:u});

var r_job = {
data: state[u]['m_results'],
Expand All @@ -100,6 +103,15 @@ socket.on('connection', function(client) {
state[u]['m_jobs'].push(r_job);
}
}
} else if (data.message == 'monitor') {
var u = data.uuid;

// Send status of computation to monitor that just connected
var job_done_percent = ((state[u]['total_jobs'] - state[u]['m_jobs'].length) * 100) / state[u]['total_jobs'];
var status_message = {message:"status", uuid:u, percentage: job_done_percent };
client.send( status_message );

console.log("Someone is not doing any work (just monitoring progress)");
} else {
console.log("Unknown message " + data);
}
Expand Down Expand Up @@ -148,6 +160,10 @@ app.post('/new_job', function(req, res) {

console.log("Slicing.....");
var m_results = [];
// Run slicing function in a sandbox
//var sandbox = new Sandbox();
//sandbox.run("(" + fields['s'] + ")()", function(output){console.log(output)});

var m_jobs = s.runInNewContext({})(file_data);
for(var i=0; i<m_jobs.length; i++) {
m_jobs[i] = {
Expand All @@ -162,6 +178,8 @@ app.post('/new_job', function(req, res) {
state[u]['m_jobs'] = m_jobs;
state[u]['total_jobs'] = m_jobs.length;
state[u]['r'] = r;
state[u]['clients'] = [];


console.log("Redirecting to computation");

Expand All @@ -187,6 +205,15 @@ app.get('/compute/:id', function(req, res) {
});
});

app.get('/monitor/:id', function(req, res) {
res.render('monitor.haml', {
locals: {
title: "Monitor",
uuid: req.params.id
}
});
});

app.get('/worker.js', function(req, res) {
res.sendfile(__dirname + "/views/worker.js");
});
Expand Down
7 changes: 7 additions & 0 deletions public/mcr.js
@@ -0,0 +1,7 @@
function smessage(m){
document.getElementById("status").innerHTML += '<p>' + m + '</p>';
}

function supports_web_workers() { return !!window.Worker; }


29 changes: 16 additions & 13 deletions views/compute.haml
Expand Up @@ -3,8 +3,8 @@
%link{href:"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css", rel:"stylesheet", type:"text/css"}
%script{src: "https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"}
%script{src: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"}
%script{src: "/socket.io/socket.io.js"}
%script{src: "/mcr.js"}
:javascript
function supports_web_workers() { return !!window.Worker; }
Expand All @@ -15,12 +15,12 @@
if ( supports_web_workers() ) { socket.connect(); }
else {
document.getElementById("status").innerHTML = '<p>Your browser does not support WebWorkers!</p>';
smessage('Your browser does not support WebWorkers!');
}
});
socket.on('connect', function(){
document.getElementById("status").innerHTML += '<p>Connected. Requesting job...</p>';
smessage('Connected. Requesting job...');
socket.send({message: 'next_job', uuid: "#{uuid}"});
});

Expand All @@ -29,42 +29,45 @@

window.worker = new Worker("/worker.js");
window.worker.onmessage = function(event) {
// console.log(event.data);

// comment this line to work in firefox
//console.log(event.data);
var data = event.data;
socket.send({message: 'job_completed', id:data.id, data:data.data, uuid:"#{uuid}"});
window.worker.terminate();
document.getElementById("status").innerHTML += '<p>Requesting another job...</p>';
smessage('Requesting another job...');
socket.send({message: 'next_job', uuid:"#{uuid}"});
};

// console.log(data);
document.getElementById("status").innerHTML += ('<p>Starting new job ' + data.job.id + ' ....</p>');
//comment this line to work in firefox
//console.log(data);
smessage('Starting new job ' + data.job.id + ' ....');
window.worker.postMessage(data.job);
} else if(data.message == "stop" && data.uuid == "#{uuid}") {
// console.log("Terminating worker...");
document.getElementById("status").innerHTML += '<p>Terminating worker...</p>';
smessage('Terminating worker...');

if(window.worker) {
window.worker.terminate();
document.getElementById("status").innerHTML += '<p>Worker terminated!</p>';
smessage('Worker terminated!');
}
} else if(data.message == "result" && data.uuid == "#{uuid}") {
document.getElementById("status").innerHTML += '<p>The result of this awesome computation is: ' + data.data + '</p>';
smessage('The result of this awesome computation is: ' + data.data );
$('#progressbar').progressbar({ value: 100 });
} else if(data.message == "status" && data.uuid == "#{uuid}") {
$('#progressbar').progressbar({ value: data.percentage });
}
});

socket.on('disconnect', function(){
document.getElementById("status").innerHTML += '<p>Disconnected!</p>';
smessage('Disconnected!');
if(window.worker) {
document.getElementById("status").innerHTML += '<p>Terminating worker...</p>';
smessage('Terminating worker...');
window.worker.terminate();
document.getElementById("status").innerHTML += '<p>Worker terminated!</p>';
smessage('Worker terminated!');
}
});

Expand Down
4 changes: 1 addition & 3 deletions views/index.haml
@@ -1,5 +1,4 @@
%script{src: "examples.js"}
%script{src: "md5_oneline.js"}
%h1= title
%p Welcome to #{title}

Expand Down Expand Up @@ -28,7 +27,7 @@
%h3 Prewritten examples

%button{onclick: "load_example_1()"}
Find the number of prime numbers between 0 and 6000
Find the number of primes

%button{onclick: "load_example_2()"}
Find md5 collision
Expand Down Expand Up @@ -71,6 +70,5 @@

bespin.useBespin('reduce', { syntax:'js' }).then(function(env) {
window.reduce_editor = env.editor;
load_example_1();
}, function(error) {});
}
46 changes: 46 additions & 0 deletions views/monitor.haml
@@ -0,0 +1,46 @@
%h1= title

%link{href:"http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css", rel:"stylesheet", type:"text/css"}
%script{src: "https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"}
%script{src: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"}
%script{src: "/socket.io/socket.io.js"}
%script{src: "/mcr.js"}
:javascript
$(function() {
$('#progressbar').progressbar({ value: 0 });
});
var socket = new io.Socket(window.location.hostname, {'port': window.location.port});
socket.on('connect', function(){
smessage('Connected. Not working, just monitoring...');
socket.send({message: 'monitor', uuid: "#{uuid}"});
});

socket.on('message', function(data) {
if(data.message == "result" && data.uuid == "#{uuid}") {
smessage('The result of this awesome computation is: ' + data.data );
$('#progressbar').progressbar({ value: 100 });
} else if(data.message == "status" && data.uuid == "#{uuid}") {
$('#progressbar').progressbar({ value: data.percentage });
}
});

socket.on('disconnect', function(){
smessage('Disconnected!');
});

if ( ! supports_web_workers() ) {
smessage('Your browser does not support WebWorkers! But you can still monitor.');
}
socket.connect();


#progressbar

#text
If you want to do some work, click
%a{href: '/compute/' + uuid}this link

#status

7 changes: 5 additions & 2 deletions views/ready.haml
Expand Up @@ -3,9 +3,12 @@
%p Your computation is ready

%p
Now share
Now share&nbsp;
%a{href:'/compute/' + uuid} this link
with your minions :)
&nbsp;with your minions :)

%p If you just want to monitor the progress&nbsp;
%a{href:'/monitor/' + uuid} this is your link.

%p
Happy global warming

0 comments on commit 12aae9b

Please sign in to comment.