aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Services/Connectors/SimianGrid/SimianGridServiceConnector.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Services/Connectors/SimianGrid/SimianGridServiceConnector.cs418
1 files changed, 418 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/SimianGrid/SimianGridServiceConnector.cs b/OpenSim/Services/Connectors/SimianGrid/SimianGridServiceConnector.cs
new file mode 100644
index 0000000..16819d1
--- /dev/null
+++ b/OpenSim/Services/Connectors/SimianGrid/SimianGridServiceConnector.cs
@@ -0,0 +1,418 @@
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
28using System;
29using System.Collections.Generic;
30using System.Collections.Specialized;
31using System.Net;
32using System.Reflection;
33using log4net;
34using Mono.Addins;
35using Nini.Config;
36using OpenSim.Framework;
37using OpenSim.Framework.Servers.HttpServer;
38using OpenSim.Region.Framework.Interfaces;
39using OpenSim.Region.Framework.Scenes;
40using OpenSim.Services.Interfaces;
41using OpenSim.Server.Base;
42using OpenMetaverse;
43using OpenMetaverse.StructuredData;
44
45using GridRegion = OpenSim.Services.Interfaces.GridRegion;
46
47namespace OpenSim.Services.Connectors.SimianGrid
48{
49 /// <summary>
50 /// Connects region registration and neighbor lookups to the SimianGrid
51 /// backend
52 /// </summary>
53 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
54 public class SimianGridServiceConnector : IGridService, ISharedRegionModule
55 {
56 private static readonly ILog m_log =
57 LogManager.GetLogger(
58 MethodBase.GetCurrentMethod().DeclaringType);
59
60 private string m_serverUrl = String.Empty;
61
62 #region ISharedRegionModule
63
64 public Type ReplaceableInterface { get { return null; } }
65 public void RegionLoaded(Scene scene) { }
66 public void PostInitialise() { }
67 public void Close() { }
68
69 public SimianGridServiceConnector() { }
70 public string Name { get { return "SimianGridServiceConnector"; } }
71 public void AddRegion(Scene scene) { scene.RegisterModuleInterface<IGridService>(this); }
72 public void RemoveRegion(Scene scene) { scene.UnregisterModuleInterface<IGridService>(this); }
73
74 #endregion ISharedRegionModule
75
76 public SimianGridServiceConnector(IConfigSource source)
77 {
78 Initialise(source);
79 }
80
81 public void Initialise(IConfigSource source)
82 {
83 IConfig gridConfig = source.Configs["GridService"];
84 if (gridConfig == null)
85 {
86 m_log.Error("[GRID CONNECTOR]: GridService missing from OpenSim.ini");
87 throw new Exception("Grid connector init error");
88 }
89
90 string serviceUrl = gridConfig.GetString("GridServerURI");
91 if (String.IsNullOrEmpty(serviceUrl))
92 {
93 m_log.Error("[GRID CONNECTOR]: No Server URI named in section GridService");
94 throw new Exception("Grid connector init error");
95 }
96
97 m_serverUrl = serviceUrl;
98 }
99
100 #region IGridService
101
102 public string RegisterRegion(UUID scopeID, GridRegion regionInfo)
103 {
104 Vector3d minPosition = new Vector3d(regionInfo.RegionLocX, regionInfo.RegionLocY, 0.0);
105 Vector3d maxPosition = minPosition + new Vector3d(Constants.RegionSize, Constants.RegionSize, 4096.0);
106
107 string httpAddress = "http://" + regionInfo.ExternalHostName + ":" + regionInfo.HttpPort + "/";
108
109 OSDMap extraData = new OSDMap
110 {
111 { "ServerURI", OSD.FromString(regionInfo.ServerURI) },
112 { "InternalAddress", OSD.FromString(regionInfo.InternalEndPoint.Address.ToString()) },
113 { "InternalPort", OSD.FromInteger(regionInfo.InternalEndPoint.Port) },
114 { "ExternalAddress", OSD.FromString(regionInfo.ExternalEndPoint.Address.ToString()) },
115 { "ExternalPort", OSD.FromInteger(regionInfo.ExternalEndPoint.Port) },
116 { "MapTexture", OSD.FromUUID(regionInfo.TerrainImage) },
117 { "Access", OSD.FromInteger(regionInfo.Access) },
118 { "RegionSecret", OSD.FromString(regionInfo.RegionSecret) },
119 { "EstateOwner", OSD.FromUUID(regionInfo.EstateOwner) },
120 { "Token", OSD.FromString(regionInfo.Token) }
121 };
122
123 NameValueCollection requestArgs = new NameValueCollection
124 {
125 { "RequestMethod", "AddScene" },
126 { "SceneID", regionInfo.RegionID.ToString() },
127 { "Name", regionInfo.RegionName },
128 { "MinPosition", minPosition.ToString() },
129 { "MaxPosition", maxPosition.ToString() },
130 { "Address", httpAddress },
131 { "Enabled", "1" },
132 { "ExtraData", OSDParser.SerializeJsonString(extraData) }
133 };
134
135 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
136 if (response["Success"].AsBoolean())
137 return String.Empty;
138 else
139 return "Region registration for " + regionInfo.RegionName + " failed: " + response["Message"].AsString();
140 }
141
142 public bool DeregisterRegion(UUID regionID)
143 {
144 NameValueCollection requestArgs = new NameValueCollection
145 {
146 { "RequestMethod", "AddScene" },
147 { "SceneID", regionID.ToString() },
148 { "Enabled", "0" }
149 };
150
151 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
152 bool success = response["Success"].AsBoolean();
153
154 if (!success)
155 m_log.Warn("[GRID CONNECTOR]: Region deregistration for " + regionID + " failed: " + response["Message"].AsString());
156
157 return success;
158 }
159
160 public List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
161 {
162 const int NEIGHBOR_RADIUS = 128;
163
164 GridRegion region = GetRegionByUUID(scopeID, regionID);
165
166 if (region != null)
167 {
168 List<GridRegion> regions = GetRegionRange(scopeID,
169 region.RegionLocX - NEIGHBOR_RADIUS, region.RegionLocX + (int)Constants.RegionSize + NEIGHBOR_RADIUS,
170 region.RegionLocY - NEIGHBOR_RADIUS, region.RegionLocY + (int)Constants.RegionSize + NEIGHBOR_RADIUS);
171
172 for (int i = 0; i < regions.Count; i++)
173 {
174 if (regions[i].RegionID == regionID)
175 {
176 regions.RemoveAt(i);
177 break;
178 }
179 }
180
181 m_log.Debug("[GRID CONNECTOR]: Found " + regions.Count + " neighbors for region " + regionID);
182 return regions;
183 }
184
185 return new List<GridRegion>(0);
186 }
187
188 public GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
189 {
190 NameValueCollection requestArgs = new NameValueCollection
191 {
192 { "RequestMethod", "GetScene" },
193 { "SceneID", regionID.ToString() }
194 };
195
196 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
197 if (response["Success"].AsBoolean())
198 {
199 return ResponseToGridRegion(response);
200 }
201 else
202 {
203 m_log.Warn("[GRID CONNECTOR]: Grid service did not find a match for region " + regionID);
204 return null;
205 }
206 }
207
208 public GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
209 {
210 // Go one meter in from the requested x/y coords to avoid requesting a position
211 // that falls on the border of two sims
212 Vector3d position = new Vector3d(x + 1, y + 1, 0.0);
213
214 NameValueCollection requestArgs = new NameValueCollection
215 {
216 { "RequestMethod", "GetScene" },
217 { "Position", position.ToString() },
218 { "Enabled", "1" }
219 };
220
221 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
222 if (response["Success"].AsBoolean())
223 {
224 return ResponseToGridRegion(response);
225 }
226 else
227 {
228 //m_log.InfoFormat("[GRID CONNECTOR]: Grid service did not find a match for region at {0},{1}",
229 // x / Constants.RegionSize, y / Constants.RegionSize);
230 return null;
231 }
232 }
233
234 public GridRegion GetRegionByName(UUID scopeID, string regionName)
235 {
236 List<GridRegion> regions = GetRegionsByName(scopeID, regionName, 1);
237
238 m_log.Debug("[GRID CONNECTOR]: Got " + regions.Count + " matches for region name " + regionName);
239
240 if (regions.Count > 0)
241 return regions[0];
242
243 return null;
244 }
245
246 public List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
247 {
248 List<GridRegion> foundRegions = new List<GridRegion>();
249
250 NameValueCollection requestArgs = new NameValueCollection
251 {
252 { "RequestMethod", "GetScenes" },
253 { "NameQuery", name },
254 { "Enabled", "1" }
255 };
256 if (maxNumber > 0)
257 requestArgs["MaxNumber"] = maxNumber.ToString();
258
259 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
260 if (response["Success"].AsBoolean())
261 {
262 OSDArray array = response["Scenes"] as OSDArray;
263 if (array != null)
264 {
265 for (int i = 0; i < array.Count; i++)
266 {
267 GridRegion region = ResponseToGridRegion(array[i] as OSDMap);
268 if (region != null)
269 foundRegions.Add(region);
270 }
271 }
272 }
273
274 return foundRegions;
275 }
276
277 public List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
278 {
279 List<GridRegion> foundRegions = new List<GridRegion>();
280
281 Vector3d minPosition = new Vector3d(xmin, ymin, 0.0);
282 Vector3d maxPosition = new Vector3d(xmax, ymax, 4096.0);
283
284 NameValueCollection requestArgs = new NameValueCollection
285 {
286 { "RequestMethod", "GetScenes" },
287 { "MinPosition", minPosition.ToString() },
288 { "MaxPosition", maxPosition.ToString() },
289 { "Enabled", "1" }
290 };
291
292 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
293 if (response["Success"].AsBoolean())
294 {
295 OSDArray array = response["Scenes"] as OSDArray;
296 if (array != null)
297 {
298 for (int i = 0; i < array.Count; i++)
299 {
300 GridRegion region = ResponseToGridRegion(array[i] as OSDMap);
301 if (region != null)
302 foundRegions.Add(region);
303 }
304 }
305 }
306
307 return foundRegions;
308 }
309
310 public List<GridRegion> GetDefaultRegions(UUID scopeID)
311 {
312 // TODO: Allow specifying the default grid location
313 const int DEFAULT_X = 1000 * 256;
314 const int DEFAULT_Y = 1000 * 256;
315
316 GridRegion defRegion = GetNearestRegion(new Vector3d(DEFAULT_X, DEFAULT_Y, 0.0), true);
317 if (defRegion != null)
318 return new List<GridRegion>(1) { defRegion };
319 else
320 return new List<GridRegion>(0);
321 }
322
323 public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
324 {
325 GridRegion defRegion = GetNearestRegion(new Vector3d(x, y, 0.0), true);
326 if (defRegion != null)
327 return new List<GridRegion>(1) { defRegion };
328 else
329 return new List<GridRegion>(0);
330 }
331
332 public int GetRegionFlags(UUID scopeID, UUID regionID)
333 {
334 const int REGION_ONLINE = 4;
335
336 NameValueCollection requestArgs = new NameValueCollection
337 {
338 { "RequestMethod", "GetScene" },
339 { "SceneID", regionID.ToString() }
340 };
341
342 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
343 if (response["Success"].AsBoolean())
344 {
345 return response["Enabled"].AsBoolean() ? REGION_ONLINE : 0;
346 }
347 else
348 {
349 m_log.Warn("[GRID CONNECTOR]: Grid service did not find a match for region " + regionID + " during region flags check");
350 return -1;
351 }
352 }
353
354 #endregion IGridService
355
356 private GridRegion GetNearestRegion(Vector3d position, bool onlyEnabled)
357 {
358 NameValueCollection requestArgs = new NameValueCollection
359 {
360 { "RequestMethod", "GetScene" },
361 { "Position", position.ToString() },
362 { "FindClosest", "1" }
363 };
364 if (onlyEnabled)
365 requestArgs["Enabled"] = "1";
366
367 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
368 if (response["Success"].AsBoolean())
369 {
370 return ResponseToGridRegion(response);
371 }
372 else
373 {
374 m_log.Warn("[GRID CONNECTOR]: Grid service did not find a match for region at " + position);
375 return null;
376 }
377 }
378
379 private GridRegion ResponseToGridRegion(OSDMap response)
380 {
381 if (response == null)
382 return null;
383
384 OSDMap extraData = response["ExtraData"] as OSDMap;
385 if (extraData == null)
386 return null;
387
388 GridRegion region = new GridRegion();
389
390 region.RegionID = response["SceneID"].AsUUID();
391 region.RegionName = response["Name"].AsString();
392
393 Vector3d minPosition = response["MinPosition"].AsVector3d();
394 region.RegionLocX = (int)minPosition.X;
395 region.RegionLocY = (int)minPosition.Y;
396
397 Uri httpAddress = response["Address"].AsUri();
398 region.ExternalHostName = httpAddress.Host;
399 region.HttpPort = (uint)httpAddress.Port;
400
401 region.ServerURI = extraData["ServerURI"].AsString();
402
403 IPAddress internalAddress;
404 IPAddress.TryParse(extraData["InternalAddress"].AsString(), out internalAddress);
405 if (internalAddress == null)
406 internalAddress = IPAddress.Any;
407
408 region.InternalEndPoint = new IPEndPoint(internalAddress, extraData["InternalPort"].AsInteger());
409 region.TerrainImage = extraData["MapTexture"].AsUUID();
410 region.Access = (byte)extraData["Access"].AsInteger();
411 region.RegionSecret = extraData["RegionSecret"].AsString();
412 region.EstateOwner = extraData["EstateOwner"].AsUUID();
413 region.Token = extraData["Token"].AsString();
414
415 return region;
416 }
417 }
418}