forked from michaeljenkin/unityros
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathROSBridgeConnectionsManager.cs
93 lines (86 loc) · 4.02 KB
/
ROSBridgeConnectionsManager.cs
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
using System.Collections.Generic;
using UnityEngine;
namespace ROSBridgeLib
{
/// <summary>
/// A singleton class that ensures there is only one ROSBridgeWebSocketConnection per address/port pair.
/// It handles opening, giving thread time to, and closing each ROSBridgeWebSocketConnection.
/// <remarks>
/// This allows multiple data client objects to share a ROSBridgeWebSocketConnection to the same rosbridge server.
/// It also reduces unecessary calls to ROSBridgeWebSocketConnection.Render(). Before each user of the ROSBridgeWebSocketConnection
/// object would call its Render() function to guarantee it had at least one object giving it render thread runtime to execute its
/// necessary functions. Having it be managed by one script unifies this so that it is given one call per render cycle.
/// </remarks>
/// </summary>
public class ROSBridgeConnectionsManager : MonoBehaviour
{
/// <value> The reference to the singleton instance of this class.</value>
private static ROSBridgeConnectionsManager _instance;
/// <value> A lock to ensure that only one object can check and set the _instance variable at a time.</value>
private static object instance_lock = new object();
/// <value> The public accessor variable to the singleton instance.</value>
public static ROSBridgeConnectionsManager Instance
{
get
{
return _instance;
}
}
/// <value> The mapping from rosbridge address "ws://address:port" to the ROSBridgeWebSocketConnection object associated with that address.</value>
private static Dictionary<string, ROSBridgeWebSocketConnection> connections = new Dictionary<string, ROSBridgeWebSocketConnection>();
/// <value> The lock for the connections dictionary.</value>
private object connection_lock = new object();
private void Awake()
{
// Fill the singleton instance variable with the current object if it is the first instance.
lock (instance_lock)
{
if (_instance == null)
{
_instance = this;
}
}
}
/// <summary>
/// Obtain the unique ROSBridgeWebSocketConnection object for a given address and port.
/// </summary>
/// <param name="address">The network or web address of the rosbridge server.</param>
/// <param name="port">The port that the rosbridge server uses on the remote machine.</param>
/// <returns></returns>
public ROSBridgeWebSocketConnection GetConnection(string address, int port)
{
string full_address = string.Format("ws://{0}:{1}", address, port);
ROSBridgeWebSocketConnection conn;
lock (connection_lock)
{
// Check whether a connection has already been established for this address/port pair.
if (!connections.TryGetValue(full_address, out conn))
{
Debug.Log("Starting submap connection to " + full_address);
conn = new ROSBridgeWebSocketConnection("ws://" + address, port);
conn.Connect();
connections[full_address] = conn;
}
}
return conn;
}
// Update is called once per frame
void Update()
{
// Give each ROSBridgeWebSocketConnection render thread time.
foreach (ROSBridgeWebSocketConnection conn in connections.Values)
{
conn.Render();
}
}
void OnApplicationQuit()
{
// Disconnect connections to all remote machines.
Debug.Log("Disconnecting from ROS");
foreach (ROSBridgeWebSocketConnection conn in connections.Values)
{
conn.Disconnect();
}
}
}
}