diff options
Diffstat (limited to 'OpenSim.Framework.Console/ConsoleBase.cs')
-rw-r--r-- | OpenSim.Framework.Console/ConsoleBase.cs | 144 |
1 files changed, 125 insertions, 19 deletions
diff --git a/OpenSim.Framework.Console/ConsoleBase.cs b/OpenSim.Framework.Console/ConsoleBase.cs index 5343e71..e2e4457 100644 --- a/OpenSim.Framework.Console/ConsoleBase.cs +++ b/OpenSim.Framework.Console/ConsoleBase.cs | |||
@@ -1,45 +1,151 @@ | |||
1 | using System; | 1 | using System; |
2 | using System.IO; | ||
2 | 3 | ||
3 | namespace OpenSim.Framework.Console | 4 | namespace OpenSim.Framework.Console |
4 | { | 5 | { |
5 | public abstract class ConsoleBase | 6 | public class ConsoleBase |
6 | { | 7 | { |
8 | StreamWriter Log; | ||
9 | public conscmd_callback cmdparser; | ||
10 | public string componentname; | ||
7 | 11 | ||
8 | public enum ConsoleType | 12 | // STUPID HACK ALERT!!!! STUPID HACK ALERT!!!!! |
13 | // constype - the type of console to use (see enum ConsoleType) | ||
14 | // sparam - depending on the console type: | ||
15 | // TCP - the IP to bind to (127.0.0.1 if blank) | ||
16 | // Local - param ignored | ||
17 | // and for the iparam: | ||
18 | // TCP - the port to bind to | ||
19 | // Local - param ignored | ||
20 | // LogFile - duh | ||
21 | // componentname - which component of the OGS system? (user, asset etc) | ||
22 | // cmdparser - a reference to a conscmd_callback object | ||
23 | |||
24 | public ConsoleBase(string LogFile, string componentname, conscmd_callback cmdparser) | ||
9 | { | 25 | { |
10 | Local, // Use stdio | 26 | this.componentname = componentname; |
11 | TCP, // Use TCP/telnet | 27 | this.cmdparser = cmdparser; |
12 | SimChat // Use in-world chat (for gods) | 28 | |
29 | System.Console.WriteLine("ServerConsole.cs - creating new local console"); | ||
30 | System.Console.WriteLine("Logs will be saved to current directory in " + LogFile); | ||
31 | Log = File.AppendText(LogFile); | ||
32 | Log.WriteLine("========================================================================"); | ||
33 | Log.WriteLine(componentname + " Started at " + DateTime.Now.ToString()); | ||
13 | } | 34 | } |
14 | 35 | ||
15 | public abstract void Close(); | 36 | public void Close() |
37 | { | ||
38 | Log.WriteLine("Shutdown at " + DateTime.Now.ToString()); | ||
39 | Log.Close(); | ||
40 | } | ||
41 | |||
42 | public void Write(string format, params object[] args) | ||
43 | { | ||
44 | Log.Write(format, args); | ||
45 | System.Console.Write(format, args); | ||
46 | return; | ||
47 | } | ||
16 | 48 | ||
17 | public abstract void Write(string format, params object[] args); | 49 | public void WriteLine(string format, params object[] args) |
50 | { | ||
51 | Log.WriteLine(format, args); | ||
52 | System.Console.WriteLine(format, args); | ||
53 | return; | ||
54 | } | ||
18 | 55 | ||
19 | public abstract void WriteLine(string format, params object[] args); | 56 | public string ReadLine() |
57 | { | ||
58 | string TempStr = System.Console.ReadLine(); | ||
59 | Log.WriteLine(TempStr); | ||
60 | return TempStr; | ||
61 | } | ||
20 | 62 | ||
21 | public abstract string ReadLine(); | 63 | public int Read() |
64 | { | ||
65 | int TempInt = System.Console.Read(); | ||
66 | Log.Write((char)TempInt); | ||
67 | return TempInt; | ||
68 | } | ||
22 | 69 | ||
23 | public abstract int Read(); | 70 | // Displays a prompt and waits for the user to enter a string, then returns that string |
71 | // Done with no echo and suitable for passwords | ||
72 | public string PasswdPrompt(string prompt) | ||
73 | { | ||
74 | // FIXME: Needs to be better abstracted | ||
75 | Log.WriteLine(prompt); | ||
76 | this.Write(prompt); | ||
77 | ConsoleColor oldfg = System.Console.ForegroundColor; | ||
78 | System.Console.ForegroundColor = System.Console.BackgroundColor; | ||
79 | string temp = System.Console.ReadLine(); | ||
80 | System.Console.ForegroundColor = oldfg; | ||
81 | return temp; | ||
82 | } | ||
24 | 83 | ||
25 | // Displays a command prompt and waits for the user to enter a string, then returns that string | 84 | // Displays a command prompt and waits for the user to enter a string, then returns that string |
26 | public abstract string CmdPrompt(string prompt); | 85 | public string CmdPrompt(string prompt) |
86 | { | ||
87 | this.Write(prompt); | ||
88 | return this.ReadLine(); | ||
89 | } | ||
27 | 90 | ||
28 | // Displays a command prompt and returns a default value if the user simply presses enter | 91 | // Displays a command prompt and returns a default value if the user simply presses enter |
29 | public abstract string CmdPrompt(string prompt, string defaultresponse); | 92 | public string CmdPrompt(string prompt, string defaultresponse) |
93 | { | ||
94 | string temp = CmdPrompt(prompt); | ||
95 | if (temp == "") | ||
96 | { | ||
97 | return defaultresponse; | ||
98 | } | ||
99 | else | ||
100 | { | ||
101 | return temp; | ||
102 | } | ||
103 | } | ||
30 | 104 | ||
31 | // Displays a command prompt and returns a default value, user may only enter 1 of 2 options | 105 | // Displays a command prompt and returns a default value, user may only enter 1 of 2 options |
32 | public abstract string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB); | 106 | public string CmdPrompt(string prompt, string defaultresponse, string OptionA, string OptionB) |
107 | { | ||
108 | bool itisdone = false; | ||
109 | string temp = CmdPrompt(prompt, defaultresponse); | ||
110 | while (itisdone == false) | ||
111 | { | ||
112 | if ((temp == OptionA) || (temp == OptionB)) | ||
113 | { | ||
114 | itisdone = true; | ||
115 | } | ||
116 | else | ||
117 | { | ||
118 | this.WriteLine("Valid options are " + OptionA + " or " + OptionB); | ||
119 | temp = CmdPrompt(prompt, defaultresponse); | ||
120 | } | ||
121 | } | ||
122 | return temp; | ||
123 | } | ||
33 | 124 | ||
34 | // Runs a command with a number of parameters | 125 | // Runs a command with a number of parameters |
35 | public abstract Object RunCmd(string Cmd, string[] cmdparams); | 126 | public Object RunCmd(string Cmd, string[] cmdparams) |
127 | { | ||
128 | cmdparser.RunCmd(Cmd, cmdparams); | ||
129 | return null; | ||
130 | } | ||
36 | 131 | ||
37 | // Shows data about something | 132 | // Shows data about something |
38 | public abstract void ShowCommands(string ShowWhat); | 133 | public void ShowCommands(string ShowWhat) |
39 | 134 | { | |
40 | // Displays a prompt to the user and then runs the command they entered | 135 | cmdparser.Show(ShowWhat); |
41 | public abstract void MainConsolePrompt(); | 136 | } |
42 | 137 | ||
43 | public abstract void SetStatus(string status); | 138 | public void MainConsolePrompt() |
139 | { | ||
140 | string[] tempstrarray; | ||
141 | string tempstr = this.CmdPrompt(this.componentname + "# "); | ||
142 | tempstrarray = tempstr.Split(' '); | ||
143 | string cmd = tempstrarray[0]; | ||
144 | Array.Reverse(tempstrarray); | ||
145 | Array.Resize<string>(ref tempstrarray, tempstrarray.Length - 1); | ||
146 | Array.Reverse(tempstrarray); | ||
147 | string[] cmdparams = (string[])tempstrarray; | ||
148 | RunCmd(cmd, cmdparams); | ||
149 | } | ||
44 | } | 150 | } |
45 | } | 151 | } |