InterregionModule.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 System;
  28. using System.Collections.Generic;
  29. using System.Runtime.Remoting;
  30. using System.Runtime.Remoting.Channels;
  31. using System.Runtime.Remoting.Channels.Tcp;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Environment.Interfaces;
  35. using OpenSim.Region.Environment.Scenes;
  36. namespace OpenSim.Region.Environment.Modules.Grid.Interregion
  37. {
  38. public class InterregionModule : IInterregionModule, IRegionModule
  39. {
  40. #region Direction enum
  41. public enum Direction
  42. {
  43. North,
  44. NorthEast,
  45. East,
  46. SouthEast,
  47. South,
  48. SouthWest,
  49. West,
  50. NorthWest
  51. }
  52. #endregion
  53. private readonly Dictionary<Type, Object> m_interfaces = new Dictionary<Type, object>();
  54. private readonly Object m_lockObject = new object();
  55. private readonly List<Location> m_myLocations = new List<Location>();
  56. private readonly Dictionary<Location, string[]> m_neighbourInterfaces = new Dictionary<Location, string[]>();
  57. private readonly Dictionary<Location, RemotingObject> m_neighbourRemote = new Dictionary<Location, RemotingObject>();
  58. // private IConfigSource m_config;
  59. private const bool m_enabled = false;
  60. private RemotingObject m_myRemote;
  61. private TcpChannel m_tcpChannel;
  62. private int m_tcpPort = 10101;
  63. #region IInterregionModule Members
  64. public void internal_CreateRemotingObjects()
  65. {
  66. lock (m_lockObject)
  67. {
  68. if (m_tcpChannel == null)
  69. {
  70. m_myRemote = new RemotingObject(m_interfaces, m_myLocations.ToArray());
  71. m_tcpChannel = new TcpChannel(m_tcpPort);
  72. ChannelServices.RegisterChannel(m_tcpChannel, false);
  73. RemotingServices.Marshal(m_myRemote, "OpenSimRemote2", typeof (RemotingObject));
  74. }
  75. }
  76. }
  77. public void RegisterMethod<T>(T e)
  78. {
  79. m_interfaces[typeof (T)] = e;
  80. }
  81. public bool HasInterface<T>(Location loc)
  82. {
  83. foreach (string val in m_neighbourInterfaces[loc])
  84. {
  85. if (val == typeof (T).FullName)
  86. {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. public T RequestInterface<T>(Location loc)
  93. {
  94. if (m_neighbourRemote.ContainsKey(loc))
  95. {
  96. return m_neighbourRemote[loc].RequestInterface<T>();
  97. }
  98. throw new IndexOutOfRangeException("No neighbour availible at that location");
  99. }
  100. public T[] RequestInterface<T>()
  101. {
  102. List<T> m_t = new List<T>();
  103. foreach (RemotingObject remote in m_neighbourRemote.Values)
  104. {
  105. try
  106. {
  107. m_t.Add(remote.RequestInterface<T>());
  108. }
  109. catch (NotSupportedException)
  110. {
  111. }
  112. }
  113. return m_t.ToArray();
  114. }
  115. public Location GetLocationByDirection(Scene scene, Direction dir)
  116. {
  117. return new Location(0, 0);
  118. }
  119. public void RegisterRemoteRegion(string uri)
  120. {
  121. RegisterRemotingInterface((RemotingObject) Activator.GetObject(typeof (RemotingObject), uri));
  122. }
  123. #endregion
  124. #region IRegionModule Members
  125. public void Initialise(Scene scene, IConfigSource source)
  126. {
  127. m_myLocations.Add(new Location((int) scene.RegionInfo.RegionLocX,
  128. (int) scene.RegionInfo.RegionLocY));
  129. // m_config = source;
  130. scene.RegisterModuleInterface<IInterregionModule>(this);
  131. }
  132. public void PostInitialise()
  133. {
  134. // Commenting out to remove 'unreachable code' warning since m_enabled is never true
  135. // if (m_enabled)
  136. // {
  137. // try
  138. // {
  139. // m_tcpPort = m_config.Configs["Comms"].GetInt("remoting_port", m_tcpPort);
  140. // }
  141. // catch
  142. // {
  143. // }
  144. //
  145. // internal_CreateRemotingObjects();
  146. // }
  147. }
  148. public void Close()
  149. {
  150. if (null != m_tcpChannel)
  151. ChannelServices.UnregisterChannel(m_tcpChannel);
  152. }
  153. public string Name
  154. {
  155. get { return "InterregionModule"; }
  156. }
  157. public bool IsSharedModule
  158. {
  159. get { return true; }
  160. }
  161. #endregion
  162. private void RegisterRemotingInterface(RemotingObject remote)
  163. {
  164. Location[] locs = remote.GetLocations();
  165. string[] interfaces = remote.GetInterfaces();
  166. foreach (Location loc in locs)
  167. {
  168. m_neighbourInterfaces[loc] = interfaces;
  169. m_neighbourRemote[loc] = remote;
  170. }
  171. }
  172. }
  173. }