-
Notifications
You must be signed in to change notification settings - Fork 0
/
app2.js
executable file
·146 lines (128 loc) · 4.6 KB
/
app2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Created by Saurabh on 24/06/2015.
*/
var zmq = require('zmq');
var localIP = require('ip');
var APP_PORT = '9003';
var db = {};
function Peer(ip){
var self = this;
this.ip = ip;
this.socket = zmq.socket('req');
this.socket.connect(ip);
console.log('Connected to '+ip);
this.onMessageRecieved = function(data){
console.log('Peer '+self.ip+' recieved '+data.toString());
var message = JSON.parse(data.toString());
if(message.type == 'STATUS'){
if(message.status == 'PEER_ADDED'){
for (var key in db) {
if (db.hasOwnProperty(key)) {
db[key].message(JSON.stringify({type:'GET_PEER_LIST'}));
}
}
}
}
if(message.type == 'PEER_LIST'){
for(var i=0; i<message.peerList.length; i++){
var ip = message.peerList[i];
if(!db.hasOwnProperty(ip)){
db[ip] = new Peer(ip);
db[ip].message(JSON.stringify({
type:'CONNECT',
ip:'tcp://'+localIP.address()+':'+APP_PORT
}));
}
}
}
//self.message(JSON.stringify({STATUS:'OK'}))
};
this.socket.on('message', this.onMessageRecieved);
this.message = function(message){
self.socket.send(message);
console.log('Peer '+self.ip+' sent '+message);
};
}
function Application(ip){
self = this;
this.ip = ip;
this.socket = zmq.socket('rep');
this.socket.bind(self.ip, function(err) {
if (err) throw err;
});
console.log('Application Bound to '+ip);
this.onMessageRecieved = function(data){
console.log('Application '+self.ip+' recieved '+data.toString());
var message = JSON.parse(data.toString());
if(message.type == 'CONNECT'){
db[message.ip] = new Peer(message.ip);
console.log('PEER_ADDED '+JSON.stringify(db[message.ip]));
self.socket.send(JSON.stringify({
type:'STATUS',
status:'PEER_ADDED'
}));
}
if(message.type == 'GET_PEER_LIST'){
var peerList = [];
for (var key in db) {
if (db.hasOwnProperty(key)) {
peerList.push(key);
}
}
self.socket.send(JSON.stringify({
type:'PEER_LIST',
peerList:peerList
}));
}
if(message.type == 'APP'){
if(message.app[0]!='.'&&message.app[1]!='/'){
message.app = './'+message.app
}
if(!message.hasOwnProperty('target')){
var module = require(message.app);
module(message.payload);
if(!message.hasOwnProperty('repeat')||message.repeat==true){
message['repeat'] = false;
for (var key in db) {
if (db.hasOwnProperty(key) && key!='tcp://'+localIP.address()+':'+APP_PORT) {
db[key].message(JSON.stringify(message));
}
}
}
}
else
if(message.hasOwnProperty('target')&&message.target=='tcp://'+localIP.address()+':'+APP_PORT){
var module = require(message.app);
module(message.payload);
}
else
if(message.hasOwnProperty('target')&&message.target!='tcp://'+localIP.address()+':'+APP_PORT){
if(db.hasOwnProperty(message.target)){
db[message.target].message(JSON.stringify(message));
}
}
self.socket.send(JSON.stringify({type:'STATUS', status:'OK'}));
}
console.log('Connected To:<---------');
for(var key in db){
if(db.hasOwnProperty(key)){
console.log(key);
}
}
console.log('--------->');
};
this.socket.on('message', this.onMessageRecieved);
if(process.argv.slice(2).length!=0){
var ip = process.argv.slice(2)[0];
}
else{
var ip = 'tcp://'+localIP.address()+':'+APP_PORT
}
var peer = new Peer(ip);
peer.message(JSON.stringify({
type:'CONNECT',
ip:'tcp://'+localIP.address()+':'+APP_PORT
}));
db[ip]=peer;
};
var app = new Application('tcp://*:'+APP_PORT);