static void Main(string[] args) { Logger.Info("Starting Publish-Subscribe Broker test application..."); if (args.Length > 1) { if (args[0].ToLower() == "-subscriber") { // Start a subscriber client with a list of topic names as arguments Logger.Info("-- Subscriber Client Instance --"); string[] subscriberArgs = new string[args.Length - 1]; for (int i = 0; i < args.Length - 1; i++) { subscriberArgs[i] = args[i + 1]; } StartSubscriberClient("localhost", PORT, subscriberArgs); } else if (args[0].ToLower() == "-publisher") { // Start a publisher client with a topic name as an argument Logger.Info("-- Publisher Client Instance --"); StartPublisherClient("localhost", PORT, args[1]); } } else if (args.Length == 1) { // Show some help information Logger.Error("Invalid arguments!" + Environment.NewLine + " Start without arguments to run the full test application with a broker server" + Environment.NewLine + " Use \"-publisher <topic name>\" to create a publisher client for the specified topic" + Environment.NewLine + " Use \"-subscriber <topic name> ... <topic name>\" to create a subscriber client and subscribe to the specified topics"); } else { // No args: Start a broker server and some test clients Logger.Info("-- Broker Server Instance --"); BrokerServer server = StartBrokerServer("127.0.0.1", PORT); // Create separate app instances to allow separate console output windows Logger.Info("Starting new application instances for clients..."); StartNewAppInstance("-publisher test-topic-1"); StartNewAppInstance("-publisher test-topic-2"); StartNewAppInstance("-publisher test-topic-3"); StartNewAppInstance("-subscriber test-topic-1 test-topic-2"); StartNewAppInstance("-subscriber test-topic-2 test-topic-3"); // Keep process alive while server is running while (server.IsRunning()) { Thread.Sleep(500); } Logger.Warn("Server has stopped"); } // End-of-program wait (to preserve separate console windows until user is ready to close them) Logger.Info("-- Instance Terminated --" + Environment.NewLine + "Press any key to exit..."); Console.ReadKey(); }
/// <summary> /// Initialize a new broker server at the specified IP and port to test with /// </summary> /// <returns>The new BrokerServer instance</returns> private static BrokerServer StartBrokerServer(string ipAddress, int port) { BrokerServer server = new BrokerServer(ipAddress, port); try { // Start the broker server (it will listen for clients in a separate thread) server.StartServer(); Logger.Info("Broker server started"); } catch (Exception e) { Logger.Error("Broker server failed to start" + Environment.NewLine + e.Message); } return(server); }