Exemplo n.º 1
0
        /// <summary>
        /// Initialize a new publisher client for the specified IP and port to test with
        /// </summary>
        /// <param name="topic">The name of the publisher's topic (created upon starting the publisher client)</param>
        private static void StartPublisherClient(string ipAddress, int port, string topic)
        {
            try
            {
                // Start the publisher client
                PublisherClient client = new PublisherClient(ipAddress, port);
                client.StartClient();

                Logger.Info("Publisher client started");

                // Create a topic
                Logger.Info("Requesting creation of topic \"" + topic + "\"...");
                client.AddRequest(new CreateTopicRequest(topic));
                Thread.Sleep(1000);

                // Give subscribers time to subscribe
                Logger.Info("Waiting for subscribers...");
                Thread.Sleep(5000);

                // Publish some random-data messages to the topic
                Random rand = new Random();
                for (int i = 0; i < 10; i++)
                {
                    string message = RandomText.Substring(rand.Next(0, RandomText.Length - 21), 20);
                    Logger.Info("Attempting to publish message: " + message);
                    client.AddRequest(client.MakePublishRequest(client.OwnedTopics[0], message));
                    Thread.Sleep(rand.Next(1000, 6000));
                }
            }
            catch (Exception e)
            {
                Logger.Error("Publisher client failed to start" + Environment.NewLine + e.Message);
            }
        }