clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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.IO;
31 using System.Net;
32 using System.Reflection;
33 using System.Text;
34 using System.Collections;
35  
36 using OpenSim.Framework;
37 using OpenSim.Services.Interfaces;
38 using GridRegion = OpenSim.Services.Interfaces.GridRegion;
39  
40 using OpenMetaverse;
41 using OpenMetaverse.StructuredData;
42 using log4net;
43 using Nini.Config;
44  
45 namespace OpenSim.Services.Connectors.Simulation
46 {
47 public class SimulationServiceConnector : ISimulationService
48 {
49 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
50  
51 // we use this dictionary to track the pending updateagent requests, maps URI --> position update
52 private Dictionary<string,AgentPosition> m_updateAgentQueue = new Dictionary<string,AgentPosition>();
53  
54 //private GridRegion m_Region;
55  
56 public SimulationServiceConnector()
57 {
58 }
59  
60 public SimulationServiceConnector(IConfigSource config)
61 {
62 //m_Region = region;
63 }
64  
65 public IScene GetScene(UUID regionId)
66 {
67 return null;
68 }
69  
70 public ISimulationService GetInnerService()
71 {
72 return null;
73 }
74  
75 #region Agents
76  
77 protected virtual string AgentPath()
78 {
79 return "agent/";
80 }
81  
82 protected virtual void PackData(OSDMap args, GridRegion source, AgentCircuitData aCircuit, GridRegion destination, uint flags)
83 {
84 if (source != null)
85 {
86 args["source_x"] = OSD.FromString(source.RegionLocX.ToString());
87 args["source_y"] = OSD.FromString(source.RegionLocY.ToString());
88 args["source_name"] = OSD.FromString(source.RegionName);
89 args["source_uuid"] = OSD.FromString(source.RegionID.ToString());
90 if (!String.IsNullOrEmpty(source.RawServerURI))
91 args["source_server_uri"] = OSD.FromString(source.RawServerURI);
92 }
93  
94 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
95 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
96 args["destination_name"] = OSD.FromString(destination.RegionName);
97 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
98 args["teleport_flags"] = OSD.FromString(flags.ToString());
99 }
100  
101 public bool CreateAgent(GridRegion source, GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason)
102 {
103 string tmp = String.Empty;
104 return CreateAgent(source, destination, aCircuit, flags, out tmp, out reason);
105 }
106  
107 public bool CreateAgent(GridRegion source, GridRegion destination, AgentCircuitData aCircuit, uint flags, out string myipaddress, out string reason)
108 {
109 m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: Creating agent at {0}", destination.ServerURI);
110 reason = String.Empty;
111 myipaddress = String.Empty;
112  
113 if (destination == null)
114 {
115 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Given destination is null");
116 return false;
117 }
118  
119 string uri = destination.ServerURI + AgentPath() + aCircuit.AgentID + "/";
120  
121 try
122 {
123 OSDMap args = aCircuit.PackAgentCircuitData();
124 PackData(args, source, aCircuit, destination, flags);
125  
126 OSDMap result = WebUtil.PostToServiceCompressed(uri, args, 30000);
127 bool success = result["success"].AsBoolean();
128 if (success && result.ContainsKey("_Result"))
129 {
130 OSDMap data = (OSDMap)result["_Result"];
131  
132 reason = data["reason"].AsString();
133 success = data["success"].AsBoolean();
134 myipaddress = data["your_ip"].AsString();
135 return success;
136 }
137  
138 // Try the old version, uncompressed
139 result = WebUtil.PostToService(uri, args, 30000, false);
140  
141 if (result["Success"].AsBoolean())
142 {
143 if (result.ContainsKey("_Result"))
144 {
145 OSDMap data = (OSDMap)result["_Result"];
146  
147 reason = data["reason"].AsString();
148 success = data["success"].AsBoolean();
149 myipaddress = data["your_ip"].AsString();
150 m_log.WarnFormat(
151 "[REMOTE SIMULATION CONNECTOR]: Remote simulator {0} did not accept compressed transfer, suggest updating it.", destination.RegionName);
152 return success;
153 }
154 }
155  
156 m_log.WarnFormat(
157 "[REMOTE SIMULATION CONNECTOR]: Failed to create agent {0} {1} at remote simulator {2}",
158 aCircuit.firstname, aCircuit.lastname, destination.RegionName);
159 reason = result["Message"] != null ? result["Message"].AsString() : "error";
160 return false;
161 }
162 catch (Exception e)
163 {
164 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: CreateAgent failed with exception: " + e.ToString());
165 reason = e.Message;
166 }
167  
168 return false;
169 }
170  
171 /// <summary>
172 /// Send complete data about an agent in this region to a neighbor
173 /// </summary>
174 public bool UpdateAgent(GridRegion destination, AgentData data)
175 {
176 return UpdateAgent(destination, (IAgentData)data, 200000); // yes, 200 seconds
177 }
178  
179 private ExpiringCache<string, bool> _failedSims = new ExpiringCache<string, bool>();
180 /// <summary>
181 /// Send updated position information about an agent in this region to a neighbor
182 /// This operation may be called very frequently if an avatar is moving about in
183 /// the region.
184 /// </summary>
185 public bool UpdateAgent(GridRegion destination, AgentPosition data)
186 {
187 bool v = true;
188 if (_failedSims.TryGetValue(destination.ServerURI, out v))
189 return false;
190  
191 // The basic idea of this code is that the first thread that needs to
192 // send an update for a specific avatar becomes the worker for any subsequent
193 // requests until there are no more outstanding requests. Further, only send the most
194 // recent update; this *should* never be needed but some requests get
195 // slowed down and once that happens the problem with service end point
196 // limits kicks in and nothing proceeds
197 string uri = destination.ServerURI + AgentPath() + data.AgentID + "/";
198 lock (m_updateAgentQueue)
199 {
200 if (m_updateAgentQueue.ContainsKey(uri))
201 {
202 // Another thread is already handling
203 // updates for this simulator, just update
204 // the position and return, overwrites are
205 // not a problem since we only care about the
206 // last update anyway
207 m_updateAgentQueue[uri] = data;
208 return true;
209 }
210  
211 // Otherwise update the reference and start processing
212 m_updateAgentQueue[uri] = data;
213 }
214  
215 AgentPosition pos = null;
216 bool success = true;
217 while (success)
218 {
219 lock (m_updateAgentQueue)
220 {
221 // save the position
222 AgentPosition lastpos = pos;
223  
224 pos = m_updateAgentQueue[uri];
225  
226 // this is true if no one put a new
227 // update in the map since the last
228 // one we processed, if thats the
229 // case then we are done
230 if (pos == lastpos)
231 {
232 m_updateAgentQueue.Remove(uri);
233 return true;
234 }
235 }
236  
237 success = UpdateAgent(destination, (IAgentData)pos, 10000);
238 }
239 // we get here iff success == false
240 // blacklist sim for 2 minutes
241 lock (m_updateAgentQueue)
242 {
243 _failedSims.AddOrUpdate(destination.ServerURI, true, 120);
244 m_updateAgentQueue.Remove(uri);
245 }
246 return false;
247 }
248  
249 /// <summary>
250 /// This is the worker function to send AgentData to a neighbor region
251 /// </summary>
252 private bool UpdateAgent(GridRegion destination, IAgentData cAgentData, int timeout)
253 {
254 // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: UpdateAgent in {0}", destination.ServerURI);
255  
256 // Eventually, we want to use a caps url instead of the agentID
257 string uri = destination.ServerURI + AgentPath() + cAgentData.AgentID + "/";
258  
259 try
260 {
261 OSDMap args = cAgentData.Pack();
262  
263 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
264 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
265 args["destination_name"] = OSD.FromString(destination.RegionName);
266 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
267  
268 OSDMap result = WebUtil.PutToServiceCompressed(uri, args, timeout);
269 if (result["Success"].AsBoolean())
270 return true;
271  
272 result = WebUtil.PutToService(uri, args, timeout);
273  
274 return result["Success"].AsBoolean();
275 }
276 catch (Exception e)
277 {
278 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: UpdateAgent failed with exception: " + e.ToString());
279 }
280  
281 return false;
282 }
283  
284  
285 public bool QueryAccess(GridRegion destination, UUID agentID, string agentHomeURI, bool viaTeleport, Vector3 position, string myversion, out string version, out string reason)
286 {
287 reason = "Failed to contact destination";
288 version = "Unknown";
289  
290 // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: QueryAccess start, position={0}", position);
291  
292 IPEndPoint ext = destination.ExternalEndPoint;
293 if (ext == null) return false;
294  
295 // Eventually, we want to use a caps url instead of the agentID
296 string uri = destination.ServerURI + AgentPath() + agentID + "/" + destination.RegionID.ToString() + "/";
297  
298 OSDMap request = new OSDMap();
299 request.Add("viaTeleport", OSD.FromBoolean(viaTeleport));
300 request.Add("position", OSD.FromString(position.ToString()));
301 request.Add("my_version", OSD.FromString(myversion));
302 if (agentHomeURI != null)
303 request.Add("agent_home_uri", OSD.FromString(agentHomeURI));
304  
305 try
306 {
307 OSDMap result = WebUtil.ServiceOSDRequest(uri, request, "QUERYACCESS", 30000, false, false);
308 bool success = result["success"].AsBoolean();
309 if (result.ContainsKey("_Result"))
310 {
311 OSDMap data = (OSDMap)result["_Result"];
312  
313 // FIXME: If there is a _Result map then it's the success key here that indicates the true success
314 // or failure, not the sibling result node.
315 success = data["success"];
316  
317 reason = data["reason"].AsString();
318 if (data["version"] != null && data["version"].AsString() != string.Empty)
319 version = data["version"].AsString();
320  
321 m_log.DebugFormat(
322 "[REMOTE SIMULATION CONNECTOR]: QueryAccess to {0} returned {1}, reason {2}, version {3} ({4})",
323 uri, success, reason, version, data["version"].AsString());
324 }
325  
326 if (!success)
327 {
328 // If we don't check this then OpenSimulator 0.7.3.1 and some period before will never see the
329 // actual failure message
330 if (!result.ContainsKey("_Result"))
331 {
332 if (result.ContainsKey("Message"))
333 {
334 string message = result["Message"].AsString();
335 if (message == "Service request failed: [MethodNotAllowed] MethodNotAllowed") // Old style region
336 {
337 m_log.Info("[REMOTE SIMULATION CONNECTOR]: The above web util error was caused by a TP to a sim that doesn't support QUERYACCESS and can be ignored");
338 return true;
339 }
340  
341 reason = result["Message"];
342 }
343 else
344 {
345 reason = "Communications failure";
346 }
347 }
348  
349 return false;
350 }
351  
352 return success;
353 }
354 catch (Exception e)
355 {
356 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] QueryAcesss failed with exception; {0}",e.ToString());
357 }
358  
359 return false;
360 }
361  
362 /// <summary>
363 /// </summary>
364 public bool ReleaseAgent(UUID origin, UUID id, string uri)
365 {
366 // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: ReleaseAgent start");
367  
368 try
369 {
370 WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000, false, false);
371 }
372 catch (Exception e)
373 {
374 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] ReleaseAgent failed with exception; {0}",e.ToString());
375 }
376  
377 return true;
378 }
379  
380 /// <summary>
381 /// </summary>
382 public bool CloseAgent(GridRegion destination, UUID id, string auth_code)
383 {
384 string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/?auth=" + auth_code;
385 m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CloseAgent {0}", uri);
386  
387 try
388 {
389 WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000, false, false);
390 }
391 catch (Exception e)
392 {
393 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] CloseAgent failed with exception; {0}",e.ToString());
394 }
395  
396 return true;
397 }
398  
399 #endregion Agents
400  
401 #region Objects
402  
403 protected virtual string ObjectPath()
404 {
405 return "object/";
406 }
407  
408 /// <summary>
409 ///
410 /// </summary>
411 public bool CreateObject(GridRegion destination, Vector3 newPosition, ISceneObject sog, bool isLocalCall)
412 {
413 // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CreateObject start");
414  
415 string uri = destination.ServerURI + ObjectPath() + sog.UUID + "/";
416  
417 try
418 {
419 OSDMap args = new OSDMap(2);
420  
421 args["sog"] = OSD.FromString(sog.ToXml2());
422 args["extra"] = OSD.FromString(sog.ExtraToXmlString());
423 args["modified"] = OSD.FromBoolean(sog.HasGroupChanged);
424 args["new_position"] = newPosition.ToString();
425  
426 string state = sog.GetStateSnapshot();
427 if (state.Length > 0)
428 args["state"] = OSD.FromString(state);
429  
430 // Add the input general arguments
431 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
432 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
433 args["destination_name"] = OSD.FromString(destination.RegionName);
434 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
435  
436 OSDMap result = WebUtil.PostToService(uri, args, 40000, false);
437  
438 if (result == null)
439 return false;
440 bool success = result["success"].AsBoolean();
441 if (!success)
442 return false;
443 }
444 catch (Exception e)
445 {
446 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] CreateObject failed with exception; {0}",e.ToString());
447 return false;
448 }
449  
450 return true;
451 }
452  
453 /// <summary>
454 ///
455 /// </summary>
456 public bool CreateObject(GridRegion destination, UUID userID, UUID itemID)
457 {
458 // TODO, not that urgent
459 return false;
460 }
461  
462 #endregion Objects
463 }
464 }