-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.js
72 lines (66 loc) · 1.91 KB
/
messages.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
exports.HELLO = "hello";
exports.HI = "hi";
exports.ROUTE_LIST = "routeList";
exports.BROADCAST = "broadcast";
exports.REPLY = "reply";
exports.ROUTING_TABLE = "routingTable";
exports.NOTE = "note";
exports.POST = "post";
exports.NONE = "none";
// Komponuje wiadomosc na podstawie podanej tablicy routingu
exports.getRouteList = function(routingTable) {
var routingMessage = {type: exports.ROUTE_LIST, routes: []};
routingTable.forEach( function(record) {
if (record.listeningPort != 0)
{
routingMessage.routes.push({ "address": record.socket.remoteAddress, "port": record.listeningPort, "name": record.name });
}
});
return routingMessage;
};
// Connection Acknowledgment Message
exports.getConnAckMsg = function(address,port) {
if (typeof(address) != 'undefined' && typeof(port) != 'undefined')
{
return {type: exports.HELLO, address: address, port: port, name: name};
}
else
{
return {type: exports.HELLO, name: name}
}
};
exports.getBroadcastMsg = function(payload, visited, reply, id ) {
if (typeof payload == 'undefined')
{
return {type: exports.NONE};
}
if (typeof id == 'undefined')
{
id = Math.floor(Math.random()*Math.pow(2,31));
}
if (typeof reply == 'undefined')
{
reply = false;
}
if (typeof visited == 'undefined')
{
visited = [];
}
return {type: reply ? exports.REPLY : exports.BROADCAST, payload: payload, visited: visited, id: id };
};
exports.getGreetingMsg = function(port) {
return {type: exports.HI, port: port, name: name};
};
exports.getRoutingTableMsg = function(to) {
if (typeof to == "undefined")
{
to = [];
}
return exports.getBroadcastMsg({type: exports.ROUTING_TABLE, to: to},[]);
};
exports.getNoteMsg = function(payload) {
return {type: exports.NOTE, payload: payload, visited: [name]};
};
exports.getPostMsg = function(data) {
return {type: exports.POST, text: data.text ? data.text : "", topic: data.topic ? data.topic : "", name: data.name, time: data.time };
};