Ejemplo n.º 1
0
        protected override void OnContinue()
        {
            eventLog.WriteEntry("Continue the execution of the service.");

            try {
                if (service != null)
                {
                    service.Start();
                }

                eventLog.WriteEntry("Service continued its execution");
            } catch (Exception e) {
                eventLog.WriteEntry("Error on continue of the execution of the service: " + e.Message, EventLogEntryType.Error);
                throw;
            }
        }
Ejemplo n.º 2
0
        private static int Main(string[] args)
        {
            string nodeConfig = null, netConfig = null;
            string hostArg = null, portArg = null;

            StringWriter wout    = new StringWriter();
            Options      options = GetOptions();

            CommandLine commandLine = null;

            bool failed    = false;
            bool isService = false;

            try {
                ICommandLineParser parser = new GnuParser(options);
                commandLine = parser.Parse(args);

                nodeConfig = commandLine.GetOptionValue("nodeconfig", "./node.conf");
                netConfig  = commandLine.GetOptionValue("netconfig", "./network.conf");
                hostArg    = commandLine.GetOptionValue("host");
                portArg    = commandLine.GetOptionValue("port");
            } catch (ParseException) {
                wout.WriteLine("Error parsing arguments.");
                failed = true;
            }

            if (commandLine != null)
            {
                if (commandLine.HasOption("install"))
                {
                    try {
                        Install(commandLine);
                        Console.Out.WriteLine("Service installed succesfully.");
                        return(0);
                    } catch (Exception e) {
                        Console.Error.WriteLine("Error installing service: " + e.Message);
#if DEBUG
                        Console.Error.WriteLine(e.StackTrace);
#endif
                        return(1);
                    }
                }
                if (commandLine.HasOption("uninstall"))
                {
                    try {
                        Uninstall();
                        Console.Out.WriteLine("Service uninstalled succesfully.");
                        return(0);
                    } catch (Exception e) {
                        Console.Error.WriteLine("Error uninstalling service: " + e.Message);
#if DEBUG
                        Console.Error.WriteLine(e.StackTrace);
#endif
                        return(1);
                    }
                }

                isService = commandLine.HasOption("service");
            }

            if (isService)
            {
                MachineNodeService mnodeService = new MachineNodeService(commandLine);

                try {
                    if (Environment.UserInteractive)
                    {
                        mnodeService.Start(args);
                        Console.Out.WriteLine("Press any key to stop...");
                        Console.Read();
                        mnodeService.Stop();
                    }
                    else
                    {
                        ServiceBase.Run(mnodeService);
                    }
                } catch (Exception) {
                    return(1);
                }

                return(0);
            }

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
            SetEventHandlers();

            ProductInfo libInfo  = ProductInfo.GetProductInfo(typeof(TcpAdminService));
            ProductInfo nodeInfo = ProductInfo.GetProductInfo(typeof(MachineNode));

            Console.Out.WriteLine("{0} {1} ( {2} )", nodeInfo.Title, nodeInfo.Version, nodeInfo.Copyright);
            Console.Out.WriteLine(nodeInfo.Description);
            Console.Out.WriteLine();
            Console.Out.WriteLine("{0} {1} ( {2} )", libInfo.Title, libInfo.Version, libInfo.Copyright);

            // Check arguments that can be null,
            if (netConfig == null)
            {
                wout.WriteLine("Error, no network configuration given.");
                failed = true;
            }
            else if (nodeConfig == null)
            {
                wout.WriteLine("Error, no node configuration file given.");
                failed = true;
            }
            //if (portArg == null) {
            //    wout.WriteLine("Error, no port address given.");
            //    failed = true;
            //}

            if (!failed)
            {
                //TODO: support for remote (eg. HTTP, FTP, TCP/IP) configurations)

                nodeConfig = NormalizeFilePath(nodeConfig);
                netConfig  = NormalizeFilePath(netConfig);

                if (!File.Exists(nodeConfig))
                {
                    wout.WriteLine("Error, node configuration file not found ({0}).", nodeConfig);
                    failed = true;
                }
                else if (!File.Exists(netConfig))
                {
                    wout.WriteLine("Error, node configuration file not found ({0}).", netConfig);
                    failed = true;
                }
            }

            wout.Flush();

            // If failed,
            if (failed)
            {
                HelpFormatter formatter = new HelpFormatter();
                if (!IsConsoleRedirected())
                {
                    formatter.Width = Console.WindowWidth;
                }
                formatter.CommandLineSyntax = "mnode";
                formatter.Options           = options;
                formatter.PrintHelp();
                Console.Out.WriteLine();
                Console.Out.WriteLine(wout.ToString());
                return(1);
            }

            try {
#if DEBUG
                Console.Out.WriteLine("Retrieving node configuration from {0}", nodeConfig);
#endif

                // Get the node configuration file,
                ConfigSource nodeConfigSource = new ConfigSource();
                using (FileStream fin = new FileStream(nodeConfig, FileMode.Open, FileAccess.Read, FileShare.None)) {
                    //TODO: make it configurable ...
                    nodeConfigSource.LoadProperties(new BufferedStream(fin));
                }

#if DEBUG
                Console.Out.WriteLine("Retrieving network configuration from {0}", netConfig);
#endif

                // Parse the network configuration string,
                NetworkConfigSource netConfigSource;
                using (FileStream stream = new FileStream(netConfig, FileMode.Open, FileAccess.Read, FileShare.None)) {
                    netConfigSource = new NetworkConfigSource();
                    //TODO: make it configurable ...
                    netConfigSource.LoadProperties(stream);
                }

                string password = nodeConfigSource.GetString("network_password", null);
                if (password == null)
                {
                    Console.Out.WriteLine("Error: couldn't determine the network password.");
                    return(1);
                }

                // configure the loggers
                Logger.Init(nodeConfigSource);

                //TODO: support also IPv6

                // The base path,
                IPAddress host = null;
                if (hostArg != null)
                {
                    IPAddress[] addresses = Dns.GetHostAddresses(hostArg);
                    for (int i = 0; i < addresses.Length; i++)
                    {
                        IPAddress address = addresses[i];
                        if (address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            host = address;
                            break;
                        }
                    }
                }
                else
                {
                    host = IPAddress.Loopback;
                }

                if (host == null)
                {
                    Console.Out.WriteLine("Error: couldn't determine the host address.");
                    return(1);
                }

                int port = DefaultPort;
                if (!String.IsNullOrEmpty(portArg))
                {
                    if (!Int32.TryParse(portArg, out port))
                    {
                        Console.Out.WriteLine("Error: couldn't parse port argument: " + portArg);
                        return(1);
                    }
                }

                string          storage        = commandLine.GetOptionValue("storage", null);
                IServiceFactory serviceFactory = GetServiceFactory(storage, nodeConfigSource);

                Console.Out.WriteLine("Machine Node, " + host + " : " + port);
                service        = new TcpAdminService(serviceFactory, host, port, password);
                service.Config = netConfigSource;
                service.Start();

                Console.Out.WriteLine();
                Console.Out.WriteLine();
                Console.Out.WriteLine("Press CTRL+C to quit...");

                waitHandle = new AutoResetEvent(false);
                waitHandle.WaitOne();
            } catch (Exception e) {
                Console.Out.WriteLine(e.Message);
                Console.Out.WriteLine(e.StackTrace);
                return(1);
            } finally {
                if (service != null)
                {
                    service.Dispose();
                }
            }

            return(0);
        }
Ejemplo n.º 3
0
        private static int Main(string[] args)
        {
            string nodeConfig = null, netConfig = null;
            string hostArg = null, portArg = null;

            StringWriter wout = new StringWriter();
            Options options = GetOptions();

            CommandLine commandLine = null;

            bool failed = false;
            bool isService = false;

            try {
                ICommandLineParser parser = new GnuParser(options);
                commandLine = parser.Parse(args);

                nodeConfig = commandLine.GetOptionValue("nodeconfig", "./node.conf");
                netConfig = commandLine.GetOptionValue("netconfig", "./network.conf");
                hostArg = commandLine.GetOptionValue("host");
                portArg = commandLine.GetOptionValue("port");
            } catch (ParseException) {
                wout.WriteLine("Error parsing arguments.");
                failed = true;
            }

            if (commandLine != null) {
                if (commandLine.HasOption("install")) {
                    try {
                        Install(commandLine);
                        Console.Out.WriteLine("Service installed succesfully.");
                        return 0;
                    } catch (Exception e) {
                        Console.Error.WriteLine("Error installing service: " + e.Message);
            #if DEBUG
                        Console.Error.WriteLine(e.StackTrace);
            #endif
                        return 1;
                    }
                }
                if (commandLine.HasOption("uninstall")) {
                    try {
                        Uninstall();
                        Console.Out.WriteLine("Service uninstalled succesfully.");
                        return 0;
                    } catch (Exception e) {
                        Console.Error.WriteLine("Error uninstalling service: " + e.Message);
            #if DEBUG
                        Console.Error.WriteLine(e.StackTrace);
            #endif
                        return 1;
                    }
                }

                isService = commandLine.HasOption("service");
            }

            if (isService) {
                MachineNodeService mnodeService = new MachineNodeService(commandLine);

                try {
                    if (Environment.UserInteractive) {
                        mnodeService.Start(args);
                        Console.Out.WriteLine("Press any key to stop...");
                        Console.Read();
                        mnodeService.Stop();
                    } else {
                        ServiceBase.Run(mnodeService);
                    }
                } catch(Exception) {
                    return 1;
                }

                return 0;
            }

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
            SetEventHandlers();

            ProductInfo libInfo = ProductInfo.GetProductInfo(typeof (TcpAdminService));
            ProductInfo nodeInfo = ProductInfo.GetProductInfo(typeof (MachineNode));

            Console.Out.WriteLine("{0} {1} ( {2} )", nodeInfo.Title, nodeInfo.Version, nodeInfo.Copyright);
            Console.Out.WriteLine(nodeInfo.Description);
            Console.Out.WriteLine();
            Console.Out.WriteLine("{0} {1} ( {2} )", libInfo.Title, libInfo.Version, libInfo.Copyright);

            // Check arguments that can be null,
            if (netConfig == null) {
                wout.WriteLine("Error, no network configuration given.");
                failed = true;
            } else if (nodeConfig == null) {
                wout.WriteLine("Error, no node configuration file given.");
                failed = true;
            }
            if (portArg == null) {
                wout.WriteLine("Error, no port address given.");
                failed = true;
            }

            if (!failed) {
                //TODO: support for remote (eg. HTTP, FTP, TCP/IP) configurations)

                nodeConfig = NormalizeFilePath(nodeConfig);
                netConfig = NormalizeFilePath(netConfig);

                if (!File.Exists(nodeConfig)) {
                    wout.WriteLine("Error, node configuration file not found ({0}).", nodeConfig);
                    failed = true;
                } else if (!File.Exists(netConfig)) {
                    wout.WriteLine("Error, node configuration file not found ({0}).", netConfig);
                    failed = true;
                }
            }

            wout.Flush();

            // If failed,
            if (failed) {
                HelpFormatter formatter = new HelpFormatter();
                if (!IsConsoleRedirected()) {
                    formatter.Width = Console.WindowWidth;
                }
                formatter.CommandLineSyntax = "mnode";
                formatter.Options = options;
                formatter.PrintHelp();
                Console.Out.WriteLine();
                Console.Out.WriteLine(wout.ToString());
                return 1;
            }

            try {
            #if DEBUG
                Console.Out.WriteLine("Retrieving node configuration from {0}", nodeConfig);
            #endif

                // Get the node configuration file,
                ConfigSource nodeConfigSource = new ConfigSource();
                using (FileStream fin = new FileStream(nodeConfig, FileMode.Open, FileAccess.Read, FileShare.None)) {
                    //TODO: make it configurable ...
                    nodeConfigSource.LoadProperties(new BufferedStream(fin));
                }

            #if DEBUG
                Console.Out.WriteLine("Retrieving network configuration from {0}", netConfig);
            #endif

                // Parse the network configuration string,
                NetworkConfigSource netConfigSource;
                using (FileStream stream = new FileStream(netConfig, FileMode.Open, FileAccess.Read, FileShare.None)) {
                    netConfigSource = new NetworkConfigSource();
                    //TODO: make it configurable ...
                    netConfigSource.LoadProperties(stream);
                }

                string password = nodeConfigSource.GetString("network_password", null);
                if (password == null) {
                    Console.Out.WriteLine("Error: couldn't determine the network password.");
                    return 1;
                }

                // configure the loggers
                Logger.Init(nodeConfigSource);

                //TODO: support also IPv6

                // The base path,
                IPAddress host = null;
                if (hostArg != null) {
                    IPAddress[] addresses = Dns.GetHostAddresses(hostArg);
                    for (int i = 0; i < addresses.Length; i++) {
                        IPAddress address = addresses[i];
                        if (address.AddressFamily == AddressFamily.InterNetwork) {
                            host = address;
                            break;
                        }
                    }
                } else {
                    host = IPAddress.Loopback;
                }

                if (host == null) {
                    Console.Out.WriteLine("Error: couldn't determine the host address.");
                    return 1;
                }

                int port;
                if (!Int32.TryParse(portArg, out port)) {
                    Console.Out.WriteLine("Error: couldn't parse port argument: " + portArg);
                    return 1;
                }

                string storage = commandLine.GetOptionValue("storage", null);
                IServiceFactory serviceFactory = GetServiceFactory(storage, nodeConfigSource);

                Console.Out.WriteLine("Machine Node, " + host + " : " + port);
                service = new TcpAdminService(serviceFactory, host, port, password);
                service.Config = netConfigSource;
                service.Start();

                waitHandle = new AutoResetEvent(false);
                waitHandle.WaitOne();
            } catch (Exception e) {
                Console.Out.WriteLine(e.Message);
                Console.Out.WriteLine(e.StackTrace);
                return 1;
            } finally {
                if (service != null)
                    service.Dispose();
            }

            return 0;
        }
Ejemplo n.º 4
0
        protected override void OnStart(string[] args)
        {
            eventLog.WriteEntry("Starting the service.", EventLogEntryType.Information);

            string nodeConfig, netConfig;
            string hostArg, portArg;

            try {
                nodeConfig = commandLine.GetOptionValue("nodeconfig", "node.conf");
                netConfig  = commandLine.GetOptionValue("netconfig", "network.conf");
                hostArg    = commandLine.GetOptionValue("host");
                portArg    = commandLine.GetOptionValue("port");
            } catch (ParseException e) {
                eventLog.WriteEntry("Parse Error: " + e.Message, EventLogEntryType.Error);
                ExitCode = 1;
                throw new ApplicationException("Invalid arguments passed.");
            }

            try {
                // Get the node configuration file,
                ConfigSource nodeConfigSource = new ConfigSource();
                using (FileStream fin = new FileStream(nodeConfig, FileMode.Open, FileAccess.Read, FileShare.None)) {
                    nodeConfigSource.LoadProperties(new BufferedStream(fin));
                }

                // Parse the network configuration string,
                NetworkConfigSource netConfigSource;
                using (FileStream stream = new FileStream(netConfig, FileMode.Open, FileAccess.Read, FileShare.None)) {
                    netConfigSource = new NetworkConfigSource();
                    //TODO: make it configurable ...
                    netConfigSource.LoadProperties(stream);
                }

                string password = nodeConfigSource.GetString("network_password", null);
                if (password == null)
                {
                    throw new ApplicationException("Error: couldn't determine the network password.");
                }

                // configure the loggers
                Logger.Init(nodeConfigSource);

                //TODO: support also IPv6

                // The base path,
                IPAddress host = null;
                if (hostArg != null)
                {
                    IPAddress[] addresses = Dns.GetHostAddresses(hostArg);
                    for (int i = 0; i < addresses.Length; i++)
                    {
                        IPAddress address = addresses[i];
                        if (address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            host = address;
                            break;
                        }
                    }
                }
                else
                {
                    host = IPAddress.Loopback;
                }

                if (host == null)
                {
                    throw new ApplicationException("Could not determine the host address.");
                }

                int port;
                if (!Int32.TryParse(portArg, out port))
                {
                    throw new ApplicationException("Error: couldn't parse port argument: " + portArg);
                }

                string          storage        = commandLine.GetOptionValue("storage", null);
                IServiceFactory serviceFactory = GetServiceFactory(storage, nodeConfigSource);

                service        = new TcpAdminService(serviceFactory, host, port, password);
                service.Config = netConfigSource;
                service.Start();

                eventLog.WriteEntry("TCP/IP service started successfully: " + host + ":" + port, EventLogEntryType.Information);
                eventLog.WriteEntry("Storage system: " + storage);
            } catch (Exception e) {
                if (service != null)
                {
                    service.Dispose();
                }

                eventLog.WriteEntry("Error on start: " + e.Message, EventLogEntryType.Error);
                throw;
            }
        }