1
0

Console.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.Text;
  30. namespace OpenSim.GridLaunch.GUI.Console
  31. {
  32. internal class Console: IGUI
  33. {
  34. private List<string> Apps = new List<string>();
  35. public Console ()
  36. {
  37. Program.AppCreated += Program_AppCreated;
  38. Program.AppRemoved += Program_AppRemoved;
  39. Program.AppConsoleOutput += Program_AppConsoleOutput;
  40. Program.Command.CommandLine += Command_CommandLine;
  41. }
  42. private string currentApp = "";
  43. private bool quitTyped = false;
  44. void Command_CommandLine(string application, string command, string arguments)
  45. {
  46. // If command is a number then someone might be trying to change console: /1, /2, etc.
  47. int currentAppNum = 0;
  48. if (int.TryParse(command, out currentAppNum))
  49. if (currentAppNum <= Apps.Count)
  50. {
  51. currentApp = Apps[currentAppNum - 1];
  52. System.Console.WriteLine("Changed console to app: " + currentApp);
  53. } else
  54. System.Console.WriteLine("Unable to change to app number: " + currentAppNum);
  55. // Has user typed quit?
  56. if (command.ToLower() == "quit")
  57. quitTyped = true;
  58. // Has user typed /list?
  59. if (command.ToLower() == "list")
  60. {
  61. System.Console.WriteLine("/0 Log console");
  62. for (int i = 1; i <= Apps.Count; i++)
  63. {
  64. System.Console.WriteLine(string.Format("/{0} {1}", i, Apps[i - 1]));
  65. }
  66. }
  67. }
  68. #region Module Start / Stop
  69. public void StartGUI()
  70. {
  71. // Console start
  72. System.Console.WriteLine("Console GUI");
  73. System.Console.WriteLine("Use commands /0, /1, /2, etc to switch between applications.");
  74. System.Console.WriteLine("Type /list for list of applications.");
  75. System.Console.WriteLine("Anything that doesn't start with a / will be sent to selected application");
  76. System.Console.WriteLine("type /quit to exit");
  77. while (quitTyped == false)
  78. {
  79. string line = System.Console.ReadLine().TrimEnd("\r\n".ToCharArray());
  80. Program.Write(currentApp, line);
  81. }
  82. // We are done
  83. System.Console.WriteLine("Console exit.");
  84. }
  85. public void StopGUI()
  86. {
  87. // Console stop
  88. }
  89. #endregion
  90. #region GridLaunch Events
  91. void Program_AppCreated(string App)
  92. {
  93. System.Console.WriteLine("Started: " + App);
  94. if (!Apps.Contains(App))
  95. Apps.Add(App);
  96. }
  97. void Program_AppRemoved(string App)
  98. {
  99. System.Console.WriteLine("Stopped: " + App);
  100. if (Apps.Contains(App))
  101. Apps.Remove(App);
  102. }
  103. void Program_AppConsoleOutput(string App, string Text)
  104. {
  105. System.Console.Write(App + ": " + Text);
  106. }
  107. #endregion
  108. }
  109. }