Skip to content
Browse files

Initial commit of support.js, which adds support server functionality

  • Loading branch information...
1 parent 892fc96 commit c6f7de6034690372a32194f0c0368533219783f6 @sridatta sridatta committed Aug 10, 2011
Showing with 97 additions and 0 deletions.
  1. +97 −0 lib/support.js
View
97 lib/support.js
@@ -0,0 +1,97 @@
+var net = require('net');
+var nowUtil = require('./nowUtil').nowUtil;
+
+exports.initialize = function(nowjs){
+ var fn = require('./function').init(nowjs);
+
+ function Support(host, port){
+ this.socket = net.createConnection(port, host);
+ this._attachListeners();
+ this.socket.setNoDelay();
+ this.availableFunctions = {};
+ }
+
+ Support.prototype._startHandshake = function(){
+ this.socket.write(JSON.stringify({type:'new'}));
+ }
+
+ Support.prototype._attachListeners = function(){
+ var self = this;
+ this.socket.on('connect', function(){self._startHandshake()});
+ this.socket.on('data', function(message){self._handleMessage(message)});
+ }
+
+ Support.prototype.getRemoteFunction = function(fn) {
+ var self = this;
+ return function(){
+ var args = [];
+ for (var i = 1, ii = arguments.length; i < ii; i++) {
+ args[i-1] = arguments[i];
+ }
+ for (i = 0, ii = args.length; i < ii; i++) {
+ if (typeof args[i] === 'function') {
+ closureId = 'closure_' + args[i].name + '_' + nowUtil.generateRandomString();
+ nowjs.closures[closureId] = args[i];
+ args[i] = {fqn: closureId};
+ setTimeout(function () {
+ nowjs.closures[closureId] = nowUtil.noop;
+ }, nowjs.options.closureTimeout);
+ }
+ }
+
+ self.socket.write(JSON.stringify({type: 'rfc',fqn:fn, args:args}));
+ }
+ }
+
+ Support.prototype._handleMessage = function(message){
+ message = JSON.parse(message.toString());
+ switch(message.type){
+ case "usercall":
+ var user = nowjs.users[message.id];
+ if (user && message.serverId !== nowjs.serverId) {
+ args = message.args;
+ // Convert any remote function stubs into remote functions
+ for (i = 0, ll = args.length; i < ll; i++) {
+ if (nowUtil.hasProperty(args[i], 'fqn')) {
+ obj = {host: message.host, port:message.port, fqn: args[i].fqn};
+ args[i] = fn.closurecall.bind(obj);
+ }
+ }
+ theFunction = user.get(message.fqn);
+ theFunction.apply({}, args);
+ }
+ break;
+ case "multicall":
+ group = nowjs.getGroup(message.groupName);
+ //group = nowUtil.clone(group, {excludes: message.excludes});
+ if (group) {
+ args = message.args;
+ // Convert any remote function stubs into remote functions
+ for (i = 0, ll = args.length; i < ll; i++) {
+ if (nowUtil.hasProperty(args[i], 'fqn')) {
+ obj = {socket: this.socket, host: message.host, port:message.port, fqn: args[i].fqn};
+ args[i] = fn.closurecall.bind(obj);
+ }
+ }
+ group = nowUtil.clone(group, {fqn: message.fqn});
+ nowjs.emit('multicall', group, args);
+ }
+ break;
+ case 'closurecall':
+ var args = message.args;
+ // Convert any remote function stubs into remote functions
+ for (i = 0, ll = args.length; i < ll; i++) {
+ if (nowUtil.hasProperty(args[i], 'fqn')) {
+ obj = {socket: this.socket, host: message.host, port:message.port, fqn: args[i].fqn};
+ args[i] = fn.closurecall.bind(obj);
+ }
+ }
+ theFunction = nowjs.closures[message.fqn];
+ theFunction.apply({}, args);
+ break;
+ }
+ }
+
+
+ return Support;
+}

0 comments on commit c6f7de6

Please sign in to comment.
Something went wrong with that request. Please try again.