-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (215 loc) · 9.2 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Created by paulo.simao on 23/12/2015.
*/
var net = require('net');
var stimpcommons = require('stimp-commons');
var mongo = require('mongodb').MongoClient;
var EventEmitter = require('events');
var ll = require('lillog');
/**
module.exports.CMD_CONNECT = 'CONNECT';
module.exports.CMD_CONNECTED = 'CONNECTED';
module.exports.CMD_SEND = 'SEND';
module.exports.CMD_SUBSCRIBE = 'SUBSCRIBE';
module.exports.CMD_UNSUBSCRIBE = 'UNSUBSCRIBE';
module.exports.CMD_ACK = 'ACK';
module.exports.CMD_NACK = 'NACK';
module.exports.CMD_BEGIN = 'BEGIN';
module.exports.CMD_COMMIT = 'COMMIT';
module.exports.CMD_ABORT = 'ABORT';
module.exports.CMD_DISCONNECT = 'DISCONNECT';
module.exports.CMD_MESSAGE = 'MESSAGE';
module.exports.CMD_RECEIPT = 'RECEIPT';
module.exports.CMD_ERROR = 'ERROR';
*/
module.exports = function () {
var server = {};
var tcpserver = {};
var subscriptions = new EventEmitter();
server.config = {
serverport: 61613,
mongourl: 'mongodb://localhost:27017/stimp'
};
var socketwrapper = function (sock) {
var ret = {
sock: sock,
uuid: stimpcommons.uuid(),
parser: stimpcommons.createparser(sock),
socksubscriptions: {},
onstomp: function (msg) {
var retmsg = stimpcommons.createmsg(stimpcommons.CMD_CONNECTED);
retmsg.addheader('version', '1.2');
retmsg.addheader('session', stimpcommons.uuid());
//In case msg was sent w receipt, we reply w it
if (msg.headers.receipt) {
retmsg.addheader('receipt-id', msg.headers.receipt);
}
this.sock.write(retmsg.torawmsg());
},
ondisconnect: function (msg) {
for (dest in this.socksubscriptions) {
subscriptions.removeListener(dest, ret.dodelivertosubscription);
}
var retmsg = stimpcommons.createmsg(stimpcommons.CMD_RECEIPT);
//In case msg was sent w receipt, we reply w it
if (msg.headers.receipt) {
retmsg.addheader('receipt-id', msg.headers.receipt);
}
ret.sock.write(retmsg.torawmsg());
//TODO ADD CLEANUP CODE HERE
//sock.destroy();
},
onsend: function (msg) {
//ll.debug(JSON.stringify(msg));
var fwdmsg = stimpcommons.createmsg(stimpcommons.CMD_MESSAGE);
fwdmsg.headers = msg.headers;
fwdmsg.headers.subscription = fwdmsg.headers.destination;
fwdmsg.headers['message-id'] = fwdmsg.headers.receipt;
//delete fwdmsg.headers.destination;
fwdmsg.body = msg.body;
ret.dbsavemsgonsend(fwdmsg, function (err, data) {
if (err) {
//TODO SEND ERROR BACK TO CLIENT
return ll.error(err);
}
ret.dofwdmsg(fwdmsg);
var retmsg = stimpcommons.createmsg(stimpcommons.CMD_RECEIPT);
if (msg.headers.receipt) {
retmsg.addheader('receipt-id', msg.headers.receipt);
}
ret.sock.write(retmsg.torawmsg());
});
},
onsubscribe: function (msg) {
subscriptions.on(msg.headers.destination, ret.dodelivertosubscription);
ret.socksubscriptions[msg.headers.destination] = true;
ret.dbcheckpendingmsgs(msg.headers.destination);
var retmsg = stimpcommons.createmsg(stimpcommons.CMD_RECEIPT);
if (msg.headers.receipt) {
retmsg.addheader('receipt-id', msg.headers.receipt);
}
ret.sock.write(retmsg.torawmsg());
},
onunsubscribe: function (msg) {
delete ret.socksubscriptions[msg.headers.destination];
subscriptions.removeListener(msg.headers.destination, ret.dodelivertosubscription);
var retmsg = stimpcommons.createmsg(stimpcommons.CMD_RECEIPT);
if (msg.headers.receipt) {
retmsg.addheader('receipt-id', msg.headers.receipt);
}
ret.sock.write(retmsg.torawmsg());
},
onack: function (msg) {
console.log(JSON.stringify(msg));
ret.dbarchivemsg(msg.headers.id, false, function (err) {
if (err) {
//TODO SEND ERROR FRAME IN THIS CASE
ll.error(err);
}
})
},
onnack: function (msg) {
ret.dbarchivemsg(msg.headers.id, true, function (err) {
if (err) {
//TODO SEND ERROR FRAME IN THIS CASE
ll.error(err);
}
})
},
dofwdmsg: function (msg) {
try {
if (msg.headers.destination.startsWith('/queue')) {
var fwdmethod = subscriptions.listeners(msg.headers.destination)[0];
fwdmethod(msg);
} else {
subscriptions.emit(msg.headers.destination, msg);
}
} catch (err) {
ll.debug(msg)
ll.error(err)
}
},
dodelivertosubscription: function (msg) {
//ll.log('Sending:' + JSON.stringify(msg) + ' to:' + sock);
ret.sock.write(msg.torawmsg());
},
dbsavemsgonsend: function (msg, cb) {
server.db.collection(msg.headers.destination).insertOne(msg);
server.db.collection('msgindex').insertOne({
msgid: msg.headers.receipt,
destination: msg.headers.destination
}, cb);
},
dbarchivemsg: function (id, deadletter, cb) {
var finalcol = deadletter ? 'deadletter' : 'archive';
server.db.collection('msgindex').findOne({msgid: id}, function (err, doc) {
var destcol = doc.destination;
if (err) {
return cb(err);
}
if (doc) {
server.db.collection(destcol).findOne({'headers.message-id': id}, function (err, doc) {
server.db.collection(finalcol + doc.headers.destination).insertOne(doc, function (err, data) {
if (err) {
return cb(err);
}
server.db.collection(destcol).deleteOne({'headers.message-id': id}, function (err, data) {
if (err) {
ll.error(err);
}
server.db.collection('msgindex').deleteOne({msgid: id}, function (err, data) {
if (err) {
return cb(err);
} else {
cb(null);
}
});
});
});
});
}
});
},
dbcheckpendingmsgs: function (destination) {
server.db.collection(destination).find({}).forEach(function (doc) {
process.nextTick(function () {
var msg = stimpcommons.addmethodstomsg(doc);
ret.sock.write(msg.torawmsg());
});
});
},
init: function () {
ret.parser.on(stimpcommons.CMD_STOMP, ret.onstomp);
ret.parser.on(stimpcommons.CMD_CONNECT, ret.onstomp);
ret.parser.on(stimpcommons.CMD_DISCONNECT, ret.ondisconnect);
ret.parser.on(stimpcommons.CMD_SEND, ret.onsend);
ret.parser.on(stimpcommons.CMD_SUBSCRIBE, ret.onsubscribe);
ret.parser.on(stimpcommons.CMD_UNSUBSCRIBE, ret.onunsubscribe);
ret.parser.on(stimpcommons.CMD_ACK, ret.onack);
ret.parser.on(stimpcommons.CMD_NACK, ret.onnack);
}
}
ret.init();
return ret;
}
server.start = function () {
mongo.connect(server.config.mongourl, function (err, db) {
if (err) {
ll.error(err);
process.exit(1);
}
server.db = db;
tcpserver = net.createServer(function (sock) {
sock.wrapper = new socketwrapper(sock);
sock.on('error', function (err) {
ll.error(err);
});
});
tcpserver.listen(server.config.serverport, '127.0.0.1');
});
};
server.stop = function () {
tcpserver.close();
};
return server;
};