Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

HTTP web server & other enhancements #31

Merged
merged 5 commits into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ The folder called `test` contains a number of Mocha tests.
1. Run `npm test` to execute a series of tests (all the `fbptestxx.js` tests in sequence).
2. Alternatively, you can directly execute `node.exe node_modules/mocha/bin/mocha --recursive --require test/test_helper.js` in case you need to adjust the path to Node's binary or pass further parameters to Mocha.

# Testing Sample HTTP Server

Run `node examples/httpserver/fbphttpserver.js`, which is a simple HTTP server which is similar to the one in the sample at: http://blog.modulus.io/build-your-first-http-server-in-nodejs

NOTE: The HTTP server components are currently all custom components, based on the components used in the simple web socket chat server described below.

# Testing Simple Web Socket Chat Server

Run `node examples/websocketchat/fbptestwschat.js`, which is a simple web socket chat server which responds to any request by broadcasting it to all connected clients. It is similar to the chat sample at: http://socket.io/get-started/chat/ except for serving the client HTML.
Expand Down
4 changes: 2 additions & 2 deletions components/collate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module.exports = function collate() {
var outport = this.openOutputPort('OUT');

var ctlfieldsP = ctlfields.receive();
var fields = ctlfieldsP.contents.split(',').map(function(str) { return parseInt(str); });
this.dropIP(ctlfieldsP);

var fields = ctlfieldsP.contents.split(',').map(function(str) { return parseInt(str); });
var totalFieldLength = fields.reduce(function(acc, n) { return acc + n; }, 0);

var portCount = inportArray.length;
Expand Down Expand Up @@ -40,4 +40,4 @@ module.exports = function collate() {
portCount--;
}
}
}
}
64 changes: 64 additions & 0 deletions components/httpserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

var IP = require('../core/IP')
, http = require('http');

module.exports = function httpserver(runtime) {
var inport = this.openInputPort('PORTNO');
var outport = this.openOutputPort('OUT');

var ip = inport.receive();
var portno = ip.contents;
var server = http.createServer(handleServerRequest);

runtime.runAsyncCallback(genListenFun(runtime, server, portno, this));

while (true) {
var result = runtime.runAsyncCallback(genReceiveFun(runtime, server, portno, this));

for (var i=0; i<result.length; ++i) {
var r = result[i];
outport.send(this.createIPBracket(IP.OPEN));
outport.send(this.createIP(r[0]));
outport.send(this.createIP(r[1]));
outport.send(this.createIPBracket(IP.CLOSE));
}
}

wss.close();
this.dropIP(ip);
}

// TODO move globals into function:
var rx = null;

var wq = [];

function handleServerRequest(req, res) {
wq.push([req, res]);

if (!!rx) {
var q = wq;
wq = [];
rx(q);
}
}

function genListenFun(runtime, server, portno, proc) {
return function (done) {
// In next tick (TODO use process.nextTick() instead):
setTimeout(function() {
done();
}, 0);

server.listen(portno, function() { console.log('server listen cb'); });
};
}

function genReceiveFun(runtime, server, portno, proc) {
return function (done) {
rx = function(q) {
done(q);
}
};
}
16 changes: 16 additions & 0 deletions examples/httpserver/fbphttpserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var fbp = require('../..');

// --- define network ---
var network = new fbp.Network();

var receiver = network.defProc(require('../../components/httpserver'));
var myproc = network.defProc(require('./myproc'));
var send = network.defProc(require('./myresponse'));

network.initialize(receiver, 'PORTNO', '8080');
network.connect(receiver, 'OUT', myproc, 'IN', 6);
network.connect(myproc, 'OUT', send, 'IN', 6);

// --- run ---
var fiberRuntime = new fbp.FiberRuntime();
network.run(fiberRuntime, { trace: true });
22 changes: 22 additions & 0 deletions examples/httpserver/myproc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

module.exports = function myproc() {
var inport = this.openInputPort('IN');
var outport = this.openOutputPort('OUT');
while (true) {
var ip = inport.receive();
if (ip == null) {
break;
}
// not null, so this IP was open bracket
outport.send(ip); //send it on
ip = inport.receive(); // request
var req = ip.contents;
this.dropIP(ip);
outport.send(this.createIP('Response from URL: ' + req.url + '\n'));
ip = inport.receive(); // res
outport.send(ip); // send it on
ip = inport.receive(); // close bracket
outport.send(ip); // send it on...
}
}
25 changes: 25 additions & 0 deletions examples/httpserver/myresponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var IP = require('../../core/IP');

module.exports = function myresp() {
var ip;
var inport = this.openInputPort('IN');

while (true) {
ip = inport.receive(); // shd be open bracket
if (ip === null) {
break;
}
this.dropIP(ip);
ip = inport.receive(); // shd be response string
var message = ip.contents;
this.dropIP(ip);
ip = inport.receive(); // shd be res object
var res = ip.contents;
this.dropIP(ip);
res.end(message);
ip = inport.receive(); // shd be close bracket
this.dropIP(ip);
}
}
1 change: 1 addition & 0 deletions examples/websocketchat/wsbroadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function wsbroadcast() {

ip = wssin.receive(); // shd be wss
var wss = ip.contents;
this.dropIP(ip);

while (true) {
ip = inport.receive(); // shd be open bracket
Expand Down