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