Skip to content

Commit a0b6193

Browse files
authored
Merge pull request #177 from alexdrenea/feature-SharingAutoDiscovery
AutoDiscover in Sharing Stage
2 parents 52225b5 + 9bd99d1 commit a0b6193

File tree

3 files changed

+148
-26
lines changed

3 files changed

+148
-26
lines changed

Assets/HoloToolkit/Sharing/Scripts/SharingStage.cs

Lines changed: 126 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEngine;
23

34
namespace HoloToolkit.Sharing
45
{
56
public class SharingStage : MonoBehaviour
67
{
8+
/// <summary>
9+
/// SharingManagerConnected event notifies when the sharing manager is created and connected.
10+
/// </summary>
11+
public event EventHandler SharingManagerConnected;
12+
713
public static SharingStage Instance = null;
814

915
/// <summary>
@@ -24,45 +30,81 @@ public class SharingStage : MonoBehaviour
2430
/// </summary>
2531
public bool IsAudioEndpoint = true;
2632

33+
public bool AutoDiscoverServer = false;
34+
35+
[Tooltip("Determines how often the discovery service should ping the network in search of a server.")]
36+
public float PingIntervalSec = 2;
37+
2738
/// <summary>
2839
/// Pipes XTools console output to Unity's output window for debugging
2940
/// </summary>
3041
private ConsoleLogWriter logWriter;
3142

43+
/// <summary>
44+
/// Enables Server Discovery on the network
45+
/// </summary>
46+
private DiscoveryClient discoveryClient;
47+
/// <summary>
48+
/// Provides callbacks when server is discovered or lost.
49+
/// </summary>
50+
private DiscoveryClientAdapter discoveryClientAdapter;
51+
52+
private float pingIntervalCurrent = 0;
53+
private bool isTryingToFindServer = false;
54+
3255
private void Awake()
3356
{
3457
Instance = this;
3558

3659
this.logWriter = new ConsoleLogWriter();
3760

38-
ClientConfig config = new ClientConfig(this.ClientRole);
39-
config.SetIsAudioEndpoint(this.IsAudioEndpoint);
40-
config.SetLogWriter(this.logWriter);
41-
config.SetServerAddress(this.ServerAddress);
42-
config.SetServerPort(this.ServerPort);
43-
config.SetProfilerEnabled(false);
44-
45-
this.sharingMgr = SharingManager.Create(config);
61+
if (AutoDiscoverServer)
62+
{
63+
AutoDiscoverInit();
64+
}
65+
else
66+
{
67+
Connect();
68+
}
4669
}
4770

4871
protected void OnDestroy()
4972
{
5073
Instance = null;
5174

52-
// Force a disconnection so that we can stop and start Unity without connections hanging around
53-
this.sharingMgr.GetPairedConnection().Disconnect();
54-
this.sharingMgr.GetServerConnection().Disconnect();
75+
if (this.discoveryClient != null)
76+
{
77+
discoveryClient.RemoveListener(discoveryClientAdapter);
78+
discoveryClient.Dispose();
79+
discoveryClient = null;
80+
81+
if (discoveryClientAdapter != null)
82+
{
83+
discoveryClientAdapter.Dispose();
84+
discoveryClientAdapter = null;
85+
}
86+
}
5587

56-
// Release the XTools manager so that it cleans up the C++ copy
57-
this.sharingMgr.Dispose();
58-
this.sharingMgr = null;
88+
if (this.sharingMgr != null)
89+
{
90+
// Force a disconnection so that we can stop and start Unity without connections hanging around
91+
this.sharingMgr.GetPairedConnection().Disconnect();
92+
this.sharingMgr.GetServerConnection().Disconnect();
5993

94+
// Release the XTools manager so that it cleans up the C++ copy
95+
this.sharingMgr.Dispose();
96+
this.sharingMgr = null;
97+
}
6098
// Forces a garbage collection to try to clean up any additional reference to SWIG-wrapped objects
6199
System.GC.Collect();
62100
}
63101

64102
private void LateUpdate()
65103
{
104+
if (this.isTryingToFindServer)
105+
{
106+
AutoDiscoverUpdate();
107+
}
66108
if (this.sharingMgr != null)
67109
{
68110
// Update the XToolsManager to processes any network messages that have arrived
@@ -80,6 +122,75 @@ private void OnDisable()
80122
Application.logMessageReceived -= HandleLog;
81123
}
82124

125+
private void Connect()
126+
{
127+
ClientConfig config = new ClientConfig(this.ClientRole);
128+
config.SetIsAudioEndpoint(this.IsAudioEndpoint);
129+
config.SetLogWriter(this.logWriter);
130+
config.SetServerAddress(this.ServerAddress);
131+
config.SetServerPort(this.ServerPort);
132+
config.SetProfilerEnabled(false);
133+
134+
this.sharingMgr = SharingManager.Create(config);
135+
136+
//delay sending notification so everything is initialized properly
137+
Invoke("SendConnectedNotification", 1);
138+
}
139+
140+
private void SendConnectedNotification()
141+
{
142+
if (Manager.GetServerConnection().IsConnected())
143+
{
144+
//Send notification that we're connected
145+
EventHandler connectedEvent = SharingManagerConnected;
146+
if (connectedEvent != null)
147+
{
148+
connectedEvent(this, EventArgs.Empty);
149+
}
150+
}
151+
else
152+
{
153+
Log.Error(string.Format("Cannot connect to server {0}:{1}", ServerAddress, ServerPort));
154+
}
155+
}
156+
157+
private void AutoDiscoverInit()
158+
{
159+
discoveryClientAdapter = new DiscoveryClientAdapter();
160+
discoveryClientAdapter.DiscoveredEvent += OnSystemDiscovered;
161+
162+
discoveryClient = DiscoveryClient.Create();
163+
discoveryClient.AddListener(discoveryClientAdapter);
164+
165+
//Start Finding Server
166+
isTryingToFindServer = true;
167+
}
168+
169+
private void AutoDiscoverUpdate()
170+
{
171+
//Searching Enabled-> Update DiscoveryClient to check results, Wait Interval then Ping network.
172+
pingIntervalCurrent += Time.deltaTime;
173+
if (pingIntervalCurrent > PingIntervalSec)
174+
{
175+
pingIntervalCurrent = 0;
176+
discoveryClient.Ping();
177+
}
178+
discoveryClient.Update();
179+
}
180+
181+
private void OnSystemDiscovered(DiscoveredSystem obj)
182+
{
183+
if (obj.GetRole() == SystemRole.SessionDiscoveryServerRole)
184+
{
185+
//Found a server. Stop pinging the network and connect
186+
isTryingToFindServer = false;
187+
this.ServerAddress = obj.GetAddress();
188+
Debug.Log("System Discovered at: " + this.ServerAddress);
189+
Connect();
190+
Debug.Log(string.Format("Connected to: {0}:{1}", this.ServerAddress, this.ServerPort));
191+
}
192+
}
193+
83194
void HandleLog(string logString, string stackTrace, LogType type)
84195
{
85196
switch (type)

Assets/HoloToolkit/Sharing/Tests/CustomMessages.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public Dictionary<TestMessageID, MessageCallback> MessageHandlers
5454
NetworkConnection serverConnection;
5555

5656
void Start()
57+
{
58+
SharingStage.Instance.SharingManagerConnected += SharingManagerConnected;
59+
}
60+
61+
private void SharingManagerConnected(object sender, System.EventArgs e)
5762
{
5863
InitializeMessageHandlers();
5964
}

Assets/HoloToolkit/Sharing/Tests/ImportExportAnchorManager.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,8 @@ void Start()
126126
currentState = ImportExportState.AnchorStore_Initializing;
127127
WorldAnchorStore.GetAsync(AnchorStoreReady);
128128

129-
// We will register for session joined to indicate when the sharing service
130-
// is ready for us to make room related requests.
131-
SharingSessionTracker.Instance.SessionJoined += Instance_SessionJoined;
132-
133-
// Setup the room manager callbacks.
134-
roomManager = SharingStage.Instance.Manager.GetRoomManager();
135-
roomManagerCallbacks = new RoomManagerAdapter();
136-
137-
roomManagerCallbacks.AnchorsDownloadedEvent += RoomManagerCallbacks_AnchorsDownloaded;
138-
roomManagerCallbacks.AnchorUploadedEvent += RoomManagerCallbacks_AnchorUploaded;
139-
roomManager.AddListener(roomManagerCallbacks);
129+
//Wait for a notification that the sharing manager has been initialized (connected to sever)
130+
SharingStage.Instance.SharingManagerConnected += SharingManagerConnected;
140131
}
141132

142133
void OnDestroy()
@@ -153,6 +144,21 @@ void OnDestroy()
153144
}
154145
}
155146

147+
private void SharingManagerConnected(object sender, EventArgs e)
148+
{
149+
// Setup the room manager callbacks.
150+
roomManager = SharingStage.Instance.Manager.GetRoomManager();
151+
roomManagerCallbacks = new RoomManagerAdapter();
152+
153+
roomManagerCallbacks.AnchorsDownloadedEvent += RoomManagerCallbacks_AnchorsDownloaded;
154+
roomManagerCallbacks.AnchorUploadedEvent += RoomManagerCallbacks_AnchorUploaded;
155+
roomManager.AddListener(roomManagerCallbacks);
156+
157+
// We will register for session joined to indicate when the sharing service
158+
// is ready for us to make room related requests.
159+
SharingSessionTracker.Instance.SessionJoined += Instance_SessionJoined;
160+
}
161+
156162
/// <summary>
157163
/// Called when anchor upload operations complete.
158164
/// </summary>

0 commit comments

Comments
 (0)