-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
362 lines (317 loc) · 12 KB
/
app.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
var stun = require('vs-stun');
var crypto = require('crypto'),
shasum = crypto.createHash('sha1');
var clj_fuzzy = require('clj-fuzzy');
var socket, server = {
host: 'stun.l.google.com',
port: 19302
}
var myPeerID = "";
var peers = [];
var data = {};
function createConnection(cb) {
var callback = function callback(error, value) {
if (!error) {
socket = value;
cb();
} else console.log('Something went wrong: ' + error);
}
stun.connect(server, callback);
}
createConnection(function () {
console.log("Connection to STUN complete");
console.log("Connection Address: " + socket.stun.public.host);
console.log("Connection Port: " + socket.stun.public.port);
myPeerID = getSHA(socket.stun.public.host + "p" + socket.stun.public.port);
console.log("My Peer ID: " + myPeerID);
//Start keepalive with stun
setInterval(keepAlive, 10000);
createMessageListener();
onConnectionComplete();
});
function onConnectionComplete() {
}
function keepAlive() {
stun.resolve(socket, server, function () {});
//Send keep alive packet to all of our current peers
for (var i = 0; i < peers.length; i++){
sendPIN(peers[i].split("p")[0], peers[i].split("p")[1]);
//If client has less than max peers ask our peers to give us there closest matching pair
if (peers.length < MAX_PEERS){
sendREQ(peers[i].split("p")[0], peers[i].split("p")[1]);
}
}
}
function connectToClient(host, port, count) {
if (count === undefined) {
count = 0;
}
setTimeout(function () {
if (inPeerList(host, port) == false) {
if (count < 5) {
sendAWK(host, port);
connectToClient(host, port, count + 1);
} else {
console.log("Aborted connection attempt to peer at " + host + ":" + port);
}
}
}, 1000);
}
function getSHA(string) {
shasum = crypto.createHash('sha1');
shasum.update(string);
return shasum.digest('hex');
}
function inPeerList(host, port) {
if (peers.indexOf(host + "p" + port) < 0) {
return false;
} else {
return true;
}
}
function sendAWK(host, port) {
sendMessage(host, port, {
"m": "awk"
});
}
function sendADD(host, port, peer){
sendMessage(host, port, {
"m": "add",
"peer": peer
});
}
var peersToBeDeleted = [];
function sendPIN(host, port){
sendMessage(host, port, {
"m": "pin",
});
peersToBeDeleted.push(host + "p" + port);
//Delete if peer doesn't response in 3 seconds
setTimeout(function(){
if (peersToBeDeleted.indexOf(host + "p" + port) > -1){
//Still in list lets remove from peers
peersToBeDeleted.splice(peersToBeDeleted.indexOf(host + "p" + port), 1);
peers.splice(peers.indexOf(host + "p" + port), 1);
console.log("Removed offline peer - " + getSHA(host + "p" + port));
}
}, 3000);
}
function sendPON(host, port){
sendMessage(host, port, {
"m": "pon",
});
}
function sendREQ(host, port){
sendMessage(host, port, {
"m": "req",
"peer": socket.stun.public.host + "p" + socket.stun.public.port
});
}
function sendPUT(host, port, key, value){
sendMessage(host,port,{"m":"put","key":key,"value":value,"count":0});
}
function sendGET(host, port, key){
sendMessage(host,port,{"m":"get","key":key,"dest":socket.stun.public.host + "p" + socket.stun.public.port});
}
function sendMessage(host, port, message) {
// console.log("Sent a " + message.m);
message = new Buffer(JSON.stringify(message));
socket.send(message, 0, message.length, port, host, function (err, bytes) {
if (err) throw err;
//console.log('UDP message sent to ' + host + ':' + port);
});
}
function createMessageListener() {
socket.on('message', function (message, remote) {
try {
message = JSON.parse(message);
//console.log("Recieved a " + message.m);
if (message.m == "awk"){
//We discovered a peer if they are not already a peer lets send an awk back
if (inPeerList(remote.address, remote.port) == false){
sendAWK(remote.address, remote.port);
addPeer(remote.address, remote.port);
}
}
if (message.m == "add"){
connectToClient(message.peer.split("p")[0], message.peer.split("p")[1]);
}
if (message.m == "put"){
//Client wants to store a value in the datastore
var keyHash = getSHA(message.key);
var value = message.value;
var key = message.key;
var storCount = message.count;
//Add it to our own data store (yes thats right - we store some of the data)
//console.log("Set " + key + " = " + value);
data[key] = value;
if (storCount < 8){
storCount++;
var cDist = 0;
var cPos = -1;
//Loop through our peers and forward the put request to the closest peer
for (var i = 0; i < peers.length; i++){
if (peers[i] != remote.address + "p" + remote.port){
if (getMeasure(getSHA(peers[i]), keyHash) > cDist){
cDist = getMeasure(getSHA(peers[i]), keyHash);
cPos = i;
}
}
}
if (cPos != -1){
sendMessage(peers[cPos].split("p")[0],peers[cPos].split("p")[1],{"m":"put","key":key,"value":value,"count":storCount});
}
}
}
if (message.m == "get"){
if (data[message.key] == undefined){
//We don't have it - forward it to the closest
var cDist = 0;
var cPos = -1;
//Loop through our peers and forward the put request to the closest peer
for (var i = 0; i < peers.length; i++){
if (peers[i] != remote.address + "p" + remote.port && peers[i] != message.dest){
if (getMeasure(getSHA(peers[i]), getSHA(message.key)) > cDist){
cDist = getMeasure(getSHA(peers[i]), getSHA(message.key));
cPos = i;
}
}
}
if (cPos != -1){
sendMessage(peers[cPos].split("p")[0],peers[cPos].split("p")[1],{"m":"get","key":message.key,"dest":message.dest});
}
} else {
//We have the information the user wants, lets return it
//Find the closest peer to the destination peer
var cDist = 0;
var cPos = -1;
for (var i = 0; i < peers.length; i++){
if (getMeasure(getSHA(peers[i]),getSHA(message.dest)) > cDist){
cDist = getMeasure(getSHA(peers[i]),getSHA(message.dest));
cPos = i;
}
}
if (cPos != -1){
sendMessage(peers[cPos].split("p")[0],peers[cPos].split("p")[1],{"m":"dat","key":message.key, "value":data[message.key],"dest":message.dest});
}
}
}
if (message.m == "dat"){
//console.log("Recevied key: " + message.key);
io.sockets.emit('message', {"key":message.key, "value": message.value});
data[message.key] = message.value;
if (message.dest != socket.stun.public.host + "p" + socket.stun.public.port){
var cDist = 0;
var cPos = -1;
for (var i = 0; i < peers.length; i++){
if (getMeasure(getSHA(peers[i]),getSHA(message.dest)) > cDist){
cDist = getMeasure(getSHA(peers[i]),getSHA(message.dest));
cPos = i;
}
}
if (cPos != -1){
sendMessage(peers[cPos].split("p")[0],peers[cPos].split("p")[1],{"m":"dat","key":message.key, "value":data[message.key],"dest":message.dest});
}
} else {
//console.log("We got a message back!!!!!!!!! OMG!!!!!!!! It worked!!!!!!");
}
}
if (message.m == "req"){
for (var i = 0; i < peers.length; i++){
if (peers[i] != message.peer){
//console.log("Sending ADD");
sendADD(peers[i].split("p")[0],peers[i].split("p")[1],message.peer);
sendADD( message.peer.split("p")[0],message.peer.split("p")[1], peers[i]);
}
}
}
if (message.m == "pin"){
sendPON(remote.address, remote.port);
}
if (message.m == "pon"){
//Remove peer from the to be deleted list
peersToBeDeleted.splice(peersToBeDeleted.indexOf(remote.address + "p" + remote.port), 1);
//console.log("Peers:" + peers.length);
}
} catch (e){
//console.log(e);
}
});
}
var MAX_PEERS = 16;
function addPeer(host, port){
if (peers.length < MAX_PEERS){
console.log("New Peer: " + getSHA(host + "p" + port));
peers.push(host + "p" + port);
} else {
//We already have enough peers but lets check if they are closer than any other of our peers
//If they are we no longer need that peer in our list and we can remove them
var smallestMatchAmount = 0;
var smallestMatch = -1;
var distance = 0;
//Loop through all the peers
for (var i = 0; i < peers.length; i++){
distance = getMeasure(myPeerID, getSHA(peers[i]));
if (distance < smallestMatchAmount){
smallestMatchAmount = distance;
smallestMatch = i;
}
}
//Check if our new peer is closer than our furthest peer
if (getMeasure(myPeerID, getSHA(host + "p" + port)) > smallestMatchAmount){
//New peer is closer than one of our other peers, lets remove the other peer from the list
peers[smallestMatch] = host + "p" + port;
}
}
//Lets send our new peer to the closest of our other peers
var matchDist = 0;
var matchDistID = -1;
var distance = 0;
for (var i = 0; i < peers.length; i++){
if (peers[i] != host + "p" + port){
distance = getMeasure(getSHA(peers[i]),getSHA(host + "p" + port));
if (distance > matchDist){
matchDist = distance;
matchDistID = i;
}
}
}
//Tell the pair to connect to each other
if (matchDistID != -1){
sendADD(peers[matchDistID].split("p")[0],peers[matchDistID].split("p")[1],host + "p" + port);
sendADD(host, port, peers[i]);
}
}
function getMeasure(a,b){
return 1 - clj_fuzzy.metrics.jaccard(a, b);
}
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
app.listen(8123);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.on('connecttopeer', function (data){
connectToClient(data.host, data.port);
});
socket.on('sendmessage', function (data){
for (var i = 0; i < peers.length; i++){
sendPUT(peers[i].split("p")[0],peers[i].split("p")[1],data.user, data.message);
}
});
socket.on('getmessages', function (data){
for (var i = 0; i < peers.length; i++){
sendGET(peers[i].split("p")[0],peers[i].split("p")[1],data.username);
}
});
});