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