-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbs_node.js
More file actions
65 lines (54 loc) · 1.5 KB
/
Copy pathbs_node.js
File metadata and controls
65 lines (54 loc) · 1.5 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
// Blackscholes implementation using node js and message passing
// Author: Sirui Sun (sirui.sun@yale.edu)
// ------------------ node code --------------------
// Required libraries
var net = require('net');
var NUM_RETRIES = 10;
function Node(totalNodes, nodeID, port) {
this.totalNodes = totalNodes;
this.nodeID = nodeID;
this.nPort = port;
this.socket = new net.Socket();
this.toReturn = "";
this.socket.connect(this.nPort, function () {
console.log ('socket connected to port ' + port);
})
// on data receipt, process and shut down
this.socket.on('data', function(data) {
var realData = data.toString();
var dataStr = JSON.stringify(realData);
console.log('Node received data: ' + dataStr);
toReturn = dataStr;
});
this.socket.on('end', function() {
console.log('socket disconnected');
});
}
// send a request
Node.prototype.sendRequest = function(JSONdata, callback)
{
var JSONstring = JSON.stringify(JSONdata);// + "|";
console.log('writing: ' + JSONstring);
this.socket.write(JSONstring, 'utf8', callback);
};
// send a 'send' message
Node.prototype.sendMessage = function(toNode, msg, callback) {
var toSend =
{
"send": "send"
, "receiver": toNode
, "sender": this.nodeID
, "message": msg
}
this.sendRequest(toSend, callback);
}
// send a 'receive' message
Node.prototype.receiveMessage = function(fromNode, callback) {
var toSend =
{
"receive": "receive"
, "receiver": this.nodeID
, "sender": fromNode
}
this.sendRequest(toSend, callback);
}