king
Version:
A powerful server infrastructure management platform - "The King of your Nodes"
87 lines (74 loc) • 2.35 kB
JavaScript
// Generated by CoffeeScript 1.6.3
var Base, WebUser, dnode, helper,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Base = require("../common/base");
helper = require("../common/helper");
dnode = require("dnode");
module.exports = WebUser = (function(_super) {
__extends(WebUser, _super);
WebUser.prototype.name = 'WebUser';
function WebUser(king, stream) {
var _this = this;
this.king = king;
this.stream = stream;
this.id = "...";
this.remote = null;
helper.bindAll(this);
this.api = this.makeApi();
this.d = dnode(this.api);
this.d.on("ready", this.onReady);
this.d.on("remote", this.onRemote);
this.d.pipe(this.stream).pipe(this.d);
helper.tap(this.stream, 'emit', function(e) {
if (e === 'close') {
return _this.onClose();
}
});
}
WebUser.prototype.onReady = function() {
this.log("connected");
this.remote.broadcast('servants-init', this.king.servants.map(function(s) {
return s.serialize();
}));
return this.king.users.add(this);
};
WebUser.prototype.onRemote = function(remote) {
this.id = remote.id;
return this.remote = remote;
};
WebUser.prototype.onClose = function() {
this.log("disconnected");
return this.king.users.remove(this);
};
WebUser.prototype.makeApi = function() {
var _this = this;
return {
hi: function(n, cb) {
_this.log("hi!");
return cb(n + 42);
},
exec: function(index, cmd, callback) {
var servant;
servant = _this.king.servants.get(index);
if (!servant) {
return callback({
type: 'error',
msg: "missing servant: " + index
});
}
_this.log("executing: '" + cmd + "'");
return servant.remote.exec(cmd, callback);
},
config: {
get: function(k, cb) {
return _this.log("get " + k);
},
set: function(k, v, cb) {
return _this.log("set " + k + " = " + v);
}
}
};
};
return WebUser;
})(Base);