-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
132 lines (121 loc) · 3.89 KB
/
cli.js
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
const readline = require('readline');
const cluster = require('cluster');
const tmitter = require('tmitter');
const request = require('request');
var employer;
var count = 0;
if (cluster.isMaster) {
employer = createEmployer();
employer.on('foundblock', function(message) {
console.log('won', count++);
console.log(message.block)
var url = 'http://localhost:3000/solution';
request.post({ url, form: message.block }, function(err, res, data) {
console.log('foundSolution:', data)
})
})
function processNextTask(data) {
request('http://localhost:3000/currentTask', function(err, res, body) {
employer.setTask(JSON.parse(body));
});
}
processNextTask();
setInterval(processNextTask, 3000);
loop();
} else {
require('./cliMinerManager.js')
}
function loop() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(':', (answer) => {
if (answer === 'hire') {
employer.hire();
} else if (answer === 'fire') {
employer.fire();
} else if (answer === 'status' || answer === 's') {
console.log('number of workers:', employer.workers.length)
console.log('speed :', employer.getSpeed())
console.log('win count :', count)
console.log('miningAmount :', employer.miningAmount)
} else if (answer === 'quit') {
process.exit();
}
//console.log(answer);
stop();
loop();
});
var timeout = setTimeout(function() {
stop();
loop();
}, 60000);
function stop() {
clearTimeout(timeout);
rl.close();
}
}
/**
* employer for webworker
*/
function createEmployer() {
const employer = {
miningAmount: 0,
workers: [],
task: undefined,
setTask(task) {
employer.task = task;
employer.workers.forEach(worker => worker.send(JSON.stringify({ type: 'message', task })));
},
getSpeed() {
return employer.workers.reduce((sum, worker) => sum + worker.getSpeed(), 0)
},
hire() {
var miningWorker = cluster.fork();
miningWorker.getSpeed = function() {
if (!miningWorker.lastWork) return 0;
return Math.round(miningWorker.lastWork.amount / (miningWorker.lastWork.time / 1000));
}
miningWorker.on('message', function(message) {
message = JSON.parse(message);
//console.log(message)
if (message.type == 'solution') {
console.log('found solution')
employer.task = undefined;
employer.emit('foundblock', message)
} else if (message.type === 'worked') {
//console.log('worked')
employer.miningAmount += message.amount;
miningWorker.lastWork = message;
} else {
console.log('unknown message,', message)
}
});
if (employer.task) {
miningWorker.send(JSON.stringify({ type: 'message', task: employer.task }));
}
employer.workers.push(miningWorker);
},
/**
*
* @param {WebWorker | number} [worker]
*/
fire(worker) {
if (!worker) worker = 0;
if (typeof worker === 'number') {
worker = employer.workers[worker];
}
return employer.fireWorker(worker);
},
/**
* @param {WebWorker} worker
*/
fireWorker(worker) {
worker.kill();
employer.workers.splice(employer.workers.indexOf(worker), 1);
}
};
tMitter(employer);
return employer;
}