OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
LocalGridServiceConnector.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 Nini.Config;
31 using System;
32 using System.Collections.Generic;
33 using System.Reflection;
34 using OpenSim.Framework;
35 using OpenSim.Framework.Console;
36 using OpenSim.Server.Base;
37 using OpenSim.Region.Framework.Interfaces;
38 using OpenSim.Region.Framework.Scenes;
39 using OpenSim.Services.Interfaces;
41 using OpenMetaverse;
42 
43 namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid
44 {
45  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LocalGridServicesConnector")]
47  {
48  private static readonly ILog m_log =
49  LogManager.GetLogger(
50  MethodBase.GetCurrentMethod().DeclaringType);
51  private static string LogHeader = "[LOCAL GRID SERVICE CONNECTOR]";
52 
53  private IGridService m_GridService;
54  private Dictionary<UUID, RegionCache> m_LocalCache = new Dictionary<UUID, RegionCache>();
55 
56  private bool m_Enabled;
57 
59  {
60  m_log.DebugFormat("{0} LocalGridServicesConnector no parms.", LogHeader);
61  }
62 
63  public LocalGridServicesConnector(IConfigSource source)
64  {
65  m_log.DebugFormat("{0} LocalGridServicesConnector instantiated directly.", LogHeader);
66  InitialiseService(source);
67  }
68 
69  #region ISharedRegionModule
70 
71  public Type ReplaceableInterface
72  {
73  get { return null; }
74  }
75 
76  public string Name
77  {
78  get { return "LocalGridServicesConnector"; }
79  }
80 
81  public void Initialise(IConfigSource source)
82  {
83  IConfig moduleConfig = source.Configs["Modules"];
84  if (moduleConfig != null)
85  {
86  string name = moduleConfig.GetString("GridServices", "");
87  if (name == Name)
88  {
89  InitialiseService(source);
90  m_log.Info("[LOCAL GRID SERVICE CONNECTOR]: Local grid connector enabled");
91  }
92  }
93  }
94 
95  private void InitialiseService(IConfigSource source)
96  {
97  IConfig config = source.Configs["GridService"];
98  if (config == null)
99  {
100  m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: GridService missing from OpenSim.ini");
101  return;
102  }
103 
104  string serviceDll = config.GetString("LocalServiceModule", String.Empty);
105 
106  if (serviceDll == String.Empty)
107  {
108  m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: No LocalServiceModule named in section GridService");
109  return;
110  }
111 
112  Object[] args = new Object[] { source };
113  m_GridService =
114  ServerUtils.LoadPlugin<IGridService>(serviceDll,
115  args);
116 
117  if (m_GridService == null)
118  {
119  m_log.Error("[LOCAL GRID SERVICE CONNECTOR]: Can't load grid service");
120  return;
121  }
122 
123  m_Enabled = true;
124  }
125 
126  public void PostInitialise()
127  {
128  // FIXME: We will still add this command even if we aren't enabled since RemoteGridServiceConnector
129  // will have instantiated us directly.
130  MainConsole.Instance.Commands.AddCommand("Regions", false, "show neighbours",
131  "show neighbours",
132  "Shows the local regions' neighbours", HandleShowNeighboursCommand);
133  }
134 
135  public void Close()
136  {
137  }
138 
139  public void AddRegion(Scene scene)
140  {
141  if (!m_Enabled)
142  return;
143 
144  scene.RegisterModuleInterface<IGridService>(this);
145 
146  lock (m_LocalCache)
147  {
148  if (m_LocalCache.ContainsKey(scene.RegionInfo.RegionID))
149  m_log.ErrorFormat("[LOCAL GRID SERVICE CONNECTOR]: simulator seems to have more than one region with the same UUID. Please correct this!");
150  else
151  m_LocalCache.Add(scene.RegionInfo.RegionID, new RegionCache(scene));
152  }
153  }
154 
155  public void RemoveRegion(Scene scene)
156  {
157  if (!m_Enabled)
158  return;
159 
160  lock (m_LocalCache)
161  {
162  m_LocalCache[scene.RegionInfo.RegionID].Clear();
163  m_LocalCache.Remove(scene.RegionInfo.RegionID);
164  }
165  }
166 
167  public void RegionLoaded(Scene scene)
168  {
169  }
170 
171  #endregion
172 
173  #region IGridService
174 
175  public string RegisterRegion(UUID scopeID, GridRegion regionInfo)
176  {
177  return m_GridService.RegisterRegion(scopeID, regionInfo);
178  }
179 
180  public bool DeregisterRegion(UUID regionID)
181  {
182  return m_GridService.DeregisterRegion(regionID);
183  }
184 
185  public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
186  {
187  return m_GridService.GetNeighbours(scopeID, regionID);
188  }
189 
190  public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
191  {
192  return m_GridService.GetRegionByUUID(scopeID, regionID);
193  }
194 
195  // Get a region given its base coordinates.
196  // NOTE: this is NOT 'get a region by some point in the region'. The coordinate MUST
197  // be the base coordinate of the region.
198  public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
199  {
200  GridRegion region = null;
201  uint regionX = Util.WorldToRegionLoc((uint)x);
202  uint regionY = Util.WorldToRegionLoc((uint)y);
203 
204  // First see if it's a neighbour, even if it isn't on this sim.
205  // Neighbour data is cached in memory, so this is fast
206 
207  lock (m_LocalCache)
208  {
209  foreach (RegionCache rcache in m_LocalCache.Values)
210  {
211  region = rcache.GetRegionByPosition(x, y);
212  if (region != null)
213  {
214  //m_log.DebugFormat("{0} GetRegionByPosition. Found region {1} in cache (of region {2}). Pos=<{3},{4}>",
215  // LogHeader, region.RegionName, rcache.RegionName,
216  // Util.WorldToRegionLoc((uint)region.RegionLocX), Util.WorldToRegionLoc((uint)region.RegionLocY));
217  break;
218  }
219  }
220  }
221 
222  // Then try on this sim (may be a lookup in DB if this is using MySql).
223  if (region == null)
224  {
225  region = m_GridService.GetRegionByPosition(scopeID, x, y);
226 
227  if (region == null)
228  {
229  m_log.DebugFormat("{0} GetRegionByPosition. Region not found by grid service. Pos=<{1},{2}>",
230  LogHeader, regionX, regionY);
231  }
232  else
233  {
234  m_log.DebugFormat("{0} GetRegionByPosition. Got region {1} from grid service. Pos=<{2},{3}>",
235  LogHeader, region.RegionName,
236  Util.WorldToRegionLoc((uint)region.RegionLocX), Util.WorldToRegionLoc((uint)region.RegionLocY));
237  }
238  }
239 
240  return region;
241  }
242 
243  public GridRegion GetRegionByName(UUID scopeID, string regionName)
244  {
245  return m_GridService.GetRegionByName(scopeID, regionName);
246  }
247 
248  public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
249  {
250  return m_GridService.GetRegionsByName(scopeID, name, maxNumber);
251  }
252 
253  public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
254  {
255  return m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
256  }
257 
258  public List<GridRegion> GetDefaultRegions(UUID scopeID)
259  {
260  return m_GridService.GetDefaultRegions(scopeID);
261  }
262 
263  public List<GridRegion> GetDefaultHypergridRegions(UUID scopeID)
264  {
265  return m_GridService.GetDefaultHypergridRegions(scopeID);
266  }
267 
268  public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
269  {
270  return m_GridService.GetFallbackRegions(scopeID, x, y);
271  }
272 
273  public List<GridRegion> GetHyperlinks(UUID scopeID)
274  {
275  return m_GridService.GetHyperlinks(scopeID);
276  }
277 
278  public int GetRegionFlags(UUID scopeID, UUID regionID)
279  {
280  return m_GridService.GetRegionFlags(scopeID, regionID);
281  }
282 
283  public Dictionary<string, object> GetExtraFeatures()
284  {
285  return m_GridService.GetExtraFeatures();
286  }
287 
288  #endregion
289 
290  public void HandleShowNeighboursCommand(string module, string[] cmdparams)
291  {
292  System.Text.StringBuilder caps = new System.Text.StringBuilder();
293 
294  lock (m_LocalCache)
295  {
296  foreach (KeyValuePair<UUID, RegionCache> kvp in m_LocalCache)
297  {
298  caps.AppendFormat("*** Neighbours of {0} ({1}) ***\n", kvp.Value.RegionName, kvp.Key);
299  List<GridRegion> regions = kvp.Value.GetNeighbours();
300  foreach (GridRegion r in regions)
301  caps.AppendFormat(" {0} @ {1}-{2}\n", r.RegionName, Util.WorldToRegionLoc((uint)r.RegionLocX), Util.WorldToRegionLoc((uint)r.RegionLocY));
302  }
303  }
304 
305  MainConsole.Instance.Output(caps.ToString());
306  }
307  }
308 }
GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
Get the region at the given position (in meters)
List< GridRegion > GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
int GetRegionFlags(UUID scopeID, UUID regionID)
Get internal OpenSimulator region flags.
string RegisterRegion(UUID scopeID, GridRegion regionInfo)
Register a region with the grid service.
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
int RegionLocX
The location of this region in meters. DANGER DANGER! Note that this name means something different i...
int RegionLocY
The location of this region in meters. DANGER DANGER! Note that this name means something different i...
OpenSim.Services.Interfaces.GridRegion GridRegion
Interactive OpenSim region server
Definition: OpenSim.cs:55
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
bool DeregisterRegion(UUID regionID)
Deregister a region with the grid service.
List< GridRegion > GetRegionsByName(UUID scopeID, string name, int maxNumber)
Get information about regions starting with the provided name.
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...
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.
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...