aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Communications
diff options
context:
space:
mode:
authordiva2009-04-01 23:35:48 +0000
committerdiva2009-04-01 23:35:48 +0000
commit86c753a6bdce8788c6a23bebaf9a6015d5af238a (patch)
tree1cf7297e6e62cbaefa9d3bfa66fb089300545916 /OpenSim/Framework/Communications
parentOne more bit of refactoring, so this can be used outside region code. (diff)
downloadopensim-SC_OLD-86c753a6bdce8788c6a23bebaf9a6015d5af238a.zip
opensim-SC_OLD-86c753a6bdce8788c6a23bebaf9a6015d5af238a.tar.gz
opensim-SC_OLD-86c753a6bdce8788c6a23bebaf9a6015d5af238a.tar.bz2
opensim-SC_OLD-86c753a6bdce8788c6a23bebaf9a6015d5af238a.tar.xz
More refactoring. This time extracting the client-side of RESTInterregionComms into a RegionClient class.
Diffstat (limited to 'OpenSim/Framework/Communications')
-rw-r--r--OpenSim/Framework/Communications/Clients/RegionClient.cs555
1 files changed, 555 insertions, 0 deletions
diff --git a/OpenSim/Framework/Communications/Clients/RegionClient.cs b/OpenSim/Framework/Communications/Clients/RegionClient.cs
new file mode 100644
index 0000000..d98852b
--- /dev/null
+++ b/OpenSim/Framework/Communications/Clients/RegionClient.cs
@@ -0,0 +1,555 @@
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 OpenSim 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 OpenMetaverse;
36using OpenMetaverse.StructuredData;
37
38using log4net;
39
40namespace OpenSim.Framework.Communications.Clients
41{
42 public class RegionClient
43 {
44 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
45
46 public bool DoCreateChildAgentCall(RegionInfo region, AgentCircuitData aCircuit)
47 {
48 // Eventually, we want to use a caps url instead of the agentID
49 string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/agent/" + aCircuit.AgentID + "/";
50 //Console.WriteLine(" >>> DoCreateChildAgentCall <<< " + uri);
51
52 HttpWebRequest AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri);
53 AgentCreateRequest.Method = "POST";
54 AgentCreateRequest.ContentType = "application/json";
55 AgentCreateRequest.Timeout = 10000;
56 //AgentCreateRequest.KeepAlive = false;
57
58 // Fill it in
59 OSDMap args = null;
60 try
61 {
62 args = aCircuit.PackAgentCircuitData();
63 }
64 catch (Exception e)
65 {
66 m_log.Debug("[REST COMMS]: PackAgentCircuitData failed with exception: " + e.Message);
67 }
68 // Add the regionhandle of the destination region
69 ulong regionHandle = GetRegionHandle(region.RegionHandle);
70 args["destination_handle"] = OSD.FromString(regionHandle.ToString());
71
72 string strBuffer = "";
73 byte[] buffer = new byte[1];
74 try
75 {
76 strBuffer = OSDParser.SerializeJsonString(args);
77 UTF8Encoding str = new UTF8Encoding();
78 buffer = str.GetBytes(strBuffer);
79
80 }
81 catch (Exception e)
82 {
83 m_log.WarnFormat("[OSG2]: Exception thrown on serialization of ChildCreate: {0}", e.Message);
84 // ignore. buffer will be empty, caller should check.
85 }
86
87 Stream os = null;
88 try
89 { // send the Post
90 AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send
91 os = AgentCreateRequest.GetRequestStream();
92 os.Write(buffer, 0, strBuffer.Length); //Send it
93 os.Close();
94 //m_log.InfoFormat("[REST COMMS]: Posted ChildAgentUpdate request to remote sim {0}", uri);
95 }
96 //catch (WebException ex)
97 catch
98 {
99 //m_log.InfoFormat("[REST COMMS]: Bad send on ChildAgentUpdate {0}", ex.Message);
100
101 return false;
102 }
103
104 // Let's wait for the response
105 //m_log.Info("[REST COMMS]: Waiting for a reply after DoCreateChildAgentCall");
106
107 try
108 {
109 WebResponse webResponse = AgentCreateRequest.GetResponse();
110 if (webResponse == null)
111 {
112 m_log.Info("[REST COMMS]: Null reply on DoCreateChildAgentCall post");
113 }
114
115 StreamReader sr = new StreamReader(webResponse.GetResponseStream());
116 //reply = sr.ReadToEnd().Trim();
117 sr.ReadToEnd().Trim();
118 sr.Close();
119 //m_log.InfoFormat("[REST COMMS]: DoCreateChildAgentCall reply was {0} ", reply);
120
121 }
122 catch (WebException ex)
123 {
124 m_log.InfoFormat("[REST COMMS]: exception on reply of DoCreateChildAgentCall {0}", ex.Message);
125 // ignore, really
126 }
127
128 return true;
129
130 }
131
132 public bool DoChildAgentUpdateCall(RegionInfo region, IAgentData cAgentData)
133 {
134 // Eventually, we want to use a caps url instead of the agentID
135 string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/agent/" + cAgentData.AgentID + "/";
136 //Console.WriteLine(" >>> DoChildAgentUpdateCall <<< " + uri);
137
138 HttpWebRequest ChildUpdateRequest = (HttpWebRequest)WebRequest.Create(uri);
139 ChildUpdateRequest.Method = "PUT";
140 ChildUpdateRequest.ContentType = "application/json";
141 ChildUpdateRequest.Timeout = 10000;
142 //ChildUpdateRequest.KeepAlive = false;
143
144 // Fill it in
145 OSDMap args = null;
146 try
147 {
148 args = cAgentData.Pack();
149 }
150 catch (Exception e)
151 {
152 m_log.Debug("[REST COMMS]: PackUpdateMessage failed with exception: " + e.Message);
153 }
154 // Add the regionhandle of the destination region
155 ulong regionHandle = GetRegionHandle(region.RegionHandle);
156 args["destination_handle"] = OSD.FromString(regionHandle.ToString());
157
158 string strBuffer = "";
159 byte[] buffer = new byte[1];
160 try
161 {
162 strBuffer = OSDParser.SerializeJsonString(args);
163 UTF8Encoding str = new UTF8Encoding();
164 buffer = str.GetBytes(strBuffer);
165
166 }
167 catch (Exception e)
168 {
169 m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of ChildUpdate: {0}", e.Message);
170 // ignore. buffer will be empty, caller should check.
171 }
172
173 Stream os = null;
174 try
175 { // send the Post
176 ChildUpdateRequest.ContentLength = buffer.Length; //Count bytes to send
177 os = ChildUpdateRequest.GetRequestStream();
178 os.Write(buffer, 0, strBuffer.Length); //Send it
179 os.Close();
180 //m_log.InfoFormat("[REST COMMS]: Posted ChildAgentUpdate request to remote sim {0}", uri);
181 }
182 //catch (WebException ex)
183 catch
184 {
185 //m_log.InfoFormat("[REST COMMS]: Bad send on ChildAgentUpdate {0}", ex.Message);
186
187 return false;
188 }
189
190 // Let's wait for the response
191 //m_log.Info("[REST COMMS]: Waiting for a reply after ChildAgentUpdate");
192
193 try
194 {
195 WebResponse webResponse = ChildUpdateRequest.GetResponse();
196 if (webResponse == null)
197 {
198 m_log.Info("[REST COMMS]: Null reply on ChilAgentUpdate post");
199 }
200
201 StreamReader sr = new StreamReader(webResponse.GetResponseStream());
202 //reply = sr.ReadToEnd().Trim();
203 sr.ReadToEnd().Trim();
204 sr.Close();
205 //m_log.InfoFormat("[REST COMMS]: ChilAgentUpdate reply was {0} ", reply);
206
207 }
208 catch (WebException ex)
209 {
210 m_log.InfoFormat("[REST COMMS]: exception on reply of ChilAgentUpdate {0}", ex.Message);
211 // ignore, really
212 }
213
214 return true;
215 }
216
217 public bool DoRetrieveRootAgentCall(RegionInfo region, UUID id, out IAgentData agent)
218 {
219 agent = null;
220 // Eventually, we want to use a caps url instead of the agentID
221 string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/agent/" + id + "/" + region.RegionHandle.ToString() + "/";
222 //Console.WriteLine(" >>> DoRetrieveRootAgentCall <<< " + uri);
223
224 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
225 request.Method = "GET";
226 request.Timeout = 10000;
227 //request.Headers.Add("authorization", ""); // coming soon
228
229 HttpWebResponse webResponse = null;
230 string reply = string.Empty;
231 try
232 {
233 webResponse = (HttpWebResponse)request.GetResponse();
234 if (webResponse == null)
235 {
236 m_log.Info("[REST COMMS]: Null reply on agent get ");
237 }
238
239 StreamReader sr = new StreamReader(webResponse.GetResponseStream());
240 reply = sr.ReadToEnd().Trim();
241 sr.Close();
242
243 //Console.WriteLine("[REST COMMS]: ChilAgentUpdate reply was " + reply);
244
245 }
246 catch (WebException ex)
247 {
248 m_log.InfoFormat("[REST COMMS]: exception on reply of agent get {0}", ex.Message);
249 // ignore, really
250 return false;
251 }
252
253 if (webResponse.StatusCode == HttpStatusCode.OK)
254 {
255 // we know it's jason
256 OSDMap args = GetOSDMap(reply);
257 if (args == null)
258 {
259 //Console.WriteLine("[REST COMMS]: Error getting OSDMap from reply");
260 return false;
261 }
262
263 agent = new CompleteAgentData();
264 agent.Unpack(args);
265 return true;
266 }
267
268 //Console.WriteLine("[REST COMMS]: DoRetrieveRootAgentCall returned status " + webResponse.StatusCode);
269 return false;
270 }
271
272 public bool DoReleaseAgentCall(ulong regionHandle, UUID id, string uri)
273 {
274 //m_log.Debug(" >>> DoReleaseAgentCall <<< " + uri);
275
276 WebRequest request = WebRequest.Create(uri);
277 request.Method = "DELETE";
278 request.Timeout = 10000;
279
280 try
281 {
282 WebResponse webResponse = request.GetResponse();
283 if (webResponse == null)
284 {
285 m_log.Info("[REST COMMS]: Null reply on agent delete ");
286 }
287
288 StreamReader sr = new StreamReader(webResponse.GetResponseStream());
289 //reply = sr.ReadToEnd().Trim();
290 sr.ReadToEnd().Trim();
291 sr.Close();
292 //m_log.InfoFormat("[REST COMMS]: ChilAgentUpdate reply was {0} ", reply);
293
294 }
295 catch (WebException ex)
296 {
297 m_log.InfoFormat("[REST COMMS]: exception on reply of agent delete {0}", ex.Message);
298 // ignore, really
299 }
300
301 return true;
302 }
303
304
305 public bool DoCloseAgentCall(RegionInfo region, UUID id)
306 {
307 string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/agent/" + id + "/" + region.RegionHandle.ToString() + "/";
308
309 //Console.WriteLine(" >>> DoCloseAgentCall <<< " + uri);
310
311 WebRequest request = WebRequest.Create(uri);
312 request.Method = "DELETE";
313 request.Timeout = 10000;
314
315 try
316 {
317 WebResponse webResponse = request.GetResponse();
318 if (webResponse == null)
319 {
320 m_log.Info("[REST COMMS]: Null reply on agent delete ");
321 }
322
323 StreamReader sr = new StreamReader(webResponse.GetResponseStream());
324 //reply = sr.ReadToEnd().Trim();
325 sr.ReadToEnd().Trim();
326 sr.Close();
327 //m_log.InfoFormat("[REST COMMS]: ChilAgentUpdate reply was {0} ", reply);
328
329 }
330 catch (WebException ex)
331 {
332 m_log.InfoFormat("[REST COMMS]: exception on reply of agent delete {0}", ex.Message);
333 // ignore, really
334 }
335
336 return true;
337 }
338
339 public bool DoCreateObjectCall(RegionInfo region, ISceneObject sog, bool allowScriptCrossing)
340 {
341 ulong regionHandle = GetRegionHandle(region.RegionHandle);
342 string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/object/" + sog.UUID + "/" + regionHandle.ToString() + "/";
343 //m_log.Debug(" >>> DoCreateChildAgentCall <<< " + uri);
344
345 WebRequest ObjectCreateRequest = WebRequest.Create(uri);
346 ObjectCreateRequest.Method = "POST";
347 ObjectCreateRequest.ContentType = "application/json";
348 ObjectCreateRequest.Timeout = 10000;
349
350 OSDMap args = new OSDMap(2);
351 args["sog"] = OSD.FromString(sog.ToXmlString2());
352 args["extra"] = OSD.FromString(sog.ExtraToXmlString());
353 if (allowScriptCrossing)
354 {
355 string state = sog.GetStateSnapshot();
356 if (state.Length > 0)
357 args["state"] = OSD.FromString(state);
358 }
359
360 string strBuffer = "";
361 byte[] buffer = new byte[1];
362 try
363 {
364 strBuffer = OSDParser.SerializeJsonString(args);
365 UTF8Encoding str = new UTF8Encoding();
366 buffer = str.GetBytes(strBuffer);
367
368 }
369 catch (Exception e)
370 {
371 m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of CreateObject: {0}", e.Message);
372 // ignore. buffer will be empty, caller should check.
373 }
374
375 Stream os = null;
376 try
377 { // send the Post
378 ObjectCreateRequest.ContentLength = buffer.Length; //Count bytes to send
379 os = ObjectCreateRequest.GetRequestStream();
380 os.Write(buffer, 0, strBuffer.Length); //Send it
381 os.Close();
382 m_log.InfoFormat("[REST COMMS]: Posted ChildAgentUpdate request to remote sim {0}", uri);
383 }
384 //catch (WebException ex)
385 catch
386 {
387 // m_log.InfoFormat("[REST COMMS]: Bad send on CreateObject {0}", ex.Message);
388
389 return false;
390 }
391
392 // Let's wait for the response
393 //m_log.Info("[REST COMMS]: Waiting for a reply after DoCreateChildAgentCall");
394
395 try
396 {
397 WebResponse webResponse = ObjectCreateRequest.GetResponse();
398 if (webResponse == null)
399 {
400 m_log.Info("[REST COMMS]: Null reply on DoCreateObjectCall post");
401 }
402
403 StreamReader sr = new StreamReader(webResponse.GetResponseStream());
404 //reply = sr.ReadToEnd().Trim();
405 sr.ReadToEnd().Trim();
406 sr.Close();
407 //m_log.InfoFormat("[REST COMMS]: DoCreateChildAgentCall reply was {0} ", reply);
408
409 }
410 catch (WebException ex)
411 {
412 m_log.InfoFormat("[REST COMMS]: exception on reply of DoCreateObjectCall {0}", ex.Message);
413 // ignore, really
414 }
415
416 return true;
417
418 }
419
420 public bool DoHelloNeighbourCall(RegionInfo region, RegionInfo thisRegion)
421 {
422 string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/region/" + thisRegion.RegionID + "/";
423 //m_log.Debug(" >>> DoHelloNeighbourCall <<< " + uri);
424
425 WebRequest HelloNeighbourRequest = WebRequest.Create(uri);
426 HelloNeighbourRequest.Method = "POST";
427 HelloNeighbourRequest.ContentType = "application/json";
428 HelloNeighbourRequest.Timeout = 10000;
429
430 // Fill it in
431 OSDMap args = null;
432 try
433 {
434 args = thisRegion.PackRegionInfoData();
435 }
436 catch (Exception e)
437 {
438 m_log.Debug("[REST COMMS]: PackRegionInfoData failed with exception: " + e.Message);
439 }
440 // Add the regionhandle of the destination region
441 ulong regionHandle = GetRegionHandle(region.RegionHandle);
442 args["destination_handle"] = OSD.FromString(regionHandle.ToString());
443
444 string strBuffer = "";
445 byte[] buffer = new byte[1];
446 try
447 {
448 strBuffer = OSDParser.SerializeJsonString(args);
449 UTF8Encoding str = new UTF8Encoding();
450 buffer = str.GetBytes(strBuffer);
451
452 }
453 catch (Exception e)
454 {
455 m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of HelloNeighbour: {0}", e.Message);
456 // ignore. buffer will be empty, caller should check.
457 }
458
459 Stream os = null;
460 try
461 { // send the Post
462 HelloNeighbourRequest.ContentLength = buffer.Length; //Count bytes to send
463 os = HelloNeighbourRequest.GetRequestStream();
464 os.Write(buffer, 0, strBuffer.Length); //Send it
465 os.Close();
466 //m_log.InfoFormat("[REST COMMS]: Posted HelloNeighbour request to remote sim {0}", uri);
467 }
468 //catch (WebException ex)
469 catch
470 {
471 //m_log.InfoFormat("[REST COMMS]: Bad send on HelloNeighbour {0}", ex.Message);
472
473 return false;
474 }
475
476 // Let's wait for the response
477 //m_log.Info("[REST COMMS]: Waiting for a reply after DoHelloNeighbourCall");
478
479 try
480 {
481 WebResponse webResponse = HelloNeighbourRequest.GetResponse();
482 if (webResponse == null)
483 {
484 m_log.Info("[REST COMMS]: Null reply on DoHelloNeighbourCall post");
485 }
486
487 StreamReader sr = new StreamReader(webResponse.GetResponseStream());
488 //reply = sr.ReadToEnd().Trim();
489 sr.ReadToEnd().Trim();
490 sr.Close();
491 //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply);
492
493 }
494 catch (WebException ex)
495 {
496 m_log.InfoFormat("[REST COMMS]: exception on reply of DoHelloNeighbourCall {0}", ex.Message);
497 // ignore, really
498 }
499
500 return true;
501
502 }
503
504 #region Hyperlinks
505
506 public virtual ulong GetRegionHandle(ulong handle)
507 {
508 return handle;
509 }
510
511 public virtual bool IsHyperlink(ulong handle)
512 {
513 return false;
514 }
515
516 public virtual void SendUserInformation(RegionInfo regInfo, AgentCircuitData aCircuit)
517 {
518 }
519
520 public virtual void AdjustUserInformation(AgentCircuitData aCircuit)
521 {
522 }
523
524 #endregion /* Hyperlinks */
525
526 public static OSDMap GetOSDMap(string data)
527 {
528 OSDMap args = null;
529 try
530 {
531 OSD buffer;
532 // We should pay attention to the content-type, but let's assume we know it's Json
533 buffer = OSDParser.DeserializeJson(data);
534 if (buffer.Type == OSDType.Map)
535 {
536 args = (OSDMap)buffer;
537 return args;
538 }
539 else
540 {
541 // uh?
542 System.Console.WriteLine("[REST COMMS]: Got OSD of type " + buffer.Type.ToString());
543 return null;
544 }
545 }
546 catch (Exception ex)
547 {
548 System.Console.WriteLine("[REST COMMS]: exception on parse of REST message " + ex.Message);
549 return null;
550 }
551 }
552
553
554 }
555}