Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 3c49a03493231ea84ab0fb80f2300dda5f618dd2 @namjul committed Apr 30, 2011
114 app.js
@@ -0,0 +1,114 @@
+
+///////////////////////////////////////////
+// SETUP Dependencies //
+///////////////////////////////////////////
+var connect = require('connect'),
+ express = require('express'),
+ mongoStore = require('connect-mongodb'),
+ model = require('./models/model-native-driver').db,
+ util = require('util'),
+ port = (process.env.PORT || 8081);
+
+
+///////////////////////////////////////////
+// SETUP Express //
+///////////////////////////////////////////
+var app = module.exports = express.createServer();
+
+
+app.configure(function(){
+ //views is the default folder already
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.bodyParser());
+ //app.use(express.methodOverride());
+ app.use(express.cookieParser());
+
+ app.use(express.session({ store: mongoStore(app.set('db-uri')), secret: 'keyboard cat'}));
+ //app.use(express.logger());
+ app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }));
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('test', function() {
+ app.set('db-uri', 'mongodb://localhost/sharedesk-test');
+ app.model = new model('sharedesk-test', function() {});
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.set('db-uri', 'mongodb://localhost/sharedesk-development');
+ app.model = new model('sharedesk-development', function() {});
+});
+
+app.configure('production', function(){
+ app.set('db-uri', 'mongodb://localhost/sharedesk-production');
+ app.model = new model('sharedesk-production', function() {});
+});
+
+
+///////////////////////////////////////////
+// ERROR Handling //
+///////////////////////////////////////////
+
+function NotFound(msg) {
+ this.name = 'NotFound';
+ Error.call(this, msg);
+ Error.captureStackTrace(this, arguments.callee);
+}
+
+//equals to NotFound.prototype.__proto__ = Error.prototype; i think ^^
+util.inherits(NotFound, Error);
+
+app.get('/404', function(req, res) {
+ throw new NotFound;
+});
+
+app.get('/500', function(req, res) {
+ throw new Error('An expected error');
+});
+
+app.get('/bad', function(req, res) {
+ unknownMethod();
+});
+
+app.error(function(err, req, res, next) {
+ if (err instanceof NotFound) {
+ res.render('404', { status: 404 });
+ } else {
+ next(err);
+ }
+});
+
+///////////////////////////////////////////
+// ROUTES Controller //
+///////////////////////////////////////////
+
+app.get('/', function(req, res){
+ res.render('home', {
+ layout: false
+ });
+});
+
+app.get('/:id', function(req, res){
+ res.render('index', {
+ locals: {pageTitle: ('shareDesk - ' + req.params.id) }
+ });
+});
+
+
+
+// start websockets controller
+require('./controllers/websockets')(app);
+
+
+
+// Only listen on $ node app.js
+if (!module.parent) {
+ app.listen(port);
+ console.log("ShareDesk server listening on port %d", app.address().port);
+}
+
+
+
@@ -0,0 +1,5 @@
+exports.database = {
+ type: 'mongodb',
+ hostname: 'localhost',
+ port: 27017
+};
@@ -0,0 +1,82 @@
+
+///////////////////////////////////////////
+// socket.io routing //
+///////////////////////////////////////////
+
+var io = require('socket.io'),
+ util = require('util'),
+ rooms = require('../logics/rooms.js');
+
+module.exports = function(app) {
+
+ var socket = io.listen(app),
+ model = app.model;
+
+ socket.on('connection', function(client) {
+
+ // new client is here!
+
+ client.on('message', function( message ) {
+
+ console.log("action: " + message.action + " -- data: " + util.inspect(message.data) );
+
+ if (!message.action) return;
+
+ switch (message.action) {
+ case 'initializeMe':
+ initClient(client);
+ break;
+
+ case 'joinRoom':
+ joinRoom(client, message.data, function(clients) {
+ client.send( { action: 'roomAccept', data: '' } );
+ });
+ break;
+
+ default:
+ console.log('unknown action');
+ break;
+ }
+ });
+
+ client.on('disconnect', function() {
+ //leaveRoom(client);
+ });
+
+ //tell all others that someone has connected
+ //client.broadcast('someone has connected');
+ });
+
+
+ //--------------
+ // Some Functions
+ //--------------
+
+ function initClient (client) {
+
+ getRoom(client, function(room) {
+
+ //Send client all the files from the room
+ model.getAllFiles(room, function(err, files) {
+ client.send({ action: 'initFiles', data: files});
+ });
+
+ });
+ }
+
+ function joinRoom (client, room, successFunction) {
+ var msg = {};
+ msg.action = 'join-announce';
+ msg.data = { sid: client.sessionId, user_name: client.user_name };
+
+ rooms.add_to_room_and_announce(client, room, msg);
+ successFunction();
+ }
+
+ function getRoom( client , callback ) {
+ room = rooms.get_room( client );
+ //console.log( 'client: ' + client.sessionId + " is in " + room);
+ callback(room);
+ }
+
+}
@@ -0,0 +1,91 @@
+var Collection = require('./collection').Collection,
+ Cursor = require('./cursor').Cursor,
+ DbCommand = require('./commands/db_command').DbCommand;
+
+var Admin = exports.Admin = function(db) {
+ this.db = db;
+};
+
+Admin.prototype.serverInfo = function(callback) {
+ var self = this;
+ var command = {buildinfo:1};
+ var databaseName = self.db.databaseName;
+ self.db.databaseName = 'admin';
+ this.db.executeDbCommand(command, function(err, doc) {
+ if(err != null) return callback(err, null);
+ return callback(null, doc.documents[0]);
+ });
+ // Ensure change before event loop executes
+ self.db.databaseName = databaseName;
+}
+
+Admin.prototype.profilingLevel = function(callback) {
+ var command = {profile:-1};
+ this.db.executeDbCommand(command, function(err, doc) {
+ doc = doc.documents[0];
+ if(err == null && (doc.ok == 1 || doc.was.constructor == Numeric)) {
+ var was = doc.was;
+ if(was == 0) {
+ callback(null, "off");
+ } else if(was == 1) {
+ callback(null, "slow_only");
+ } else if(was == 2) {
+ callback(null, "all");
+ } else {
+ callback(new Error("Error: illegal profiling level value " + was), null);
+ }
+ } else {
+ err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
+ }
+ });
+};
+
+Admin.prototype.setProfilingLevel = function(level, callback) {
+ var command = {};
+ var profile = 0;
+ if(level == "off") {
+ profile = 0;
+ } else if(level == "slow_only") {
+ profile = 1;
+ } else if(level == "all") {
+ profile = 2;
+ } else {
+ callback(new Error("Error: illegal profiling level value " + level));
+ return;
+ }
+ command['profile'] = profile;
+
+ this.db.executeDbCommand(command, function(err, doc) {
+ doc = doc.documents[0];
+ if(err == null && (doc.ok == 1 || doc.was.constructor == Numeric)) {
+ callback(null, level);
+ } else {
+ err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
+ }
+ });
+};
+
+Admin.prototype.profilingInfo = function(callback) {
+ new Cursor(this.db, new Collection(this.db, DbCommand.SYSTEM_PROFILE_COLLECTION), {}).toArray(function(err, items) {
+ callback(err, items);
+ });
+};
+
+Admin.prototype.validatCollection = function(collectionName, callback) {
+ var command = {validate: collectionName};
+ this.db.executeDbCommand(command, function(err, doc) {
+ doc = doc.documents[0];
+
+ if(err != null) {
+ callback(err, null);
+ } else if(doc.ok == 0) {
+ callback(new Error("Error with validate command"), null);
+ } else if(doc.result.constructor != String) {
+ callback(new Error("Error with validation data"), null);
+ } else if(doc.result.match(/exception|corrupt/) != null) {
+ callback(new Error("Error: invalid collection " + collectionName), null);
+ } else {
+ callback(null, doc);
+ }
+ });
+};
Oops, something went wrong.

0 comments on commit 3c49a03

Please sign in to comment.