public void StructuredProperties()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("test.me = pass");
            sb.AppendLine("test.two = ed");
            sb.AppendLine("stress.it.to.the.max = 200");
            sb.AppendLine("stress.it.again = 23d");

            ConfigSource source = new ConfigSource();
            source.LoadProperties(GetStream(sb));

            Assert.AreEqual(2, source.GetChild("test").Keys.Length);
            Assert.AreEqual("ed", source.GetChild("test").GetString("two"));
        }
        public void SimpleProperties()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("test=passed");
            sb.AppendLine("me=spaced value");

            ConfigSource source = new ConfigSource();
            source.LoadProperties(GetStream(sb));

            Assert.AreEqual("test", source.Keys[0]);
            Assert.AreEqual("me", source.Keys[1]);
            Assert.AreEqual("passed", source.GetString("test"));
            Assert.AreEqual("spaced value", source.GetString("me"));
        }
Пример #3
0
        public void LoadProperties()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("test=23");
            sb.AppendLine("test.foo=12");
            sb.AppendLine("test.bar=test");
            Stream input = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));

            ConfigSource config = new ConfigSource();
            config.LoadProperties(input);

            Assert.AreEqual(1, config.Keys.Length);
            Assert.AreEqual("test", config.Keys[0]);
            Assert.AreEqual(2, config.GetChild("test").Keys.Length);
            Assert.AreEqual("foo", config.GetChild("test").Keys[0]);
            Assert.AreEqual("bar", config.GetChild("test").Keys[1]);
            Assert.AreEqual(12, config.GetChild("test").GetInt32("foo"));
            Assert.AreEqual("test", config.GetChild("test").GetString("bar"));
        }
Пример #4
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);
        }
Пример #5
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;
        }
Пример #6
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;
            }
        }