compoments/master.js
game-server/
app.js
var pomelo = require('pomelo');
var app = pomelo.createApp();
game-server/node_modules/pomelo/lib/pomelo.js
var application = require('./application');
Pomelo.createApp = function (opts) {
console.log('Pomelo.createApp----->>',opts);
var app = application;
app.init(opts); //初始化application
self.app = app;
return app;
};
game-server/node_modules/pomelo/lib/application.js
var appUtil = require('./util/appUtil');
Application.init = function(opts) {
console.log('Application.init----->>>>>');
opts = opts || {};
this.loaded = []; // loaded component list
this.components = {}; // name -> component map
this.settings = {}; // collection keep set/get
var base = opts.base || path.dirname(require.main.filename);
this.set(Constants.RESERVED.BASE, base, true);
this.event = new EventEmitter(); // event object to sub/pub events
// current server info
this.serverId = null; // current server id
this.serverType = null; // current server type
this.curServer = null; // current server info
this.startTime = null; // current server start time
// global server infos
this.master = null; // master server info
this.servers = {}; // current global server info maps, id -> info
this.serverTypeMaps = {}; // current global type maps, type -> [info]
this.serverTypes = []; // current global server type list
this.lifecycleCbs = {}; // current server custom lifecycle callbacks
this.clusterSeq = {}; // cluster id seqence
appUtil.defaultConfiguration(this); //讀取配置文件
this.state = STATE_INITED;
logger.info('application inited: %j', this.getServerId());
};
game-server/node_modules/pomelo/lib/util/appUtil.js
module.exports.defaultConfiguration = function(app) {
var args = parseArgs(process.argv);
setupEnv(app, args);
loadMaster(app); //先加載master.json服務(wù)器配置
loadServers(app); //加載servers.json服務(wù)器配置
processArgs(app, args);
configLogger(app);
loadLifecycle(app);
};
//application.js
var loadMaster = function(app) {
app.loadConfigBaseApp(Constants.RESERVED.MASTER, Constants.FILEPATH.MASTER);//讀取文件內(nèi)容
app.master = app.get(Constants.RESERVED.MASTER);//設(shè)置master配置
};
//加載配置完畢.接下來啟動master服務(wù)器
./lib/application.js
/**
- Start application. It would load the default components and start all the loaded components.
- @param {Function} cb callback function
- @memberOf Application
*/
Application.start = function(cb) {
this.startTime = Date.now();
if(this.state > STATE_INITED) {
utils.invokeCallback(cb, new Error('application has already start.'));
return;
}
var self = this;
appUtil.startByType(self, function() {
appUtil.loadDefaultComponents(self);
var startUp = function() {
appUtil.optComponents(self.loaded, Constants.RESERVED.START, function(err) { //判斷是否loaded的服務(wù)器是否有start方法,有則執(zhí)行
//啟動master, monitor服務(wù)器 loaded : [master :[],monitor : [object]]
// ./lib/master/master.js && monitor.js 寫有start方法
self.state = STATE_START;
if(err) {
utils.invokeCallback(cb, err);
} else {//走
logger.info('%j enter after start...', self.getServerId());//當前服務(wù)器Id為master服務(wù)器Id
self.afterStart(cb);
}
});
};
var beforeFun = self.lifecycleCbs[Constants.LIFECYCLE.BEFORE_STARTUP];
if(!!beforeFun) {//沒走
beforeFun.call(null, self, startUp);
} else {//走
startUp();
}
});
};