OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
RemoteGridServiceConnector.cs
Go to the documentation of this file.
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 
28 using log4net;
29 using Mono.Addins;
30 using System;
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Reflection;
34 using Nini.Config;
35 using OpenMetaverse;
36 
37 using OpenSim.Framework;
38 using OpenSim.Services.Connectors;
39 using OpenSim.Region.Framework.Interfaces;
40 using OpenSim.Region.Framework.Scenes;
41 using OpenSim.Server.Base;
42 using OpenSim.Services.Interfaces;
44 
45 namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
46 {
47  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "RemoteGridServicesConnector")]
49  {
50  private static readonly ILog m_log =
51  LogManager.GetLogger(
52  MethodBase.GetCurrentMethod().DeclaringType);
53 
54  private bool m_Enabled = false;
55 
56  private IGridService m_LocalGridService;
57  private IGridService m_RemoteGridService;
58 
59  private RegionInfoCache m_RegionInfoCache = new RegionInfoCache();
60 
62  {
63  }
64 
65  public RemoteGridServicesConnector(IConfigSource source)
66  {
67  InitialiseServices(source);
68  }
69 
70  #region ISharedRegionmodule
71 
72  public Type ReplaceableInterface
73  {
74  get { return null; }
75  }
76 
77  public string Name
78  {
79  get { return "RemoteGridServicesConnector"; }
80  }
81 
82  public void Initialise(IConfigSource source)
83  {
84  IConfig moduleConfig = source.Configs["Modules"];
85  if (moduleConfig != null)
86  {
87  string name = moduleConfig.GetString("GridServices", "");
88  if (name == Name)
89  {
90  InitialiseServices(source);
91  m_Enabled = true;
92  m_log.Info("[REMOTE GRID CONNECTOR]: Remote grid enabled");
93  }
94  }
95  }
96 
97  private void InitialiseServices(IConfigSource source)
98  {
99  IConfig gridConfig = source.Configs["GridService"];
100  if (gridConfig == null)
101  {
102  m_log.Error("[REMOTE GRID CONNECTOR]: GridService missing from OpenSim.ini");
103  return;
104  }
105 
106  string networkConnector = gridConfig.GetString("NetworkConnector", string.Empty);
107  if (networkConnector == string.Empty)
108  {
109  m_log.Error("[REMOTE GRID CONNECTOR]: Please specify a network connector under [GridService]");
110  return;
111  }
112 
113  Object[] args = new Object[] { source };
114  m_RemoteGridService = ServerUtils.LoadPlugin<IGridService>(networkConnector, args);
115 
116  m_LocalGridService = new LocalGridServicesConnector(source);
117  }
118 
119  public void PostInitialise()
120  {
121  if (m_LocalGridService != null)
122  ((ISharedRegionModule)m_LocalGridService).PostInitialise();
123  }
124 
125  public void Close()
126  {
127  }
128 
129  public void AddRegion(Scene scene)
130  {
131  if (m_Enabled)
132  scene.RegisterModuleInterface<IGridService>(this);
133 
134  if (m_LocalGridService != null)
135  ((ISharedRegionModule)m_LocalGridService).AddRegion(scene);
136  }
137 
138  public void RemoveRegion(Scene scene)
139  {
140  if (m_LocalGridService != null)
141  ((ISharedRegionModule)m_LocalGridService).RemoveRegion(scene);
142  }
143 
144  public void RegionLoaded(Scene scene)
145  {
146  }
147 
148  #endregion
149 
150  #region IGridService
151 
152  public string RegisterRegion(UUID scopeID, GridRegion regionInfo)
153  {
154  string msg = m_LocalGridService.RegisterRegion(scopeID, regionInfo);
155 
156  if (msg == String.Empty)
157  return m_RemoteGridService.RegisterRegion(scopeID, regionInfo);
158 
159  return msg;
160  }
161 
162  public bool DeregisterRegion(UUID regionID)
163  {
164  if (m_LocalGridService.DeregisterRegion(regionID))
165  return m_RemoteGridService.DeregisterRegion(regionID);
166 
167  return false;
168  }
169 
170  public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
171  {
172  return m_RemoteGridService.GetNeighbours(scopeID, regionID);
173  }
174 
175  public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
176  {
177  bool inCache = false;
178  GridRegion rinfo = m_RegionInfoCache.Get(scopeID,regionID,out inCache);
179  if (inCache)
180  return rinfo;
181 
182  rinfo = m_LocalGridService.GetRegionByUUID(scopeID, regionID);
183  if (rinfo == null)
184  rinfo = m_RemoteGridService.GetRegionByUUID(scopeID, regionID);
185 
186  m_RegionInfoCache.Cache(scopeID,regionID,rinfo);
187  return rinfo;
188  }
189 
190  // Get a region given its base world coordinates (in meters).
191  // NOTE: this is NOT 'get a region by some point in the region'. The coordinate MUST
192  // be the base coordinate of the region.
193  // The coordinates are world coords (meters), NOT region units.
194  public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
195  {
196  ulong regionHandle = Util.RegionWorldLocToHandle((uint)x, (uint)y);
197  uint regionX = Util.WorldToRegionLoc((uint)x);
198  uint regionY = Util.WorldToRegionLoc((uint)y);
199 
200 /* this is no longer valid
201  // Sanity check
202  if ((Util.RegionToWorldLoc(regionX) != (uint)x) || (Util.RegionToWorldLoc(regionY) != (uint)y))
203  {
204  m_log.WarnFormat("[REMOTE GRID CONNECTOR]: GetRegionByPosition. Bad position requested: not the base of the region. Requested Pos=<{0},{1}>, Should Be=<{2},{3}>",
205  x, y, Util.RegionToWorldLoc(regionX), Util.RegionToWorldLoc(regionY));
206  }
207 */
208  bool inCache = false;
209  GridRegion rinfo = m_RegionInfoCache.Get(scopeID, regionHandle, out inCache);
210  if (inCache)
211  {
212 // m_log.DebugFormat("[REMOTE GRID CONNECTOR]: GetRegionByPosition. Found region {0} in cache. Pos=<{1},{2}>, RegionHandle={3}",
213 // (rinfo == null) ? "<missing>" : rinfo.RegionName, regionX, regionY, (rinfo == null) ? regionHandle : rinfo.RegionHandle);
214  return rinfo;
215  }
216 
217  rinfo = m_LocalGridService.GetRegionByPosition(scopeID, x, y);
218  if (rinfo == null)
219  rinfo = m_RemoteGridService.GetRegionByPosition(scopeID, x, y);
220 
221  m_RegionInfoCache.Cache(rinfo);
222 
223  m_log.DebugFormat("[REMOTE GRID CONNECTOR]: GetRegionByPosition. Added region {0} to the cache. Pos=<{1},{2}>, RegionHandle={3}",
224  (rinfo == null) ? "<missing>" : rinfo.RegionName, regionX, regionY, (rinfo == null) ? regionHandle : rinfo.RegionHandle);
225 
226  return rinfo;
227  }
228 
229  public GridRegion GetRegionByName(UUID scopeID, string regionName)
230  {
231  bool inCache = false;
232  GridRegion rinfo = m_RegionInfoCache.Get(scopeID,regionName, out inCache);
233  if (inCache)
234  return rinfo;
235 
236  rinfo = m_LocalGridService.GetRegionByName(scopeID, regionName);
237  if (rinfo == null)
238  rinfo = m_RemoteGridService.GetRegionByName(scopeID, regionName);
239 
240  // can't cache negative results for name lookups
241  m_RegionInfoCache.Cache(rinfo);
242  return rinfo;
243  }
244 
245  public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
246  {
247  List<GridRegion> rinfo = m_LocalGridService.GetRegionsByName(scopeID, name, maxNumber);
248  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetRegionsByName {0} found {1} regions", name, rinfo.Count);
249  List<GridRegion> grinfo = m_RemoteGridService.GetRegionsByName(scopeID, name, maxNumber);
250 
251  if (grinfo != null)
252  {
253  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetRegionsByName {0} found {1} regions", name, grinfo.Count);
254  foreach (GridRegion r in grinfo)
255  {
256  m_RegionInfoCache.Cache(r);
257  if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
258  rinfo.Add(r);
259  }
260  }
261 
262  return rinfo;
263  }
264 
265  public virtual List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
266  {
267  List<GridRegion> rinfo = m_LocalGridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
268  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetRegionRange {0} found {1} regions", name, rinfo.Count);
269  List<GridRegion> grinfo = m_RemoteGridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
270 
271  if (grinfo != null)
272  {
273  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetRegionRange {0} found {1} regions", name, grinfo.Count);
274  foreach (GridRegion r in grinfo)
275  {
276  m_RegionInfoCache.Cache(r);
277  if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
278  rinfo.Add(r);
279  }
280  }
281 
282  return rinfo;
283  }
284 
285  public List<GridRegion> GetDefaultRegions(UUID scopeID)
286  {
287  List<GridRegion> rinfo = m_LocalGridService.GetDefaultRegions(scopeID);
288  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetDefaultRegions {0} found {1} regions", name, rinfo.Count);
289  List<GridRegion> grinfo = m_RemoteGridService.GetDefaultRegions(scopeID);
290 
291  if (grinfo != null)
292  {
293  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetDefaultRegions {0} found {1} regions", name, grinfo.Count);
294  foreach (GridRegion r in grinfo)
295  {
296  m_RegionInfoCache.Cache(r);
297  if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
298  rinfo.Add(r);
299  }
300  }
301 
302  return rinfo;
303  }
304 
305  public List<GridRegion> GetDefaultHypergridRegions(UUID scopeID)
306  {
307  List<GridRegion> rinfo = m_LocalGridService.GetDefaultHypergridRegions(scopeID);
308  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetDefaultHypergridRegions {0} found {1} regions", name, rinfo.Count);
309  List<GridRegion> grinfo = m_RemoteGridService.GetDefaultHypergridRegions(scopeID);
310 
311  if (grinfo != null)
312  {
313  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetDefaultHypergridRegions {0} found {1} regions", name, grinfo.Count);
314  foreach (GridRegion r in grinfo)
315  {
316  m_RegionInfoCache.Cache(r);
317  if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
318  rinfo.Add(r);
319  }
320  }
321 
322  return rinfo;
323  }
324 
325  public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
326  {
327  List<GridRegion> rinfo = m_LocalGridService.GetFallbackRegions(scopeID, x, y);
328  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetFallbackRegions {0} found {1} regions", name, rinfo.Count);
329  List<GridRegion> grinfo = m_RemoteGridService.GetFallbackRegions(scopeID, x, y);
330 
331  if (grinfo != null)
332  {
333  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetFallbackRegions {0} found {1} regions", name, grinfo.Count);
334  foreach (GridRegion r in grinfo)
335  {
336  m_RegionInfoCache.Cache(r);
337  if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
338  rinfo.Add(r);
339  }
340  }
341 
342  return rinfo;
343  }
344 
345  public List<GridRegion> GetHyperlinks(UUID scopeID)
346  {
347  List<GridRegion> rinfo = m_LocalGridService.GetHyperlinks(scopeID);
348  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Local GetHyperlinks {0} found {1} regions", name, rinfo.Count);
349  List<GridRegion> grinfo = m_RemoteGridService.GetHyperlinks(scopeID);
350 
351  if (grinfo != null)
352  {
353  //m_log.DebugFormat("[REMOTE GRID CONNECTOR]: Remote GetHyperlinks {0} found {1} regions", name, grinfo.Count);
354  foreach (GridRegion r in grinfo)
355  {
356  m_RegionInfoCache.Cache(r);
357  if (rinfo.Find(delegate(GridRegion gr) { return gr.RegionID == r.RegionID; }) == null)
358  rinfo.Add(r);
359  }
360  }
361 
362  return rinfo;
363  }
364 
365  public int GetRegionFlags(UUID scopeID, UUID regionID)
366  {
367  int flags = m_LocalGridService.GetRegionFlags(scopeID, regionID);
368  if (flags == -1)
369  flags = m_RemoteGridService.GetRegionFlags(scopeID, regionID);
370 
371  return flags;
372  }
373 
374  public Dictionary<string, object> GetExtraFeatures()
375  {
376  Dictionary<string, object> extraFeatures;
377  extraFeatures = m_LocalGridService.GetExtraFeatures();
378 
379  if (extraFeatures.Count == 0)
380  extraFeatures = m_RemoteGridService.GetExtraFeatures();
381 
382  return extraFeatures;
383  }
384  #endregion
385  }
386 }
virtual List< GridRegion > GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
List< GridRegion > GetNeighbours(UUID scopeID, UUID regionID)
Get information about the regions neighbouring the given co-ordinates (in meters).
GridRegion GetRegionByName(UUID scopeID, string regionName)
Get information about a region which exactly matches the name given.
List< GridRegion > GetRegionsByName(UUID scopeID, string name, int maxNumber)
Get information about regions starting with the provided name.
bool DeregisterRegion(UUID regionID)
Deregister a region with the grid service.
OpenSim.Services.Interfaces.GridRegion GridRegion
string RegisterRegion(UUID scopeID, GridRegion regionInfo)
Register a region with the grid service.
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
Get the region at the given position (in meters)
int GetRegionFlags(UUID scopeID, UUID regionID)
Get internal OpenSimulator region flags.
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...