Example #1
0
        public static int Main(string[] args)
        {
            string 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);

                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) {
                CloudBClientService clientService = new CloudBClientService(commandLine);

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

                return 0;
            }

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

            ProductInfo libInfo = ProductInfo.GetProductInfo(typeof(PathClientService));
            ProductInfo nodeInfo = ProductInfo.GetProductInfo(typeof(PathClient));

            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;
            }

            if (portArg == null) {
                wout.WriteLine("Error, no port address given.");
                failed = true;
            }

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

                netConfig = NormalizeFilePath(netConfig);

                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 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);
                }

                //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);

                Console.Out.WriteLine("Path Client Service, " + host + " : " + port);
                //TODO:
                service = new TcpPathClientService(null, null, null);
                service.Init();

                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;
        }
Example #2
0
        public static int Main(string[] args)
        {
            string 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);

                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)
            {
                CloudBClientService clientService = new CloudBClientService(commandLine);

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

                return(0);
            }

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

            ProductInfo libInfo  = ProductInfo.GetProductInfo(typeof(PathClientService));
            ProductInfo nodeInfo = ProductInfo.GetProductInfo(typeof(PathClient));

            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;
            }

            if (portArg == null)
            {
                wout.WriteLine("Error, no port address given.");
                failed = true;
            }

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

                netConfig = NormalizeFilePath(netConfig);

                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 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);
                }

                //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);

                Console.Out.WriteLine("Path Client Service, " + host + " : " + port);
                //TODO:
                service = new TcpPathClientService(null, null, null);
                service.Init();

                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);
        }