2

So, I wrote a server for specific needs on C#, and now I looking for comfortable methods to manage this server. At first it was a simple web-based manager, but this is not scalable solution, 'cause architecture of my server is changing every day. So now I understand, that the most comfortable and scalable solution is the Command Line Interface. However, I don't wanna use primitive IF\CASE (Command line interface inside your C# application) structure at all. So there is my question:

For example I have a Server class:

public class Server
    {
        private int succes_interval;
        private int fail_interval;
        private int threshold;
        private int failed_requests;
        private int request_timeout;
        private int update_timer;
        private string updt_path;
        private DateTime start_time;
        private bool snmp_enabled;
        private int snmp_port;
        [XmlIgnore()]
        public string Version { get { return Assembly.GetEntryAssembly().GetName().Version.ToString(); } }
        [XmlIgnore()]
        public string Path { get { return Assembly.GetEntryAssembly().Location; } }
        [XmlIgnore()]
        public string ServerName { get { return System.Environment.MachineName.ToLower(); } }
        [XmlIgnore()]
        public int SuccessInterval { get { return succes_interval; } }
        public void SetSuccessInterval(int interval) { succes_interval = interval; }
        [XmlIgnore()]
        public int FailInterval { get { return fail_interval; } }
        public void SetFailInterval(int interval) { fail_interval = interval; }
        [XmlIgnore()]
        public int FailThreshold { get { return threshold; } }
        public void SetFailThreshold(int thresh) { threshold = thresh; }
        [XmlIgnore()]
        public int FailedRequests { get { return failed_requests; } }
        public void SetFailedRequests(int value) { threshold = value; }
        [XmlElement("RequestTimeout")]
        public int RequestTimeout { get { return request_timeout; } set { request_timeout = value; } }
        [XmlIgnore()]
        public int UpdateCheckInterval { get { return update_timer; } set { update_timer = value; } }
        [XmlElement("UpdatePath")]
        public string UpdatePath { get { return updt_path; } set { updt_path = value; } }
        [XmlElement("SNMPEnabled")]
        public bool SNMPEnabled { get { return snmp_enabled; } set { snmp_enabled = value; } }
        [XmlElement("SNMPPort")]
        public int SNMPPort { get { return snmp_port; } set { snmp_port = value; } }
}

and I want to pass command "server threshold 50" to set this variable in runtime. It's no matter how to pass the command in (Telnet\ssh\http\whateverprotocol). Or for example command "show server" lists all variables in this object.

I understand it must be a tree approach, but can somebody give me a some direction to implement this.

2 Answers 2

1

You could use a Dictionary of string commands, and Function delegates. Dictionary<string, Func<string, object>> . When you receive some input just go through each key in the dictionary to see if they input string .StartsWith() that key, if it does, send the rest of it to the Function, you could then reply with the returned value.

Example of how you would add/implement an interface:

myDict = new Dictionary<string, Func<string, object>>();

//add each handler below
myDict.Add("server threshold", (str) =>
{
    int threshold;
    if (int.TryParse(str, out threshold))
        SetFailThreshold(threshold);
    else
        return false;
    return true;
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can use iron python as a CLI, and use python scripts to handle whatever you may need

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.