ConsoleClient.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. using Nini.Config;
  28. using log4net;
  29. using System.Reflection;
  30. using System;
  31. using System.IO;
  32. using System.Xml;
  33. using System.Collections.Generic;
  34. using OpenSim.Server.Base;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Console;
  37. using OpenMetaverse;
  38. namespace OpenSim.ConsoleClient
  39. {
  40. public class OpenSimConsoleClient
  41. {
  42. protected static ServicesServerBase m_Server = null;
  43. private static string m_Host;
  44. private static int m_Port;
  45. private static string m_User;
  46. private static string m_Pass;
  47. private static UUID m_SessionID;
  48. static int Main(string[] args)
  49. {
  50. m_Server = new ServicesServerBase("Client", args);
  51. IConfig serverConfig = m_Server.Config.Configs["Startup"];
  52. if (serverConfig == null)
  53. {
  54. System.Console.WriteLine("Startup config section missing in .ini file");
  55. throw new Exception("Configuration error");
  56. }
  57. ArgvConfigSource argvConfig = new ArgvConfigSource(args);
  58. argvConfig.AddSwitch("Startup", "host", "h");
  59. argvConfig.AddSwitch("Startup", "port", "p");
  60. argvConfig.AddSwitch("Startup", "user", "u");
  61. argvConfig.AddSwitch("Startup", "pass", "P");
  62. m_Server.Config.Merge(argvConfig);
  63. m_User = serverConfig.GetString("user", "Test");
  64. m_Host = serverConfig.GetString("host", "localhost");
  65. m_Port = serverConfig.GetInt("port", 8003);
  66. m_Pass = serverConfig.GetString("pass", "secret");
  67. Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/StartSession/", String.Format("USER={0}&PASS={1}", m_User, m_Pass), LoginReply);
  68. string pidFile = serverConfig.GetString("PIDFile", String.Empty);
  69. while (m_Server.Running)
  70. {
  71. System.Threading.Thread.Sleep(500);
  72. // MainConsole.Instance.Prompt();
  73. }
  74. if (pidFile != String.Empty)
  75. File.Delete(pidFile);
  76. Environment.Exit(0);
  77. return 0;
  78. }
  79. private static void SendCommand(string module, string[] cmd)
  80. {
  81. string sendCmd = "";
  82. string[] cmdlist = new string[cmd.Length - 1];
  83. sendCmd = cmd[0];
  84. if (cmd.Length > 1)
  85. {
  86. Array.Copy(cmd, 1, cmdlist, 0, cmd.Length - 1);
  87. sendCmd += " \"" + String.Join("\" \"", cmdlist) + "\"";
  88. }
  89. Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/SessionCommand/", String.Format("ID={0}&COMMAND={1}", m_SessionID, sendCmd), CommandReply);
  90. }
  91. public static void LoginReply(string requestUrl, string requestData, string replyData)
  92. {
  93. XmlDocument doc = new XmlDocument();
  94. doc.LoadXml(replyData);
  95. XmlNodeList rootL = doc.GetElementsByTagName("ConsoleSession");
  96. if (rootL.Count != 1)
  97. {
  98. MainConsole.Instance.Output("Connection data info was not valid");
  99. Environment.Exit(1);
  100. }
  101. XmlElement rootNode = (XmlElement)rootL[0];
  102. if (rootNode == null)
  103. {
  104. MainConsole.Instance.Output("Connection data info was not valid");
  105. Environment.Exit(1);
  106. }
  107. XmlNodeList helpNodeL = rootNode.GetElementsByTagName("HelpTree");
  108. if (helpNodeL.Count != 1)
  109. {
  110. MainConsole.Instance.Output("Connection data info was not valid");
  111. Environment.Exit(1);
  112. }
  113. XmlElement helpNode = (XmlElement)helpNodeL[0];
  114. if (helpNode == null)
  115. {
  116. MainConsole.Instance.Output("Connection data info was not valid");
  117. Environment.Exit(1);
  118. }
  119. XmlNodeList sessionL = rootNode.GetElementsByTagName("SessionID");
  120. if (sessionL.Count != 1)
  121. {
  122. MainConsole.Instance.Output("Connection data info was not valid");
  123. Environment.Exit(1);
  124. }
  125. XmlElement sessionNode = (XmlElement)sessionL[0];
  126. if (sessionNode == null)
  127. {
  128. MainConsole.Instance.Output("Connection data info was not valid");
  129. Environment.Exit(1);
  130. }
  131. if (!UUID.TryParse(sessionNode.InnerText, out m_SessionID))
  132. {
  133. MainConsole.Instance.Output("Connection data info was not valid");
  134. Environment.Exit(1);
  135. }
  136. MainConsole.Instance.Commands.FromXml(helpNode, SendCommand);
  137. Requester.MakeRequest("http://"+m_Host+":"+m_Port.ToString()+"/ReadResponses/"+m_SessionID.ToString()+"/", String.Empty, ReadResponses);
  138. }
  139. public static void ReadResponses(string requestUrl, string requestData, string replyData)
  140. {
  141. XmlDocument doc = new XmlDocument();
  142. doc.LoadXml(replyData);
  143. XmlNodeList rootNodeL = doc.GetElementsByTagName("ConsoleSession");
  144. if (rootNodeL.Count != 1 || rootNodeL[0] == null)
  145. {
  146. Requester.MakeRequest(requestUrl, requestData, ReadResponses);
  147. return;
  148. }
  149. List<string> lines = new List<string>();
  150. foreach (XmlNode part in rootNodeL[0].ChildNodes)
  151. {
  152. if (part.Name != "Line")
  153. continue;
  154. lines.Add(part.InnerText);
  155. }
  156. // Cut down scrollback to 100 lines (4 screens)
  157. // for the command line client
  158. //
  159. while (lines.Count > 100)
  160. lines.RemoveAt(0);
  161. string prompt = String.Empty;
  162. foreach (string l in lines)
  163. {
  164. string[] parts = l.Split(new char[] {':'}, 3);
  165. if (parts.Length != 3)
  166. continue;
  167. if (parts[2].StartsWith("+++") || parts[2].StartsWith("-++"))
  168. prompt = parts[2];
  169. else
  170. MainConsole.Instance.Output(parts[2].Trim(), parts[1]);
  171. }
  172. Requester.MakeRequest(requestUrl, requestData, ReadResponses);
  173. if (prompt.StartsWith("+++"))
  174. MainConsole.Instance.ReadLine(prompt.Substring(3), true, true);
  175. else if (prompt.StartsWith("-++"))
  176. SendCommand(String.Empty, new string[] { MainConsole.Instance.ReadLine(prompt.Substring(3), false, true) });
  177. }
  178. public static void CommandReply(string requestUrl, string requestData, string replyData)
  179. {
  180. }
  181. }
  182. }