public void Setup() { TopicGenerator.Generate(NumPublishers, NumTopicsPerPublisher, out var topicsByPublisher, out var singleWildcardTopicsByPublisher, out var multiWildcardTopicsByPublisher); _topics = topicsByPublisher.Values.First(); var serverOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build(); var factory = new MqttFactory(); _mqttServer = factory.CreateMqttServer(serverOptions); _mqttClient = factory.CreateMqttClient(); _mqttServer.StartAsync().GetAwaiter().GetResult(); var clientOptions = new MqttClientOptionsBuilder() .WithTcpServer("localhost").Build(); _mqttClient.ConnectAsync(clientOptions).GetAwaiter().GetResult(); foreach (var topic in _topics) { var subscribeOptions = new MqttClientSubscribeOptionsBuilder() .WithTopicFilter(topic) .Build(); _mqttClient.SubscribeAsync(subscribeOptions).GetAwaiter().GetResult(); } }
public void Setup() { _lockMsgCount = new object(); Dictionary <string, List <string> > singleWildcardTopicsByPublisher; Dictionary <string, List <string> > multiWildcardTopicsByPublisher; TopicGenerator.Generate(NumPublishers, NumTopicsPerPublisher, out _topicsByPublisher, out singleWildcardTopicsByPublisher, out multiWildcardTopicsByPublisher); var topics = _topicsByPublisher.First().Value; _topicPublishMessages = new List <MqttApplicationMessage>(); // Prepare messages, same for each publisher foreach (var topic in topics) { var message = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }) .Build(); _topicPublishMessages.Add(message); } // Create server var factory = new MqttFactory(); var serverOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build(); _mqttServer = factory.CreateMqttServer(serverOptions); _mqttServer.StartAsync().GetAwaiter().GetResult(); // Create publisher clients _mqttPublisherClientsByPublisherName = new Dictionary <string, IMqttClient>(); foreach (var pt in _topicsByPublisher) { var publisherName = pt.Key; var mqttClient = factory.CreateMqttClient(); var publisherOptions = new MqttClientOptionsBuilder() .WithTcpServer("localhost") .WithClientId(publisherName) .WithKeepAlivePeriod(TimeSpan.FromSeconds(30)) .Build(); mqttClient.ConnectAsync(publisherOptions).GetAwaiter().GetResult(); _mqttPublisherClientsByPublisherName.Add(publisherName, mqttClient); } // Create subscriber clients _mqttSubscriberClients = new List <IMqttClient>(); for (var i = 0; i < NumSubscribers; i++) { var mqttSubscriberClient = factory.CreateMqttClient(); _mqttSubscriberClients.Add(mqttSubscriberClient); var subscriberOptions = new MqttClientOptionsBuilder() .WithTcpServer("localhost") .WithClientId("subscriber" + i) .Build(); mqttSubscriberClient.ApplicationMessageReceivedAsync += r => { // count messages and signal cancellation when expected message count is reached lock (_lockMsgCount) { ++_messagesReceivedCount; if (_messagesReceivedCount == _messagesExpectedCount) { _cancellationTokenSource.Cancel(); } } return(Task.CompletedTask); }; mqttSubscriberClient.ConnectAsync(subscriberOptions).GetAwaiter().GetResult(); } List <string> allTopics = new List <string>(); _publisherByTopic = new Dictionary <string, string>(); foreach (var t in _topicsByPublisher) { foreach (var topic in t.Value) { _publisherByTopic.Add(topic, t.Key); allTopics.Add(topic); } } // Subscribe to NumSubscribedTopics topics spread across all topics _allSubscribedTopics = new List <string>(); var totalNumTopics = NumPublishers * NumTopicsPerPublisher; int topicIndexStep = totalNumTopics / (NumSubscribedTopicsPerSubscriber * NumSubscribers); if (topicIndexStep * NumSubscribedTopicsPerSubscriber * NumSubscribers != totalNumTopics) { throw new System.Exception( String.Format("The total number of topics must be divisible by the number of subscribed topics across all subscribers. Total number of topics: {0}, topic step: {1}", totalNumTopics, topicIndexStep )); } var topicIndex = 0; foreach (var mqttSubscriber in _mqttSubscriberClients) { for (var i = 0; i < NumSubscribedTopicsPerSubscriber; ++i, topicIndex += topicIndexStep) { var topic = allTopics[topicIndex]; _allSubscribedTopics.Add(topic); var subOptions = new Client.MqttClientSubscribeOptionsBuilder().WithTopicFilter( new Packets.MqttTopicFilter() { Topic = topic }) .Build(); mqttSubscriber.SubscribeAsync(subOptions).GetAwaiter().GetResult(); } } Task.Delay(1000).GetAwaiter().GetResult(); }