RemoteConsole.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Threading;
  33. using Nini.Config;
  34. using OpenSim.Framework.Servers.HttpServer;
  35. using log4net;
  36. namespace OpenSim.Framework.Console
  37. {
  38. // A console that uses REST interfaces
  39. //
  40. public class RemoteConsole : CommandConsole
  41. {
  42. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. // private IHttpServer m_Server = null;
  44. // private IConfigSource m_Config = null;
  45. private List<string> m_Scrollback = new List<string>();
  46. private ManualResetEvent m_DataEvent = new ManualResetEvent(false);
  47. private List<string> m_InputData = new List<string>();
  48. private uint m_LineNumber = 1;
  49. public RemoteConsole(string defaultPrompt) : base(defaultPrompt)
  50. {
  51. }
  52. public void ReadConfig(IConfigSource config)
  53. {
  54. // m_Config = config;
  55. }
  56. public void SetServer(IHttpServer server)
  57. {
  58. // m_Server = server;
  59. }
  60. public override void Output(string text, string level)
  61. {
  62. lock (m_Scrollback)
  63. {
  64. while (m_Scrollback.Count >= 1000)
  65. m_Scrollback.RemoveAt(0);
  66. m_Scrollback.Add(String.Format("{0}", m_LineNumber)+":"+level+":"+text);
  67. m_LineNumber++;
  68. }
  69. System.Console.Write(text);
  70. }
  71. public override string ReadLine(string p, bool isCommand, bool e)
  72. {
  73. System.Console.Write("{0}", prompt);
  74. m_DataEvent.WaitOne();
  75. lock (m_InputData)
  76. {
  77. if (m_InputData.Count == 0)
  78. {
  79. m_DataEvent.Reset();
  80. return "";
  81. }
  82. string cmdinput = m_InputData[0];
  83. m_InputData.RemoveAt(0);
  84. if (m_InputData.Count == 0)
  85. m_DataEvent.Reset();
  86. if (isCommand)
  87. {
  88. string[] cmd = Commands.Resolve(Parser.Parse(cmdinput));
  89. if (cmd.Length != 0)
  90. {
  91. int i;
  92. for (i=0 ; i < cmd.Length ; i++)
  93. {
  94. if (cmd[i].Contains(" "))
  95. cmd[i] = "\"" + cmd[i] + "\"";
  96. }
  97. return String.Empty;
  98. }
  99. }
  100. return cmdinput;
  101. }
  102. }
  103. }
  104. }