Remote.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 OpenSim 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 OpenMetaverse;
  28. using Nini.Config;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Threading;
  33. using System.Xml;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Environment.Scenes;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Communications;
  38. using OpenSim.Framework.Communications.Cache;
  39. namespace OpenSim.ApplicationPlugins.Rest.Inventory.Tests
  40. {
  41. public class Remote : ITest
  42. {
  43. private static readonly int PARM_TESTID = 0;
  44. private static readonly int PARM_COMMAND = 1;
  45. private static readonly int PARM_MOVE_AVATAR = 2;
  46. private static readonly int PARM_MOVE_X = 3;
  47. private static readonly int PARM_MOVE_Y = 4;
  48. private static readonly int PARM_MOVE_Z = 5;
  49. private bool enabled = false;
  50. // No constructor code is required.
  51. public Remote()
  52. {
  53. Rest.Log.InfoFormat("{0} Remote services constructor", MsgId);
  54. }
  55. // Post-construction, pre-enabled initialization opportunity
  56. // Not currently exploited.
  57. public void Initialize()
  58. {
  59. enabled = true;
  60. Rest.Log.InfoFormat("{0} Remote services initialized", MsgId);
  61. }
  62. // Called by the plug-in to halt REST processing. Local processing is
  63. // disabled, and control blocks until all current processing has
  64. // completed. No new processing will be started
  65. public void Close()
  66. {
  67. enabled = false;
  68. Rest.Log.InfoFormat("{0} Remote services closing down", MsgId);
  69. }
  70. // Properties
  71. internal string MsgId
  72. {
  73. get { return Rest.MsgId; }
  74. }
  75. // Remote Handler
  76. // Key information of interest here is the Parameters array, each
  77. // entry represents an element of the URI, with element zero being
  78. // the
  79. public void Execute(RequestData rdata)
  80. {
  81. if (!enabled) return;
  82. // If we can't relate to what's there, leave it for others.
  83. if (rdata.Parameters.Length == 0 || rdata.Parameters[PARM_TESTID] != "remote")
  84. return;
  85. Rest.Log.DebugFormat("{0} REST Remote handler ENTRY", MsgId);
  86. // Remove the prefix and what's left are the parameters. If we don't have
  87. // the parameters we need, fail the request. Parameters do NOT include
  88. // any supplied query values.
  89. if (rdata.Parameters.Length > 1)
  90. {
  91. switch (rdata.Parameters[PARM_COMMAND].ToLower())
  92. {
  93. case "move" :
  94. DoMove(rdata);
  95. break;
  96. default :
  97. DoHelp(rdata);
  98. break;
  99. }
  100. }
  101. else
  102. {
  103. DoHelp(rdata);
  104. }
  105. }
  106. private void DoHelp(RequestData rdata)
  107. {
  108. rdata.body = Help;
  109. rdata.Complete();
  110. rdata.Respond("Help");
  111. }
  112. private void DoMove(RequestData rdata)
  113. {
  114. if (rdata.Parameters.Length >= 6)
  115. {
  116. string[] names = rdata.Parameters[PARM_MOVE_AVATAR].Split(Rest.CA_SPACE);
  117. ScenePresence avatar = null;
  118. Scene scene = null;
  119. if (names.Length != 2)
  120. {
  121. rdata.Fail(Rest.HttpStatusCodeBadRequest,
  122. String.Format("invalid avatar name: <{0}>",rdata.Parameters[PARM_MOVE_AVATAR]));
  123. }
  124. Rest.Log.WarnFormat("{0} '{1}' command received for {2} {3}",
  125. MsgId, rdata.Parameters[0], names[0], names[1]);
  126. // The first parameter should be an avatar name, look for the
  127. // avatar in the known regions first.
  128. foreach (Scene cs in Rest.main.SceneManager.Scenes)
  129. {
  130. foreach (ScenePresence presence in cs.GetAvatars())
  131. {
  132. if (presence.Firstname == names[0] &&
  133. presence.Lastname == names[1])
  134. {
  135. scene = cs;
  136. avatar = presence;
  137. break;
  138. }
  139. }
  140. }
  141. if (avatar != null)
  142. {
  143. Rest.Log.DebugFormat("{0} Move : Avatar {1} located in region {2}",
  144. MsgId, rdata.Parameters[PARM_MOVE_AVATAR], scene.RegionInfo.RegionName);
  145. try
  146. {
  147. float x = Convert.ToSingle(rdata.Parameters[PARM_MOVE_X]);
  148. float y = Convert.ToSingle(rdata.Parameters[PARM_MOVE_Y]);
  149. float z = Convert.ToSingle(rdata.Parameters[PARM_MOVE_Z]);
  150. Vector3 vector = new Vector3(x,y,z);
  151. avatar.DoAutoPilot(0,vector,avatar.ControllingClient);
  152. }
  153. catch (Exception e)
  154. {
  155. rdata.Fail(Rest.HttpStatusCodeBadRequest,
  156. String.Format("invalid parameters: {0}", e.Message));
  157. }
  158. }
  159. else
  160. {
  161. rdata.Fail(Rest.HttpStatusCodeBadRequest,
  162. String.Format("avatar {0} not present", rdata.Parameters[PARM_MOVE_AVATAR]));
  163. }
  164. rdata.Complete();
  165. rdata.Respond("OK");
  166. }
  167. else
  168. {
  169. Rest.Log.WarnFormat("{0} Move: No movement information provided", MsgId);
  170. rdata.Fail(Rest.HttpStatusCodeBadRequest, "no movement information provided");
  171. }
  172. }
  173. private static readonly string Help =
  174. "<html>"
  175. + "<head><title>Remote Command Usage</title></head>"
  176. + "<body>"
  177. + "<p>Supported commands are:</p>"
  178. + "<dl>"
  179. + "<dt>move/avatar-name/x/y/z</dt>"
  180. + "<dd>moves the specified avatar to another location</dd>"
  181. + "</dl>"
  182. + "</body>"
  183. + "</html>"
  184. ;
  185. }
  186. }