-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
322 lines (273 loc) · 8.62 KB
/
main.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
var http = require('http');
var https = require('https');
var sys = require('util');
var fs = require('fs');
require('./bus.js');
var busCounter = 1;
var eventUrl = '/event';
var postUrl = '/post';
var busExistsUrl = '/exists' ;
var createUrl = '/create';
var host = "event.ris.dev.muze.nl";
var longPollTimeout = 5000;
var securePassword = 'paashaas';
var eventPort = 7000;
var adminPort = 7001;
var busses = {};
var httpsOptions = {
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./cert.pem')
};
//create the http server
var varEventServer = http.createServer(function(request, response) {
console.log( 'listen:'+request.url );
//parse the requested url
var requestUrl = parseRequestUrl(request.url);
//ROUTING!
//events
if(requestUrl['location'] == eventUrl){
//check if params received
if(typeof requestUrl['params'] != 'undefined'){
//check if ID was given
if(typeof requestUrl['params']['id'] != 'undefined'){
//if an eventstream was requested
//if (request.headers.accept && request.headers.accept == 'text/event-stream') {
response.writeHead(200, {
'Content-Type' : 'text/event-stream',
'Access-Control-Allow-Origin': '*'
});
//longpolling
var longPoll = false;
if(typeof requestUrl['params']['longpoll'] != 'undefined'){
if(requestUrl['params']['longpoll'] == 1){
longPoll = true;
console.log('request received with longpoll enabled...');
setTimeout(function(){
console.log('setTimeout function called, longPollTimeout has probably been reached....');
response.end();
},longPollTimeout);
}
}
console.log('client event stream request initiated');
handleEventRequest(requestUrl['params']['id'],request,response,longPoll);
//} else {
// console.log('no proper headers received for eventstream request, not sending any events');
//}
} else {
console.log('no id received for a bus, not sending any events');
}
} else {
console.log('no parameters received for an eventstream request');
}
}
if(requestUrl['location'] == busExistsUrl){
console.log('busExists received');
if(typeof requestUrl['params'] != 'undefined'){
console.log(requestUrl);
if(typeof requestUrl['params']['id'] != 'undefined'){
if(typeof busses[requestUrl['params']['id']] != 'undefined'){
var busExistsResponse = {
id: requestUrl['params']['id'],
exists: true
};
response.write(JSON.stringify(busExistsResponse));
response.end();
} else {
var busExistsResponse = {
id: requestUrl['params']['id'],
exists: false
};
response.write(JSON.stringify(busExistsResponse));
response.end();
}
} else {
console.log('no id received as param');
}
} else {
console.log('no params given');
}
}
//tempshiturl
if(request.url == '/test'){
console.log('testUrl requested');
//dostuff return the testpage to show the correct bus
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(fs.readFileSync(__dirname + '/sse-node.html'));
console.log('sse-node sent to client');
response.end();
}
if(request.url == '/testPost'){
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(fs.readFileSync(__dirname + '/postform.html'));
console.log('form sent to client');
response.end();
}
if(request.url == '/testCreate'){
console.log('createShow url requested');
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(fs.readFileSync(__dirname + '/buscreate.html'));
response.end();
}
}).listen(eventPort, host);
var adminServer = http.createServer(function(request,response){
console.log('admin:'+request.url);
//everything that goes to the adminService should be a POST and use ssl
//start receiving the post request
var body = "";
//data receival
request.on('data', function (chunk){
console.log('starting post receival');
body += chunk;
});
//end of data receival start doing things
request.on('end', function(){
console.log(request.url);
console.log('received a post');
console.log('data: ' + body);
//post has been done, run handlePost(request,response)
if(request.url == postUrl){
//if something is posted to me to send over the bus!
console.log('posturl requested');
console.log('posturl body: '+body);
handlePost(postDataToAssocArray(body),response);
response.writeHead(200);
response.end(JSON.stringify(['ok']));
}
if(request.url == createUrl){
//create a new bus!
busId = handleCreate(postDataToAssocArray(body));
var returnValue = { id: busId };
// returnValue["id"] = busId;
//response.writeHead(200,{'Content-Type': 'text/html', 'z-test-header' : busId });
response.write(JSON.stringify(returnValue));
console.log('ID: '+busId);
//response.end('ID: '+busId+"\n");
response.end();
}
});
//response.writeHead(200);
//response.end();
}).listen(adminPort, host);
//translates a string like key1=value1&key2=value2 etc. to an associative array
function postDataToAssocArray(postData){
console.log('translating: ' + postData);
var returnValue = {};
if(typeof postData != 'undefined'){
var parts = [];
if(postData.indexOf('&') != -1){
parts = postData.split('&');
} else {
parts.push(postData);
}
for(var key in parts){
var keyValue = parts[key].split("=");
if ( keyValue[1] ) {
//decode the html characters and replace the +'s with spaces
returnValue[keyValue[0]] = keyValue[1].replace(/\+/gi,' ');
}
}
console.log(returnValue);
} else {
returnValue.push('');
}
return returnValue;
}
function handleCreate(postData){
if(typeof(postData['password'] != 'undefined')){
console.log('creating bus with pasword: ' + postData['password']);
busId = createBus(postData['password']);
} else {
console.log('no password received, not creating a bus.');
}
return busId;
}
function handlePost(postData,response){
//check for ID
if(typeof postData['id'] != 'undefined'){
//check if bus exists
if(typeof busses[postData['id']] != 'undefined'){
//check if the password is correct.
if(postData['password'] == busses[postData['id']].password){
console.log('correct password received! Sending message.');
if(postData['message'] != 'undefined'){
busses[postData['id']].writeToAllSockets(postData['message']);
} else {
console.log('No message received, not sending anything.');
}
} else {
//incorrect password
console.log('incorrect password received, not sending anything.');
}
} else {
//invalid bus
console.log('invalid ID received, bus does not exist.');
response.write(JSON.stringify({error: { code: 1, message: 'Invalid id received, bus does not exist.'}}));
response.end();
}
} else {
//no id received
console.log('no id received with the post request, specify which bus.');
}
}
//add a new bus and return the Id
function createBus(password){
busId = busCounter;
var bus = new Bus(busId,password);
busses[busId] = bus;
busCounter++;
return busId;
}
var longPollConnections = [];
function handleEventRequest(id,request,response, longPoll){
console.log('eventRequest for id: ' + id);
if(typeof busses[id] != 'undefined'){
busses[id].addConnection(request,response,longPoll);
} else {
//bus does not exist
console.log('invalid bus requested');
errorResponse = {
error: {
code: 1,
message: 'Invalid bus requested, bus does not exist (yet).'
}
};
response.write(JSON.stringify(errorResponse));
if (longPoll) {
console.log('handleEventRequest received longpoll and the bus does not exist...');
response.end();
}
}
}
function parseRequestUrl(url){
var returnValue = {};
//break the url by ?
if(url.indexOf('?') != -1){
//there is a ? in the url, there are params
var urlLocation = url.split('?')[0];
var paramString = url.split('?')[1];
var params = {};
if(paramString.indexOf('&') != -1){
//there is more then one param
//split params by & to get the keyValue combinations
var keyValueCombinations = paramString.split('&');
//for every keyvalue combination add the key as a key in the array with the value as a value in the array
//by splitting on the =
for(var key in keyValueCombinations){
var keyValue = keyValueCombinations[key].split('=');
if ( keyValue[1] ) {
params[keyValue[0]] = keyValue[1];
}
}
}
else {
//there is a single parameter
params[paramString.split('=')[0]] = paramString.split('=')[1];
}
returnValue['location'] = urlLocation;
returnValue['params'] = params;
}
else {
returnValue['location'] = url;
}
return returnValue;
}