-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibsock.js
More file actions
executable file
·134 lines (105 loc) · 3.58 KB
/
libsock.js
File metadata and controls
executable file
·134 lines (105 loc) · 3.58 KB
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
/**
* Socket / websocket utils library. Handles outgoing and incoming messages
*/
var utils = require('./utils.js');
var socket = require('dgram').createSocket('udp4');
var config = require('./config.js');
var libsock = {
EVENT_KEY_ON_LIBSOCK_READY: 'ready',
EVENT_KEY_ON_LIBSOCK_BIND: 'bind',
EVENT_KEY_ON_LIBSOCK_MESSAGE: 'message',
EVENT_KEY_ON_LIBSOCK_CLOSE: 'close',
EVENT_KEY_ON_LIBSOCK_ERROR: 'close',
socketio_clients: {},
callbacks: {},
/**
* Bind socket to specified ip address and port
*
* @emits libsock.EVENT_KEY_ON_LIBSOCK_READY
*/
init: function(callback) {
if(typeof callback != 'function') {
callback = function() {};
}
socket.bind(config.get_config('mavlink').incoming_port, config.get_config('mavlink').incoming_host, function() {
libsock.emit(libsock.EVENT_KEY_ON_LIBSOCK_BIND, []);
});
// once socket is listening on the port
// call our callback function and continue
// the program's execution
socket.on('listening', function() {
callback.call(socket, config.get_config('mavlink').incoming_host, config.get_config('mavlink').incoming_port);
libsock.emit(libsock.EVENT_KEY_ON_LIBSOCK_READY, [config.get_config('mavlink').incoming_host, config.get_config('mavlink').incoming_port]);
});
socket.on('close', function() {
utils.log('libsock> Socket connection closed.');
libsock.emit(libsock.EVENT_KEY_ON_LIBSOCK_CLOSE, []);
});
socket.on('error', function(error) {
utils.log('ERR libsock> ' + error.toString());
libsock.emit(libsock.EVENT_KEY_ON_LIBSOCK_ERROR, [error]);
});
socket.on('message', function(message, rinfo) {
libsock.emit(libsock.EVENT_KEY_ON_LIBSOCK_MESSAGE, [message, rinfo]);
});
},
/**
* Sends data to all connected socket.io clients
*
* @param bcast_id String with id of broadcast message
* @param data Object containing data to send to clients
*/
io_broadcast: function(bcast_id, data) {
for(var i in libsock.socketio_clients) {
libsock.socketio_clients[i].emit(bcast_id, data);
}
},
/**
* Wrapper method for socket.send. Sends message to target host
*
* @param buffer Buffer to be sent to target host
* @param buff_start int defining buffer index containing beginning of content
* @param buff_end int defining buffer index containing end of content
* @param target_port Integer containing port of target host
* @param target_ip String containing ip address of target host
* @param callback Function to be called on buffer sent
*/
send: function(buffer, buff_start, buff_end, target_port, target_ip, callback) {
socket.send(buffer, buff_start, buff_end, target_port, target_ip, callback);
},
/**
* Register new client to be sent application events
*
* @param client_id String with hash of client connection
* @param client Object containing client connection data
*/
io_register: function(client_id, client) {
libsock.socketio_clients[client_id] = client;
},
/**
* Delete client previously signed up to receive socket events
*
* @param client_id String with hash of client connection
*/
io_unregister: function(client_id) {
delete libsock.socketio_clients[client_id];
},
on: function(evtKey, callback) {
if(!libsock.callbacks[evtKey]) {
libsock.callbacks[evtKey] = [];
}
libsock.callbacks[evtKey].push(callback);
},
emit: function(evtKey, params) {
if(!libsock.callbacks[evtKey]) {
return;
}
if(!(params instanceof Array)) {
params = [params];
}
for(var i = 0; i < libsock.callbacks[evtKey].length; i++) {
libsock.callbacks[evtKey][i].apply(libsock, params);
}
}
};
module.exports = libsock;