private async Task <Scene> GetScene(string sceneId, SceneEndpoint ci) { if (_serverConnection == null) { if (!_transport.IsRunning) { cts = new CancellationTokenSource(); _transport.Start("client", new ConnectionHandler(), cts.Token, null, (ushort)(_maxPeers + 1)); } if (!ci.TokenData.Endpoints.ContainsKey(_transport.Name)) { throw new InvalidOperationException($"No endpoint available on server for transport '{_transport.Name}'"); } _serverConnection = await _transport.Connect(ci.TokenData.Endpoints[_transport.Name]); foreach (var kvp in _metadata) { _serverConnection.Metadata[kvp.Key] = kvp.Value; } await UpdateServerMetadata(); if (ci.TokenData.Version > 0) { StartSyncClock(); } } var parameter = new Stormancer.Dto.SceneInfosRequestDto { Metadata = _serverConnection.Metadata, Token = ci.Token }; var result = await SendSystemRequest <Stormancer.Dto.SceneInfosRequestDto, Stormancer.Dto.SceneInfosDto>((byte)SystemRequestIDTypes.ID_GET_SCENE_INFOS, parameter); if (_serverConnection.GetComponent <ISerializer>() == null) { if (result.SelectedSerializer == null) { throw new InvalidOperationException("No seralizer selected."); } _serverConnection.RegisterComponent(_serializers[result.SelectedSerializer]); _serverConnection.Metadata.Add("serializer", result.SelectedSerializer); } await UpdateServerMetadata(); var scene = new Scene(this._serverConnection, this, _pluginCtx.BuildingSceneResolver, sceneId, ci.Token, result); if (_pluginCtx.SceneCreated != null) { _pluginCtx.SceneCreated(scene); } return(scene); }
/// <summary> /// Returns a private scene (requires a token obtained from strong authentication with the Stormancer API). /// </summary> /// <remarks> /// The effective connection happens when "Connect" is called on the scene. Note that when you call GetScene, /// a connection token is requested from the Stormancer API.this token is only valid for a few minutes: Don't get scenes /// a long time before connecting to them. /// </remarks> /// <param name="token">The token securing the connection.</param> /// <returns>A task returning the scene object on completion.</returns> private Task <Scene> GetScene(string sceneId, SceneEndpoint ci) { return(TaskHelper.If(_serverConnection == null, () => { return TaskHelper.If(!_transport.IsRunning, () => { _cts = new CancellationTokenSource(); return _transport.Start("client", new ConnectionHandler(), _cts.Token, null, (ushort)(_maxPeers + 1)); }) .Then(() => _transport.Connect(ci.TokenData.Endpoints[_transport.Name])) .Then(connection => { this.Logger.Log(Stormancer.Diagnostics.LogLevel.Trace, sceneId, string.Format("Trying to connect to scene '{0}' through endpoint : '{1}' on application : '{2}' with id : '{3}'", sceneId, ci.TokenData.Endpoints[_transport.Name], ci.TokenData.Application, ci.TokenData.AccountId)); _serverConnection = connection; foreach (var kvp in _metadata) { _serverConnection.Metadata[kvp.Key] = kvp.Value; } return this.UpdateServerMetadata(); }) .Then(() => { if (ci.TokenData.Version > 0) { StartSyncClock(); } }); }).Then(() => { var parameter = new Stormancer.Dto.SceneInfosRequestDto { Metadata = _serverConnection.Metadata, Token = ci.Token }; return SendSystemRequest <Stormancer.Dto.SceneInfosRequestDto, Stormancer.Dto.SceneInfosDto>((byte)SystemRequestIDTypes.ID_GET_SCENE_INFOS, parameter); }).Then(result => { if (_serverConnection.GetComponent <ISerializer>() == null) { if (result.SelectedSerializer == null) { this.Logger.Error("No serializer selected"); throw new InvalidOperationException("No serializer selected."); } _serverConnection.RegisterComponent(_serializers[result.SelectedSerializer]); _serverConnection.Metadata.Add("serializer", result.SelectedSerializer); this.Logger.Info("Serializer selected: " + result.SelectedSerializer); } return UpdateServerMetadata().Then(() => { var scene = new Scene(this._serverConnection, this, sceneId, ci.Token, result, _pluginCtx, DependencyResolver); var ev = _pluginCtx.SceneCreated; if (ev != null) { ev(scene); } return scene; }); })); //if (_serverConnection == null) //{ // if (!_transport.IsRunning) // { // cts = new CancellationTokenSource(); // await _transport.Start("client", new ConnectionHandler(), cts.Token, null, (ushort)(_maxPeers + 1)); // } // _serverConnection = await _transport.Connect(ci.TokenData.Endpoints[_transport.Name]); //} //var parameter = new Stormancer.Dto.SceneInfosRequestDto { Metadata = _metadata, Token = ci.Token }; //var result = await SendSystemRequest<Stormancer.Dto.SceneInfosRequestDto, Stormancer.Dto.SceneInfosDto>((byte)MessageIDTypes.ID_GET_SCENE_INFOS, parameter); //if (!_serverConnection.Components.ContainsKey("serializer")) //{ // if (result.SelectedSerializer == null) // { // throw new InvalidOperationException("No seralizer selected."); // } // _serverConnection.Components["serializer"] = _serializers[result.SelectedSerializer]; //} //var scene = new Scene(this._serverConnection, this, sceneId, ci.Token, result); //_scenesDispatcher.AddScene(scene); //return scene; }