Skip to content

Commit

Permalink
调通网关到center间的注册
Browse files Browse the repository at this point in the history
  • Loading branch information
shihuaping authored and shihuaping committed Aug 14, 2017
1 parent 0fc2a8c commit eebb74b
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions center_svr/center-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@


//-------------------------------------
// redis
// type : server1 server2 server3
//
//
//-------------------------------------

const rdsOper = require('./redis-oper');
const cmdDefine = require('../protocol/cmd-define');
const packet = require('../protocol/packet');

//
// {
// name
// type
// ip
// port
// ext
// }
function registerServer(serverInfo) {

rdsOper.saveServer(serverInfo);
}

function removeServer() {

rdsOper.removeServer(serverInfo)
}

async function getServer(socket, serverInfo) {

let serverList = await rdsOper.getServerList(serverInfo.type);
}

function process(socket, data) {


let jObj = JSON.parse(data);

console.log(jObj);

let mainCmd = jObj.head.mcmd;
let subCmd = jObj.head.scmd;
switch (subCmd) {
case cmdDefine.SUB_CENTER_UPDATE:
let serverInfo = jObj.body;
registerServer(serverInfo);
break;
case cmdDefine.SUB_CENTER_GET:
let serverList = getServer(socket, serverInfo);
let retObj = packet.getPacket(mainCmd,subCmd+100);
retObj.body = serverList;
socket.write(retObj);
break;
default:
break
}

}

exports.process = process;
58 changes: 58 additions & 0 deletions center_svr/center-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const net = require('net');
const sysConfig = require('./config/sys-config.json');
const logger = require('./logger');
const process = require('process');
const centerHandler = require('./center-handler');

const server = net.createServer();

//attention:
//1.to be more stable
//all request should put into a queue

function startServer() {

server.on('listening', function () {
logger.info("server is listening on port:%d", sysConfig.svrPort);
});

server.on('connection', function (socket) {

socket.setTimeout(60 * 1000, function () {
logger.error("ip:%s,idle timeout, disconncting, bye", socket.remoteAddress);
socket.end('idle timeout, disconnecting, bye!');
socket.destroy();
});

socket.on('data', function (data) {
logger.info("ip:%s, get data", socket.remoteAddress);
console.debug(data);
centerHandler.process(socket, data);
});

socket.on('error', function (err) {
logger.info("ip:%s,error", socket.remoteAddress);
logger.error(err);
socket.destroy();
});

socket.on('close', function (had_error) {
logger.info("ip:%s,socket closed", socket.remoteAddress);
if (!socket.destroyed) {
logger.info("destroy socket");
socket.destroy();
}
})
});

server.on('error', (err) => {
logger.error(err);
process.exit(1);
});

server.listen(sysConfig.svrPort, sysConfig.svrHost, () => {
logger.info('server bound on host:%s,port:%d', sysConfig.svrHost, sysConfig.svrPort);
});
}

exports.startServer = startServer;
4 changes: 4 additions & 0 deletions center_svr/center.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

centerServer = require('./center-server');

centerServer.startServer();
25 changes: 25 additions & 0 deletions center_svr/config/log4js.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"appenders": {
"gatefile": {
"type": "file",
"filename": "log/center.log",
"maxLogSize": 10485760,
"numBackups": 3
},
"console": {
"type": "console",
"level": "debug"
}
},
"categories": {
"default": {
"appenders": [
"console",
"gatefile"
],
"level": "debug"
}
}
}


8 changes: 8 additions & 0 deletions center_svr/config/sys-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

{
"svrHost":"127.0.0.1",
"svrPort":9200,
"redisHost":"127.0.0.1",
"redisPort":6379,
"redisPassword":""
}
35 changes: 35 additions & 0 deletions center_svr/log/center.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[2017-08-14 14:33:18.443] [INFO] default - server is listening on port:9200
[2017-08-14 14:33:18.447] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:03:57.593] [INFO] default - server is listening on port:9200
[2017-08-14 16:03:57.598] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:46:25.930] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 16:47:21.990] [INFO] default - server is listening on port:9200
[2017-08-14 16:47:21.994] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:47:36.603] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 16:48:06.344] [INFO] default - server is listening on port:9200
[2017-08-14 16:48:06.348] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:48:21.468] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 16:49:32.588] [INFO] default - server is listening on port:9200
[2017-08-14 16:49:32.592] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:49:47.492] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 16:51:55.449] [INFO] default - server is listening on port:9200
[2017-08-14 16:51:55.452] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:52:09.691] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 16:53:04.346] [INFO] default - server is listening on port:9200
[2017-08-14 16:53:04.349] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:53:55.331] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 16:54:32.299] [INFO] default - server is listening on port:9200
[2017-08-14 16:54:32.301] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:57:20.241] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 16:57:20.244] [INFO] default - ip:127.0.0.1,socket closed
[2017-08-14 16:58:30.262] [INFO] default - server is listening on port:9200
[2017-08-14 16:58:30.265] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 16:58:50.251] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 16:59:55.004] [INFO] default - server is listening on port:9200
[2017-08-14 16:59:55.007] [INFO] default - server bound on host:127.0.0.1,port:9200
[2017-08-14 17:00:10.251] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 17:00:10.254] [INFO] default - ip:127.0.0.1,socket closed
[2017-08-14 17:00:30.253] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 17:00:30.254] [INFO] default - ip:127.0.0.1,socket closed
[2017-08-14 17:00:50.256] [INFO] default - ip:127.0.0.1, get data
[2017-08-14 17:00:50.257] [INFO] default - ip:127.0.0.1,socket closed
7 changes: 7 additions & 0 deletions center_svr/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

const config = require('./config/log4js.json');

const log4js = require('log4js');
log4js.configure(config);

module.exports = log4js.getLogger();
90 changes: 90 additions & 0 deletions center_svr/redis-oper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const sysConfig = require('./config/sys-config.json');
const redis = require('redis');
const rdsKey = require('../lib/rds-key');
const logger = require('./logger');

const host = sysConfig.redisHost;
const port = sysConfig.redisPort;
const password = sysConfig.redisPassword;
let rds = null;

function getConnection() {

if (rds) return rds;

if (!!password && password.length > 0) {
rds = redis.createClient({host: host, port: port, password: password});
} else {
rds = redis.createClient({host: host, port: port});
}
return rds;
}

getConnection();

function saveServer(serverInfo) {

let keyName = rdsKey.KEY_SERVER_TYPE + serverInfo.type;

rds.get(keyName, function (err, reply) {
if (err) {
logger.error(err);
return;
}

jArray = JSON.parse(reply);

if (!jArray) {
jArray = [];
}
let found = false;
for (let v of jArray) {
if (v.ip === serverInfo.ip) {
// do nothing
found = true;
break;
}
}

if (!found) {
jArray.push(serverInfo);
let json = JSON.stringify(jArray);
rds.set(keyName, json);
}

});
}

function getServerList(type) {

return new Promise(function (resolve, reject) {
let keyName = rdsKey.KEY_SERVER_TYPE + serverInfo.type;
rds.get(keyName, function (err, reply) {
if (err) {
reject(err);
return;
}
resolve(reply);
})
});
}

async function removeServer(serverInfo) {
let serverList = await getServerList(serverInfo.type);
let jArray = JSON.parse(serverList);
let idx = 0;
for (let v of jArray) {
if (v.ip === serverInfo.ip) {
delete jArray[idx];
break;
}
idx++;
}
let keyName = rdsKey.KEY_SERVER_TYPE + serverInfo.type;
let json = JSON.stringify(jArray);
rds.set(keyName, json);
}

exports.getServerList = getServerList;
exports.saveServer = saveServer;
exports.removeServer = removeServer;
13 changes: 13 additions & 0 deletions center_svr/short-ID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

var idx = 1024;

function getNextID() {
if(idx > 4200000000) {
idx = 1024;
}
idx++;
return idx;
}


exports.getNextID = getNextID;
Empty file added gate_svr/register-center.js
Empty file.
Empty file added lib/const-define.js
Empty file.
Empty file added lib/rds-key.js
Empty file.
Empty file added protocol/packet.js
Empty file.

0 comments on commit eebb74b

Please sign in to comment.