//Function: Main //The main method, contains an input loop for console interactivity static void Main(string[] args) { bool active = true; //This bool keeps the loop going until the user chooses to exit controller = new ThreadController(); //This class will handle all of the functionality of the Application while (active) { var input = Console.ReadLine(); string command; string parameter; if (input.Contains(" "))//this code block seperates commands with multiple qords into a command and a parameter { command = input.Substring(0, input.IndexOf(" ")).Trim(); parameter = input.Substring(input.IndexOf(" "), input.Length - command.Length).Trim(); } else { command = input; parameter = ""; } Console.WriteLine("COMMAND----------> " + input); if (command.Equals("Add-Channel")) { controller.addClient(parameter); } else if (command.Equals("Drop-Channel")) { controller.dropClient(parameter); } else if (command.Equals("List-Channels")) { Console.WriteLine("The active threads are:"); controller.listThreads(); } else if (command.Equals("Count")) { Console.WriteLine("The message queue has " + controller.queueSize() + " messages in it right now"); } else if (command.Equals("Exit")) { controller.exit(); active = false; } } }
/** * The main loop to be run as a thread. Generates a Kafka client that checks * for new messages COMMANDS and then calls the appropriete function from <THreadController> */ public void CommandThread() { Console.WriteLine("CommandThread Start"); Active = true; //Objs: Kafka Client // //consumer - the Kafka consumer that will connect to the Kafka server //topicp - what partition to start looking for messages in. var consumer = new ConsumerBuilder <string, string>(config).Build(); var topicp = new TopicPartition(topic, 0); consumer.Assign(topicp); while (Active) { try { /** * the result of <consumer> checking for nes messages */ var consumeresult = consumer.Consume(canceltoken); if (!consumeresult.IsPartitionEOF) { /* * This section breaks down the message and then exicutes the appropriete command */ //Vars: Commad Strings // //input - the raw message //command - the part of the message that specifies what function to exicute //parameter - the part of a message that may be passed into a function at exicution var input = consumeresult.Value; string command; string parameter; if (input.Contains(" ")) { command = input.Substring(0, input.IndexOf(" ")).Trim(); parameter = input.Substring(input.IndexOf(" "), input.Length - command.Length).Trim(); } else { command = input; parameter = ""; } Console.WriteLine("COMMAND----------> " + input); if (command.Equals("Add-Channel")) { controller.addClient(parameter); } else if (command.Equals("Drop-Channel")) { controller.dropClient(parameter); } else if (command.Equals("TRBList-Channels")) { Console.WriteLine("The active threads are:"); controller.listThreads(); } else if (command.Equals("TRBCount")) { Console.WriteLine("The message queue has " + controller.queueSize() + " messages in it right now"); } else if (command.Equals("TRBExit") || command.Equals("System-Shutdown")) { controller.exit(); Active = false; source.Cancel(); } } else { Thread.Sleep(100); } } catch (System.OperationCanceledException e) { } } }