Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharing fixes #306

Merged
merged 11 commits into from
Nov 3, 2016
51 changes: 40 additions & 11 deletions Assets/HoloToolkit/Sharing/Scripts/SharingStage.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using HoloToolkit.Unity;
using System;
using UnityEngine;

namespace HoloToolkit.Sharing
{
public class SharingStage : MonoBehaviour
public class SharingStage : Singleton<SharingStage>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice :)

{
/// <summary>
/// SharingManagerConnected event notifies when the sharing manager is created and connected.
/// </summary>
public event EventHandler SharingManagerConnected;

public static SharingStage Instance = null;

/// <summary>
/// Set whether this app should be a Primary or Secondary client.
/// Primary: Connects directly to the Session Server, can create/join/leave sessions
Expand Down Expand Up @@ -48,30 +47,39 @@ public class SharingStage : MonoBehaviour
/// Provides callbacks when server is discovered or lost.
/// </summary>
private DiscoveryClientAdapter discoveryClientAdapter;

/// <summary>
/// Provides callbacks for when we connect to a server.
/// </summary>
private NetworkConnectionAdapter networkConnectionAdapter = null;

private NetworkConnection networkConnection = null;

private float pingIntervalCurrent = 0;
private bool isTryingToFindServer = false;

private void Awake()
{
Instance = this;

this.logWriter = new ConsoleLogWriter();

if (AutoDiscoverServer)
{
AutoDiscoverInit();
}
else

networkConnectionAdapter = new NetworkConnectionAdapter();
}

private void Start()
{
if (!AutoDiscoverServer)
{
Connect();
}
}

protected void OnDestroy()
{
Instance = null;

if (this.discoveryClient != null)
{
discoveryClient.RemoveListener(discoveryClientAdapter);
Expand All @@ -85,6 +93,19 @@ protected void OnDestroy()
}
}

if (this.networkConnection != null)
{
networkConnection.RemoveListener((byte)MessageID.StatusOnly, networkConnectionAdapter);
networkConnection.Dispose();
networkConnection = null;

if (networkConnectionAdapter != null)
{
networkConnectionAdapter.Dispose();
networkConnectionAdapter = null;
}
}

if (this.sharingMgr != null)
{
// Force a disconnection so that we can stop and start Unity without connections hanging around
Expand Down Expand Up @@ -133,8 +154,16 @@ private void Connect()

this.sharingMgr = SharingManager.Create(config);

//delay sending notification so everything is initialized properly
Invoke("SendConnectedNotification", 1);
//set up callbacks so that we know when we've connected successfully
this.networkConnection = sharingMgr.GetServerConnection();
this.networkConnectionAdapter = new NetworkConnectionAdapter();
networkConnectionAdapter.ConnectedCallback += NetworkConnectionAdapter_ConnectedCallback;
networkConnection.AddListener((byte)MessageID.StatusOnly, networkConnectionAdapter);
}

private void NetworkConnectionAdapter_ConnectedCallback(NetworkConnection obj)
{
SendConnectedNotification();
}

private void SendConnectedNotification()
Expand Down
71 changes: 38 additions & 33 deletions Assets/HoloToolkit/Sharing/Tests/ImportExportAnchorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ImportExportAnchorManager : Singleton<ImportExportAnchorManager>
/// <summary>
/// Enum to track the progress through establishing a shared coordinate system.
/// </summary>
enum ImportExportState
private enum ImportExportState
{
// Overall states
Start,
Expand All @@ -39,7 +39,7 @@ enum ImportExportState
Importing
}

ImportExportState currentState = ImportExportState.Start;
private ImportExportState currentState = ImportExportState.Start;

public string StateName
{
Expand All @@ -60,78 +60,87 @@ public bool AnchorEstablished
/// <summary>
/// WorldAnchorTransferBatch is the primary object in serializing/deserializing anchors.
/// </summary>
WorldAnchorTransferBatch sharedAnchorInterface;
private WorldAnchorTransferBatch sharedAnchorInterface;

/// <summary>
/// Keeps track of stored anchor data blob.
/// </summary>
byte[] rawAnchorData = null;
private byte[] rawAnchorData = null;

/// <summary>
/// Keeps track of locally stored anchors.
/// </summary>
WorldAnchorStore anchorStore = null;
private WorldAnchorStore anchorStore = null;

/// <summary>
/// Keeps track of the name of the anchor we are exporting.
/// </summary>
string exportingAnchorName { get; set; }
private string exportingAnchorName { get; set; }

/// <summary>
/// The datablob of the anchor.
/// </summary>
List<byte> exportingAnchorBytes = new List<byte>();
private List<byte> exportingAnchorBytes = new List<byte>();

/// <summary>
/// Keeps track of if the sharing service is ready.
/// We need the sharing service to be ready so we can
/// upload and download data for sharing anchors.
/// </summary>
bool sharingServiceReady = false;
private bool sharingServiceReady = false;

/// <summary>
/// The room manager API for the sharing service.
/// </summary>
RoomManager roomManager;
private RoomManager roomManager;

/// <summary>
/// Keeps track of the current room we are connected to. Anchors
/// are kept in rooms.
/// </summary>
Room currentRoom;
private Room currentRoom;

/// <summary>
/// Sometimes we'll see a really small anchor blob get generated.
/// These tend to not work, so we have a minimum trustable size.
/// </summary>
const uint minTrustworthySerializedAnchorDataSize = 100000;
private const uint minTrustworthySerializedAnchorDataSize = 100000;

/// <summary>
/// Some room ID for indicating which room we are in.
/// </summary>
const long roomID = 8675309;
private const long roomID = 8675309;

/// <summary>
/// Provides updates when anchor data is uploaded/downloaded.
/// </summary>
RoomManagerAdapter roomManagerCallbacks;
private RoomManagerAdapter roomManagerCallbacks;

void Start()
private void Awake()
{
Debug.Log("Import Export Manager starting");

currentState = ImportExportState.Ready;

// We need to get our local anchor store started up.
currentState = ImportExportState.AnchorStore_Initializing;
WorldAnchorStore.GetAsync(AnchorStoreReady);
}

private void Start()
{
//Wait for a notification that the sharing manager has been initialized (connected to sever)
SharingStage.Instance.SharingManagerConnected += SharingManagerConnected;

// We will register for session joined to indicate when the sharing service
// is ready for us to make room related requests.
SharingSessionTracker.Instance.SessionJoined += Instance_SessionJoined;
}

void OnDestroy()
private void OnDestroy()
{
if (SharingStage.Instance != null)
{
SharingStage.Instance.SharingManagerConnected -= SharingManagerConnected;
}

if (roomManagerCallbacks != null)
{
roomManagerCallbacks.AnchorsDownloadedEvent -= RoomManagerCallbacks_AnchorsDownloaded;
Expand All @@ -153,10 +162,6 @@ private void SharingManagerConnected(object sender, EventArgs e)
roomManagerCallbacks.AnchorsDownloadedEvent += RoomManagerCallbacks_AnchorsDownloaded;
roomManagerCallbacks.AnchorUploadedEvent += RoomManagerCallbacks_AnchorUploaded;
roomManager.AddListener(roomManagerCallbacks);

// We will register for session joined to indicate when the sharing service
// is ready for us to make room related requests.
SharingSessionTracker.Instance.SessionJoined += Instance_SessionJoined;
}

/// <summary>
Expand Down Expand Up @@ -202,7 +207,7 @@ private void RoomManagerCallbacks_AnchorsDownloaded(bool successful, AnchorDownl
/// Called when the local anchor store is ready.
/// </summary>
/// <param name="store"></param>
void AnchorStoreReady(WorldAnchorStore store)
private void AnchorStoreReady(WorldAnchorStore store)
{
anchorStore = store;
currentState = ImportExportState.AnchorStore_Initialized;
Expand All @@ -224,15 +229,15 @@ private void Instance_SessionJoined(object sender, SharingSessionTracker.Session
Invoke("MarkSharingServiceReady", 5);
}

void MarkSharingServiceReady()
private void MarkSharingServiceReady()
{
sharingServiceReady = true;
}

/// <summary>
/// Initializes the room api.
/// </summary>
void InitRoomApi()
private void InitRoomApi()
{
// If we have a room, we'll join the first room we see.
// If we are the user with the lowest user ID, we will create the room.
Expand Down Expand Up @@ -262,7 +267,7 @@ void InitRoomApi()
}
}

bool LocalUserHasLowestUserId()
private bool LocalUserHasLowestUserId()
{
long localUserId = CustomMessages.Instance.localUserID;
foreach (long userid in SharingSessionTracker.Instance.UserIds)
Expand All @@ -279,7 +284,7 @@ bool LocalUserHasLowestUserId()
/// <summary>
/// Kicks off the process of creating the shared space.
/// </summary>
void StartAnchorProcess()
private void StartAnchorProcess()
{
// First, are there any anchors in this room?
int anchorCount = currentRoom.GetAnchorCount();
Expand All @@ -306,7 +311,7 @@ void StartAnchorProcess()
/// <summary>
/// Kicks off getting the datablob required to import the shared anchor.
/// </summary>
void MakeAnchorDataRequest()
private void MakeAnchorDataRequest()
{
if (roomManager.DownloadAnchor(currentRoom, currentRoom.GetAnchorName(0)))
{
Expand All @@ -319,7 +324,7 @@ void MakeAnchorDataRequest()
}
}

void Update()
private void Update()
{
switch (currentState)
{
Expand Down Expand Up @@ -353,7 +358,7 @@ void Update()
/// <summary>
/// Starts establishing a new anchor.
/// </summary>
void CreateAnchorLocally()
private void CreateAnchorLocally()
{
WorldAnchor anchor = GetComponent<WorldAnchor>();
if (anchor == null)
Expand Down Expand Up @@ -394,7 +399,7 @@ private void Anchor_OnTrackingChanged_InitialAnchor(WorldAnchor self, bool locat
/// Attempts to attach to an anchor by anchorName in the local store..
/// </summary>
/// <returns>True if it attached, false if it could not attach</returns>
bool AttachToCachedAnchor(string AnchorName)
private bool AttachToCachedAnchor(string AnchorName)
{
Debug.Log("Looking for " + AnchorName);
string[] ids = anchorStore.GetAllIds();
Expand Down Expand Up @@ -446,7 +451,7 @@ private void ImportExportAnchorManager_OnTrackingChanged_Attaching(WorldAnchor s
/// </summary>
/// <param name="status"></param>
/// <param name="wat"></param>
void ImportComplete(SerializationCompletionReason status, WorldAnchorTransferBatch wat)
private void ImportComplete(SerializationCompletionReason status, WorldAnchorTransferBatch wat)
{
if (status == SerializationCompletionReason.Succeeded && wat.GetAllIds().Length > 0)
{
Expand All @@ -469,7 +474,7 @@ void ImportComplete(SerializationCompletionReason status, WorldAnchorTransferBat
/// <summary>
/// Exports the currently created anchor.
/// </summary>
void Export()
private void Export()
{
WorldAnchor anchor = GetComponent<WorldAnchor>();

Expand Down