public async Task <AsyncHandleResult> ConnectAsync( DiscoveredService discovered ) { WFDSvcWrapperHandle sessionHandle = null; Exception error = null; bool success = false; try { ThrowIfDisposed(); WiFiDirectTestLogger.Log("ConnectAsync... (Discovery={0})", discovered.Handle); sessionHandle = await discovered.ConnectAsync(); ServiceSession session = GetSession(sessionHandle); if (session.Session.Status != WiFiDirectServiceSessionStatus.Open) { throw new Exception("Session did not start successfully"); } success = true; } catch (Exception ex) { WiFiDirectTestLogger.Log("Exception in ConnectAsync (this may be expected)"); error = ex; } return(new AsyncHandleResult(sessionHandle, success, error)); }
public async Task <AsyncHandleResult> AddDatagramSocketAsync( ServiceSession session, UInt16 port ) { WFDSvcWrapperHandle socketHandle = null; Exception error = null; bool success = false; try { ThrowIfDisposed(); WiFiDirectTestLogger.Log( "AddDatagramSocketAsync... (port={0}) (Session={1})", port.ToString(), session.Handle ); SocketWrapper socket = await session.AddDatagramSocketListenerAsync(port); socketHandle = socket.Handle; success = true; } catch (Exception ex) { WiFiDirectTestLogger.Log("Exception in AddDatagramSocketAsync (this may be expected)"); error = ex; } return(new AsyncHandleResult(socketHandle, success, error)); }
public void PingSessionSuccess() { var loggerMock = new Mock <ILogger <ServiceSessionManager> >(); var sessionRepoMock = new Mock <IServiceSessionRepository>(MockBehavior.Strict); var mapperMock = new Mock <IMapper>(MockBehavior.Strict); var sessionManager = new ServiceSessionManager(loggerMock.Object, sessionRepoMock.Object, mapperMock.Object); var sessionId = Guid.NewGuid(); var registeredServiceId = "teste_service"; var serviceSession = new ServiceSession() { Id = sessionId, LastPing = null, RegisteredServiceId = registeredServiceId }; sessionRepoMock .Setup(r => r.GetById(sessionId)) .Returns(serviceSession) .Verifiable(); sessionRepoMock .Setup(r => r.Update(It.Is <ServiceSession>(s => s.Id == sessionId && s.RegisteredServiceId == registeredServiceId && s.LastPing != null))) .Returns(true) .Verifiable(); sessionManager.PingSession(sessionId); sessionRepoMock.Verify(); mapperMock.Verify(); }
/// <summary> /// Adds the session. /// </summary> /// <param name="registeredServiceId">The registered service identifier.</param> /// <returns> /// Returns the created <see cref="T:NAME.Registry.Domain.ServiceSession" /> /// </returns> public ServiceSessionDTO CreateSession(string registeredServiceId) { Guard.NotNull(registeredServiceId, nameof(registeredServiceId)); this.logger.LogInformation($"Creating a new session for the {nameof(RegisteredService)} with id {registeredServiceId}."); var oldValidSessions = this.sessionRepository.GetAllInRegisteredService(registeredServiceId, false).ToList(); ServiceSession session = new ServiceSession() { RegisteredServiceId = registeredServiceId, Bootstrapped = DateTime.UtcNow }; this.sessionRepository.Insert(session); if (oldValidSessions.Count() > 0) { foreach (var validSession in oldValidSessions) { validSession.Invalidated = DateTime.UtcNow; } this.sessionRepository.UpdateMany(oldValidSessions); } return(this.MapToDTO(session)); }
/// <summary> /// Adds the manifest snapshot to a service session. /// </summary> /// <param name="sessionId">The session identifier.</param> /// <param name="manifest">The manifest.</param> /// <returns> /// Returns the added <see cref="T:NAME.Registry.Domain.ManifestSnapshot" />. /// </returns> /// <exception cref="NAME.Registry.Exceptions.EntityNotFoundException">Happens when the entity is not found.</exception> /// <exception cref="RepositoryEntityOperationFailedException">ServiceSession</exception> public ManifestSnapshotDTO AddManifestSnapshot(Guid sessionId, string manifest) { Guard.NotNull(sessionId, nameof(sessionId)); Guard.NotNull(manifest, nameof(manifest)); ServiceSession session = this.sessionRepository.GetById(sessionId); if (session == null) { this.logger.LogWarning($"Received a {nameof(this.AddManifestSnapshot)} request for a nonexistent Session."); throw new EntityNotFoundException(); } this.logger.LogInformation($"Adding the manifest with to the session {session.Id}."); var now = DateTime.UtcNow; var snapshot = new ManifestSnapshot() { Manifest = manifest, DateAndTime = now }; session.LastPing = now; session.ManifestSnapshots.Add(snapshot); if (!this.sessionRepository.Update(session)) { throw new RepositoryEntityOperationFailedException(session, $"Failed to update the {nameof(ServiceSession)}."); } return(this.mapper.Map <ManifestSnapshot, ManifestSnapshotDTO>(snapshot)); }
/// <summary> /// 初始化上下文 /// </summary> public void InitContext() { if (ServiceSession.Context != null) { return; } ServiceSession.InitinalSession(null); ServiceSession.EnterOperator(this); }
public void GetSessionExistingId() { var loggerMock = new Mock <ILogger <ServiceSessionManager> >(); var sessionRepoMock = new Mock <IServiceSessionRepository>(MockBehavior.Strict); var mapperMock = new Mock <IMapper>(MockBehavior.Strict); var sessionManager = new ServiceSessionManager(loggerMock.Object, sessionRepoMock.Object, mapperMock.Object); var manifest = "test_manifest"; var dateAndTime = DateTime.UtcNow; var manifestSnapshot = new ManifestSnapshot { Manifest = manifest, DateAndTime = dateAndTime }; var sessionId = Guid.NewGuid(); var serviceSession = new ServiceSession() { Id = sessionId, ManifestSnapshots = new List <ManifestSnapshot> { manifestSnapshot } }; sessionRepoMock .Setup(r => r.GetById(sessionId)) .Returns(serviceSession) .Verifiable(); mapperMock .Setup(m => m.Map <ServiceSession, ServiceSessionDTO>(It.Is <ServiceSession>(s => s == serviceSession))) .Returns(new ServiceSessionDTO { Id = sessionId.ToString() }).Verifiable(); mapperMock .Setup(m => m.Map <ManifestSnapshot, ManifestSnapshotDTO>(It.Is <ManifestSnapshot>(s => s == manifestSnapshot))) .Returns(new ManifestSnapshotDTO { Manifest = manifest, DateAndTime = dateAndTime }).Verifiable(); var result = sessionManager.GetById(sessionId); Assert.Equal(serviceSession.Id.ToString(), result.Id); Assert.Equal(manifestSnapshot.Manifest, result.LastManifestSnapshot.Manifest); Assert.Equal(manifestSnapshot.DateAndTime, result.LastManifestSnapshot.DateAndTime); sessionRepoMock.Verify(); mapperMock.Verify(); }
private ServiceSessionDTO MapToDTO(ServiceSession domainObject) { var mappedSession = this.mapper.Map <ServiceSession, ServiceSessionDTO>(domainObject); var lastSnapshot = domainObject.ManifestSnapshots.OrderBy(m => m.DateAndTime).LastOrDefault(); if (lastSnapshot != null) { mappedSession.LastManifestSnapshot = this.mapper.Map <ManifestSnapshot, ManifestSnapshotDTO>(lastSnapshot); } return(mappedSession); }
public WFDSvcWrapperHandle GetLastRemoteSocketAdded( ServiceSession session ) { WiFiDirectTestLogger.Log("Waiting for session to get RemotePortAdded event and setup socket (Session={0})", session.Handle); session.WaitForRemotePortAdded(); WFDSvcWrapperHandle socketHandle = session.LastRemoteSocketAdded; return(socketHandle); }
public ServiceSession GetSession(WFDSvcWrapperHandle handle) { ThrowIfDisposed(); ServiceSession session = sessionCollection[handle]; if (session == null) { throw new Exception(String.Format(CultureInfo.InvariantCulture, "Session not found for handle={0}", handle)); } return(session); }
public void AddManifestSnapshotValidValues() { var loggerMock = new Mock <ILogger <ServiceSessionManager> >(); var sessionRepoMock = new Mock <IServiceSessionRepository>(MockBehavior.Strict); var mapperMock = new Mock <IMapper>(MockBehavior.Strict); var sessionManager = new ServiceSessionManager(loggerMock.Object, sessionRepoMock.Object, mapperMock.Object); var manifest = "teste_manifest"; var sessionId = Guid.NewGuid(); var registeredServiceId = "teste_service"; var session = new ServiceSession { Id = sessionId, RegisteredServiceId = registeredServiceId }; sessionRepoMock .Setup(r => r.GetById(It.Is <Guid>(g => g == sessionId))) .Returns(new ServiceSession() { Id = sessionId, RegisteredServiceId = registeredServiceId }).Verifiable(); sessionRepoMock .Setup(r => r.Update(It.Is <ServiceSession>(s => s.ManifestSnapshots[0].Manifest == manifest && s.Id == sessionId && s.RegisteredServiceId == registeredServiceId) )).Returns(true) .Verifiable(); mapperMock .Setup(m => m.Map <ManifestSnapshot, ManifestSnapshotDTO>(It.Is <ManifestSnapshot>(s => s.Manifest == manifest ))).Returns(new ManifestSnapshotDTO { Manifest = manifest }).Verifiable(); var result = sessionManager.AddManifestSnapshot(sessionId, manifest); Assert.Equal(manifest, result.Manifest); sessionRepoMock.Verify(); mapperMock.Verify(); }
/// <summary> /// 清除上下文 /// </summary> public void ClearContext() { if (ServiceSession.Context == null || ServiceSession.Context.Operator != this) { return; } try { ServiceSession.Context.Dispose(); ServiceSession.ExitOperator(this); } finally { ServiceSession.ReleaseSession(); } }
/// <summary> /// Pings the session. /// </summary> /// <param name="sessionId">The session identifier.</param> /// <exception cref="EntityNotFoundException">Happens when the entity was not found.</exception> /// <exception cref="RepositoryEntityOperationFailedException">ServiceSession</exception> public void PingSession(Guid sessionId) { Guard.NotNull(sessionId, nameof(sessionId)); ServiceSession session = this.sessionRepository.GetById(sessionId); if (session == null) { this.logger.LogWarning($"Received a {nameof(this.AddManifestSnapshot)} request for a nonexistent Session."); throw new EntityNotFoundException(); } var theTime = DateTime.UtcNow; this.logger.LogInformation($"Setting the {nameof(session.LastPing)} of the {nameof(ServiceSession)} with id {session.Id} to {theTime}."); session.LastPing = DateTime.UtcNow; if (!this.sessionRepository.Update(session)) { throw new RepositoryEntityOperationFailedException(session, $"Failed to update the {nameof(ServiceSession)}."); } }
public void PingSessionUpdateFailure() { var loggerMock = new Mock <ILogger <ServiceSessionManager> >(); var sessionRepoMock = new Mock <IServiceSessionRepository>(MockBehavior.Strict); var mapperMock = new Mock <IMapper>(MockBehavior.Strict); var sessionManager = new ServiceSessionManager(loggerMock.Object, sessionRepoMock.Object, mapperMock.Object); var sessionId = Guid.NewGuid(); var registeredServiceId = "teste_service"; var serviceSession = new ServiceSession() { Id = sessionId, LastPing = null, RegisteredServiceId = registeredServiceId }; sessionRepoMock .Setup(r => r.GetById(sessionId)) .Returns(serviceSession) .Verifiable(); sessionRepoMock .Setup(r => r.Update(serviceSession)) .Returns(false) .Verifiable(); var thrownException = Assert.Throws <RepositoryEntityOperationFailedException>(() => { sessionManager.PingSession(sessionId); }); Assert.IsType <ServiceSession>(thrownException.Entity); Assert.Equal(serviceSession, thrownException.Entity); sessionRepoMock.Verify(); mapperMock.Verify(); }
public void AddSession(ServiceSession session) { sessionCollection.Add(session.Handle, session); // Make sure we clear the global pin display/entry state for new connections ClearPin(); }
/// <summary> /// Inserts the specified service. /// </summary> /// <param name="service">The service.</param> public void Insert(ServiceSession service) { this.sessionCollection.Insert(service); }
/// <summary> /// Updates the specified service session. /// </summary> /// <param name="session">The service session.</param> /// <returns> /// Returns true if a service session is updated. /// </returns> public bool Update(ServiceSession session) { return(this.sessionCollection.Update(session)); }