-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.js
More file actions
executable file
·177 lines (147 loc) · 4.28 KB
/
driver.js
File metadata and controls
executable file
·177 lines (147 loc) · 4.28 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// (c) 2011 Laurent Vaucher
// http://www.slowfrog.com
// This file is under Apache License V2
//
"use strict";
var pool;
var create = function() {
if (pool) {
alert("Pool already exists.\nPress \"Kill\" if you want to resize it.");
} else {
var count_txt = document.getElementById("worker_count").value;
var count = parseInt(count_txt);
if (!count) {
alert("Invalid number of workers: " + count_txt);
return;
}
try {
pool = new Pool(count);
} catch (e) {
console.log("Exception", e);
}
}
};
var kill = function() {
if (!pool) {
return;
}
pool.broadcastMessage({cmd: "kill"});
pool = null;
};
var Pool = function(count) {
var that = this;
this.queue = [];
this.task_id = 0;
this.callbacks = [];
var status = document.getElementById("status");
this.workers = [];
for (var i = 0; i < count; ++i) {
this.workers[i] = new Worker("webwork.js");
this.workers[i].addEventListener("message", Pool.makeMessageHandler(that, i), false);
this.workers[i].addEventListener("error", Pool.makeErrorHandler(that, i), false);
this.workers[i].postMessage({cmd: "set_id", id: i});
var indic = document.createElement("div");
indic.id = "indic" + i;
indic.className = "worker_indicator";
indic.innerHTML = i;
var style = indic.style;
status.appendChild(indic);
}
};
Pool.makeMessageHandler = function(that, i) {
return function(ev) { that.handleMessage(ev, i); };
}
Pool.makeErrorHandler = function(that, i) {
return function(ev) { that.handleError(ev, i); };
}
Pool.prototype.handleMessage = function(ev, i) {
var data = ev.data;
if (data.cmd) {
switch (data.cmd) {
case "request_task":
this.provideTask(i);
break;
case "busy":
this.showBusy(i);
break;
case "idle":
this.showIdle(i);
break;
case "task_result":
this.receiveResults(data, i);
break;
case "worker_closed":
this.closeWorker(i);
break;
default:
console.log("Unknown command: " + data.cmd, data);
}
} else if (data.msg) {
console.log("Message from " + i + ": ", data.msg);
} else {
console.log("Received from " + i, data);
}
};
Pool.prototype.handleError = function(ev, i) {
console.log("Error from " + i + ": ", ev);
};
Pool.prototype.broadcastMessage = function(msg) {
for (var i = 0; i < this.workers.length; ++i) {
this.postMessage(msg, i);
}
};
Pool.prototype.postMessage = function(msg, i) {
this.workers[i].postMessage(msg);
};
Pool.prototype.postTask = function(task, callback) {
this.queue.push({task: task, callback: callback});
if (this.queue.length == 1) {
this.broadcastMessage({cmd: "new_task"});
}
this.showQueueInfo();
};
Pool.prototype.provideTask = function(i) {
if (this.queue.length > 0) {
console.log("#" + i + " requests task");
var task_def = this.queue.shift();
++this.task_id;
this.callbacks[this.task_id] = task_def.callback;
this.postMessage({cmd: "start_task", id: this.task_id, task: task_def.task}, i);
this.showQueueInfo();
} else {
console.log("#" + i + " no tasks available");
this.postMessage({cmd: "no_task"}, i);
}
};
Pool.prototype.receiveResults = function(res, i) {
var task_id = res.id;
if (task_id === undefined) {
console.log("Unidentified task result from #" + i, res);
return;
}
var callback = this.callbacks[task_id];
if (callback) {
this.callbacks[task_id] = null;
callback.call(null, res.result);
}
};
Pool.prototype.showQueueInfo = function() {
console.log("Task queue: " + this.queue.length);
var q = document.getElementById("queue");
q.innerHTML = "" + this.queue.length;
q.style.width = (20 * this.queue.length) + "px";
};
Pool.prototype.showBusy = function(i) {
this.setColor(i, "red");
};
Pool.prototype.showIdle = function(i) {
this.setColor(i, "#0c0");
};
Pool.prototype.setColor = function(i, color) {
document.getElementById("indic" + i).style.backgroundColor = color;
};
Pool.prototype.closeWorker = function(i) {
var div = document.getElementById("indic" + i);
div.parentNode.removeChild(div);
};