-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm_remote.js
139 lines (113 loc) · 3.67 KB
/
dm_remote.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
var zmq = require('zmq');
var req = new zmq.socket('req');
exports.Start = function (host, port, cb) {
req.connect('tcp://'+host+':'+port);
console.log('Connected to: ' + host + ':' + port);
if (cb != null) cb();
}
var callbacks = {} // hash of callbacks. Key is invoId
var invoCounter = 0; // current invocation number is key to access "callbacks".
req.on ('message', function (data) {
console.log ('data comes in: ' + data);
var reply = JSON.parse (data.toString());
switch (reply.what) {
case 'add user':
console.log ('We received a reply for: ' + reply.what + ':' + reply.invoId);
callbacks [reply.invoId] (); // call the stored callback, 0 argument
delete callbacks [reply.invoId]; // remove from hash
break;
case 'add subject':
console.log ('We received a reply for: ' + reply.what + ':' + reply.invoId);
callbacks [reply.invoId] ();
delete callbacks [reply.invoId];
break;
case 'get user list':
console.log ('We received a reply for: ' + reply.what + ':' + reply.invoId);
callbacks [reply.invoId] (reply.obj);
delete callbacks [reply.invoId];
break;
case 'login':
console.log ('We received a reply for: ' + reply.what + ':' + reply.invoId);
callbacks [reply.invoId] (reply.obj);
delete callbacks [reply.invoId];
break;
case 'add private message':
console.log ('We received a reply for: ' + reply.what + ':' + reply.invoId);
callbacks [reply.invoId] ();
delete callbacks [reply.invoId];
break;
case 'get private message list':
case 'get public message list':
case 'get subject list':
console.log ('We received a reply for: ' + reply.what + ':' + reply.invoId);
callbacks [reply.invoId] (reply.obj); // call the stored callback, one argument
delete callbacks [reply.invoId]; // remove from hash
break;
case 'add private message':
case 'add public message':
console.log ('We received a reply for add command');
callbacks [reply.invoId] (); // call the stored callback, no arguments
delete callbacks [reply.invoId]; // remove from hash
break;
default:
console.log ("Panic: we got this: " + reply.what);
}
});
req.on('close', function() {
console.log('Connection closed');
});
function Invo (str, cb) {
this.what = str;
this.invoId = ++invoCounter;
callbacks[invoCounter] = cb;
}
exports.addUser = function (u,p, cb) {
var invo = new Invo('add user', cb);
invo.u = u;
invo.p = p;
req.send(JSON.stringify(invo));
}
exports.addSubject = function (s, cb) {
var invo = new Invo('add subject', cb);
invo.s = s;
req.send(JSON.stringify(invo));
}
exports.getUserList = function (cb) {
req.send(JSON.stringify(new Invo ('get user list', cb)));
}
exports.login = function (u, p, cb) {
var invo = new Invo('login', cb);
invo.u = u;
invo.p = p;
req.send(JSON.stringify(invo));
}
exports.addPrivateMessage = function (msg, cb) {
var invo = new Invo('add private message', cb);
invo.msg = msg;
req.send(JSON.stringify(invo));
}
function getSubject (sbj, cb) {
var invo = new Invo('get subject',cb);
invo.sbj = sbj;
req.send(JSON.stringify(invo));
}
exports.addPublicMessage = function (msg, cb) {
var invo = new Invo('add public message', cb);
invo.msg = msg;
req.send(JSON.stringify(invo));
}
exports.getPublicMessageList = function (sbj, cb) {
var invo = new Invo ('get public message list', cb);
invo.sbj = sbj;
req.send (JSON.stringify(invo));
}
exports.getPrivateMessageList = function (u1, u2, cb) {
invo = new Invo ('get private message list', cb);
invo.u1 = u1;
invo.u2 = u2;
req.send (JSON.stringify(invo));
}
exports.getSubjectList = function (cb) {
req.send (JSON.stringify(new Invo ('get subject list', cb)));
}
// TODO: complete the rest of the forum functions.