public ChatRoomViewModel CreateViewModelAndSubscribe(ChatMessageReceivedCallback messageReceivedCallback) { lock (synchronization) { var previousMessages = messages.ToList().Select(kvp => kvp.Key).ToList(); var viewModel = new ChatRoomViewModel(this, previousMessages, messageReceivedCallback); viewModels.AddOrThrow(viewModel); return(viewModel); } }
public async Task RunAsync() { Console.WriteLine(DateTime.Now); const int kMessagesPerWorker = 200000; var sink = courierFacades[0]; var senders = courierFacades.Skip(1).ToArray(); var counter = kMessagesPerWorker * senders.Length; var doneSignal = new AsyncLatch(); int upCounter = 0; var set = new ConcurrentSet<int>(); sink.InboundMessageRouter.RegisterHandler<string>( x => { set.AddOrThrow(int.Parse(x.Body)); var newCounter = Interlocked.Decrement(ref counter); Interlocked.Increment(ref upCounter); if (upCounter % 500 == 0) Console.WriteLine(newCounter + " " + upCounter); if (newCounter == 0) { doneSignal.Set(); } return Task.FromResult(false); }); var sync = new AsyncCountdownLatch(senders.Length); var senderTasks = senders.Select((s, id) => Go(async () => { await s.PeerTable.GetOrAdd(sink.Identity.Id).WaitForDiscoveryAsync().ConfigureAwait(false); sync.Signal(); await sync.WaitAsync().ConfigureAwait(false); Console.WriteLine("Sink discovered: " + DateTime.Now); const int kBatchFactor = 1; for (var batch = 0; batch < kBatchFactor; batch++) { var batchSize = kMessagesPerWorker / kBatchFactor; await Task.WhenAll(Util.Generate( batchSize, i => s.Messenger.SendReliableAsync( "" + (batch * batchSize + i + id * kMessagesPerWorker), sink.Identity.Id)) ).ConfigureAwait(false); } Console.WriteLine("Worker Done: " + DateTime.Now); })); await Task.WhenAll(senderTasks).ConfigureAwait(false); Console.WriteLine("Senders Done: " + DateTime.Now); await doneSignal.WaitAsync().ConfigureAwait(false); Console.WriteLine("Done Signalled: " + DateTime.Now); AssertCollectionDeepEquals(set, new ConcurrentSet<int>(Enumerable.Range(0, kMessagesPerWorker * senders.Length))); while (true) { GC.Collect(); } }
public override void OnTick() { base.OnTick(); foreach (var command in commands) { bool executing = executingCommands.Contains(command); if (!executing && command.IsExecutable) { var subsystemConflict = (command.Subsystem & activeSubsystems) != 0; if (((command.IsPassive || command.IsTriggered) && !subsystemConflict) || command.IsForceTriggered) { foreach (var executingCommand in executingCommands) { if ((executingCommand.Subsystem & command.Subsystem) != 0) { activeSubsystems &= ~executingCommand.Subsystem; executingCommand.Cancel(); executingCommands.RemoveOrThrow(executingCommand); } } Console.WriteLine($"Start Command {command}."); command.Start(); executing = true; executingCommands.AddOrThrow(command); if (!command.IsPassive) { activeSubsystems |= command.Subsystem; } } } if (executing) { var status = command.RunIteration(); if (status == CommandStatus.Complete || status == CommandStatus.Abort) { Console.WriteLine($"Command {command} finishing: {status}"); activeSubsystems &= ~command.Subsystem; executingCommands.RemoveOrThrow(command); } } } }
public async Task RunAsync() { Console.WriteLine(DateTime.Now); const int kMessagesPerWorker = 200000; var sink = courierFacades[0]; var senders = courierFacades.Skip(1).ToArray(); var counter = kMessagesPerWorker * senders.Length; var doneSignal = new AsyncLatch(); int upCounter = 0; var set = new ConcurrentSet <int>(); sink.InboundMessageRouter.RegisterHandler <string>( x => { set.AddOrThrow(int.Parse(x.Body)); var newCounter = Interlocked.Decrement(ref counter); Interlocked.Increment(ref upCounter); if (upCounter % 500 == 0) { Console.WriteLine(newCounter + " " + upCounter); } if (newCounter == 0) { doneSignal.Set(); } return(Task.FromResult(false)); }); var sync = new AsyncCountdownLatch(senders.Length); var senderTasks = senders.Select((s, id) => Go(async() => { await s.PeerTable.GetOrAdd(sink.Identity.Id).WaitForDiscoveryAsync().ConfigureAwait(false); sync.Signal(); await sync.WaitAsync().ConfigureAwait(false); Console.WriteLine("Sink discovered: " + DateTime.Now); const int kBatchFactor = 1; for (var batch = 0; batch < kBatchFactor; batch++) { var batchSize = kMessagesPerWorker / kBatchFactor; await Task.WhenAll(Util.Generate( batchSize, i => s.Messenger.SendReliableAsync( "" + (batch * batchSize + i + id * kMessagesPerWorker), sink.Identity.Id)) ).ConfigureAwait(false); } Console.WriteLine("Worker Done: " + DateTime.Now); })); await Task.WhenAll(senderTasks).ConfigureAwait(false); Console.WriteLine("Senders Done: " + DateTime.Now); await doneSignal.WaitAsync().ConfigureAwait(false); Console.WriteLine("Done Signalled: " + DateTime.Now); AssertCollectionDeepEquals(set, new ConcurrentSet <int>(Enumerable.Range(0, kMessagesPerWorker * senders.Length))); while (true) { GC.Collect(); } }
internal void AddDependency(IMemberDefinition member) { dependencyFields.AddOrThrow(member); }
public async Task <List <BluetoothDevice> > FetchResultsAsync() { var running = true; var discoveryCanceled = false; while (running) { await new Select { Case(ChannelFactory.Timeout(TimeSpan.FromSeconds(30)), async() => { Console.WriteLine("Watchdog timeout at discovery!"); adapter.CancelDiscovery(); Console.WriteLine("Adapter enabled: " + adapter.IsEnabled); Console.WriteLine("Disabling adapter..."); adapter.Disable(); await Task.Delay(5000); Console.WriteLine("Enabling adapter..."); adapter.Enable(); await Task.Delay(10000); running = false; }), Case(intentChannel, intent => { Console.WriteLine($"GOT INTENT: " + intent.Action); var device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice); switch (intent.Action) { case BluetoothAdapter.ActionDiscoveryStarted: if (state != 0) { Console.WriteLine("WARN: STATE IS " + state + " NOT 0"); } state = 1; Console.WriteLine($"Started Discovery"); break; case BluetoothDevice.ActionFound: if (state != 1 && state != 2) { Console.WriteLine("WARN: STATE IS " + state + " NOT 1 or 2"); } state = 2; Console.WriteLine($"Found: {device.Address} {device.Name ?? "[no name]"}"); if (device.Name == null) { Console.WriteLine("Skip as no name!"); return; } allDiscoveredDevicesByMac.TryAdd(device.Address, device); break; case BluetoothAdapter.ActionDiscoveryFinished: if (state != 2) { Console.WriteLine("WARN: STATE IS " + state + " NOT 2"); return; } state = 3; Console.WriteLine($"Finished Discovery, Performing Service Discovery for Filtering"); adapter.CancelDiscovery(); allDiscoveredDevicesByMac.ForEach(kvp => pendingServiceDiscoveryDevices.AddOrThrow(kvp.Value)); running = TriggerNextServiceDiscoveryElseCompletion(); break; case BluetoothDevice.ActionUuid: if (state != 3 && state != 4) { Console.WriteLine("WARN: STATE IS " + state + " NOT 3 or 4"); } state = 4; Console.WriteLine($"Got UUIDs of device {device.Address} {device.Name ?? "[no name]"}"); var uuidObjects = intent.GetParcelableArrayExtra(BluetoothDevice.ExtraUuid); if (uuidObjects != null) { var uuids = uuidObjects.Cast <ParcelUuid>().ToArray(); uuids.ForEach(Console.WriteLine); // Equality isn't implemented by uuid, so compare tostrings... if (uuids.Any(uuid => uuid.ToString().Equals(CampfireNetBluetoothConstants.APP_UUID.ToString())) || uuids.Any(uuid => uuid.ToString().Equals(CampfireNetBluetoothConstants.FIRMWARE_BUG_REVERSE_APP_UUID.ToString()))) { Console.WriteLine($"Found CampfireNet device {device.Address} {device.Name ?? "[no name]"}"); BluetoothDevice existing; if (discoveredCampfireNetDevices.TryGetValue(device.Address, out existing)) { Console.WriteLine("Device already discovered!"); } else { discoveredCampfireNetDevices.TryAdd(device.Address, device); } } } if (!allDiscoveredDevicesByMac.ContainsKey(device.Address)) { Console.WriteLine("Unrequested UUID, so tossing"); return; } if (serviceDiscoveredDevicesByMac.TryAdd(device.Address, device)) { running = TriggerNextServiceDiscoveryElseCompletion(); } break; default: throw new NotImplementedException($"Unhandled intent action: {intent.Action}"); } }) }.ConfigureAwait(false); } Console.WriteLine("Returning discovered list. Count: " + discoveredCampfireNetDevices.Values.ToList().Count); return(discoveredCampfireNetDevices.Values.ToList()); }