forked from baalexander/rosnodejs
/
node.js
129 lines (105 loc) · 4.25 KB
/
node.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
// Registers Publishers and Subscribers with ROS Master and helps negotiate
// connections with other Nodes. While the Node itself is not a Publisher or
// Subscriber, it contains a collection of each and acts as the contact point
// for them and ROS Master.
//
// Node.js extends the core Node model to communicate with ROS directly.
var url = require('url')
, xmlrpc = require('xmlrpc')
, portscanner = require('portscanner')
, ros = require('../public/ros')
, environment = require('./environment')
// Creates an XML-RPC server to handle the ROS Slave API calls.
ros.Node.prototype.save = function(attributes, options) {
this.server = this.createSlaveServer(function(error) {
options.success()
})
}
// Starts an XML-RPC server at an available port and listens for method calls
// for the ROS Slave API.
ros.Node.prototype.createSlaveServer = function(callback) {
var that = this
var hostname = environment.getHostname()
// Finds an available port to start the XML-RPC server.
portscanner.findAPortNotInUse(9000, null, hostname, function(error, port) {
var uriFields = { protocol: 'http', hostname: hostname, port: port }
, uri = url.format(uriFields)
that.set({ uri: uri })
// Creates an XML-RPC server listening at the given URI.
var server = xmlrpc.createServer(uri)
// Retrieves transport/topic statistics.
server.on('getBusStats', function(error, params, callback) {
callback()
})
// Retrieves transport/topic connection information.
server.on('getBusInfo', function(error, params, callback) {
callback()
})
// Gets the URI of the Master node.
server.on('getMasterUri', function(error, params, callback) {
callback()
})
// Stops this XML-RPC server.
server.on('shutdown', function(error, params, callback) {
callback()
})
// Gets the Process ID of this XML-RPC server.
server.on('getPid', function(error, params, callback) {
var callerId = params[0]
var code = 1
, statusMessage = 'Retrieved node PID'
, pid = process.pid
, params = [code, statusMessage, params]
callback(null, params)
})
// Returns a list of topics the Node currently subscribes to.
server.on('getSubscriptions', function(error, params, callback) {
callback()
})
// Returns a list of topics the Node currently publishes.
server.on('getPublications', function(error, params, callback) {
callback()
})
// Callback from Master with updated value of subscribed parameter.
server.on('paramUpdate', function(error, params, callback) {
callback()
})
// Callback from Master with the current list of publishers for a topic.
// Informs any Subscribers for the given topic of the updated list of
// publishers.
server.on('publisherUpdate', function(error, params, callback) {
var callerId = params[0]
, topicName = params[1]
, publishers = params[2]
if (topicName.length > 0 && topicName.charAt(0) === '/') {
topicName = topicName.substr(1, topicName.length - 1)
}
// If there's currently a Subscriber for the topic, passes on the updated
// list of Publishers for the topic.
var subscriber = that.subscribers.get(topicName)
subscriber.publisherUpdate(publishers, function(error) {
callback(error)
})
})
// A 'requestTopic' call is sent by a Subscriber node to a Publisher node to
// negotiate the protocol for the Publisher to send messages on.
//
// The preferred protocol params are passed to the Publisher of the
// specified topic.
server.on('requestTopic', function(error, params, callback) {
var callerId = params[0]
, topicName = params[1]
, protocols = params[2]
if (topicName.length > 0 && topicName.charAt(0) === '/') {
topicName = topicName.substr(1, topicName.length - 1)
}
// If there's currently a Publisher for the topic, passes the protocol
// options to that Publisher.
var publisher = that.publishers.get(topicName)
publisher.selectProtocol(protocols, function(error, protocolParams) {
callback(error, protocolParams)
})
})
callback(error)
})
}