コード例 #1
0
        private static void ParseGlobalProperties()
        {
            //  Look for options that do not affect the AgentProperties...
            for (int i = 0; i < sArguments.Length; i++)
            {
                if (sArguments[i].ToUpper().Equals("/debug".ToUpper()))
                {
                    if (i < sArguments.Length - 1)
                    {
                        try
                        {
                            Adk.Debug = AdkDebugFlags.None;
                            int k = Int32.Parse(sArguments[++i]);
                            if (k == 1)
                            {
                                Adk.Debug = AdkDebugFlags.Minimal;
                            }
                            else if (k == 2)
                            {
                                Adk.Debug = AdkDebugFlags.Moderate;
                            }
                            else if (k == 3)
                            {
                                Adk.Debug = AdkDebugFlags.Detailed;
                            }
                            else if (k == 4)
                            {
                                Adk.Debug = AdkDebugFlags.Very_Detailed;
                            }
                            else if (k == 5)
                            {
                                Adk.Debug = AdkDebugFlags.All;
                            }
                        }
                        catch (Exception)
                        {
                            Adk.Debug = AdkDebugFlags.All;
                        }
                    }
                    else
                    {
                        Adk.Debug = AdkDebugFlags.All;
                    }
                }
                else if (sArguments[i].StartsWith("/D"))
                {
                    string prop = sArguments[i].Substring(2);
                    if (i != sArguments.Length - 1)
                    {
                        Properties.SetProperty(prop, sArguments[++i]);
                    }
                    else
                    {
                        Console.WriteLine("Usage: /Dproperty value");
                    }
                }
                else if (sArguments[i].ToUpper().Equals("/log".ToUpper()) &&
                         i != sArguments.Length - 1)
                {
                    try
                    {
                        Adk.SetLogFile(sArguments[++i]);
                    }
                    catch (IOException ioe)
                    {
                        Console.WriteLine("Could not redirect debug output to log file: " + ioe);
                    }
                }
                else if (sArguments[i].ToUpper().Equals("/ver".ToUpper()) &&
                         i != sArguments.Length - 1)
                {
                    Version = SifVersion.Parse(sArguments[++i]);
                }
                else if (sArguments[i].Equals("/?"))
                {
                    Console.WriteLine();
                    Console.WriteLine
                        ("These options are common to all Adk Example agents. For help on the usage");
                    Console.WriteLine
                        ("of this agent in particular, run the agent without any parameters. Note that");
                    Console.WriteLine
                        ("most agents support multiple zones if a zones.properties file is found in");
                    Console.WriteLine("the current directory.");
                    Console.WriteLine();
                    printHelp();

                    Environment.Exit(0);
                }
            }
        }
コード例 #2
0
    /// <summary>  Parse the command-line. This method may be called repeatedly, usually
    /// once from the sample agent's <c>main</code> function prior to
    /// initializing the Adk and again from the <c>Agent.initialize</code>
    /// method after initializing the Agent superclass. When called without an
    /// Agent instance, only those options that do not rely on an AgentProperties
    /// object are processed (e.g. the /D option).
    /// <p>
    /// *
    /// If a file named 'agent.rsp' exists in the current directory, any command
    /// line options specified will be appended to the command-line arguments
    /// passed to this method. Each line of the agent.rsp text file may be
    /// comprised of one or more arguments separated by spaces, so that the
    /// entirely set of arguments can be on one line or broken up onto many
    /// lines.<p>
    /// *
    /// </summary>
    /// <param name="agent">An Agent instance that will be updated when certain
    /// command-line options are parsed
    /// </param>
    /// <param name="arguments">The string of arguments provided by the <c>main</code>
    /// function
    ///
    /// </param>
    public static NameValueCollection parseCL(Agent agent,
                                              string[] arguments)
    {
        if (args == null)
        {
            args = arguments;

            if (args.Length > 0 && args[0][0] != '/')
            {
                //  Look for an agent.rsp response file
                FileInfo rsp = new FileInfo(args[0]);
                if (rsp.Exists)
                {
                    try
                    {
                        ArrayList v = new ArrayList();
                        using (StreamReader reader = File.OpenText(rsp.FullName))
                        {
                            string line = null;
                            while ((line = reader.ReadLine()) != null)
                            {
                                // allow comment lines, starting with a ;
                                if (!line.StartsWith(";"))
                                {
                                    foreach (string token in line.Split(' '))
                                    {
                                        v.Add(token);
                                    }
                                }
                            }
                            reader.Close();
                        }

                        //  Append any arguments found to the args array
                        if (v.Count > 0)
                        {
                            args = new string[args.Length + v.Count];
                            Array.Copy(arguments, 0, args, 0, arguments.Length);
                            v.CopyTo(args, arguments.Length);
                            Console.Out.Write
                                ("Reading command-line arguments from " + args[0] + ": ");
                            for (int i = 0; i < args.Length; i++)
                            {
                                Console.Out.Write(args[i] + " ");
                            }
                            Console.WriteLine();
                            Console.WriteLine();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine
                            ("Error reading command-line arguments from agent.rsp file: " + ex);
                    }
                }
            }
        }

        if (agent == null)
        {
            //  Look for options that do not affect the AgentProperties...
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].ToUpper().Equals("/debug".ToUpper()))
                {
                    if (i < args.Length - 1)
                    {
                        try
                        {
                            Adk.Debug = AdkDebugFlags.None;
                            int k = Int32.Parse(args[++i]);
                            if (k == 1)
                            {
                                Adk.Debug = AdkDebugFlags.Minimal;
                            }
                            else if (k == 2)
                            {
                                Adk.Debug = AdkDebugFlags.Moderate;
                            }
                            else if (k == 3)
                            {
                                Adk.Debug = AdkDebugFlags.Detailed;
                            }
                            else if (k == 4)
                            {
                                Adk.Debug = AdkDebugFlags.Very_Detailed;
                            }
                            else if (k == 5)
                            {
                                Adk.Debug = AdkDebugFlags.All;
                            }
                        }
                        catch (Exception)
                        {
                            Adk.Debug = AdkDebugFlags.All;
                        }
                    }
                    else
                    {
                        Adk.Debug = AdkDebugFlags.All;
                    }
                }
                else if (args[i].StartsWith("/D"))
                {
                    string prop = args[i].Substring(2);
                    if (i != args.Length - 1)
                    {
                        Properties.SetProperty(prop, args[++i]);
                    }
                    else
                    {
                        Console.WriteLine("Usage: /Dproperty value");
                    }
                }
                else if (args[i].ToUpper().Equals("/log".ToUpper()) && i != args.Length - 1)
                {
                    try
                    {
                        Adk.SetLogFile(args[++i]);
                    }
                    catch (IOException ioe)
                    {
                        Console.WriteLine("Could not redirect debug output to log file: " + ioe);
                    }
                }
                else if (args[i].ToUpper().Equals("/ver".ToUpper()) && i != args.Length - 1)
                {
                    Version = SifVersion.Parse(args[++i]);
                }
                else if (args[i].Equals("/?"))
                {
                    Console.WriteLine();
                    Console.WriteLine
                    (
                        "These options are common to all Adk Example agents. For help on the usage");
                    Console.WriteLine
                    (
                        "of this agent in particular, run the agent without any parameters. Note that");
                    Console.WriteLine
                    (
                        "most agents support multiple zones if a zones.properties file is found in");
                    Console.WriteLine("the current directory.");
                    Console.WriteLine();
                    printHelp();

                    Environment.Exit(0);
                }
            }

            return(null);
        }

        //  Parse all other options...
        AgentProperties     props = agent.Properties;
        NameValueCollection misc  = new NameValueCollection();

        int    port       = -1;
        string host       = null;
        bool   useHttps   = false;
        string sslCert    = null;
        string clientCert = null;
        int    clientAuth = 0;

        for (int i = 0; i < args.Length; i++)
        {
            if (args[i].ToUpper().Equals("/sourceId".ToUpper()) && i != args.Length - 1)
            {
                agent.Id = args[++i];
            }
            else if (args[i].ToUpper().Equals("/noreg".ToUpper()))
            {
                Reg = false;
            }
            else if (args[i].ToUpper().Equals("/unreg".ToUpper()))
            {
                Unreg = true;
            }
            else if (args[i].ToUpper().Equals("/pull".ToUpper()))
            {
                props.MessagingMode = AgentMessagingMode.Pull;
            }
            else if (args[i].ToUpper().Equals("/push".ToUpper()))
            {
                props.MessagingMode = AgentMessagingMode.Push;
            }
            else if (args[i].ToUpper().Equals("/port".ToUpper()) && i != args.Length - 1)
            {
                try
                {
                    port = Int32.Parse(args[++i]);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid port: " + args[i - 1]);
                }
            }
            else if (args[i].ToUpper().Equals("/https".ToUpper()))
            {
                useHttps = true;
            }
            else if (args[i].ToUpper().Equals("/sslCert".ToUpper()))
            {
                sslCert = args[++i];
            }
            else if (args[i].ToUpper().Equals("/clientCert".ToUpper()))
            {
                clientCert = args[++i];
            }
            else if (args[i].ToUpper().Equals("/clientAuth".ToUpper()))
            {
                try
                {
                    clientAuth = int.Parse(args[++i]);
                }
                catch (FormatException)
                {
                    clientAuth = 0;
                }
            }
            else if (args[i].ToUpper().Equals("/host".ToUpper()) && i != args.Length - 1)
            {
                host = args[++i];
            }
            else if (args[i].ToUpper().Equals("/timeout".ToUpper()) && i != args.Length - 1)
            {
                try
                {
                    props.DefaultTimeout = TimeSpan.FromMilliseconds(Int32.Parse(args[++i]));
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid timeout: " + args[i - 1]);
                }
            }
            else if (args[i].ToUpper().Equals("/freq".ToUpper()) && i != args.Length - 1)
            {
                try
                {
                    props.PullFrequency = TimeSpan.FromMilliseconds(int.Parse(args[++i]));
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid pull frequency: " + args[i - 1]);
                }
            }
            else if (args[i].ToUpper().Equals("/opensif".ToUpper()))
            {
                //  OpenSIF reports attempts to re-subscribe to objects as an
                //  error instead of a success status code. The Adk would therefore
                //  throw an exception if it encountered the error, so we can
                //  disable that behavior here.

                props.IgnoreProvisioningErrors = true;
            }
            else if (args[i][0] == '/')
            {
                if (i == args.Length - 1 || args[i + 1].StartsWith("/"))
                {
                    misc[args[i].Substring(1)] = null;
                }
                else
                {
                    misc[args[i].Substring(1)] = args[++i];
                }
            }
        }

        if (useHttps)
        {
            //  Set transport properties (HTTPS)
            HttpsProperties https = agent.DefaultHttpsProperties;
            if (sslCert != null)
            {
                https.SSLCertName = sslCert;
            }
            if (clientCert != null)
            {
                https.ClientCertName = clientCert;
            }

            https.ClientAuthLevel = clientAuth;

            if (port != -1)
            {
                https.Port = port;
            }
            https.Host              = host;
            https.PushHost          = host;
            props.TransportProtocol = "https";
        }
        else
        {
            //  Set transport properties (HTTP)
            HttpProperties http = agent.DefaultHttpProperties;
            if (port != -1)
            {
                http.Port = port;
            }
            http.Host = host;

            props.TransportProtocol = "http";
        }

        return(misc);
    }