aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs')
-rw-r--r--OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs601
1 files changed, 601 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs b/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
new file mode 100644
index 0000000..ff0dd7e
--- /dev/null
+++ b/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
@@ -0,0 +1,601 @@
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.IO;
31using System.Net;
32using System.Reflection;
33using System.Text;
34
35using OpenSim.Framework;
36using OpenSim.Services.Interfaces;
37using GridRegion = OpenSim.Services.Interfaces.GridRegion;
38
39using OpenMetaverse;
40using OpenMetaverse.StructuredData;
41using log4net;
42using Nini.Config;
43
44namespace OpenSim.Services.Connectors.Simulation
45{
46 public class SimulationServiceConnector : ISimulationService
47 {
48 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
49
50 //private GridRegion m_Region;
51
52 public SimulationServiceConnector()
53 {
54 }
55
56 public SimulationServiceConnector(IConfigSource config)
57 {
58 //m_Region = region;
59 }
60
61 public IScene GetScene(ulong regionHandle)
62 {
63 return null;
64 }
65
66 public ISimulationService GetInnerService()
67 {
68 return null;
69 }
70
71 #region Agents
72
73 protected virtual string AgentPath()
74 {
75 return "/agent/";
76 }
77
78 public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason)
79 {
80 reason = String.Empty;
81
82 if (destination == null)
83 {
84 reason = "Destination is null";
85 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Given destination is null");
86 return false;
87 }
88
89 // Eventually, we want to use a caps url instead of the agentID
90 string uri = string.Empty;
91 try
92 {
93 uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + AgentPath() + aCircuit.AgentID + "/";
94 }
95 catch (Exception e)
96 {
97 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Unable to resolve external endpoint on agent create. Reason: " + e.Message);
98 reason = e.Message;
99 return false;
100 }
101
102 //Console.WriteLine(" >>> DoCreateChildAgentCall <<< " + uri);
103
104 HttpWebRequest AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri);
105 AgentCreateRequest.Method = "POST";
106 AgentCreateRequest.ContentType = "application/json";
107 AgentCreateRequest.Timeout = 10000;
108 //AgentCreateRequest.KeepAlive = false;
109 //AgentCreateRequest.Headers.Add("Authorization", authKey);
110
111 // Fill it in
112 OSDMap args = PackCreateAgentArguments(aCircuit, destination, flags);
113 if (args == null)
114 return false;
115
116 string strBuffer = "";
117 byte[] buffer = new byte[1];
118 try
119 {
120 strBuffer = OSDParser.SerializeJsonString(args);
121 Encoding str = Util.UTF8;
122 buffer = str.GetBytes(strBuffer);
123
124 }
125 catch (Exception e)
126 {
127 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of ChildCreate: {0}", e.Message);
128 // ignore. buffer will be empty, caller should check.
129 }
130
131 Stream os = null;
132 try
133 { // send the Post
134 AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send
135 os = AgentCreateRequest.GetRequestStream();
136 os.Write(buffer, 0, strBuffer.Length); //Send it
137 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Posted CreateAgent request to remote sim {0}, region {1}, x={2} y={3}",
138 uri, destination.RegionName, destination.RegionLocX, destination.RegionLocY);
139 }
140 //catch (WebException ex)
141 catch
142 {
143 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on ChildAgentUpdate {0}", ex.Message);
144 reason = "cannot contact remote region";
145 return false;
146 }
147 finally
148 {
149 if (os != null)
150 os.Close();
151 }
152
153 // Let's wait for the response
154 //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall");
155
156 WebResponse webResponse = null;
157 StreamReader sr = null;
158 try
159 {
160 webResponse = AgentCreateRequest.GetResponse();
161 if (webResponse == null)
162 {
163 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on DoCreateChildAgentCall post");
164 }
165 else
166 {
167
168 sr = new StreamReader(webResponse.GetResponseStream());
169 string response = sr.ReadToEnd().Trim();
170 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: DoCreateChildAgentCall reply was {0} ", response);
171
172 if (!String.IsNullOrEmpty(response))
173 {
174 try
175 {
176 // we assume we got an OSDMap back
177 OSDMap r = Util.GetOSDMap(response);
178 bool success = r["success"].AsBoolean();
179 reason = r["reason"].AsString();
180 return success;
181 }
182 catch (NullReferenceException e)
183 {
184 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", e.Message);
185
186 // check for old style response
187 if (response.ToLower().StartsWith("true"))
188 return true;
189
190 return false;
191 }
192 }
193 }
194 }
195 catch (WebException ex)
196 {
197 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", ex.Message);
198 reason = "Destination did not reply";
199 return false;
200 }
201 finally
202 {
203 if (sr != null)
204 sr.Close();
205 }
206
207 return true;
208 }
209
210 protected virtual OSDMap PackCreateAgentArguments(AgentCircuitData aCircuit, GridRegion destination, uint flags)
211 {
212 OSDMap args = null;
213 try
214 {
215 args = aCircuit.PackAgentCircuitData();
216 }
217 catch (Exception e)
218 {
219 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: PackAgentCircuitData failed with exception: " + e.Message);
220 return null;
221 }
222 // Add the input arguments
223 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
224 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
225 args["destination_name"] = OSD.FromString(destination.RegionName);
226 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
227 args["teleport_flags"] = OSD.FromString(flags.ToString());
228
229 return args;
230 }
231
232 public bool UpdateAgent(GridRegion destination, AgentData data)
233 {
234 return UpdateAgent(destination, (IAgentData)data);
235 }
236
237 public bool UpdateAgent(GridRegion destination, AgentPosition data)
238 {
239 return UpdateAgent(destination, (IAgentData)data);
240 }
241
242 private bool UpdateAgent(GridRegion destination, IAgentData cAgentData)
243 {
244 // Eventually, we want to use a caps url instead of the agentID
245 string uri = string.Empty;
246 try
247 {
248 uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + AgentPath() + cAgentData.AgentID + "/";
249 }
250 catch (Exception e)
251 {
252 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Unable to resolve external endpoint on agent update. Reason: " + e.Message);
253 return false;
254 }
255 //Console.WriteLine(" >>> DoAgentUpdateCall <<< " + uri);
256
257 HttpWebRequest ChildUpdateRequest = (HttpWebRequest)WebRequest.Create(uri);
258 ChildUpdateRequest.Method = "PUT";
259 ChildUpdateRequest.ContentType = "application/json";
260 ChildUpdateRequest.Timeout = 10000;
261 //ChildUpdateRequest.KeepAlive = false;
262
263 // Fill it in
264 OSDMap args = null;
265 try
266 {
267 args = cAgentData.Pack();
268 }
269 catch (Exception e)
270 {
271 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: PackUpdateMessage failed with exception: " + e.Message);
272 }
273 // Add the input arguments
274 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
275 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
276 args["destination_name"] = OSD.FromString(destination.RegionName);
277 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
278
279 string strBuffer = "";
280 byte[] buffer = new byte[1];
281 try
282 {
283 strBuffer = OSDParser.SerializeJsonString(args);
284 Encoding str = Util.UTF8;
285 buffer = str.GetBytes(strBuffer);
286
287 }
288 catch (Exception e)
289 {
290 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of ChildUpdate: {0}", e.Message);
291 // ignore. buffer will be empty, caller should check.
292 }
293
294 Stream os = null;
295 try
296 { // send the Post
297 ChildUpdateRequest.ContentLength = buffer.Length; //Count bytes to send
298 os = ChildUpdateRequest.GetRequestStream();
299 os.Write(buffer, 0, strBuffer.Length); //Send it
300 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Posted AgentUpdate request to remote sim {0}", uri);
301 }
302 catch (WebException ex)
303 //catch
304 {
305 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on AgentUpdate {0}", ex.Message);
306
307 return false;
308 }
309 finally
310 {
311 if (os != null)
312 os.Close();
313 }
314
315 // Let's wait for the response
316 //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after ChildAgentUpdate");
317
318 WebResponse webResponse = null;
319 StreamReader sr = null;
320 try
321 {
322 webResponse = ChildUpdateRequest.GetResponse();
323 if (webResponse == null)
324 {
325 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on ChilAgentUpdate post");
326 }
327
328 sr = new StreamReader(webResponse.GetResponseStream());
329 //reply = sr.ReadToEnd().Trim();
330 sr.ReadToEnd().Trim();
331 sr.Close();
332 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
333
334 }
335 catch (WebException ex)
336 {
337 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of ChilAgentUpdate {0}", ex.Message);
338 // ignore, really
339 }
340 finally
341 {
342 if (sr != null)
343 sr.Close();
344 }
345
346 return true;
347 }
348
349 public bool RetrieveAgent(GridRegion destination, UUID id, out IAgentData agent)
350 {
351 agent = null;
352 // Eventually, we want to use a caps url instead of the agentID
353 string uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
354 //Console.WriteLine(" >>> DoRetrieveRootAgentCall <<< " + uri);
355
356 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
357 request.Method = "GET";
358 request.Timeout = 10000;
359 //request.Headers.Add("authorization", ""); // coming soon
360
361 HttpWebResponse webResponse = null;
362 string reply = string.Empty;
363 StreamReader sr = null;
364 try
365 {
366 webResponse = (HttpWebResponse)request.GetResponse();
367 if (webResponse == null)
368 {
369 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on agent get ");
370 }
371
372 sr = new StreamReader(webResponse.GetResponseStream());
373 reply = sr.ReadToEnd().Trim();
374
375 //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was " + reply);
376
377 }
378 catch (WebException ex)
379 {
380 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of agent get {0}", ex.Message);
381 // ignore, really
382 return false;
383 }
384 finally
385 {
386 if (sr != null)
387 sr.Close();
388 }
389
390 if (webResponse.StatusCode == HttpStatusCode.OK)
391 {
392 // we know it's jason
393 OSDMap args = Util.GetOSDMap(reply);
394 if (args == null)
395 {
396 //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: Error getting OSDMap from reply");
397 return false;
398 }
399
400 agent = new CompleteAgentData();
401 agent.Unpack(args);
402 return true;
403 }
404
405 //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: DoRetrieveRootAgentCall returned status " + webResponse.StatusCode);
406 return false;
407 }
408
409 public bool ReleaseAgent(UUID origin, UUID id, string uri)
410 {
411 WebRequest request = WebRequest.Create(uri);
412 request.Method = "DELETE";
413 request.Timeout = 10000;
414
415 StreamReader sr = null;
416 try
417 {
418 WebResponse webResponse = request.GetResponse();
419 if (webResponse == null)
420 {
421 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on ReleaseAgent");
422 }
423
424 sr = new StreamReader(webResponse.GetResponseStream());
425 //reply = sr.ReadToEnd().Trim();
426 sr.ReadToEnd().Trim();
427 sr.Close();
428 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
429
430 }
431 catch (WebException ex)
432 {
433 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of ReleaseAgent {0}", ex.Message);
434 return false;
435 }
436 finally
437 {
438 if (sr != null)
439 sr.Close();
440 }
441
442 return true;
443 }
444
445 public bool CloseAgent(GridRegion destination, UUID id)
446 {
447 string uri = string.Empty;
448 try
449 {
450 uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
451 }
452 catch (Exception e)
453 {
454 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Unable to resolve external endpoint on agent close. Reason: " + e.Message);
455 return false;
456 }
457
458 //Console.WriteLine(" >>> DoCloseAgentCall <<< " + uri);
459
460 WebRequest request = WebRequest.Create(uri);
461 request.Method = "DELETE";
462 request.Timeout = 10000;
463
464 StreamReader sr = null;
465 try
466 {
467 WebResponse webResponse = request.GetResponse();
468 if (webResponse == null)
469 {
470 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on agent delete ");
471 }
472
473 sr = new StreamReader(webResponse.GetResponseStream());
474 //reply = sr.ReadToEnd().Trim();
475 sr.ReadToEnd().Trim();
476 sr.Close();
477 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
478
479 }
480 catch (WebException ex)
481 {
482 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of agent delete {0}", ex.Message);
483 return false;
484 }
485 finally
486 {
487 if (sr != null)
488 sr.Close();
489 }
490
491 return true;
492 }
493
494 #endregion Agents
495
496 #region Objects
497
498 protected virtual string ObjectPath()
499 {
500 return "/object/";
501 }
502
503 public bool CreateObject(GridRegion destination, ISceneObject sog, bool isLocalCall)
504 {
505 string uri
506 = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + ObjectPath() + sog.UUID + "/";
507 //m_log.Debug(" >>> DoCreateObjectCall <<< " + uri);
508
509 WebRequest ObjectCreateRequest = WebRequest.Create(uri);
510 ObjectCreateRequest.Method = "POST";
511 ObjectCreateRequest.ContentType = "application/json";
512 ObjectCreateRequest.Timeout = 10000;
513
514 OSDMap args = new OSDMap(2);
515 args["sog"] = OSD.FromString(sog.ToXml2());
516 args["extra"] = OSD.FromString(sog.ExtraToXmlString());
517 string state = sog.GetStateSnapshot();
518 if (state.Length > 0)
519 args["state"] = OSD.FromString(state);
520 // Add the input general arguments
521 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
522 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
523 args["destination_name"] = OSD.FromString(destination.RegionName);
524 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
525
526 string strBuffer = "";
527 byte[] buffer = new byte[1];
528 try
529 {
530 strBuffer = OSDParser.SerializeJsonString(args);
531 Encoding str = Util.UTF8;
532 buffer = str.GetBytes(strBuffer);
533
534 }
535 catch (Exception e)
536 {
537 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of CreateObject: {0}", e.Message);
538 // ignore. buffer will be empty, caller should check.
539 }
540
541 Stream os = null;
542 try
543 { // send the Post
544 ObjectCreateRequest.ContentLength = buffer.Length; //Count bytes to send
545 os = ObjectCreateRequest.GetRequestStream();
546 os.Write(buffer, 0, strBuffer.Length); //Send it
547 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Posted CreateObject request to remote sim {0}", uri);
548 }
549 catch (WebException ex)
550 {
551 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on CreateObject {0}", ex.Message);
552 return false;
553 }
554 finally
555 {
556 if (os != null)
557 os.Close();
558 }
559
560 // Let's wait for the response
561 //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall");
562
563 StreamReader sr = null;
564 try
565 {
566 WebResponse webResponse = ObjectCreateRequest.GetResponse();
567 if (webResponse == null)
568 {
569 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on CreateObject post");
570 return false;
571 }
572
573 sr = new StreamReader(webResponse.GetResponseStream());
574 //reply = sr.ReadToEnd().Trim();
575 sr.ReadToEnd().Trim();
576 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: DoCreateChildAgentCall reply was {0} ", reply);
577
578 }
579 catch (WebException ex)
580 {
581 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of CreateObject {0}", ex.Message);
582 return false;
583 }
584 finally
585 {
586 if (sr != null)
587 sr.Close();
588 }
589
590 return true;
591 }
592
593 public bool CreateObject(GridRegion destination, UUID userID, UUID itemID)
594 {
595 // TODO, not that urgent
596 return false;
597 }
598
599 #endregion Objects
600 }
601}