Remote.cs 7.6 KB

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