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.cs414
1 files changed, 414 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..dc532d0
--- /dev/null
+++ b/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
@@ -0,0 +1,414 @@
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 public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason)
69 {
70 reason = String.Empty;
71
72 if (destination == null)
73 {
74 reason = "Destination is null";
75 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Given destination is null");
76 return false;
77 }
78
79 // Eventually, we want to use a caps url instead of the agentID
80 string uri = string.Empty;
81 try
82 {
83 uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + "/agent/" + aCircuit.AgentID + "/";
84 }
85 catch (Exception e)
86 {
87 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Unable to resolve external endpoint on agent create. Reason: " + e.Message);
88 reason = e.Message;
89 return false;
90 }
91
92 //Console.WriteLine(" >>> DoCreateChildAgentCall <<< " + uri);
93
94 HttpWebRequest AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri);
95 AgentCreateRequest.Method = "POST";
96 AgentCreateRequest.ContentType = "application/json";
97 AgentCreateRequest.Timeout = 10000;
98 //AgentCreateRequest.KeepAlive = false;
99 //AgentCreateRequest.Headers.Add("Authorization", authKey);
100
101 // Fill it in
102 OSDMap args = null;
103 try
104 {
105 args = aCircuit.PackAgentCircuitData();
106 }
107 catch (Exception e)
108 {
109 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: PackAgentCircuitData failed with exception: " + e.Message);
110 }
111 // Add the input arguments
112 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
113 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
114 args["destination_name"] = OSD.FromString(destination.RegionName);
115 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
116 args["teleport_flags"] = OSD.FromString(flags.ToString());
117
118 string strBuffer = "";
119 byte[] buffer = new byte[1];
120 try
121 {
122 strBuffer = OSDParser.SerializeJsonString(args);
123 Encoding str = Util.UTF8;
124 buffer = str.GetBytes(strBuffer);
125
126 }
127 catch (Exception e)
128 {
129 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of ChildCreate: {0}", e.Message);
130 // ignore. buffer will be empty, caller should check.
131 }
132
133 Stream os = null;
134 try
135 { // send the Post
136 AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send
137 os = AgentCreateRequest.GetRequestStream();
138 os.Write(buffer, 0, strBuffer.Length); //Send it
139 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Posted CreateAgent request to remote sim {0}, region {1}, x={2} y={3}",
140 uri, destination.RegionName, destination.RegionLocX, destination.RegionLocY);
141 }
142 //catch (WebException ex)
143 catch
144 {
145 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on ChildAgentUpdate {0}", ex.Message);
146 reason = "cannot contact remote region";
147 return false;
148 }
149 finally
150 {
151 if (os != null)
152 os.Close();
153 }
154
155 // Let's wait for the response
156 //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall");
157
158 WebResponse webResponse = null;
159 StreamReader sr = null;
160 try
161 {
162 webResponse = AgentCreateRequest.GetResponse();
163 if (webResponse == null)
164 {
165 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on DoCreateChildAgentCall post");
166 }
167 else
168 {
169
170 sr = new StreamReader(webResponse.GetResponseStream());
171 string response = sr.ReadToEnd().Trim();
172 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: DoCreateChildAgentCall reply was {0} ", response);
173
174 if (!String.IsNullOrEmpty(response))
175 {
176 try
177 {
178 // we assume we got an OSDMap back
179 OSDMap r = Util.GetOSDMap(response);
180 bool success = r["success"].AsBoolean();
181 reason = r["reason"].AsString();
182 return success;
183 }
184 catch (NullReferenceException e)
185 {
186 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", e.Message);
187
188 // check for old style response
189 if (response.ToLower().StartsWith("true"))
190 return true;
191
192 return false;
193 }
194 }
195 }
196 }
197 catch (WebException ex)
198 {
199 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", ex.Message);
200 // ignore, really
201 }
202 finally
203 {
204 if (sr != null)
205 sr.Close();
206 }
207
208 return true;
209 }
210
211 public bool UpdateAgent(GridRegion destination, AgentData data)
212 {
213 return UpdateAgent(destination, data);
214 }
215
216 public bool UpdateAgent(GridRegion destination, AgentPosition data)
217 {
218 return UpdateAgent(destination, data);
219 }
220
221 private bool UpdateAgent(GridRegion destination, IAgentData cAgentData)
222 {
223 // Eventually, we want to use a caps url instead of the agentID
224 string uri = string.Empty;
225 try
226 {
227 uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + "/agent/" + cAgentData.AgentID + "/";
228 }
229 catch (Exception e)
230 {
231 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Unable to resolve external endpoint on agent update. Reason: " + e.Message);
232 return false;
233 }
234 //Console.WriteLine(" >>> DoChildAgentUpdateCall <<< " + uri);
235
236 HttpWebRequest ChildUpdateRequest = (HttpWebRequest)WebRequest.Create(uri);
237 ChildUpdateRequest.Method = "PUT";
238 ChildUpdateRequest.ContentType = "application/json";
239 ChildUpdateRequest.Timeout = 10000;
240 //ChildUpdateRequest.KeepAlive = false;
241
242 // Fill it in
243 OSDMap args = null;
244 try
245 {
246 args = cAgentData.Pack();
247 }
248 catch (Exception e)
249 {
250 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: PackUpdateMessage failed with exception: " + e.Message);
251 }
252 // Add the input arguments
253 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
254 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
255 args["destination_name"] = OSD.FromString(destination.RegionName);
256 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
257
258 string strBuffer = "";
259 byte[] buffer = new byte[1];
260 try
261 {
262 strBuffer = OSDParser.SerializeJsonString(args);
263 Encoding str = Util.UTF8;
264 buffer = str.GetBytes(strBuffer);
265
266 }
267 catch (Exception e)
268 {
269 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of ChildUpdate: {0}", e.Message);
270 // ignore. buffer will be empty, caller should check.
271 }
272
273 Stream os = null;
274 try
275 { // send the Post
276 ChildUpdateRequest.ContentLength = buffer.Length; //Count bytes to send
277 os = ChildUpdateRequest.GetRequestStream();
278 os.Write(buffer, 0, strBuffer.Length); //Send it
279 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Posted ChildAgentUpdate request to remote sim {0}", uri);
280 }
281 //catch (WebException ex)
282 catch
283 {
284 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on ChildAgentUpdate {0}", ex.Message);
285
286 return false;
287 }
288 finally
289 {
290 if (os != null)
291 os.Close();
292 }
293
294 // Let's wait for the response
295 //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after ChildAgentUpdate");
296
297 WebResponse webResponse = null;
298 StreamReader sr = null;
299 try
300 {
301 webResponse = ChildUpdateRequest.GetResponse();
302 if (webResponse == null)
303 {
304 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on ChilAgentUpdate post");
305 }
306
307 sr = new StreamReader(webResponse.GetResponseStream());
308 //reply = sr.ReadToEnd().Trim();
309 sr.ReadToEnd().Trim();
310 sr.Close();
311 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
312
313 }
314 catch (WebException ex)
315 {
316 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of ChilAgentUpdate {0}", ex.Message);
317 // ignore, really
318 }
319 finally
320 {
321 if (sr != null)
322 sr.Close();
323 }
324
325 return true;
326 }
327
328 public bool RetrieveAgent(GridRegion destination, UUID id, out IAgentData agent)
329 {
330 agent = null;
331 // Eventually, we want to use a caps url instead of the agentID
332 string uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + "/agent/" + id + "/" + destination.RegionID.ToString() + "/";
333 //Console.WriteLine(" >>> DoRetrieveRootAgentCall <<< " + uri);
334
335 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
336 request.Method = "GET";
337 request.Timeout = 10000;
338 //request.Headers.Add("authorization", ""); // coming soon
339
340 HttpWebResponse webResponse = null;
341 string reply = string.Empty;
342 StreamReader sr = null;
343 try
344 {
345 webResponse = (HttpWebResponse)request.GetResponse();
346 if (webResponse == null)
347 {
348 m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on agent get ");
349 }
350
351 sr = new StreamReader(webResponse.GetResponseStream());
352 reply = sr.ReadToEnd().Trim();
353
354 //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was " + reply);
355
356 }
357 catch (WebException ex)
358 {
359 m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of agent get {0}", ex.Message);
360 // ignore, really
361 return false;
362 }
363 finally
364 {
365 if (sr != null)
366 sr.Close();
367 }
368
369 if (webResponse.StatusCode == HttpStatusCode.OK)
370 {
371 // we know it's jason
372 OSDMap args = Util.GetOSDMap(reply);
373 if (args == null)
374 {
375 //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: Error getting OSDMap from reply");
376 return false;
377 }
378
379 agent = new CompleteAgentData();
380 agent.Unpack(args);
381 return true;
382 }
383
384 //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: DoRetrieveRootAgentCall returned status " + webResponse.StatusCode);
385 return false;
386 }
387
388 public bool ReleaseAgent(GridRegion destination, UUID id, string uri)
389 {
390 return false;
391 }
392
393 public bool CloseAgent(GridRegion destination, UUID id)
394 {
395 return false;
396 }
397
398 #endregion Agents
399
400 #region Objects
401
402 public bool CreateObject(GridRegion destination, ISceneObject sog, bool isLocalCall)
403 {
404 return false;
405 }
406
407 public bool CreateObject(GridRegion destination, UUID userID, UUID itemID)
408 {
409 return false;
410 }
411
412 #endregion Objects
413 }
414}