public async Task EnumerateDevices_FluctuatingServiceConnection_DevicesEnumerated() { var devices = new List <DeviceDTO>(); IMetaPubSub messenger = GetMessenger(); IMetaPubSub hub = new MetaPubSub(); hub.StartServer("Test1"); int devicesCount = 100; for (int i = 0; i < devicesCount; i++) { devices.Add(GetRandomDeviceDTO()); } int serviceReconnectsCount = 10; for (int i = 0; i < serviceReconnectsCount; i++) { await messenger.TryConnectToServer("Test1"); IDeviceManager deviceManager = GetDeviceManager(messenger, devices); await Task.Delay(200); Assert.AreEqual(devicesCount, deviceManager.Devices.Count()); await messenger.DisconnectFromServer(); await Task.Delay(200); Assert.AreEqual(0, deviceManager.Devices.Count()); } }
public async Task EnumerateDevices_ClearDevices_DevicesCollectionCleared() { var devices = new List <DeviceDTO>(); IMetaPubSub messenger = GetMessenger(); IMetaPubSub hub = new MetaPubSub(); hub.StartServer("Test2"); await messenger.TryConnectToServer("Test2"); IDeviceManager deviceManager = GetDeviceManager(messenger); int devicesCount = 1000; for (int i = 0; i < devicesCount; i++) { devices.Add(GetRandomDeviceDTO()); } await messenger.PublishOnServer(new DevicesCollectionChangedMessage(devices.ToArray())); await messenger.DisconnectFromServer(); await Task.Delay(200); Assert.AreEqual(0, deviceManager.Devices.Count()); }
public async Task EnumerateDevices_QuickReconnect_DevicesCollectionEnumerated() { var devices = new List <DeviceDTO>(); IMetaPubSub messenger = GetMessenger(); IDeviceManager deviceManager = GetDeviceManager(messenger); IMetaPubSub hub = new MetaPubSub(); hub.StartServer("Test3"); int devicesCount = 1000; for (int i = 0; i < devicesCount; i++) { devices.Add(GetRandomDeviceDTO()); } var connectionTask = Task.Factory.StartNew(() => { messenger.TryConnectToServer("Test3"); deviceManager = GetDeviceManager(messenger, devices); }); var disconnectionTask = Task.Factory.StartNew(messenger.DisconnectFromServer); var reconnectionTask = Task.Factory.StartNew(() => messenger.TryConnectToServer("Test3")); await Task.WhenAll(connectionTask, disconnectionTask, reconnectionTask); await Task.Delay(2000); Assert.AreEqual(devicesCount, deviceManager.Devices.Count()); }
static void RunServer() { var nLog = LogManager.GetLogger("MetaPubSub"); logger = new NLogAdapter(nLog); hub = new MetaPubSub(logger); // Servers started on the Windows process with elevated permissions need to // set up security to allow non-elevated processes to access the pipe. // Otherwise just use hub.StartServer("Meta") call hub.StartServer("Meta", () => { var pipeSecurity = new PipeSecurity(); pipeSecurity.AddAccessRule(new PipeAccessRule( new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.FullControl, AccessControlType.Allow)); var pipe = new NamedPipeServerStream("Meta", PipeDirection.InOut, 32, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 4096, 4096, pipeSecurity); return(pipe); }); hub.Subscribe <PingCommand>(OnPing); }
static void Main(string[] args) { RunServer(); Console.Write(">"); string line; while ((line = Console.ReadLine()) != "exit") { try { if (line == "stop") { hub.StopServer(); } else if (line == "start") { hub.StartServer("Meta"); hub.Subscribe <PingCommand>(OnPing); } } catch (Exception ex) { logger.Error(ex); } Console.Write(">"); } }
MetaPubSub CreateServerHub(string pipeName = null) { pipeName ??= Guid.NewGuid().ToString(); var serverHub = new MetaPubSub(); serverHub.StartServer(pipeName); return(serverHub); }
// subscribes on server and receives a message public async Task SendMessageToClient() { var pipeName = Guid.NewGuid().ToString(); var clientConnectedEvent = new ManualResetEventSlim(); var @event = new ManualResetEventSlim(); int recvCount = 0; Task Handler(MyMessage x) { if (++recvCount == 10) { @event.Set(); } return(Task.CompletedTask); } // creating remote hub var t = Task.Run(async() => { var hub = new MetaPubSub(); hub.StartServer(pipeName); // wait for the subscriber clientConnectedEvent.Wait(5000); // publishing a message at the remote hub for (int i = 0; i < 10; i++) { await hub.Publish(new MyMessage()); } }); // local hub creation var hub = new MetaPubSub(); await hub.ConnectToServer(pipeName); await hub.SubscribeOnServer <MyMessage>(Handler); // delay allowing the server to process the subscription request await Task.Delay(100); clientConnectedEvent.Set(); @event.Wait(5000); // unsubscribing await hub.Unsubscribe <MyMessage>(Handler); Assert.IsTrue(@event.IsSet && recvCount == 10); }
public async Task NoSubscribersException() { var pipeName = Guid.NewGuid().ToString(); var clientConnectedEvent = new ManualResetEventSlim(); var @event = new ManualResetEventSlim(); // creating remote hub var t = Task.Run(async() => { var hub = new MetaPubSub(); hub.StartServer(pipeName); // wait for the subscriber clientConnectedEvent.Wait(5000); try { // publishing a message at the remote hub await hub.Publish(new MyMessage() { DeliverAtLeastOnce = true }); } catch (NoSubscribersException) { @event.Set(); } }); // local hub creation var hub = new MetaPubSub(); await hub.ConnectToServer(pipeName); // delay allowing the server process the connection await Task.Delay(100); clientConnectedEvent.Set(); @event.Wait(5000); Assert.IsTrue(@event.IsSet); }
async Task BasicExample() { int count = 0; Task Handler(MyMessage x) { count++; return(Task.CompletedTask); } // Creating the server hub. // The server and the client hubs should be created in separate processes, // this example is for demo only. var serverHub = new MetaPubSub(); // Starting the hub as a server named 'Meta'. serverHub.StartServer("Meta"); // Client hub creation. There are can be several hubs connected to the same server. var clientHub = new MetaPubSub(); // Connecting to the remote server. await clientHub.ConnectToServer("Meta"); // Subscribing to MyMessage on the server and locally at the same time. await clientHub.SubscribeOnServer <MyMessage>(Handler); // The server publishes a message. await serverHub.Publish(new MyMessage()); // Client hub publishes a message and it will be received locally without being sent to the server. await clientHub.Publish(new MyMessage()); // Client hub sends a message to the server where it will be published and sent back. await clientHub.PublishOnServer(new MyMessage()); // All three messages should be received. Debug.Assert(count == 3); // Unsubscribing both on the server-side and locally. await clientHub.Unsubscribe <MyMessage>(Handler); }
public async Task DelayedMessage() { var pipeName = Guid.NewGuid().ToString(); var @event = new ManualResetEventSlim(); Task Handler(MyMessage x) { @event.Set(); return(Task.CompletedTask); } var serverHub = new MetaPubSub(); // creating remote hub var t = Task.Run(async() => { serverHub.StartServer(pipeName); // publishing a message at the remote hub await serverHub.Publish(new MyMessage() { DeliverAtLeastOnce = true, Timeout = 20_000 }); });