OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ConsoleClient.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using Nini.Config;
29 using log4net;
30 using System.Reflection;
31 using System;
32 using System.IO;
33 using System.Xml;
34 using System.Collections.Generic;
35 using OpenSim.Server.Base;
36 using OpenSim.Framework;
37 using OpenSim.Framework.Console;
38 using OpenMetaverse;
39 
40 namespace OpenSim.ConsoleClient
41 {
42  public class OpenSimConsoleClient
43  {
44  protected static ServicesServerBase m_Server = null;
45  private static string m_Host;
46  private static int m_Port;
47  private static string m_User;
48  private static string m_Pass;
49  private static UUID m_SessionID;
50 
51  static int Main(string[] args)
52  {
53  m_Server = new ServicesServerBase("Client", args);
54 
55  IConfig serverConfig = m_Server.Config.Configs["Startup"];
56  if (serverConfig == null)
57  {
58  System.Console.WriteLine("Startup config section missing in .ini file");
59  throw new Exception("Configuration error");
60  }
61 
62  ArgvConfigSource argvConfig = new ArgvConfigSource(args);
63 
64  argvConfig.AddSwitch("Startup", "host", "h");
65  argvConfig.AddSwitch("Startup", "port", "p");
66  argvConfig.AddSwitch("Startup", "user", "u");
67  argvConfig.AddSwitch("Startup", "pass", "P");
68 
69  m_Server.Config.Merge(argvConfig);
70 
71  m_User = serverConfig.GetString("user", "Test");
72  m_Host = serverConfig.GetString("host", "localhost");
73  m_Port = serverConfig.GetInt("port", 8003);
74  m_Pass = serverConfig.GetString("pass", "secret");
75 
76  Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/StartSession/", String.Format("USER={0}&PASS={1}", m_User, m_Pass), LoginReply);
77 
78  string pidFile = serverConfig.GetString("PIDFile", String.Empty);
79 
80  while (m_Server.Running)
81  {
82  System.Threading.Thread.Sleep(500);
83  // MainConsole.Instance.Prompt();
84  }
85 
86  if (pidFile != String.Empty)
87  File.Delete(pidFile);
88 
89  Environment.Exit(0);
90 
91  return 0;
92  }
93 
94  private static void SendCommand(string module, string[] cmd)
95  {
96  string sendCmd = "";
97  string[] cmdlist = new string[cmd.Length - 1];
98 
99  sendCmd = cmd[0];
100 
101  if (cmd.Length > 1)
102  {
103  Array.Copy(cmd, 1, cmdlist, 0, cmd.Length - 1);
104  sendCmd += " \"" + String.Join("\" \"", cmdlist) + "\"";
105  }
106 
107  Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/SessionCommand/", String.Format("ID={0}&COMMAND={1}", m_SessionID, sendCmd), CommandReply);
108  }
109 
110  public static void LoginReply(string requestUrl, string requestData, string replyData)
111  {
112  XmlDocument doc = new XmlDocument();
113 
114  doc.LoadXml(replyData);
115 
116  XmlNodeList rootL = doc.GetElementsByTagName("ConsoleSession");
117  if (rootL.Count != 1)
118  {
119  MainConsole.Instance.Output("Connection data info was not valid");
120  Environment.Exit(1);
121  }
122  XmlElement rootNode = (XmlElement)rootL[0];
123 
124  if (rootNode == null)
125  {
126  MainConsole.Instance.Output("Connection data info was not valid");
127  Environment.Exit(1);
128  }
129 
130  XmlNodeList helpNodeL = rootNode.GetElementsByTagName("HelpTree");
131  if (helpNodeL.Count != 1)
132  {
133  MainConsole.Instance.Output("Connection data info was not valid");
134  Environment.Exit(1);
135  }
136 
137  XmlElement helpNode = (XmlElement)helpNodeL[0];
138  if (helpNode == null)
139  {
140  MainConsole.Instance.Output("Connection data info was not valid");
141  Environment.Exit(1);
142  }
143 
144  XmlNodeList sessionL = rootNode.GetElementsByTagName("SessionID");
145  if (sessionL.Count != 1)
146  {
147  MainConsole.Instance.Output("Connection data info was not valid");
148  Environment.Exit(1);
149  }
150 
151  XmlElement sessionNode = (XmlElement)sessionL[0];
152  if (sessionNode == null)
153  {
154  MainConsole.Instance.Output("Connection data info was not valid");
155  Environment.Exit(1);
156  }
157 
158  if (!UUID.TryParse(sessionNode.InnerText, out m_SessionID))
159  {
160  MainConsole.Instance.Output("Connection data info was not valid");
161  Environment.Exit(1);
162  }
163 
164  MainConsole.Instance.Commands.FromXml(helpNode, SendCommand);
165 
166  Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/ReadResponses/"+m_SessionID.ToString()+"/", String.Empty, ReadResponses);
167  }
168 
169  public static void ReadResponses(string requestUrl, string requestData, string replyData)
170  {
171  XmlDocument doc = new XmlDocument();
172 
173  doc.LoadXml(replyData);
174 
175  XmlNodeList rootNodeL = doc.GetElementsByTagName("ConsoleSession");
176  if (rootNodeL.Count != 1 || rootNodeL[0] == null)
177  {
178  Requester.MakeRequest(requestUrl, requestData, ReadResponses);
179  return;
180  }
181 
182  List<string> lines = new List<string>();
183 
184  foreach (XmlNode part in rootNodeL[0].ChildNodes)
185  {
186  if (part.Name != "Line")
187  continue;
188 
189  lines.Add(part.InnerText);
190  }
191 
192  // Cut down scrollback to 100 lines (4 screens)
193  // for the command line client
194  //
195  while (lines.Count > 100)
196  lines.RemoveAt(0);
197 
198  string prompt = String.Empty;
199 
200  foreach (string l in lines)
201  {
202  string[] parts = l.Split(new char[] {':'}, 3);
203  if (parts.Length != 3)
204  continue;
205 
206  if (parts[2].StartsWith("+++") || parts[2].StartsWith("-++"))
207  prompt = parts[2];
208  else
209  MainConsole.Instance.Output(parts[2].Trim(), parts[1]);
210  }
211 
212 
213  Requester.MakeRequest(requestUrl, requestData, ReadResponses);
214 
215  if (prompt.StartsWith("+++"))
216  MainConsole.Instance.ReadLine(prompt.Substring(3), true, true);
217  else if (prompt.StartsWith("-++"))
218  SendCommand(String.Empty, new string[] { MainConsole.Instance.ReadLine(prompt.Substring(3), false, true) });
219  }
220 
221  public static void CommandReply(string requestUrl, string requestData, string replyData)
222  {
223  }
224  }
225 }
static void ReadResponses(string requestUrl, string requestData, string replyData)
static ICommandConsole Instance
Definition: MainConsole.cs:35
Interactive OpenSim region server
Definition: OpenSim.cs:55
static void LoginReply(string requestUrl, string requestData, string replyData)
static void CommandReply(string requestUrl, string requestData, string replyData)
string ReadLine(string p, bool isCommand, bool e)