ConsoleClient.cs 7.4 KB

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