Remote.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. Rest.Log.WarnFormat("{0} Move: No movement information provided", MsgId);
  108. rdata.Fail(Rest.HttpStatusCodeBadRequest, "no movement information provided");
  109. }
  110. else
  111. {
  112. string[] names = rdata.Parameters[PARM_MOVE_AVATAR].Split(Rest.CA_SPACE);
  113. ScenePresence presence = null;
  114. Scene scene = null;
  115. if (names.Length != 2)
  116. {
  117. rdata.Fail(Rest.HttpStatusCodeBadRequest,
  118. String.Format("invalid avatar name: <{0}>",rdata.Parameters[PARM_MOVE_AVATAR]));
  119. }
  120. Rest.Log.WarnFormat("{0} '{1}' command received for {2} {3}",
  121. MsgId, rdata.Parameters[0], names[0], names[1]);
  122. // The first parameter should be an avatar name, look for the
  123. // avatar in the known regions first.
  124. Rest.main.SceneManager.ForEachScene(delegate(Scene s)
  125. {
  126. s.ForEachScenePresence(delegate(ScenePresence sp)
  127. {
  128. if (sp.Firstname == names[0] && sp.Lastname == names[1])
  129. {
  130. scene = s;
  131. presence = sp;
  132. }
  133. });
  134. });
  135. if (presence != null)
  136. {
  137. Rest.Log.DebugFormat("{0} Move : Avatar {1} located in region {2}",
  138. MsgId, rdata.Parameters[PARM_MOVE_AVATAR], scene.RegionInfo.RegionName);
  139. try
  140. {
  141. float x = Convert.ToSingle(rdata.Parameters[PARM_MOVE_X]);
  142. float y = Convert.ToSingle(rdata.Parameters[PARM_MOVE_Y]);
  143. float z = Convert.ToSingle(rdata.Parameters[PARM_MOVE_Z]);
  144. Vector3 vector = new Vector3(x, y, z);
  145. presence.MoveToTarget(vector, false);
  146. }
  147. catch (Exception e)
  148. {
  149. rdata.Fail(Rest.HttpStatusCodeBadRequest,
  150. String.Format("invalid parameters: {0}", e.Message));
  151. }
  152. }
  153. else
  154. {
  155. rdata.Fail(Rest.HttpStatusCodeBadRequest,
  156. String.Format("avatar {0} not present", rdata.Parameters[PARM_MOVE_AVATAR]));
  157. }
  158. rdata.Complete();
  159. rdata.Respond("OK");
  160. }
  161. }
  162. private static readonly string Help =
  163. "<html>"
  164. + "<head><title>Remote Command Usage</title></head>"
  165. + "<body>"
  166. + "<p>Supported commands are:</p>"
  167. + "<dl>"
  168. + "<dt>move/avatar-name/x/y/z</dt>"
  169. + "<dd>moves the specified avatar to another location</dd>"
  170. + "</dl>"
  171. + "</body>"
  172. + "</html>"
  173. ;
  174. }
  175. }