HGCommands.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.Reflection;
  30. using System.Xml;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. //using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Region.Framework;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Region.Framework.Scenes.Hypergrid;
  39. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  40. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
  41. {
  42. public class HGCommands
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private HGGridConnector m_HGGridConnector;
  46. private Scene m_scene;
  47. private static uint m_autoMappingX = 0;
  48. private static uint m_autoMappingY = 0;
  49. private static bool m_enableAutoMapping = false;
  50. public HGCommands(HGGridConnector hgConnector, Scene scene)
  51. {
  52. m_HGGridConnector = hgConnector;
  53. m_scene = scene;
  54. }
  55. //public static Scene CreateScene(RegionInfo regionInfo, AgentCircuitManager circuitManager, CommunicationsManager m_commsManager,
  56. // StorageManager storageManager, ModuleLoader m_moduleLoader, ConfigSettings m_configSettings, OpenSimConfigSource m_config, string m_version)
  57. //{
  58. // HGSceneCommunicationService sceneGridService = new HGSceneCommunicationService(m_commsManager, HGServices);
  59. // return
  60. // new HGScene(
  61. // regionInfo, circuitManager, m_commsManager, sceneGridService, storageManager,
  62. // m_moduleLoader, false, m_configSettings.PhysicalPrim,
  63. // m_configSettings.See_into_region_from_neighbor, m_config.Source, m_version);
  64. //}
  65. public void RunCommand(string module, string[] cmdparams)
  66. {
  67. List<string> args = new List<string>(cmdparams);
  68. if (args.Count < 1)
  69. return;
  70. string command = args[0];
  71. args.RemoveAt(0);
  72. cmdparams = args.ToArray();
  73. RunHGCommand(command, cmdparams);
  74. }
  75. private void RunHGCommand(string command, string[] cmdparams)
  76. {
  77. if (command.Equals("link-mapping"))
  78. {
  79. if (cmdparams.Length == 2)
  80. {
  81. try
  82. {
  83. m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
  84. m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
  85. m_enableAutoMapping = true;
  86. }
  87. catch (Exception)
  88. {
  89. m_autoMappingX = 0;
  90. m_autoMappingY = 0;
  91. m_enableAutoMapping = false;
  92. }
  93. }
  94. }
  95. else if (command.Equals("link-region"))
  96. {
  97. if (cmdparams.Length < 3)
  98. {
  99. if ((cmdparams.Length == 1) || (cmdparams.Length == 2))
  100. {
  101. LoadXmlLinkFile(cmdparams);
  102. }
  103. else
  104. {
  105. LinkRegionCmdUsage();
  106. }
  107. return;
  108. }
  109. if (cmdparams[2].Contains(":"))
  110. {
  111. // New format
  112. int xloc, yloc;
  113. string mapName;
  114. try
  115. {
  116. xloc = Convert.ToInt32(cmdparams[0]);
  117. yloc = Convert.ToInt32(cmdparams[1]);
  118. mapName = cmdparams[2];
  119. if (cmdparams.Length > 3)
  120. for (int i = 3; i < cmdparams.Length; i++)
  121. mapName += " " + cmdparams[i];
  122. m_log.Info(">> MapName: " + mapName);
  123. //internalPort = Convert.ToUInt32(cmdparams[4]);
  124. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  125. }
  126. catch (Exception e)
  127. {
  128. m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message);
  129. LinkRegionCmdUsage();
  130. return;
  131. }
  132. // Convert cell coordinates given by the user to meters
  133. xloc = xloc * (int)Constants.RegionSize;
  134. yloc = yloc * (int)Constants.RegionSize;
  135. m_HGGridConnector.TryLinkRegionToCoords(m_scene, null, mapName, xloc, yloc);
  136. }
  137. else
  138. {
  139. // old format
  140. GridRegion regInfo;
  141. int xloc, yloc;
  142. uint externalPort;
  143. string externalHostName;
  144. try
  145. {
  146. xloc = Convert.ToInt32(cmdparams[0]);
  147. yloc = Convert.ToInt32(cmdparams[1]);
  148. externalPort = Convert.ToUInt32(cmdparams[3]);
  149. externalHostName = cmdparams[2];
  150. //internalPort = Convert.ToUInt32(cmdparams[4]);
  151. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  152. }
  153. catch (Exception e)
  154. {
  155. m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message);
  156. LinkRegionCmdUsage();
  157. return;
  158. }
  159. // Convert cell coordinates given by the user to meters
  160. xloc = xloc * (int)Constants.RegionSize;
  161. yloc = yloc * (int)Constants.RegionSize;
  162. if (m_HGGridConnector.TryCreateLink(m_scene, null, xloc, yloc, "", externalPort, externalHostName, out regInfo))
  163. {
  164. if (cmdparams.Length >= 5)
  165. {
  166. regInfo.RegionName = "";
  167. for (int i = 4; i < cmdparams.Length; i++)
  168. regInfo.RegionName += cmdparams[i] + " ";
  169. }
  170. }
  171. }
  172. return;
  173. }
  174. else if (command.Equals("unlink-region"))
  175. {
  176. if (cmdparams.Length < 1)
  177. {
  178. UnlinkRegionCmdUsage();
  179. return;
  180. }
  181. if (m_HGGridConnector.TryUnlinkRegion(m_scene, cmdparams[0]))
  182. m_log.InfoFormat("[HGrid]: Successfully unlinked {0}", cmdparams[0]);
  183. else
  184. m_log.InfoFormat("[HGrid]: Unable to unlink {0}, region not found", cmdparams[0]);
  185. }
  186. }
  187. private void LoadXmlLinkFile(string[] cmdparams)
  188. {
  189. //use http://www.hgurl.com/hypergrid.xml for test
  190. try
  191. {
  192. XmlReader r = XmlReader.Create(cmdparams[0]);
  193. XmlConfigSource cs = new XmlConfigSource(r);
  194. string[] excludeSections = null;
  195. if (cmdparams.Length == 2)
  196. {
  197. if (cmdparams[1].ToLower().StartsWith("excludelist:"))
  198. {
  199. string excludeString = cmdparams[1].ToLower();
  200. excludeString = excludeString.Remove(0, 12);
  201. char[] splitter = { ';' };
  202. excludeSections = excludeString.Split(splitter);
  203. }
  204. }
  205. for (int i = 0; i < cs.Configs.Count; i++)
  206. {
  207. bool skip = false;
  208. if ((excludeSections != null) && (excludeSections.Length > 0))
  209. {
  210. for (int n = 0; n < excludeSections.Length; n++)
  211. {
  212. if (excludeSections[n] == cs.Configs[i].Name.ToLower())
  213. {
  214. skip = true;
  215. break;
  216. }
  217. }
  218. }
  219. if (!skip)
  220. {
  221. ReadLinkFromConfig(cs.Configs[i]);
  222. }
  223. }
  224. }
  225. catch (Exception e)
  226. {
  227. m_log.Error(e.ToString());
  228. }
  229. }
  230. private void ReadLinkFromConfig(IConfig config)
  231. {
  232. GridRegion regInfo;
  233. int xloc, yloc;
  234. uint externalPort;
  235. string externalHostName;
  236. uint realXLoc, realYLoc;
  237. xloc = Convert.ToInt32(config.GetString("xloc", "0"));
  238. yloc = Convert.ToInt32(config.GetString("yloc", "0"));
  239. externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
  240. externalHostName = config.GetString("externalHostName", "");
  241. realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
  242. realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
  243. if (m_enableAutoMapping)
  244. {
  245. xloc = (int)((xloc % 100) + m_autoMappingX);
  246. yloc = (int)((yloc % 100) + m_autoMappingY);
  247. }
  248. if (((realXLoc == 0) && (realYLoc == 0)) ||
  249. (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) &&
  250. ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
  251. {
  252. xloc = xloc * (int)Constants.RegionSize;
  253. yloc = yloc * (int)Constants.RegionSize;
  254. if (
  255. m_HGGridConnector.TryCreateLink(m_scene, null, xloc, yloc, "", externalPort,
  256. externalHostName, out regInfo))
  257. {
  258. regInfo.RegionName = config.GetString("localName", "");
  259. }
  260. }
  261. }
  262. private void LinkRegionCmdUsage()
  263. {
  264. m_log.Info("Usage: link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>]");
  265. m_log.Info("Usage: link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
  266. m_log.Info("Usage: link-region <URI_of_xml> [<exclude>]");
  267. }
  268. private void UnlinkRegionCmdUsage()
  269. {
  270. m_log.Info("Usage: unlink-region <HostName>:<HttpPort>");
  271. m_log.Info("Usage: unlink-region <LocalName>");
  272. }
  273. }
  274. }