-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathNodeServer.js
167 lines (146 loc) · 4.55 KB
/
NodeServer.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
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
// NodeServer_js.php - Node.js localhost server, for testing one node of ByzAffirm multi-node communications.
// (c)2018 David C. Walley, MIT license.
var g_http = require("http");
var G = require("./G.js");
var g = G.g;
var ByzNode = require("./ByzNode.js");
// Constructor to run a node.js server.
function NodeServer() {
// Private variables:
this._isPort;
this._nNodes;
this._sName;
this._byznode;
this._ticks;
this._bOnNotOff;
this._httpserver;
}
// Factory constructor of instance of this class.
NodeServer.nodeserverNEW = function(a_byztestMom, a_iWhich, a_nNodes, a_takeErr_ms) {
var ob = new NodeServer;
if (!ob._bRenew(a_byztestMom, a_iWhich, a_nNodes, a_takeErr_ms)) {
return null;
}
return ob;
};
// Initialize or reset object.
NodeServer.prototype._bRenew = function(a_byztestMom, a_iWhich, a_nNodes, a_takeErr_ms) {
var me = this;
me._isPort = a_iWhich + NodeServer.nROOTpORT;
me._nNodes = a_nNodes;
me._sName = G.sNAME(a_iWhich);
me._sName = me._sName.toUpperCase();
me._byznode = ByzNode.byznodeNEW(a_byztestMom, a_iWhich, a_nNodes, a_takeErr_ms);
me._ticks = 0;
me._bOnNotOff = true;
me._httpserver = g_http.createServer(function(a, b) {
me.HandleRequest(a, b);
});
me._httpserver.listen(me._isPort, function() {
me._Tell("Node Server start http://localhost:" + me._isPort + " simulated error: " + a_takeErr_ms + "ms");
});
setTimeout(function() {
me.ONtICK();
}, 100 + 500 * a_iWhich);
return true;
};
NodeServer.nROOTpORT = 8080;
// Cause server to stop responding.
NodeServer.prototype.TurnOff = function() {
var me = this;
me._bOnNotOff = false;
};
// Report text of message logs of this server.
NodeServer.prototype.sShowMyCopies = function() {
var me = this;
return me._byznode.sShowMyCopies();
};
// Wrapper for creating a brand new message.
NodeServer.prototype.MakeMemo = function(a_s) {
var me = this;
me._byznode.MakeMemo(a_s);
};
// Periodic routine:
NodeServer.prototype.ONtICK = function() {
var me = this;
if (!me._bOnNotOff) {
return false;
}
me._ticks++;
// Choose a node at random (but not ourselves).
var iOther = 0;
do {
iOther = Math.floor(g.dRandom(0, me._nNodes)) + NodeServer.nROOTpORT;
} while (iOther === me._isPort);
var s = me._byznode.sHowMuchIKnow();
me._OnTick_MakeRequest("localhost", "/?igot", iOther, s);
// Run again later.
setTimeout(function() {
me.ONtICK();
}, 500);
return true;
};
// Make a call to another server on localhost.
NodeServer.prototype._OnTick_MakeRequest = function(a_sHost, a_sPath, a_isPort, a_sDataPayloadOut) {
var me = this;
var sRequestNotes = a_sDataPayloadOut + " ==> " + a_isPort + " " + G.sNAME(a_isPort - 8080);
var sBuffer = "";
var requestPost = g_http.request({"method":"POST", "hostname":a_sHost, "path":a_sPath, "port":a_isPort, "headers":{"Content-Type":"text/plain", "Content-Length":Buffer.byteLength(a_sDataPayloadOut)}}, function(a_httpresponse) {
a_httpresponse.on("data", function(a_chunk) {
sBuffer += a_chunk.toString();
});
a_httpresponse.on("end", function() {
me._byznode.Hark(sRequestNotes, sBuffer);
});
});
requestPost.on("error", function(e) {
});
// Setup a time-out.
requestPost.setTimeout(1200, function() {
requestPost.abort();
});
requestPost.write(a_sDataPayloadOut);
requestPost.end();
return true;
};
// SERVER:
// Server's request handler - decode request and send response.
NodeServer.prototype.HandleRequest = function(a_httprequest, a_httpresponse) {
var me = this;
if (!me._bOnNotOff) {
return false;
}
if ("/?kill" === a_httprequest.url) {
a_httpresponse.end("Server " + me._isPort + " die.");
a_httpresponse.end();
a_httprequest.connection.end();
a_httprequest.connection.destroy;
me._httpserver.close();
}
if (!("/?igot" === a_httprequest.url)) {
throw new Error("ASSERTION: ErrorMessage");
}
if ("POST" !== a_httprequest.method) {
a_httpresponse.end("");
return false;
}
var sBuffer = "";
a_httprequest.on("data", function(a_sChunk) {
sBuffer += a_sChunk;
if (1000000 < sBuffer.length) {
a_httprequest.connection.destroy();
}
});
// Process POSTed data after it is all re-assembled.
a_httprequest.on("end", function() {
a_httpresponse.end(me._byznode.sGetNewsForCaller(sBuffer));
});
return true;
};
// Log message to console.
NodeServer.prototype._Tell = function(a_sText) {
var me = this;
console.log("^" + g.whenNow_ms() + " " + me._sName + " " + a_sText);
return true;
};
exports.nodeserverNEW = NodeServer.nodeserverNEW;