-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiocallbacks.js
More file actions
156 lines (118 loc) · 4.53 KB
/
iocallbacks.js
File metadata and controls
156 lines (118 loc) · 4.53 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
//this file has all the callbacks that the socket.io server needs to handle.
// now now all callbacks that it makes or answers
var TaskList = require('./TaskList.js')
var WorkerList = require('./WorkerList.js')
//clear window
var util = require('util');
util.print("\u001b[2J\u001b[0;0H");
function start(io) {
io.set('log level', 1)
io.sockets.on('connection', connectionFunc);
var array;
var workerList = new WorkerList();
var taskList = new TaskList();
// when job and worker available emit. Check ever x milliseconds
// todo: how to improve?? may be run this function on some event detection??
setInterval(function(){
var workerId = workerList.nextAvailable();
var nextTCIndex = taskList.nextTaskChunkIndex();
if(workerId===-1 || nextTCIndex ===-1){
return
}
workerList.changeStatus(workerId, "busy")
var hash = taskList.tasks[nextTCIndex.task].hash;
var senderId = taskList.tasks[nextTCIndex.task].senderId;
var chunkIndex = nextTCIndex.chunk;
taskList.tasks[nextTCIndex.task].changeChunkValue(chunkIndex, 0)
if (chunkIndex == -1){
console.log("EXIT\n EXIT\n EXIT");
}
var jobDatax = new jobStruc(hash, senderId, workerId, chunkIndex, 152551)
io.sockets.socket(workerId).emit('job', jobDatax);
}, 1)
// check if solution is available for any task. If yes send answer to taskGiver. and remove the task from taskList
setInterval(function(){
var solAvailable = taskList.solutionAvailable()
if (solAvailable!==-1){
var hash = taskList.tasks[solAvailable].hash
var password = taskList.tasks[solAvailable].password
var senderId = taskList.tasks[solAvailable].senderId
io.sockets.socket(senderId).emit('answer', password);
taskList.remove(hash, senderId);
}
}, 500)
setInterval(function(){
console.log("\n\nWORKER LIST::::")
workerList.print();
console.log("\n\nTASK LIST::::")
taskList.print();
}, 5000)
function connectionFunc (socket) {
console.log(">> incoming connection: id " + socket.id)
socket.on('newWorker', function(){
workerList.add(socket.id);
console.log("newWorkerAdded")
})
socket.on('disconnect', function(){
console.log("disconnected ", socket.id);
workerList.remove(socket.id)
taskList.removeSender(socket.id)
})
socket.on("hash", function(theHash){
taskList.add(theHash, socket.id)
console.log("new hash received: ", theHash, socket.id);
});
socket.on("jobResponse", function(jobData){
//print(jobData);
print (jobData.chunkNumber+jobData.lastCheck)
taskList.chunkSolution(
jobData.senderId,
jobData.chunkNumber,
jobData.chunkStatus,
jobData.password
);
workerList.changeStatus(jobData.workerId, "free")
});
//emit solution space periodically.
setInterval(function(){
socket.emit('solutionSpace', taskList.getChunkMap());
}, 1000)
}
console.log(" info - iocallbacks registered.");
function jobStruc(hash, senderId, workerId, chunkNumber, chunkSize){
this.hash = hash;
this.senderId = senderId;
this.workerId = workerId;
this.chunkNumber = chunkNumber; //starts from 0
this.chunkSize = chunkSize;
this.password = "notKnown";
this.chunkStatus = "0"
};
function print (e){
console.log(">>print: ", e);
}
function makeArray(){
array = [];
for (var i=0;i<100;i++){
array.push(Math.random())
}
return array
}
/* usage key:
// send to current request socket client
//socket.emit('message', "this is a test");
// sending to all clients, include sender
//io.sockets.emit('message', "this is a test");
// sending to all clients except sender
//socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
//socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
//io.sockets.in('game').emit('message', 'cool game');
// sending to individual socketid
//io.sockets.socket(socketid).emit('message', 'for your eyes only');
// callback. callback function runs on sender.
//socket.on('message', function(message, callback) {})
*/
}
exports.start = start;