-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpervasive_server_ws.js
45 lines (35 loc) · 1.17 KB
/
pervasive_server_ws.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
var sys = require('sys');
var http = require('http');
var Resource = require('./lib/resource').Resource;
var setup = require('./lib/setup_ws');
var commander = setup.createManager();
// this server handles POST request
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
// this should be a request object, which handles the params
var reqData = "";
req.addListener('data', function(data) {
reqData += data.toString();
}).addListener('end', function() {
process_resource(reqData);
res.end("New ad received!");
});
});
server.listen(8080);
function process_resource(resource_data) {
// First, extract the different parts of the request
var res_parts = resource_data.split("&");
var cur_res = new Resource();
// add parameters to the resource
for (var m in res_parts) {
if (res_parts[m].match(/^topic=/i)) {
var topic_part = res_parts[m].replace(/^topic=/i, "");
cur_res.addTopic(topic_part);
}
if (res_parts[m].match(/^file_name=/i)) {
var fname_part = res_parts[m].replace(/^file_name=/i, "");
cur_res.addFileName(fname_part);
}
}
commander.dispatchResource(cur_res);
}