Skip to content

Commit

Permalink
create abstraction for a connected user
Browse files Browse the repository at this point in the history
  • Loading branch information
fjakobs committed Feb 3, 2011
1 parent b01dafe commit d83f636
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 39 deletions.
4 changes: 2 additions & 2 deletions server/cloud9/ext/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ sys.inherits(AuthPlugin, Plugin);

(function() {

this.command = function(message, client) {
this.command = function(message, client, user) {
if (message.command != "attach")
return false;

Expand All @@ -26,7 +26,7 @@ sys.inherits(AuthPlugin, Plugin);
}

client.send('{"type": "attached"}')
this.ide.execHook("connect");
this.ide.execHook("connect", client, user);
return true;
};

Expand Down
2 changes: 1 addition & 1 deletion server/cloud9/ext/debugger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sys.inherits(DebuggerPlugin, Plugin);
this.NODE_DEBUG_PORT = 5858;
this.CHROME_DEBUG_PORT = 9222;

this.command = function(message, client) {
this.command = function(message, client, user) {
var _self = this;

var cmd = (message.command || "").toLowerCase(),
Expand Down
2 changes: 1 addition & 1 deletion server/cloud9/ext/shell-git/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ sys.inherits(ShellGitPlugin, Plugin);
return this.extend(struct, map || {});
};

this.command = function(message) {
this.command = function(message, client, user) {
if (message.command != "git")
return false;

Expand Down
2 changes: 1 addition & 1 deletion server/cloud9/ext/shell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var ShellPlugin = module.exports = function(ide) {
sys.inherits(ShellPlugin, Plugin);

(function() {
this.command = function(message) {
this.command = function(message, client, user) {
if (!this[message.command])
return false;

Expand Down
2 changes: 1 addition & 1 deletion server/cloud9/ext/watcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sys.inherits(cloud9WatcherPlugin, Plugin);
return true;
};

this.command = function(message) {
this.command = function(message, client, user) {
var filename, that, subtype, files;

if (!message || message.command != "watcher")
Expand Down
77 changes: 45 additions & 32 deletions server/cloud9/ide.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
var jsDAV = require("jsdav"),
Async = require("async"),
User = require("./user"),
fs = require("fs"),
sys = require("sys"),
Path = require("path"),
Expand Down Expand Up @@ -61,7 +62,7 @@ module.exports = Ide = function(options, httpServer, exts) {
]
};

this.clients = [];
this.$users = {};
this.nodeCmd = process.argv[0];

this.registerExts(exts);
Expand Down Expand Up @@ -129,42 +130,53 @@ sys.inherits(Ide, EventEmitter);
});
};

this.addClientConnection = function(client, message) {
var _self = this;
this.clients[client.sessionId] = client;
this.onClientCountChange();

client.on("message", function(message) {
_self.onClientMessage(message, client);
});

client.on("disconnect", function() {
_self.execHook("disconnect");
delete _self.clients[client.sessionId];
_self.onClientCountChange();
});

if (message)
_self.onClientMessage(message, client);
this.addClientConnection = function(username, role, client, message) {
var permissions = ({
"owner" : User.OWNER_PERMISSIONS,
"collaborator": User.COLLABORATOR_PERMISSIONS,
"viritor" : User.VISITOR_PERMISSIONS
}[role]) || User.VISITOR_PERMISSIONS;

var user = this.$users[username];
if (user) {
user.setPermissions(permissions);
}
else {
user = this.$users[username] = new User(username, permissions);

var _self = this;
user.on("message", function(msg) {
_self.onUserMessage(msg.message, msg.client, msg,user);
});
user.on("disconnectClient", function(msg) {
_self.execHook("disconnect", msg.client, msg.user);
});
user.on("disconnectUser", function(user) {
delete _self.$users[user.name];
_self.onUserCountChange(Object.keys(_self.$users).length);
});

this.onUserCountChange();
}
user.addClientConnection(client, message);
};

this.onClientCountChange = function() {
this.emit("clientCountChange", Object.keys(this.clients).length);
this.onUserMessage = function(message, client, user) {
console.log(message);
this.execHook("command", message, client, user);
};

this.onClientMessage = function(message, client) {
try {
message = JSON.parse(message);
} catch (e) {
return this.error("Error parsing message: " + e + "\nmessage: " + message, 8);
}

this.execHook("command", message, client);

this.onUserCountChange = function() {
this.emit("userCountChange", Object.keys(this.$users).length);

// TODO remove
this.emit("clientCountChange", Object.keys(this.$users).length);
};

this.broadcast = function(msg) {
for (var id in this.clients)
this.clients[id].send(msg);
// TODO check permissions
for (var username in this.$users)
this.$users[username].broadcast(msg);
};

this.registerExts = function(exts) {
Expand All @@ -182,7 +194,7 @@ sys.inherits(Ide, EventEmitter);
return this.exts[name] || null;
};

this.execHook = function() {
this.execHook = function(hook) {
var ext, hooks,
args = Array.prototype.slice.call(arguments),
hook = args.shift().toLowerCase().replace(/^[\s]+/, "").replace(/[\s]+$/, "");
Expand All @@ -199,6 +211,7 @@ sys.inherits(Ide, EventEmitter);
// + sys.inspect(args), 9, args[0]);
};

// TODO remove
this.error = function(description, code, message, client) {
//console.log("Socket error: " + description, new Error().stack);
var sid = (message || {}).sid || -1;
Expand Down
2 changes: 1 addition & 1 deletion server/cloud9/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.main = function(options) {
};
var socketIo = IO.listen(server, socketOptions);
socketIo.on("connection", function(client) {
ide.addClientConnection(client, null);
ide.addClientConnection("fabian", "owner", client, null);
});

var name = projectDir.split("/").pop();
Expand Down
108 changes: 108 additions & 0 deletions server/cloud9/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var sys = require("sys");

var User = function (name, permissions) {
this.name = name;
this.permissions = permissions;
this.clients = [];
};

sys.inherits(User, process.EventEmitter);

User.OWNER_PERMISSIONS = {
"read": 1,
"write": 1,
"debugger": 1,
"shell": 1,
"git": 1,
"watcher": 1
};

User.COLLABORATOR_PERMISSIONS = {
"read": 1,
"write": 1,
"debugger": 1,
"shell": 1,
"watcher": 1
};

User.VISITOR_PERMISSIONS = {
"read": 1
};

(function() {

this.setPermissions = function(permissions) {
this.permissions = permissions;
};

this.addClientConnection = function(client, message) {
var id = client.sessionId;
if (this.clients[id] === client)
return;

this.clients[id] = client;
this.onClientCountChange();

var _self = this;
client.on("message", function(message) {
_self.onClientMessage(message, client);
});

client.on("disconnect", function() {
_self.emit("disconnectClient", {
user: _self,
client: client
});
delete _self.clients[client.sessionId];
_self.onClientCountChange();
});

if (message)
_self.onClientMessage(message, client);
};

this.onClientMessage = function(message, client) {
try {
message = JSON.parse(message);
} catch (e) {
return this.error("Error parsing message: " + e + "\nmessage: " + message, 8);
}

this.emit("message", {
message: message,
user: this,
client: client
});
};

this.onClientCountChange = function() {
var count = Object.keys(this.clients).length;
this.emit("clientCountChange", count);

if (count === 0)
this.emit("disconnectUser", this);
};

this.error = function(description, code, message, client) {
//console.log("Socket error: " + description, new Error().stack);
var sid = (message || {}).sid || -1;
var error = JSON.stringify({
"type": "error",
"sid": sid,
"code": code,
"message": description
});
if (client)
client.send(error)
else
this.broadcast(error);
};

this.broadcast = function(msg) {
for (var id in this.clients)
this.clients[id].send(msg);
};

}).call(User.prototype);

module.exports = User;

0 comments on commit d83f636

Please sign in to comment.