public void Udp_Discovery_Service_Lost() { ResonanceUdpDiscoveryService <DiscoveryInfo, JsonEncoder> service = new ResonanceUdpDiscoveryService <DiscoveryInfo, JsonEncoder>(new DiscoveryInfo() { Identity = "Test Identity" }, 1984); service.Start(); ResonanceUdpDiscoveryClient <DiscoveryInfo, JsonDecoder> client = new ResonanceUdpDiscoveryClient <DiscoveryInfo, JsonDecoder>(1984); AutoResetEvent waitHandle = new AutoResetEvent(false); bool lost = false; client.ServiceLost += (x, e) => { lost = true; waitHandle.Set(); }; var services = client.Discover(TimeSpan.FromSeconds(10), 1); Assert.IsTrue(services.Count == 1); Assert.IsTrue(services[0].DiscoveryInfo.Identity == service.DiscoveryInfo.Identity); service.Dispose(); waitHandle.WaitOne(TimeSpan.FromSeconds(10)); Assert.IsTrue(lost); client.Dispose(); }
public void Udp_Discovery() { ResonanceUdpDiscoveryService <DiscoveryInfo, JsonEncoder> service = new ResonanceUdpDiscoveryService <DiscoveryInfo, JsonEncoder>(new DiscoveryInfo() { Identity = "Test Identity" }, 1984); service.Start(); ResonanceUdpDiscoveryClient <DiscoveryInfo, JsonDecoder> client = new ResonanceUdpDiscoveryClient <DiscoveryInfo, JsonDecoder>(1984); var services = client.Discover(TimeSpan.FromSeconds(10)); Assert.IsTrue(services.Count == 1); Assert.IsTrue(services[0].DiscoveryInfo.Identity == service.DiscoveryInfo.Identity); service.Dispose(); client.Dispose(); }
public void Udp_Multi_Discovery() { List <DiscoveryInfo> infos = new List <DiscoveryInfo>(); List <ResonanceUdpDiscoveryService <DiscoveryInfo, JsonEncoder> > services = new List <ResonanceUdpDiscoveryService <DiscoveryInfo, JsonEncoder> >(); int servicesCount = 10; for (int i = 0; i < servicesCount; i++) { DiscoveryInfo info = new DiscoveryInfo(); info.Identity = $"Test Identity {i}"; infos.Add(info); ResonanceUdpDiscoveryService <DiscoveryInfo, JsonEncoder> service = new ResonanceUdpDiscoveryService <DiscoveryInfo, JsonEncoder>(info, 1984); services.Add(service); service.Start(); } ResonanceUdpDiscoveryClient <DiscoveryInfo, JsonDecoder> client = new ResonanceUdpDiscoveryClient <DiscoveryInfo, JsonDecoder>( 1984, (s1, s2) => s1.DiscoveryInfo.Identity == s2.DiscoveryInfo.Identity); var discoveredDervices = client.Discover(TimeSpan.FromSeconds(10)); Assert.IsTrue(discoveredDervices.Count == servicesCount); var discoveredServicesOrdered = discoveredDervices.OrderBy(x => x.DiscoveryInfo.Identity).ToList(); for (int i = 0; i < discoveredServicesOrdered.Count; i++) { Assert.IsTrue(discoveredServicesOrdered[i].DiscoveryInfo.Identity == infos[i].Identity); } services.ForEach(x => x.Dispose()); client.Dispose(); }
public void Udp_Discovery_And_Tcp_Transporter_Connection() { ResonanceUdpDiscoveryService <DiscoveryInfoTransporter, JsonEncoder> service = new ResonanceUdpDiscoveryService <DiscoveryInfoTransporter, JsonEncoder>(new DiscoveryInfoTransporter() { TcpServerPort = 9999 }, 1984); service.Start(); ResonanceUdpDiscoveryClient <DiscoveryInfoTransporter, JsonDecoder> client = new ResonanceUdpDiscoveryClient <DiscoveryInfoTransporter, JsonDecoder>(1984); var services = client.Discover(TimeSpan.FromSeconds(10), 1); Assert.IsTrue(services.Count == 1); Assert.IsTrue(services[0].DiscoveryInfo.TcpServerPort == service.DiscoveryInfo.TcpServerPort); service.Dispose(); client.Dispose(); var discoveredService = services.First(); ResonanceTransporter t1 = new ResonanceTransporter(new TcpAdapter(discoveredService.Address, discoveredService.DiscoveryInfo.TcpServerPort)); ResonanceTransporter t2 = new ResonanceTransporter(); ResonanceTcpServer server = new ResonanceTcpServer(9999); server.Start(); server.ConnectionRequest += (x, e) => { t2.Adapter = e.Accept(); t2.Connect(); }; t1.Connect(); while (t2.State != ResonanceComponentState.Connected) { Thread.Sleep(10); } t2.RequestReceived += (s, e) => { CalculateRequest receivedRequest = e.Message.Object as CalculateRequest; t2.SendResponse(new CalculateResponse() { Sum = receivedRequest.A + receivedRequest.B }, e.Message.Token); }; var request = new CalculateRequest() { A = 10, B = 15 }; var response = t1.SendRequest <CalculateRequest, CalculateResponse>(request); Assert.AreEqual(response.Sum, request.A + request.B); t1.Dispose(true); t2.Dispose(true); server.Dispose(); }