Exemplo n.º 1
0
        static void Main(string[] args)
        {
            bool useAppointedLibrary = false;

            var options = new CommandLineOptions();

            if (args.Length == 0)
            {
                // 인자가 하나도 없을 때는 그냥 기본값으로 작동한다.
            }
            else
            {
                // 여기선 인자를 일단 커맨드라인 파서로 넣고

                if (CommandLine.Parser.Default.ParseArguments(args, options))
                {
                    // 문제 없음.

                    if (options.InstallService || options.UninstallService)
                    {
                        // 서비스 인스톨, 혹은 언인스톨

                        if (options.InstallService)
                        {
                            // 인스톨을 하고, 서비스를 켜준다.
                            if (!CheckPrivilege())
                            {
                                Console.WriteLine("This action require administrator privilege");
                                Environment.Exit(0);
                            }
                            ServiceControl.InstallService("SensorMonitor(ASTP)", typeof(ClientService).Assembly);
                            ServiceControl.StartService("SensorMonitor(ASTP)");
                        }
                        else
                        {
                            // 언인스톨을 해준다.
                            if (!CheckPrivilege())
                            {
                                Console.WriteLine("This action require administrator privilege");
                                Environment.Exit(0);
                            }
                            ServiceControl.StopService("SensorMonitor(ASTP)");
                            ServiceControl.UninstallService("SensorMonitor(ASTP)", typeof(ClientService).Assembly);
                        }
                        // 인스톨/언인스톨 후에 종료
                        Environment.Exit(0);
                    }

                    useAppointedLibrary = options.LibName != "NoSuchValue";
                }
                else
                {
                    // 사용법을 잘못 넣었다면 그냥 종료
                    // 사용법이 틀린 시점에 이미 Usage 가 출력된다.
                    Environment.Exit(0);
                }
            }

            if (Environment.UserInteractive)
            {
                var core = new Core();

                if (useAppointedLibrary)
                {
                    if (!core.Attach(options.LibName))
                    {
                        Console.WriteLine("Error occur in initializing sub components");
                        Environment.Exit(0);
                    }
                }
                else
                {
                    if (!core.Attach())
                    {
                        Console.WriteLine("Error occur in initializing sub components");
                        Environment.Exit(0);
                    }
                }

                core.Log += LogEventTerminalReceiver;

                Console.CancelKeyPress += new ConsoleCancelEventHandler(
                    delegate (object sender, ConsoleCancelEventArgs arg)
                    {
                        Console.WriteLine("SIGINT Received");

                        core.Dispose();
                        arg.Cancel = true;
                    }
                );

                var t = new Thread(core.Run);
                t.Start();
            }
            else
            {
                // 서비스 모드로 동작한다.
                // 즉 콘솔이 아니다.
                // 얘는 인자를 먹지 않는다.
                // 즉 라이브러리 지정도 안된다.
                var cs = new ClientService();
                ServiceBase.Run(cs);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            bool useAppointedLibrary = false;

            var options = new CommandLineOptions();

            if (args.Length == 0)
            {
                // 인자가 하나도 없을 때는 그냥 기본값으로 작동한다.
            }
            else
            {
                // 여기선 인자를 일단 커맨드라인 파서로 넣고

                if (CommandLine.Parser.Default.ParseArguments(args, options))
                {
                    // 문제 없음.

                    if (options.InstallService || options.UninstallService)
                    {
                        // 서비스 인스톨, 혹은 언인스톨

                        if (options.InstallService)
                        {
                            // 인스톨을 하고, 서비스를 켜준다.
                            if (!CheckPrivilege())
                            {
                                Console.WriteLine("This action require administrator privilege");
                                Environment.Exit(0);
                            }
                            ServiceControl.InstallService("SensorMonitor(ASTP)", typeof(ClientService).Assembly);
                            ServiceControl.StartService("SensorMonitor(ASTP)");
                        }
                        else
                        {
                            // 언인스톨을 해준다.
                            if (!CheckPrivilege())
                            {
                                Console.WriteLine("This action require administrator privilege");
                                Environment.Exit(0);
                            }
                            ServiceControl.StopService("SensorMonitor(ASTP)");
                            ServiceControl.UninstallService("SensorMonitor(ASTP)", typeof(ClientService).Assembly);
                        }
                        // 인스톨/언인스톨 후에 종료
                        Environment.Exit(0);
                    }

                    useAppointedLibrary = options.LibName != "NoSuchValue";
                }
                else
                {
                    // 사용법을 잘못 넣었다면 그냥 종료
                    // 사용법이 틀린 시점에 이미 Usage 가 출력된다.
                    Environment.Exit(0);
                }
            }


            if (Environment.UserInteractive)
            {
                var core = new Core();

                if (useAppointedLibrary)
                {
                    if (!core.Attach(options.LibName))
                    {
                        Console.WriteLine("Error occur in initializing sub components");
                        Environment.Exit(0);
                    }
                }
                else
                {
                    if (!core.Attach())
                    {
                        Console.WriteLine("Error occur in initializing sub components");
                        Environment.Exit(0);
                    }
                }

                core.Log += LogEventTerminalReceiver;

                Console.CancelKeyPress += new ConsoleCancelEventHandler(
                    delegate(object sender, ConsoleCancelEventArgs arg)
                {
                    Console.WriteLine("SIGINT Received");

                    core.Dispose();
                    arg.Cancel = true;
                }
                    );

                var t = new Thread(core.Run);
                t.Start();
            }
            else
            {
                // 서비스 모드로 동작한다.
                // 즉 콘솔이 아니다.
                // 얘는 인자를 먹지 않는다.
                // 즉 라이브러리 지정도 안된다.
                var cs = new ClientService();
                ServiceBase.Run(cs);
            }
        }