예제 #1
0
        /**
         * The main function for <GameApplication>, holds the console input loop
         */
        static void Main(string[] args)
        {
            //bool: active
            //maintains the console input loop
            bool active = true;

            //obj: controller
            //The object that maintains and controlls the <GameApplication> threads
            ThreadController controller = new ThreadController();
            Thread           conThread  = new Thread(controller.ControlLoop);

            conThread.Start();

            while (active)
            {
                //strings: Input
                //
                //input - the input from the console
                //command - the portion of the input that determines which instruction to carry out
                //parameter - the part of a command that may be passed into a function as a parameter
                var    input = Console.ReadLine();
                string command;
                string parameter;

                if (input.Contains(" "))
                {
                    command   = input.Substring(0, input.IndexOf(" ")).Trim();
                    parameter = input.Substring(input.IndexOf(" "), input.Length - 1).Trim();
                }
                else
                {
                    command   = input;
                    parameter = "";
                }

                Console.WriteLine("COMMAND----------> " + input);

                if (command.Equals("Exit"))
                {
                    controller.exit();
                    active = false;
                }
                else if (command.Equals("List-Threads"))
                {
                    controller.listThreads();
                }
            }
        }
        /**
         * the primary function of the <KafkaConsumer> to be run as a thread
         */
        public void CommandThread()
        {
            Console.WriteLine("COMMANDSThread Start");
            Active = true;

            //objs: Kafka Consumer
            //
            //consumer - the Kafka consumer object
            //topicp - the partition to subscribe to
            var consumer = new ConsumerBuilder <string, string>(config).Build();
            var topicp   = new TopicPartition(topic, 0);

            consumer.Assign(topicp);
            while (Active)
            {
                try
                {
                    //var: consumerresult
                    //
                    //the result of checking for a new message from the Kafka server
                    var consumeresult = consumer.Consume(canceltoken);

                    if (!consumeresult.IsPartitionEOF)
                    {
                        //vars: Kafka message
                        //
                        //command - the part of the message that specifys an action to take
                        //parameter - the part of the messag that may be passed to a function as a parameter
                        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("GAExit") || command.Equals("System-Shutdown"))
                        {
                            Active = false;
                            source.Cancel();
                            controller.exit();
                        }
                    }
                    else
                    {
                        Thread.Sleep(100);
                    }
                }
                catch (System.OperationCanceledException e)
                {
                }
            }
        }