Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
Change-Id: I14dd337cfb2032de761483fa3b8448cbc2a71e12
  • Loading branch information
py8765 committed Feb 25, 2014
2 parents 8a6418a + e320105 commit add50d7
Showing 25 changed files with 897 additions and 468 deletions.
56 changes: 50 additions & 6 deletions bin/pomelo
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ var CONNECT_ERROR = 'Fail to connect to admin console server.';
var FILEREAD_ERROR = 'Fail to read the file, please check if the application is started legally.';
var CLOSEAPP_INFO = 'Closing the application......\nPlease wait......';
var ADD_SERVER_INFO = 'Successfully add server.';
var RESTART_SERVER_INFO = 'Successfully restart server.';
var INIT_PROJ_NOTICE = '\nThe default admin user is: \n\n'+ ' username'.green + ': admin\n ' + 'password'.green+ ': admin\n\nYou can configure admin users by editing adminUser.json later.\n ';
var SCRIPT_NOT_FOUND = 'Fail to find an appropriate script to run,\nplease check the current work directory or the directory specified by option `--directory`.\n'.red;
var MASTER_HA_NOT_FOUND = 'Fail to find an appropriate masterha config file, \nplease check the current work directory or the arguments passed to.\n'.red;
@@ -58,6 +59,8 @@ program.command('start')
.option('-e, --env <env>', 'the used environment', DEFAULT_ENV)
.option('-D, --daemon', 'enable the daemon start')
.option('-d, --directory, <directory>', 'the code directory', DEFAULT_GAME_SERVER_DIR)
.option('-t, --type <server-type>,', 'start server type')
.option('-i, --id <server-id>', 'start server id')
.action(function(opts) {
start(opts);
});
@@ -112,6 +115,18 @@ program.command('kill')
terminal('kill', opts);
});

program.command('restart')
.description('restart the servers, for multiple servers, use `pomelo restart server-id-1 server-id-2`')
.option('-u, --username <username>', 'administration user name', DEFAULT_USERNAME)
.option('-p, --password <password>', 'administration password', DEFAULT_PWD)
.option('-h, --host <master-host>', 'master server host', DEFAULT_MASTER_HOST)
.option('-P, --port <master-port>', 'master server port', DEFAULT_MASTER_PORT)
.option('-t, --type <server-type>,', 'start server type')
.option('-i, --id <server-id>', 'start server id')
.action(function(opts) {
restart(opts);
});

program.command('masterha')
.description('start all the slaves of the master')
.option('-d, --directory <directory>', 'the code directory', DEFAULT_GAME_SERVER_DIR)
@@ -230,15 +245,20 @@ function start(opts) {
if (!fs.existsSync(logDir)) {
fs.mkdir(logDir);
}

var ls;
var type = opts.type || constants.RESERVED.ALL;
var params = [absScript, 'env=' + opts.env, 'type=' + type];
if(!!opts.id) {
params.push('startId=' + opts.id);
}
if (opts.daemon) {
ls = spawn(process.execPath, [absScript, 'env=' + opts.env], {detached: true, stdio: 'ignore'});
ls = spawn(process.execPath, params, {detached: true, stdio: 'ignore'});
ls.unref();
console.log(DAEMON_INFO);
process.exit(0);
} else {
ls = spawn(process.execPath, [absScript, 'env=' + opts.env]);
ls = spawn(process.execPath, params);
ls.stdout.on('data', function(data) {
console.log(data.toString());
});
@@ -318,8 +338,8 @@ function add(opts) {
function terminal(signal, opts) {
console.info(CLOSEAPP_INFO);
// option force just for `kill`
if(opts.force){
if (os.platform() === 'win32') {
if(opts.force) {
if (os.platform() === constants.PLATFORM.WIN) {
exec(KILL_CMD_WIN);
} else {
exec(KILL_CMD_LUX);
@@ -347,6 +367,30 @@ function terminal(signal, opts) {
});
}

function restart(opts) {
var id = 'pomelo_restart_' + Date.now();
var serverIds = [];
var type = null;
if(!!opts.id) {
serverIds.push(opts.id);
}
if(!!opts.type) {
type = opts.type;
}
connectToMaster(id, opts, function(client) {
client.request(co.moduleId, { signal: 'restart', ids: serverIds, type: type}, function(err, fails) {
if(!!err) {
console.error(err);
} else if(!!fails.length) {
console.info('restart fails server ids: %j', fails);
} else {
console.info(RESTART_SERVER_INFO);
}
process.exit(0);
});
});
}

function connectToMaster(id, opts, cb) {
var client = new adminClient({username: opts.username, password: opts.password, md5: true});
client.connect(id, opts.host, opts.port, function(err) {
@@ -372,7 +416,7 @@ function startMasterha(opts) {
var masterha = require(configFile).masterha;
for(var i=0; i<masterha.length; i++) {
var server = masterha[i];
server.mode = 'stand-alone';
server.mode = constants.RESERVED.STAND_ALONE;
server.masterha = 'true';
server.home = opts.directory;
runServer(server);
63 changes: 44 additions & 19 deletions lib/application.js
Original file line number Diff line number Diff line change
@@ -81,6 +81,20 @@ Application.getBase = function() {
return this.get(Constants.RESERVED.BASE);
};

/**
* Configure logger with {$base}/config/log4js.json
*
* @param {Object} logger pomelo-logger instance without configuration
*
* @memberOf Application
*/
Application.configureLogger = function(logger) {
if (process.env.POMELO_LOGGER !== 'off') {
var base = this.getBase();
logger.configure(path.join(base, Constants.FILEPATH.LOG), {serverId: this.serverId, base: base});
}
};

/**
* add a filter to before and after filter
*
@@ -284,31 +298,34 @@ Application.beforeStopHook = function(fun) {
* @param {Function} cb callback function
* @memberOf Application
*/
Application.start = function(cb) {
Application.start = function(cb) {
this.startTime = Date.now();
if(this.state > STATE_INITED) {
utils.invokeCallback(cb, new Error('application has already start.'));
return;
}
appUtil.loadDefaultComponents(this);

var self = this;
var startUp = function() {
appUtil.optComponents(self.loaded, Constants.RESERVED.START, function(err) {
self.state = STATE_START;
if(err) {
utils.invokeCallback(cb, err);
} else {
logger.info('%j enter after start...', self.getServerId());
self.afterStart(cb);
}
});
};
var beforeFun = this.lifecycleCbs[Constants.LIFECYCLE.BEFORE_STARTUP];
if(!!beforeFun) {
beforeFun.call(null, this, startUp);
} else {
startUp();
}
appUtil.startByType(self, function() {
appUtil.loadDefaultComponents(self);
var startUp = function() {
appUtil.optComponents(self.loaded, Constants.RESERVED.START, function(err) {
self.state = STATE_START;
if(err) {
utils.invokeCallback(cb, err);
} else {
logger.info('%j enter after start...', self.getServerId());
self.afterStart(cb);
}
});
};
var beforeFun = self.lifecycleCbs[Constants.LIFECYCLE.BEFORE_STARTUP];
if(!!beforeFun) {
beforeFun.call(null, self, startUp);
} else {
startUp();
}
});
};

/**
@@ -356,8 +373,16 @@ Application.stop = function(force) {
}
this.state = STATE_STOPED;
var self = this;

this.stopTimer = setTimeout(function() {
process.exit(0);
}, Constants.TIME.TIME_WAIT_STOP);

var shutDown = function() {
appUtil.stopComps(self.loaded, 0, force, function() {
if(!!self.stopTimer) {
clearTimeout(self.stopTimer);
}
if(force) {
process.exit(0);
}
125 changes: 121 additions & 4 deletions lib/common/service/channelService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var countDownLatch = require('../../util/countDownLatch');
var utils = require('../../util/utils');
var ChannelRemote = require('../remote/frontend/channelRemote');
var logger = require('pomelo-logger').getLogger('pomelo', __filename);

/**
@@ -21,11 +22,21 @@ var ChannelService = function(app, opts) {
opts = opts || {};
this.app = app;
this.channels = {};
this.prefix = opts.prefix;
this.store = opts.store;
this.broadcastFilter = opts.broadcastFilter;
this.channelRemote = new ChannelRemote(app);
};

module.exports = ChannelService;


ChannelService.prototype.start = function(cb) {
restoreChannel(this, cb);
};



/**
* Create channel with name.
*
@@ -38,6 +49,7 @@ ChannelService.prototype.createChannel = function(name) {
}

var c = new Channel(name, this);
addToStore(this, genKey(this), genKey(this, name));
this.channels[name] = c;
return c;
};
@@ -54,6 +66,7 @@ ChannelService.prototype.getChannel = function(name, create) {
var channel = this.channels[name];
if(!channel && !!create) {
channel = this.channels[name] = new Channel(name, this);
addToStore(this, genKey(this), genKey(this, name));
}
return channel;
};
@@ -66,6 +79,8 @@ ChannelService.prototype.getChannel = function(name, create) {
*/
ChannelService.prototype.destroyChannel = function(name) {
delete this.channels[name];
removeFromStore(this, genKey(this), genKey(this, name));
removeAllFromStore(this, genKey(this, name));
};

/**
@@ -164,8 +179,13 @@ ChannelService.prototype.broadcast = function(stype, route, msg, opts, cb) {
}

for(var i=0, l=count; i<l; i++) {
app.rpcInvoke(servers[i].id, {namespace: namespace, service: service,
method: method, args: [route, msg, opts]}, genCB());
if(servers[i].id === app.serverId) {
this.channelRemote[method](route, msg, opts, genCB());
} else {
app.rpcInvoke(servers[i].id, {namespace: namespace, service: service,
method: method, args: [route, msg, opts]}, genCB());
}

}
};

@@ -198,6 +218,7 @@ Channel.prototype.add = function(uid, sid) {
if(res) {
this.records[uid] = {sid: sid, uid: uid};
}
addToStore(this.__channelService__, genKey(this.__channelService__, this.name), genValue(sid, uid));
return res;
}
};
@@ -214,6 +235,7 @@ Channel.prototype.leave = function(uid, sid) {
return false;
}
delete this.records[uid];
removeFromStore(this.__channelService__, genKey(this.__channelService__, this.name), genValue(sid, uid));
var res = deleteFrom(uid, sid, this.groups[sid]);
if(this.groups[sid] && this.groups[sid].length === 0) {
delete this.groups[sid];
@@ -384,11 +406,106 @@ var sendMessageByGroup = function(channelService, route, msg, groups, opts, cb)
for(var sid in groups) {
group = groups[sid];
if(group && group.length > 0) {
app.rpcInvoke(sid, {namespace: namespace, service: service,
method: method, args: [route, msg, groups[sid], opts]}, rpcCB);
if(sid === app.serverId) {
channelService.channelRemote[method](route, msg, groups[sid], opts, rpcCB);
} else {
app.rpcInvoke(sid, {namespace: namespace, service: service,
method: method, args: [route, msg, groups[sid], opts]}, rpcCB);
}
} else {
// empty group
process.nextTick(rpcCB);
}
}
};

var restoreChannel = function(self, cb) {
if(!self.store) {
utils.invokeCallback(cb);
return;
} else {
loadAllFromStore(self, genKey(self), function(err, list) {
if(!!err) {
utils.invokeCallback(cb, err);
return;
} else {
if(!list.length || !Array.isArray(list)) {
utils.invokeCallback(cb);
return;
}
for(var i=0; i<list.length; i++) {
var name = list[i].slice(genKey(self).length + 1);
self.channels[name] = new Channel(name, self);
loadAllFromStore(self, list[i], function(err, items) {
for(var j=0; j<items.length; j++) {
var array = items[j].split(':');
var sid = array[0];
var uid = array[1];
var channel = self.channels[name];
var res = add(uid, sid, channel.groups);
if(res) {
channel.records[uid] = {sid: sid, uid: uid};
}
}
});
}
utils.invokeCallback(cb);
}
});
}
};

var addToStore = function(self, key, value) {
if(!!self.store) {
self.store.add(key, value, function(err) {
if(!!err) {
logger.error('add key: %s value: %s to store, with err: %j', key, value, err.stack);
}
});
}
};

var removeFromStore = function(self, key, value) {
if(!!self.store) {
self.store.remove(key, value, function(err) {
if(!!err) {
logger.error('remove key: %s value: %s from store, with err: %j', key, value, err.stack);
}
});
}
};

var loadAllFromStore = function(self, key, cb) {
if(!!self.store) {
self.store.load(key, function(err, list) {
if(!!err) {
logger.error('load key: %s from store, with err: %j', key, err.stack);
utils.invokeCallback(cb, err);
} else {
utils.invokeCallback(cb, null, list);
}
});
}
};

var removeAllFromStore = function(self, key) {
if(!!self.store) {
self.store.removeAll(key, function(err) {
if(!!err) {
logger.error('remove key: %s all members from store, with err: %j', key, err.stack);
}
});
}
};

var genKey = function(self, name) {
if(!!name) {
return self.prefix + ':' + self.app.serverId + ':' + name;
} else {
return self.prefix + ':' + self.app.serverId;
}
};

var genValue = function(sid, uid) {
return sid + ':' + uid;
};
Loading
Oops, something went wrong.

0 comments on commit add50d7

Please sign in to comment.