-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
119 lines (102 loc) · 2.86 KB
/
server.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
/*
JackpotJS v0.0.1-alpha
*/
var _ = require('underscore'),
net = require('net'),
fs = require('fs'),
path = require('path'),
GUIServer = require('http').createServer(HTTPHandler),
static = require('node-static');
var JackpotConnection = require('./lib/JackpotConnection.js');
var JackpotQuery = require('./lib/JackpotQuery.js');
// ************ GUI Server *****************
var staticFile = new (static.Server)('./GUI');
GUIServer.listen(80, "127.0.0.1");
function HTTPHandler(req, res){
req.on('end', function(){
staticFile.serve(req, res);
});
}
// $$$$$$$$$$$$$ JackpotCore $$$$$$$$$$$$$$
//list of all known nodes
var nodeList = [];
//this server's shared directories
var shared = [];
var JackpotRequest = {
NODELIST : 0,
FILE : 1,
SEARCH : 2
};
var JACKPOT_PORT = 777;
//listen on port 777 for nodelist and file requests
net.createServer(function(c){
readJSON(c, function(obj){
switch(obj.requestType){
case JackpotRequest.NODELIST:
handleNodeListRequest(c);
break;
case JackpotRequest.FILE:
sendFile(c, obj.path);
break;
case JackpotRequest.SEARCH:
handleSearchRequest(c, obj);
break;
}
});
}).listen(JACKPOT_PORT);
function handleNodeListRequest(socket){
//send back your node list
socket.end(JSON.stringify(nodeList));
//add requestor to node list if not already present
nodeList[socket.remoteAddress] = true;
}
function sendFile(socket, filepath){
var directory = path.dirname(filepath);
if (shared.indexOf(directory) == -1){
//no way, buddy.
socket.end();
return false;
}
//set up file --> socket pipe
var readStream = fs.createReadStream(filepath);
readStream.pipe(socket);
}
function requestNodeList(host){
var conn = new JackpotConnection(host);
conn.setRequest(JackpotRequest.NODELIST);
conn.setCallback(function(newNodes){
nodeList = _.union(nodeList, newNodes);
});
conn.execute();
}
function requestFile(host, path, dest){
var request = {
type: request.FILE,
path: path
};
var client = net.connect(JACKPOT_PORT, host, function(){
//set up socket --> file pipe
var writeStream = fs.createWriteStream(dest);
client.pipe(writeStream);
//send the request
client.end(JSON.stringify(request));
});
}
function handleSearchRequest(socket, search_obj){
//search yourself
var result = searchSelf(query);
if (result){
socket.write(JSON.stringify(result));
socket.write(STREAM_SEPARATOR);
}
//search everyone else
var JPQuery = new JackpotQuery(search_obj.query, function(result){
socket.write(JSON.stringify(result));
socket.write(STREAM_SEPARATOR);
});
//don't search the guy you got the request from, duh.
search_obj.exclude.push(socket.remoteAddress);
JPQuery.setExclude(search_obj.exclude);
//do it.
JPQuery.execute();
}