Skip to content

Instantly share code, notes, and snippets.

@FLYBYME
Created September 30, 2011 21:27
Show Gist options
  • Save FLYBYME/1255016 to your computer and use it in GitHub Desktop.
Save FLYBYME/1255016 to your computer and use it in GitHub Desktop.
console.log('router start')
var inherits = function(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor : {
value : ctor,
enumerable : false,
writable : true,
configurable : true
}
});
};
var Master = function(name, clients, masters) {
this.name = name;
this.clients = clients;
this.masters = masters
}
Master.prototype.addClient = function(client) {
this.clients.push(client)
}
Master.prototype.removeClient = function(client) {
if(this.clients.indexOf(client) === -1) {
return;
}
this.clients.splice(this.clients.indexOf(client), 1)
}
var event = new Emitter()
var Router = function(type, name) {
this.type = type;
this.name = name;
this.clients = []
this.table = {
}
Emitter.call(this);
}
inherits(Router, Emitter);
Router.prototype.setupEvents = function() {
}
Router.prototype.sendRoute = function(from, gateway, to, data) {
var route = []
route.push(from)
route.push(this.name)
if(gateway === this.name) {
route.push(to)
this.emit('sendRoute', gateway, route.join(':'), data)
return;
}
var table = this.table
if(table[gateway]) {
route.push(gateway)
if(table[gateway].clients.indexOf(to) >= 0) {
route.push(to)
this.emit('sendRoute', gateway, route.join(':'), data)
return;
}
route.push(to)
this.emit('error', gateway, route.join(':'), data)
return;
}
for(var masterName in table) {
var master = table[masterName];
var masters = master.masters;
for(var i = masters.length - 1; i >= 0; i--) {
var m = masters[i];
if(gateway === m) {
route.push(masterName)
route.push(gateway)
route.push(to)
this.emit('sendRoute', gateway, route.join(':'), data)
return;
}
};
}
}
Router.prototype.receiveRoute = function(route, data) {
var routes = route.split(':')
var hopLocation = routes.indexOf(this.name);
if(!(hopLocation >= 0)) {
this.emit('error', routes[routes.length - 1], routes.join(':'), data);
return;
}
if(routes.length === 3 && hopLocation == 2 && (this.clients.indexOf(routes[hopLocation - 1]) >= 0)) {
this.sendRoute(routes[0], routes[hopLocation], routes[hopLocation + 1], data)
return;
}
if(hopLocation === routes.length - 1) {
this.emit('message', route, data);
} else if(hopLocation === routes.length - 2) {
if(this.clients.indexOf(routes[hopLocation - 1])) {
this.sendRoute(routes[0], routes[hopLocation], routes[hopLocation + 1], data)
}
} else if(hopLocation === 2) {
if(routes[hopLocation + 1] in this.table) {
this.sendRoute(routes[0], routes[hopLocation], routes[hopLocation + 1], data)
return;
}
this.sendRoute(routes[0], routes[hopLocation], routes[hopLocation + 1], data)
} else {
this.sendRoute(routes[0], routes[hopLocation], routes[hopLocation + 1], data)
}
}
Router.prototype.addMaster = function(name, clients, masters) {
if(!( name in this.table)) {
this.table[name] = new Master(name, clients, masters);
}
}
Router.prototype.addRemoteClient = function(master, name) {
if( master in this.table) {
this.table[master].addClient(name)
}
}
Router.prototype.removeRemoteClient = function(master, name) {
if( master in this.table) {
this.table[master].removeClient(name)
}
}
Router.prototype.addCleint = function(client) {
this.clients.push(client)
}
Router.prototype.removeCleint = function(client) {
if(this.clients.indexOf(client) === -1) {
return;
}
this.clients.splice(this.clients.indexOf(client), 1)
}
var router = new Router('', 'master1');
router.on('message', function(to, route, data) {
console.log('message to: ' + route)
}).on('sendRoute', function(to, route, data) {
console.log('send route to: ' + to + ' route was: ' + route)
}).on('error', function(to, route, data) {
console.log('error route to: ' + to + ' route was: ' + route)
})
router.addMaster('master2', ['c1', 'c2', 'c3', 'c4'], ['master4', 'master3'])
router.addMaster('master3', ['c1', 'c2', 'c3', 'c4'], [])
router.addRemoteClient('master4', 'c5')
router.addCleint('c5')
router.sendRoute('c1', 'master4', 'c5', {})
router.sendRoute('c1', 'master3', 'c3', {})
router.sendRoute('c1', 'master2', 'c5', {})
router.addRemoteClient('master2', 'c5')
router.sendRoute('c1', 'master2', 'c5', {})
router.removeRemoteClient('master2', 'c5')
router.sendRoute('c1', 'master2', 'c5', {})
router.sendRoute('c1', 'master1', 'c5', {})
router.receiveRoute('c1:master1:c5', {})
router.receiveRoute('c1:master4:c5', {})
router.receiveRoute('c1:master1:c5', {})
var i = 500
while(false) {
router.receiveRoute('c1:master1:c5', {})
router.receiveRoute('c1:master4:c5', {})
router.receiveRoute('c1:master1:c5', {})
}
var TABLE = {
'master2' : {
m : ['master1'],
c : ['c1', 'c2', 'c3']
},
'master3' : {
m : ['master2'],
c : ['c1', 'c2', 'c3']
},
'master4' : {
m : ['master1'],
s : ['c1', 'c2', 'c3']
}
}
var SocketRouter = function(name) {
this._table = {}
this.name = name;
this._masters = {};
this._clients = [];
Emitter.call(this);
}
inherits(SocketRouter, Emitter);
SocketRouter.prototype.addMaster = function(name) {
var masters = this._masters;
var master = masters[name]
if(master) {
return false;
} else {
masters[name] = {
m : [],
s : []
}
return true;
}
}
SocketRouter.prototype.updateMaster = function(name, type, val) {
var masters = this._masters;
var master = masters[name]
if(!master) {
return false;
} else {
if(type === 'master') {
var list = masters[name]['m']
} else if(val === 'slave') {
var list = masters[name]['s']
} else {
return false
}
if(list.indexOf(val) >= 0) {
return false;
} else {
list.push(val)
}
return true;
}
}
SocketRouter.prototype.removeMaster = function(route, data) {
var masters = this._masters;
var master = masters[name]
if(master) {
delete masters[name];
return true;
} else {
return false;
}
}
SocketRouter.prototype.addClient = function(name) {
var clients = this._clients;
clients.push(name)
return true;
}
SocketRouter.prototype.updateClient = function(name, val) {
var clients = this._clients;
var index = clients.indexOf(name)
if(index >= 0) {
clients[index] = val
return true;
} else {
return false;
}
}
SocketRouter.prototype.removeClient = function(r) {
var clients = this._clients;
var index = clients.indexOf(name)
if(index >= 0) {
clients.splice(index, 1)
return true;
} else {
return false;
}
}
//
//
SocketRouter.prototype.addRemoteClient = function(master, name) {
master = this._masters[master];
var clients = master.s
clients.push(name)
return true;
}
SocketRouter.prototype.updateRemoteClient = function(master, name, val) {
master = this._masters[master];
var clients = master.s
var index = clients.indexOf(name)
if(index >= 0) {
clients[index] = val
return true;
} else {
return false;
}
}
SocketRouter.prototype.removeRemoteClient = function(master, name) {
master = this._masters[master];
var clients = master.s
var index = clients.indexOf(name)
if(index >= 0) {
clients.splice(index, 1)
return true;
} else {
return false;
}
}
//
SocketRouter.prototype.addRemoteMaster = function(master, name) {
master = this._masters[master];
var clients = master.m
clients.push(name)
return true;
}
SocketRouter.prototype.updateRemoteMaster = function(master, name, val) {
master = this._masters[master];
var clients = master.m
var index = clients.indexOf(name)
if(index >= 0) {
clients[index] = val
return true;
} else {
return false;
}
}
SocketRouter.prototype.removeRemoteMaster = function(master, name) {
master = this._masters[master];
var clients = master.m
var index = clients.indexOf(name)
if(index >= 0) {
clients.splice(index, 1)
return true;
} else {
return false;
}
}
SocketRouter.prototype.route = function(route, data) {
var r = route.split(':')
var ourPlace = r.indexOf(this.name)
if(!(ourPlace >= 1)) {
throw 'error!!'
}
var from = r[ourPlace - 1]
var to = r[ourPlace + 1]
if(this._masters[to]) {
//we have this master so send it!
this.emit('write', to, route, data)
} else if(this._clients.indexOf(to) >= 0) {
this.emit('write', to, route, data)
} else if(((r.length - ourPlace) + 2) >= 1) {
var masters = this._masters;
for(var master in masters) {
var m = masters[master].m
var index = m.indexOf(to)
if(index >= 0) {
this.emit('write', master, route, data)
}
}
this.emit('writeBack', from, route, data)
}
}
SocketRouter.prototype.genRoute = function(master) {
}
var q = new SocketRouter('master1')
q.on('write', function() {
console.log(arguments)
}).on('writeBack', function() {
console.log(arguments)
})
q.addMaster('master2')
q.addRemoteClient('master2', 'c1')
q.addRemoteMaster('master2', 'master3')
q.addClient('c1')
q.route('c1:master1:master2:c1', {})
q.route('c1:master1:master2:master3:c1', {})
q.route('c1:master1:master3:master2:c1', {})
q.route('c1:master1:master2:c1', {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment