-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.js
52 lines (44 loc) · 1.07 KB
/
example.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
/**
* Author: dhc
*/
'use strict'
var Server = require("./Server");
var Client = require("./Client");
var cluster = require("cluster");
var numCPUs = require("os").cpus().length;
function run_client() {
var client = new Client();
client.password = "123456";
client.conn("127.0.0.1", 8185);
}
if(cluster.isMaster){
var s = new Server();
s.mapfn = function(key, value){
var i = 0;
var tmp = [];
var splitword = value.split(" ");
while( i < value.split(" ").length){
tmp.push([splitword[i++], 1]);
}
return tmp;
};
s.reducefn = function(key, value){
var total = 0;
for(var k in value){
total += value[k];
}
return total;
};
s.datasource = ["Humpty Dumpty sat on a wall",
"Humpty Dumpty had a great fall",
"All the King's horses and all the King's men",
"Couldn't put Humpty together again"
];
s.run_server("123456", 8185);
for(var i = 0; i < numCPUs; i++ ){
cluster.fork();
}
}
else{
run_client();
}