aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/ServiceConnectors/Interregion
diff options
context:
space:
mode:
authordiva2009-06-14 19:44:56 +0000
committerdiva2009-06-14 19:44:56 +0000
commit6abffedab5646c0c9d76a308cb9d7a722613fe14 (patch)
tree01f56713f19ed6157f496fd7ff86086e63e18d55 /OpenSim/Region/CoreModules/ServiceConnectors/Interregion
parentThank you kindly, M1sha, for a patch that improves the treePopulator module: (diff)
downloadopensim-SC-6abffedab5646c0c9d76a308cb9d7a722613fe14.zip
opensim-SC-6abffedab5646c0c9d76a308cb9d7a722613fe14.tar.gz
opensim-SC-6abffedab5646c0c9d76a308cb9d7a722613fe14.tar.bz2
opensim-SC-6abffedab5646c0c9d76a308cb9d7a722613fe14.tar.xz
Renamed Region/CoreModules/ServiceConnectors to Region/CoreModules/ServiceConnectorsOut. No functional changes.
Diffstat (limited to 'OpenSim/Region/CoreModules/ServiceConnectors/Interregion')
-rw-r--r--OpenSim/Region/CoreModules/ServiceConnectors/Interregion/LocalInterregionComms.cs286
-rw-r--r--OpenSim/Region/CoreModules/ServiceConnectors/Interregion/RESTInterregionComms.cs811
2 files changed, 0 insertions, 1097 deletions
diff --git a/OpenSim/Region/CoreModules/ServiceConnectors/Interregion/LocalInterregionComms.cs b/OpenSim/Region/CoreModules/ServiceConnectors/Interregion/LocalInterregionComms.cs
deleted file mode 100644
index d2af2db..0000000
--- a/OpenSim/Region/CoreModules/ServiceConnectors/Interregion/LocalInterregionComms.cs
+++ /dev/null
@@ -1,286 +0,0 @@
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 */
27using System;
28using System.Collections.Generic;
29using System.Reflection;
30using log4net;
31using Nini.Config;
32using OpenMetaverse;
33using OpenSim.Framework;
34using OpenSim.Region.Framework.Interfaces;
35using OpenSim.Region.Framework.Scenes;
36
37namespace OpenSim.Region.CoreModules.ServiceConnectors.Interregion
38{
39 public class LocalInterregionComms : IRegionModule, IInterregionCommsOut, IInterregionCommsIn
40 {
41 private bool m_enabled = false;
42
43 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
44 private List<Scene> m_sceneList = new List<Scene>();
45
46 #region Events
47 public event ChildAgentUpdateReceived OnChildAgentUpdate;
48
49 #endregion /* Events */
50
51 #region IRegionModule
52
53 public void Initialise(Scene scene, IConfigSource config)
54 {
55 if (m_sceneList.Count == 0)
56 {
57 IConfig startupConfig = config.Configs["Communications"];
58
59 if ((startupConfig != null) && (startupConfig.GetString("InterregionComms", "RESTComms") == "LocalComms"))
60 {
61 m_log.Debug("[LOCAL COMMS]: Enabling InterregionComms LocalComms module");
62 m_enabled = true;
63 }
64 }
65
66 if (!m_enabled)
67 return;
68
69 Init(scene);
70 }
71
72 public void PostInitialise()
73 {
74 }
75
76 public void Close()
77 {
78 }
79
80 public string Name
81 {
82 get { return "LocalInterregionCommsModule"; }
83 }
84
85 public bool IsSharedModule
86 {
87 get { return true; }
88 }
89 /// <summary>
90 /// Can be called from other modules.
91 /// </summary>
92 /// <param name="scene"></param>
93 public void Init(Scene scene)
94 {
95 if (!m_sceneList.Contains(scene))
96 {
97 lock (m_sceneList)
98 {
99 m_sceneList.Add(scene);
100 if (m_enabled)
101 scene.RegisterModuleInterface<IInterregionCommsOut>(this);
102 scene.RegisterModuleInterface<IInterregionCommsIn>(this);
103 }
104
105 }
106 }
107
108 #endregion /* IRegionModule */
109
110 #region IInterregionComms
111
112 /**
113 * Agent-related communications
114 */
115
116 public bool SendCreateChildAgent(ulong regionHandle, AgentCircuitData aCircuit, out string reason)
117 {
118 foreach (Scene s in m_sceneList)
119 {
120 if (s.RegionInfo.RegionHandle == regionHandle)
121 {
122// m_log.DebugFormat("[LOCAL COMMS]: Found region {0} to send SendCreateChildAgent", regionHandle);
123 return s.NewUserConnection(aCircuit, out reason);
124 }
125 }
126
127// m_log.DebugFormat("[LOCAL COMMS]: Did not find region {0} for SendCreateChildAgent", regionHandle);
128 reason = "Did not find region.";
129 return false;
130 }
131
132 public bool SendChildAgentUpdate(ulong regionHandle, AgentData cAgentData)
133 {
134 foreach (Scene s in m_sceneList)
135 {
136 if (s.RegionInfo.RegionHandle == regionHandle)
137 {
138 //m_log.DebugFormat(
139 // "[LOCAL COMMS]: Found region {0} {1} to send ChildAgentUpdate",
140 // s.RegionInfo.RegionName, regionHandle);
141
142 s.IncomingChildAgentDataUpdate(cAgentData);
143 return true;
144 }
145 }
146
147// m_log.DebugFormat("[LOCAL COMMS]: Did not find region {0} for ChildAgentUpdate", regionHandle);
148 return false;
149 }
150
151 public bool SendChildAgentUpdate(ulong regionHandle, AgentPosition cAgentData)
152 {
153 foreach (Scene s in m_sceneList)
154 {
155 if (s.RegionInfo.RegionHandle == regionHandle)
156 {
157 //m_log.Debug("[LOCAL COMMS]: Found region to send ChildAgentUpdate");
158 s.IncomingChildAgentDataUpdate(cAgentData);
159 return true;
160 }
161 }
162 //m_log.Debug("[LOCAL COMMS]: region not found for ChildAgentUpdate");
163 return false;
164 }
165
166 public bool SendRetrieveRootAgent(ulong regionHandle, UUID id, out IAgentData agent)
167 {
168 agent = null;
169 foreach (Scene s in m_sceneList)
170 {
171 if (s.RegionInfo.RegionHandle == regionHandle)
172 {
173 //m_log.Debug("[LOCAL COMMS]: Found region to send ChildAgentUpdate");
174 return s.IncomingRetrieveRootAgent(id, out agent);
175 }
176 }
177 //m_log.Debug("[LOCAL COMMS]: region not found for ChildAgentUpdate");
178 return false;
179 }
180
181 public bool SendReleaseAgent(ulong regionHandle, UUID id, string uri)
182 {
183 //uint x, y;
184 //Utils.LongToUInts(regionHandle, out x, out y);
185 //x = x / Constants.RegionSize;
186 //y = y / Constants.RegionSize;
187 //m_log.Debug("\n >>> Local SendReleaseAgent " + x + "-" + y);
188 foreach (Scene s in m_sceneList)
189 {
190 if (s.RegionInfo.RegionHandle == regionHandle)
191 {
192 //m_log.Debug("[LOCAL COMMS]: Found region to SendReleaseAgent");
193 return s.IncomingReleaseAgent(id);
194 }
195 }
196 //m_log.Debug("[LOCAL COMMS]: region not found in SendReleaseAgent");
197 return false;
198 }
199
200 public bool SendCloseAgent(ulong regionHandle, UUID id)
201 {
202 //uint x, y;
203 //Utils.LongToUInts(regionHandle, out x, out y);
204 //x = x / Constants.RegionSize;
205 //y = y / Constants.RegionSize;
206 //m_log.Debug("\n >>> Local SendCloseAgent " + x + "-" + y);
207 foreach (Scene s in m_sceneList)
208 {
209 if (s.RegionInfo.RegionHandle == regionHandle)
210 {
211 //m_log.Debug("[LOCAL COMMS]: Found region to SendCloseAgent");
212 return s.IncomingCloseAgent(id);
213 }
214 }
215 //m_log.Debug("[LOCAL COMMS]: region not found in SendCloseAgent");
216 return false;
217 }
218
219 /**
220 * Object-related communications
221 */
222
223 public bool SendCreateObject(ulong regionHandle, SceneObjectGroup sog, bool isLocalCall)
224 {
225 foreach (Scene s in m_sceneList)
226 {
227 if (s.RegionInfo.RegionHandle == regionHandle)
228 {
229 //m_log.Debug("[LOCAL COMMS]: Found region to SendCreateObject");
230 if (isLocalCall)
231 {
232 // We need to make a local copy of the object
233 ISceneObject sogClone = sog.CloneForNewScene();
234 sogClone.SetState(sog.GetStateSnapshot(),
235 s.RegionInfo.RegionID);
236 return s.IncomingCreateObject(sogClone);
237 }
238 else
239 {
240 // Use the object as it came through the wire
241 return s.IncomingCreateObject(sog);
242 }
243 }
244 }
245 return false;
246 }
247
248 public bool SendCreateObject(ulong regionHandle, UUID userID, UUID itemID)
249 {
250 foreach (Scene s in m_sceneList)
251 {
252 if (s.RegionInfo.RegionHandle == regionHandle)
253 {
254 return s.IncomingCreateObject(userID, itemID);
255 }
256 }
257 return false;
258 }
259
260
261 #endregion /* IInterregionComms */
262
263 #region Misc
264
265 public UUID GetRegionID(ulong regionhandle)
266 {
267 foreach (Scene s in m_sceneList)
268 {
269 if (s.RegionInfo.RegionHandle == regionhandle)
270 return s.RegionInfo.RegionID;
271 }
272 // ? weird. should not happen
273 return m_sceneList[0].RegionInfo.RegionID;
274 }
275
276 public bool IsLocalRegion(ulong regionhandle)
277 {
278 foreach (Scene s in m_sceneList)
279 if (s.RegionInfo.RegionHandle == regionhandle)
280 return true;
281 return false;
282 }
283
284 #endregion
285 }
286}
diff --git a/OpenSim/Region/CoreModules/ServiceConnectors/Interregion/RESTInterregionComms.cs b/OpenSim/Region/CoreModules/ServiceConnectors/Interregion/RESTInterregionComms.cs
deleted file mode 100644
index 37305d5..0000000
--- a/OpenSim/Region/CoreModules/ServiceConnectors/Interregion/RESTInterregionComms.cs
+++ /dev/null
@@ -1,811 +0,0 @@
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;
30using System.IO;
31using System.Net;
32using System.Reflection;
33using System.Text;
34using log4net;
35using Nini.Config;
36using OpenMetaverse;
37using OpenMetaverse.StructuredData;
38using OpenSim.Framework;
39using OpenSim.Framework.Communications;
40using OpenSim.Framework.Communications.Clients;
41using OpenSim.Region.Framework.Interfaces;
42using OpenSim.Region.Framework.Scenes;
43using OpenSim.Region.Framework.Scenes.Hypergrid;
44using OpenSim.Region.Framework.Scenes.Serialization;
45
46namespace OpenSim.Region.CoreModules.ServiceConnectors.Interregion
47{
48 public class RESTInterregionComms : IRegionModule, IInterregionCommsOut
49 {
50 private bool initialized = false;
51 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
52
53 protected bool m_enabled = false;
54 protected Scene m_aScene;
55 // RESTInterregionComms does not care about local regions; it delegates that to the Local module
56 protected LocalInterregionComms m_localBackend;
57
58 protected CommunicationsManager m_commsManager;
59
60 protected RegionToRegionClient m_regionClient;
61
62 protected bool m_safemode;
63 protected IPAddress m_thisIP;
64
65 #region IRegionModule
66
67 public virtual void Initialise(Scene scene, IConfigSource config)
68 {
69 if (!initialized)
70 {
71 initialized = true;
72 IConfig startupConfig = config.Configs["Communications"];
73
74 if ((startupConfig == null)
75 || (startupConfig != null)
76 && (startupConfig.GetString("InterregionComms", "RESTComms") == "RESTComms"))
77 {
78 m_log.Info("[REST COMMS]: Enabling InterregionComms RESTComms module");
79 m_enabled = true;
80 if (config.Configs["Hypergrid"] != null)
81 m_safemode = config.Configs["Hypergrid"].GetBoolean("safemode", false);
82
83 InitOnce(scene);
84 }
85 }
86
87 if (!m_enabled)
88 return;
89
90 InitEach(scene);
91
92 }
93
94 public virtual void PostInitialise()
95 {
96 if (m_enabled)
97 AddHTTPHandlers();
98 }
99
100 public virtual void Close()
101 {
102 }
103
104 public virtual string Name
105 {
106 get { return "RESTInterregionCommsModule"; }
107 }
108
109 public virtual bool IsSharedModule
110 {
111 get { return true; }
112 }
113
114 protected virtual void InitEach(Scene scene)
115 {
116 m_localBackend.Init(scene);
117 scene.RegisterModuleInterface<IInterregionCommsOut>(this);
118 }
119
120 protected virtual void InitOnce(Scene scene)
121 {
122 m_localBackend = new LocalInterregionComms();
123 m_commsManager = scene.CommsManager;
124 m_aScene = scene;
125 m_regionClient = new RegionToRegionClient(m_aScene);
126 m_thisIP = Util.GetHostFromDNS(scene.RegionInfo.ExternalHostName);
127 }
128
129 protected virtual void AddHTTPHandlers()
130 {
131 m_aScene.CommsManager.HttpServer.AddHTTPHandler("/agent/", AgentHandler);
132 m_aScene.CommsManager.HttpServer.AddHTTPHandler("/object/", ObjectHandler);
133 }
134
135 #endregion /* IRegionModule */
136
137 #region IInterregionComms
138
139 /**
140 * Agent-related communications
141 */
142
143 public bool SendCreateChildAgent(ulong regionHandle, AgentCircuitData aCircuit, out string reason)
144 {
145 // Try local first
146 if (m_localBackend.SendCreateChildAgent(regionHandle, aCircuit, out reason))
147 return true;
148
149 // else do the remote thing
150 if (!m_localBackend.IsLocalRegion(regionHandle))
151 {
152 RegionInfo regInfo = m_commsManager.GridService.RequestNeighbourInfo(regionHandle);
153 if (regInfo != null)
154 {
155 m_regionClient.SendUserInformation(regInfo, aCircuit);
156
157 return m_regionClient.DoCreateChildAgentCall(regInfo, aCircuit, "None", out reason);
158 }
159 //else
160 // m_log.Warn("[REST COMMS]: Region not found " + regionHandle);
161 }
162 return false;
163 }
164
165 public bool SendChildAgentUpdate(ulong regionHandle, AgentData cAgentData)
166 {
167 // Try local first
168 if (m_localBackend.SendChildAgentUpdate(regionHandle, cAgentData))
169 return true;
170
171 // else do the remote thing
172 if (!m_localBackend.IsLocalRegion(regionHandle))
173 {
174 RegionInfo regInfo = m_commsManager.GridService.RequestNeighbourInfo(regionHandle);
175 if (regInfo != null)
176 {
177 return m_regionClient.DoChildAgentUpdateCall(regInfo, cAgentData);
178 }
179 //else
180 // m_log.Warn("[REST COMMS]: Region not found " + regionHandle);
181 }
182 return false;
183
184 }
185
186 public bool SendChildAgentUpdate(ulong regionHandle, AgentPosition cAgentData)
187 {
188 // Try local first
189 if (m_localBackend.SendChildAgentUpdate(regionHandle, cAgentData))
190 return true;
191
192 // else do the remote thing
193 if (!m_localBackend.IsLocalRegion(regionHandle))
194 {
195 RegionInfo regInfo = m_commsManager.GridService.RequestNeighbourInfo(regionHandle);
196 if (regInfo != null)
197 {
198 return m_regionClient.DoChildAgentUpdateCall(regInfo, cAgentData);
199 }
200 //else
201 // m_log.Warn("[REST COMMS]: Region not found " + regionHandle);
202 }
203 return false;
204
205 }
206
207 public bool SendRetrieveRootAgent(ulong regionHandle, UUID id, out IAgentData agent)
208 {
209 // Try local first
210 if (m_localBackend.SendRetrieveRootAgent(regionHandle, id, out agent))
211 return true;
212
213 // else do the remote thing
214 if (!m_localBackend.IsLocalRegion(regionHandle))
215 {
216 RegionInfo regInfo = m_commsManager.GridService.RequestNeighbourInfo(regionHandle);
217 if (regInfo != null)
218 {
219 return m_regionClient.DoRetrieveRootAgentCall(regInfo, id, out agent);
220 }
221 //else
222 // m_log.Warn("[REST COMMS]: Region not found " + regionHandle);
223 }
224 return false;
225
226 }
227
228 public bool SendReleaseAgent(ulong regionHandle, UUID id, string uri)
229 {
230 // Try local first
231 if (m_localBackend.SendReleaseAgent(regionHandle, id, uri))
232 return true;
233
234 // else do the remote thing
235 return m_regionClient.DoReleaseAgentCall(regionHandle, id, uri);
236 }
237
238
239 public bool SendCloseAgent(ulong regionHandle, UUID id)
240 {
241 // Try local first
242 if (m_localBackend.SendCloseAgent(regionHandle, id))
243 return true;
244
245 // else do the remote thing
246 if (!m_localBackend.IsLocalRegion(regionHandle))
247 {
248 RegionInfo regInfo = m_commsManager.GridService.RequestNeighbourInfo(regionHandle);
249 if (regInfo != null)
250 {
251 return m_regionClient.DoCloseAgentCall(regInfo, id);
252 }
253 //else
254 // m_log.Warn("[REST COMMS]: Region not found " + regionHandle);
255 }
256 return false;
257 }
258
259 /**
260 * Object-related communications
261 */
262
263 public bool SendCreateObject(ulong regionHandle, SceneObjectGroup sog, bool isLocalCall)
264 {
265 // Try local first
266 if (m_localBackend.SendCreateObject(regionHandle, sog, true))
267 {
268 //m_log.Debug("[REST COMMS]: LocalBackEnd SendCreateObject succeeded");
269 return true;
270 }
271
272 // else do the remote thing
273 if (!m_localBackend.IsLocalRegion(regionHandle))
274 {
275 RegionInfo regInfo = m_commsManager.GridService.RequestNeighbourInfo(regionHandle);
276 if (regInfo != null)
277 {
278 return m_regionClient.DoCreateObjectCall(
279 regInfo, sog, SceneObjectSerializer.ToXml2Format(sog), m_aScene.m_allowScriptCrossings);
280 }
281 //else
282 // m_log.Warn("[REST COMMS]: Region not found " + regionHandle);
283 }
284 return false;
285 }
286
287 public bool SendCreateObject(ulong regionHandle, UUID userID, UUID itemID)
288 {
289 // Not Implemented
290 return false;
291 }
292
293 #endregion /* IInterregionComms */
294
295 #region Incoming calls from remote instances
296
297 /**
298 * Agent-related incoming calls
299 */
300
301 public Hashtable AgentHandler(Hashtable request)
302 {
303 //m_log.Debug("[CONNECTION DEBUGGING]: AgentHandler Called");
304
305 m_log.Debug("---------------------------");
306 m_log.Debug(" >> uri=" + request["uri"]);
307 m_log.Debug(" >> content-type=" + request["content-type"]);
308 m_log.Debug(" >> http-method=" + request["http-method"]);
309 m_log.Debug("---------------------------\n");
310
311 Hashtable responsedata = new Hashtable();
312 responsedata["content_type"] = "text/html";
313 responsedata["keepalive"] = false;
314
315
316 UUID agentID;
317 string action;
318 ulong regionHandle;
319 if (!GetParams((string)request["uri"], out agentID, out regionHandle, out action))
320 {
321 m_log.InfoFormat("[REST COMMS]: Invalid parameters for agent message {0}", request["uri"]);
322 responsedata["int_response_code"] = 404;
323 responsedata["str_response_string"] = "false";
324
325 return responsedata;
326 }
327
328 // Next, let's parse the verb
329 string method = (string)request["http-method"];
330 if (method.Equals("PUT"))
331 {
332 DoAgentPut(request, responsedata);
333 return responsedata;
334 }
335 else if (method.Equals("POST"))
336 {
337 DoAgentPost(request, responsedata, agentID);
338 return responsedata;
339 }
340 else if (method.Equals("GET"))
341 {
342 DoAgentGet(request, responsedata, agentID, regionHandle);
343 return responsedata;
344 }
345 else if (method.Equals("DELETE"))
346 {
347 DoAgentDelete(request, responsedata, agentID, action, regionHandle);
348 return responsedata;
349 }
350 else
351 {
352 m_log.InfoFormat("[REST COMMS]: method {0} not supported in agent message", method);
353 responsedata["int_response_code"] = 404;
354 responsedata["str_response_string"] = "false";
355
356 return responsedata;
357 }
358
359 }
360
361 protected virtual void DoAgentPost(Hashtable request, Hashtable responsedata, UUID id)
362 {
363 if (m_safemode)
364 {
365 // Authentication
366 string authority = string.Empty;
367 string authToken = string.Empty;
368 if (!GetAuthentication(request, out authority, out authToken))
369 {
370 m_log.InfoFormat("[REST COMMS]: Authentication failed for agent message {0}", request["uri"]);
371 responsedata["int_response_code"] = 403;
372 responsedata["str_response_string"] = "Forbidden";
373 return ;
374 }
375 if (!VerifyKey(id, authority, authToken))
376 {
377 m_log.InfoFormat("[REST COMMS]: Authentication failed for agent message {0}", request["uri"]);
378 responsedata["int_response_code"] = 403;
379 responsedata["str_response_string"] = "Forbidden";
380 return ;
381 }
382 m_log.DebugFormat("[REST COMMS]: Authentication succeeded for {0}", id);
383 }
384
385 OSDMap args = RegionClient.GetOSDMap((string)request["body"]);
386 if (args == null)
387 {
388 responsedata["int_response_code"] = 400;
389 responsedata["str_response_string"] = "false";
390 return;
391 }
392
393 // retrieve the regionhandle
394 ulong regionhandle = 0;
395 if (args["destination_handle"] != null)
396 UInt64.TryParse(args["destination_handle"].AsString(), out regionhandle);
397
398 AgentCircuitData aCircuit = new AgentCircuitData();
399 try
400 {
401 aCircuit.UnpackAgentCircuitData(args);
402 }
403 catch (Exception ex)
404 {
405 m_log.InfoFormat("[REST COMMS]: exception on unpacking ChildCreate message {0}", ex.Message);
406 return;
407 }
408
409 OSDMap resp = new OSDMap(2);
410 string reason = String.Empty;
411
412 // This is the meaning of POST agent
413 m_regionClient.AdjustUserInformation(aCircuit);
414 bool result = m_localBackend.SendCreateChildAgent(regionhandle, aCircuit, out reason);
415
416 resp["reason"] = OSD.FromString(reason);
417 resp["success"] = OSD.FromBoolean(result);
418
419 // TODO: add reason if not String.Empty?
420 responsedata["int_response_code"] = 200;
421 responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp);
422 }
423
424 protected virtual void DoAgentPut(Hashtable request, Hashtable responsedata)
425 {
426 OSDMap args = RegionClient.GetOSDMap((string)request["body"]);
427 if (args == null)
428 {
429 responsedata["int_response_code"] = 400;
430 responsedata["str_response_string"] = "false";
431 return;
432 }
433
434 // retrieve the regionhandle
435 ulong regionhandle = 0;
436 if (args["destination_handle"] != null)
437 UInt64.TryParse(args["destination_handle"].AsString(), out regionhandle);
438
439 string messageType;
440 if (args["message_type"] != null)
441 messageType = args["message_type"].AsString();
442 else
443 {
444 m_log.Warn("[REST COMMS]: Agent Put Message Type not found. ");
445 messageType = "AgentData";
446 }
447
448 bool result = true;
449 if ("AgentData".Equals(messageType))
450 {
451 AgentData agent = new AgentData();
452 try
453 {
454 agent.Unpack(args);
455 }
456 catch (Exception ex)
457 {
458 m_log.InfoFormat("[REST COMMS]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
459 return;
460 }
461
462 //agent.Dump();
463 // This is one of the meanings of PUT agent
464 result = m_localBackend.SendChildAgentUpdate(regionhandle, agent);
465
466 }
467 else if ("AgentPosition".Equals(messageType))
468 {
469 AgentPosition agent = new AgentPosition();
470 try
471 {
472 agent.Unpack(args);
473 }
474 catch (Exception ex)
475 {
476 m_log.InfoFormat("[REST COMMS]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
477 return;
478 }
479 //agent.Dump();
480 // This is one of the meanings of PUT agent
481 result = m_localBackend.SendChildAgentUpdate(regionhandle, agent);
482
483 }
484
485 responsedata["int_response_code"] = 200;
486 responsedata["str_response_string"] = result.ToString();
487 }
488
489 protected virtual void DoAgentGet(Hashtable request, Hashtable responsedata, UUID id, ulong regionHandle)
490 {
491 IAgentData agent = null;
492 bool result = m_localBackend.SendRetrieveRootAgent(regionHandle, id, out agent);
493 OSDMap map = null;
494 if (result)
495 {
496 if (agent != null) // just to make sure
497 {
498 map = agent.Pack();
499 string strBuffer = "";
500 try
501 {
502 strBuffer = OSDParser.SerializeJsonString(map);
503 }
504 catch (Exception e)
505 {
506 m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of CreateObject: {0}", e.Message);
507 // ignore. buffer will be empty, caller should check.
508 }
509
510 responsedata["content_type"] = "application/json";
511 responsedata["int_response_code"] = 200;
512 responsedata["str_response_string"] = strBuffer;
513 }
514 else
515 {
516 responsedata["int_response_code"] = 500;
517 responsedata["str_response_string"] = "Internal error";
518 }
519 }
520 else
521 {
522 responsedata["int_response_code"] = 404;
523 responsedata["str_response_string"] = "Not Found";
524 }
525 }
526
527 protected virtual void DoAgentDelete(Hashtable request, Hashtable responsedata, UUID id, string action, ulong regionHandle)
528 {
529 //m_log.Debug(" >>> DoDelete action:" + action + "; regionHandle:" + regionHandle);
530
531 if (action.Equals("release"))
532 m_localBackend.SendReleaseAgent(regionHandle, id, "");
533 else
534 m_localBackend.SendCloseAgent(regionHandle, id);
535
536 responsedata["int_response_code"] = 200;
537 responsedata["str_response_string"] = "OpenSim agent " + id.ToString();
538
539 m_log.Debug("[REST COMMS]: Agent Deleted.");
540 }
541
542 /**
543 * Object-related incoming calls
544 */
545
546 public Hashtable ObjectHandler(Hashtable request)
547 {
548 m_log.Debug("[CONNECTION DEBUGGING]: ObjectHandler Called");
549
550 m_log.Debug("---------------------------");
551 m_log.Debug(" >> uri=" + request["uri"]);
552 m_log.Debug(" >> content-type=" + request["content-type"]);
553 m_log.Debug(" >> http-method=" + request["http-method"]);
554 m_log.Debug("---------------------------\n");
555
556 Hashtable responsedata = new Hashtable();
557 responsedata["content_type"] = "text/html";
558
559 UUID objectID;
560 string action;
561 ulong regionHandle;
562 if (!GetParams((string)request["uri"], out objectID, out regionHandle, out action))
563 {
564 m_log.InfoFormat("[REST COMMS]: Invalid parameters for object message {0}", request["uri"]);
565 responsedata["int_response_code"] = 404;
566 responsedata["str_response_string"] = "false";
567
568 return responsedata;
569 }
570
571 // Next, let's parse the verb
572 string method = (string)request["http-method"];
573 if (method.Equals("POST"))
574 {
575 DoObjectPost(request, responsedata, regionHandle);
576 return responsedata;
577 }
578 else if (method.Equals("PUT"))
579 {
580 DoObjectPut(request, responsedata, regionHandle);
581 return responsedata;
582 }
583 //else if (method.Equals("DELETE"))
584 //{
585 // DoObjectDelete(request, responsedata, agentID, action, regionHandle);
586 // return responsedata;
587 //}
588 else
589 {
590 m_log.InfoFormat("[REST COMMS]: method {0} not supported in object message", method);
591 responsedata["int_response_code"] = 404;
592 responsedata["str_response_string"] = "false";
593
594 return responsedata;
595 }
596
597 }
598
599 protected virtual void DoObjectPost(Hashtable request, Hashtable responsedata, ulong regionhandle)
600 {
601 OSDMap args = RegionClient.GetOSDMap((string)request["body"]);
602 if (args == null)
603 {
604 responsedata["int_response_code"] = 400;
605 responsedata["str_response_string"] = "false";
606 return;
607 }
608
609 string sogXmlStr = "", extraStr = "", stateXmlStr = "";
610 if (args["sog"] != null)
611 sogXmlStr = args["sog"].AsString();
612 if (args["extra"] != null)
613 extraStr = args["extra"].AsString();
614
615 UUID regionID = m_localBackend.GetRegionID(regionhandle);
616 SceneObjectGroup sog = null;
617 try
618 {
619 sog = SceneObjectSerializer.FromXml2Format(sogXmlStr);
620 sog.ExtraFromXmlString(extraStr);
621 }
622 catch (Exception ex)
623 {
624 m_log.InfoFormat("[REST COMMS]: exception on deserializing scene object {0}", ex.Message);
625 responsedata["int_response_code"] = 400;
626 responsedata["str_response_string"] = "false";
627 return;
628 }
629
630 if ((args["state"] != null) && m_aScene.m_allowScriptCrossings)
631 {
632 stateXmlStr = args["state"].AsString();
633 if (stateXmlStr != "")
634 {
635 try
636 {
637 sog.SetState(stateXmlStr, regionID);
638 }
639 catch (Exception ex)
640 {
641 m_log.InfoFormat("[REST COMMS]: exception on setting state for scene object {0}", ex.Message);
642
643 }
644 }
645 }
646 // This is the meaning of POST object
647 bool result = m_localBackend.SendCreateObject(regionhandle, sog, false);
648
649 responsedata["int_response_code"] = 200;
650 responsedata["str_response_string"] = result.ToString();
651 }
652
653 protected virtual void DoObjectPut(Hashtable request, Hashtable responsedata, ulong regionhandle)
654 {
655 OSDMap args = RegionClient.GetOSDMap((string)request["body"]);
656 if (args == null)
657 {
658 responsedata["int_response_code"] = 400;
659 responsedata["str_response_string"] = "false";
660 return;
661 }
662
663 UUID userID = UUID.Zero, itemID = UUID.Zero;
664 if (args["userid"] != null)
665 userID = args["userid"].AsUUID();
666 if (args["itemid"] != null)
667 itemID = args["itemid"].AsUUID();
668
669 //UUID regionID = m_localBackend.GetRegionID(regionhandle);
670
671 // This is the meaning of PUT object
672 bool result = m_localBackend.SendCreateObject(regionhandle, userID, itemID);
673
674 responsedata["int_response_code"] = 200;
675 responsedata["str_response_string"] = result.ToString();
676 }
677
678 #endregion
679
680 #region Misc
681
682
683 /// <summary>
684 /// Extract the param from an uri.
685 /// </summary>
686 /// <param name="uri">Something like this: /agent/uuid/ or /agent/uuid/handle/release</param>
687 /// <param name="uri">uuid on uuid field</param>
688 /// <param name="action">optional action</param>
689 public static bool GetParams(string uri, out UUID uuid, out ulong regionHandle, out string action)
690 {
691 uuid = UUID.Zero;
692 action = "";
693 regionHandle = 0;
694
695 uri = uri.Trim(new char[] { '/' });
696 string[] parts = uri.Split('/');
697 if (parts.Length <= 1)
698 {
699 return false;
700 }
701 else
702 {
703 if (!UUID.TryParse(parts[1], out uuid))
704 return false;
705
706 if (parts.Length >= 3)
707 UInt64.TryParse(parts[2], out regionHandle);
708 if (parts.Length >= 4)
709 action = parts[3];
710
711 return true;
712 }
713 }
714
715 public static bool GetAuthentication(Hashtable request, out string authority, out string authKey)
716 {
717 authority = string.Empty;
718 authKey = string.Empty;
719
720 Uri authUri;
721 Hashtable headers = (Hashtable)request["headers"];
722
723 // Authorization keys look like this:
724 // http://orgrid.org:8002/<uuid>
725 if (headers.ContainsKey("authorization") && (string)headers["authorization"] != "None")
726 {
727 if (Uri.TryCreate((string)headers["authorization"], UriKind.Absolute, out authUri))
728 {
729 authority = authUri.Authority;
730 authKey = authUri.PathAndQuery.Trim('/');
731 m_log.DebugFormat("[REST COMMS]: Got authority {0} and key {1}", authority, authKey);
732 return true;
733 }
734 else
735 m_log.Debug("[REST COMMS]: Wrong format for Authorization header: " + (string)headers["authorization"]);
736 }
737 else
738 m_log.Debug("[REST COMMS]: Authorization header not found");
739
740 return false;
741 }
742
743 bool VerifyKey(UUID userID, string authority, string key)
744 {
745 string[] parts = authority.Split(':');
746 IPAddress ipaddr = IPAddress.None;
747 uint port = 0;
748 if (parts.Length <= 2)
749 ipaddr = Util.GetHostFromDNS(parts[0]);
750 if (parts.Length == 2)
751 UInt32.TryParse(parts[1], out port);
752
753 // local authority (standalone), local call
754 if (m_thisIP.Equals(ipaddr) && (m_aScene.RegionInfo.HttpPort == port))
755 return ((IAuthentication)m_aScene.CommsManager.UserAdminService).VerifyKey(userID, key);
756 // remote call
757 else
758 return AuthClient.VerifyKey("http://" + authority, userID, key);
759 }
760
761
762 #endregion Misc
763
764 protected class RegionToRegionClient : RegionClient
765 {
766 Scene m_aScene = null;
767
768 public RegionToRegionClient(Scene s)
769 {
770 m_aScene = s;
771 }
772
773 public override ulong GetRegionHandle(ulong handle)
774 {
775 if (m_aScene.SceneGridService is HGSceneCommunicationService)
776 return ((HGSceneCommunicationService)(m_aScene.SceneGridService)).m_hg.FindRegionHandle(handle);
777
778 return handle;
779 }
780
781 public override bool IsHyperlink(ulong handle)
782 {
783 if (m_aScene.SceneGridService is HGSceneCommunicationService)
784 return ((HGSceneCommunicationService)(m_aScene.SceneGridService)).m_hg.IsHyperlinkRegion(handle);
785
786 return false;
787 }
788
789 public override void SendUserInformation(RegionInfo regInfo, AgentCircuitData aCircuit)
790 {
791 try
792 {
793 if (m_aScene.SceneGridService is HGSceneCommunicationService)
794 {
795 ((HGSceneCommunicationService)(m_aScene.SceneGridService)).m_hg.SendUserInformation(regInfo, aCircuit);
796 }
797 }
798 catch // Bad cast
799 { }
800
801 }
802
803 public override void AdjustUserInformation(AgentCircuitData aCircuit)
804 {
805 if (m_aScene.SceneGridService is HGSceneCommunicationService)
806 ((HGSceneCommunicationService)(m_aScene.SceneGridService)).m_hg.AdjustUserInformation(aCircuit);
807 }
808 }
809
810 }
811}