-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebwork.js
More file actions
executable file
·95 lines (74 loc) · 1.97 KB
/
webwork.js
File metadata and controls
executable file
·95 lines (74 loc) · 1.97 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// (c) 2011 Laurent Vaucher
// http://www.slowfrog.com
// This file is under Apache License V2.
//
importScripts("hangman_task.js");
var Slave = function() {
this.request_pending = false;
};
var slave = new Slave();
self.addEventListener('message', function(e) { slave.handleMessage(e); }, false);
Slave.prototype.postMessage = function(msg) {
self.postMessage(msg);
};
Slave.prototype.postText = function(msg) {
this.postMessage({msg: msg});
};
Slave.prototype.postCommand = function(cmd, args) {
this.postMessage({cmd: cmd, args: args});
};
Slave.prototype.handleMessage = function(e) {
var data = e.data;
if (data.cmd === undefined) {
this.postText("#" + this.id + " Unknown message: " + data);
return;
}
switch (data.cmd) {
case "set_id":
this.id = data.id;
this.postText("Slave #" + this.id + " allocated");
break;
case "new_task":
this.requestTask();
break;
case "start_task":
this.doTask(data);
break;
case "no_task":
this.noTask();
break;
case "kill":
this.postCommand("worker_closed");
self.close();
break;
default:
this.postText("#" + this.id + " Unknown command: " + data.cmd);
}
};
Slave.prototype.requestTask = function() {
if (!this.request_pending) {
this.request_pending = true;
this.postCommand("request_task");
}
};
Slave.prototype.setBusy = function(busy) {
this.postCommand(busy ? "busy" : "idle");
if (!busy) {
this.requestTask();
}
};
Slave.prototype.doTask = function(task) {
this.request_pending = false;
// might use task object
this.setBusy(true);
var id = task.id;
var cas = task.task;
var res = solve_case(cas.dic, cas.lists);
this.postMessage({cmd: "task_result", id: id, result: {index: cas.index, result: res}});
this.setBusy(false);
this.requestTask();
};
Slave.prototype.noTask = function() {
this.request_pending = false;
};