Skip to content

Commit

Permalink
Initial commit of source code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Eisenberg committed Nov 18, 2012
0 parents commit 28ac4c3
Show file tree
Hide file tree
Showing 1,415 changed files with 366,378 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .scripted
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
mark_occurrences : {
interval : 0,
disable : false,
retain : true
}
}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
This is the source code for the UBC CS Alumni lecture: [What is this Cloud Thing?](https://www.cs.ubc.ca/event/2012/11/alumni-lecture-andrew-eisenberg-what-cloud-thing)

This code comes with two apps, rabbit-producer and rabbit-consumer. These applications each run a simple node server to and communicate using [rabbitmq](http://www.rabbitmq.com/).

To run:

1. register for an account at http://cloudfoundry.com
2. get the [vmc command line tool](http://docs.cloudfoundry.com/tools/vmc/installing-vmc.html)
3. vmc target http://api.cloudfoundry.com
4. vmc login (your user name and passwprd)
5. cd rabbit-producer
6. vmc push rabbit-producer --runtime node08 (choose your own name)
7. Use the default options and bind a rabbitmq service
8. cd ../rabbit-consumer
6. vmc push rabbit-consumer --runtime node08 (choose your own name)
7. Use the default options and bind to the same rabbitmq service defined in step 7

You should now have 2 node server running on cloud foundry. You can send messages from the producer to the consumer.
102 changes: 102 additions & 0 deletions rabbit-consumer/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*global require console __dirname process */
/*jslint node:true es5:true */

var WebSocketServer = require('ws').Server,
http = require('http'),
express = require('express'),
amqp = require('amqp'),
htmlEscape = require('sanitizer/sanitizer').escape,
sockjs = require('sockjs');

var path = process.env.PWD + '/public';
// use express module as a static file server
var app = express();

// use sockjs library to ensure compatibility across broswers and servers w/o websocket support
var wsServer = sockjs.createServer({ jsessionid: true });
var server = http.createServer(app);

app.configure(function() {
app.use(app.router);
app.use(express.static(path));
app.use(express.directory(path));
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
wsServer.installHandlers(server, { prefix: '/msg' });
});

// Cloud Foundry-specific
server.listen(process.env.VCAP_APP_PORT || 8083);
function rabbitUrl() {
if (process.env.VCAP_SERVICES) {
// Cloud Foundry-specific
var conf = JSON.parse(process.env.VCAP_SERVICES);
return conf['rabbitmq-2.4'][0].credentials.url;
} else {
return "amqp://localhost";
}
}

///////////////
// Socket stuff
///////////////
var socketConns = [];
function addSocketConn(ws) {
socketConns.push(ws);
}
function removeSocketConn(ws) {
for (var i = 0; i < socketConns.length; i++) {
if (socketConns[i] === ws) {
socketConns.slice(i, 1);
}
}
}

function writeMessage(msg) {
for (var i = 0; i < socketConns.length; i++) {
// send the message to the client over the socket
socketConns[i].write(htmlEscape(msg.body));
}
}

// listen for new socket connections
wsServer.on('connection', function(ws) {
console.log('WS connection received: %s.', JSON.stringify(ws.address));
addSocketConn(ws);
ws.on('close', function() {
removeSocketConn(ws);
});
});


///////////////
// Queue stuff
///////////////

console.log("Starting ... AMQP URL: " + rabbitUrl());
// create a new connection
/** @type Stream */
var conn = amqp.createConnection({
url: rabbitUrl()
});

function setup() {
var exchange = conn.exchange('cf-exchange', {
'type': 'fanout',
durable: false
}, function() {
var queue = conn.queue('cf-queue', {
durable: false
}, function() {
queue.subscribe(function(msg) {
// pass the message from the queue to the clients
// through the websockets
writeMessage(msg);
});
queue.bind(exchange.name, 'cf-queue');
});
});
}
conn.on('ready', setup);
1 change: 1 addition & 0 deletions rabbit-consumer/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adapted from https://github.com/andregoncalves/twitter-nodejs-websocket
1 change: 1 addition & 0 deletions rabbit-consumer/node_modules/.bin/express

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rabbit-consumer/node_modules/.bin/node-inspector

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rabbit-consumer/node_modules/.bin/wscat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rabbit-consumer/node_modules/amqp/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions rabbit-consumer/node_modules/amqp/History.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions rabbit-consumer/node_modules/amqp/LICENSE-MIT

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions rabbit-consumer/node_modules/amqp/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 28ac4c3

Please sign in to comment.