OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
GatekeeperServiceConnector.cs
Go to the documentation of this file.
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;
30 using System.Collections.Generic;
31 using System.Drawing;
32 using System.IO;
33 using System.Net;
34 using System.Reflection;
35 using OpenSim.Framework;
36 using OpenSim.Services.Interfaces;
38 using OpenMetaverse;
39 using OpenMetaverse.Imaging;
40 using OpenMetaverse.StructuredData;
41 using Nwc.XmlRpc;
42 using log4net;
43 
44 using OpenSim.Services.Connectors.Simulation;
45 
46 namespace OpenSim.Services.Connectors.Hypergrid
47 {
49  {
50  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
51 
52  private static UUID m_HGMapImage = new UUID("00000000-0000-1111-9999-000000000013");
53 
54  private IAssetService m_AssetService;
55 
57  : base()
58  {
59  }
60 
62  {
63  m_AssetService = assService;
64  }
65 
66  protected override string AgentPath()
67  {
68  return "foreignagent/";
69  }
70 
71  protected override string ObjectPath()
72  {
73  return "foreignobject/";
74  }
75 
76  public bool LinkRegion(GridRegion info, out UUID regionID, out ulong realHandle, out string externalName, out string imageURL, out string reason)
77  {
78  regionID = UUID.Zero;
79  imageURL = string.Empty;
80  realHandle = 0;
81  externalName = string.Empty;
82  reason = string.Empty;
83 
84  Hashtable hash = new Hashtable();
85  hash["region_name"] = info.RegionName;
86 
87  IList paramList = new ArrayList();
88  paramList.Add(hash);
89 
90  XmlRpcRequest request = new XmlRpcRequest("link_region", paramList);
91  m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Linking to " + info.ServerURI);
92  XmlRpcResponse response = null;
93  try
94  {
95  response = request.Send(info.ServerURI, 10000);
96  }
97  catch (Exception e)
98  {
99  m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
100  reason = "Error contacting remote server";
101  return false;
102  }
103 
104  if (response.IsFault)
105  {
106  reason = response.FaultString;
107  m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
108  return false;
109  }
110 
111  hash = (Hashtable)response.Value;
112  //foreach (Object o in hash)
113  // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
114  try
115  {
116  bool success = false;
117  Boolean.TryParse((string)hash["result"], out success);
118  if (success)
119  {
120  UUID.TryParse((string)hash["uuid"], out regionID);
121  //m_log.Debug(">> HERE, uuid: " + regionID);
122  if ((string)hash["handle"] != null)
123  {
124  realHandle = Convert.ToUInt64((string)hash["handle"]);
125  //m_log.Debug(">> HERE, realHandle: " + realHandle);
126  }
127  if (hash["region_image"] != null)
128  {
129  imageURL = (string)hash["region_image"];
130  //m_log.Debug(">> HERE, imageURL: " + imageURL);
131  }
132  if (hash["external_name"] != null)
133  {
134  externalName = (string)hash["external_name"];
135  //m_log.Debug(">> HERE, externalName: " + externalName);
136  }
137  }
138 
139  }
140  catch (Exception e)
141  {
142  reason = "Error parsing return arguments";
143  m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
144  return false;
145  }
146 
147  return true;
148  }
149 
150  public UUID GetMapImage(UUID regionID, string imageURL, string storagePath)
151  {
152  if (m_AssetService == null)
153  {
154  m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: No AssetService defined. Map tile not retrieved.");
155  return m_HGMapImage;
156  }
157 
158  UUID mapTile = m_HGMapImage;
159  string filename = string.Empty;
160 
161  try
162  {
163  WebClient c = new WebClient();
164  //m_log.Debug("JPEG: " + imageURL);
165  string name = regionID.ToString();
166  filename = Path.Combine(storagePath, name + ".jpg");
167  m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: Map image at {0}, cached at {1}", imageURL, filename);
168  if (!File.Exists(filename))
169  {
170  m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: downloading...");
171  c.DownloadFile(imageURL, filename);
172  }
173  else
174  {
175  m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: using cached image");
176  }
177 
178  byte[] imageData = null;
179 
180  using (Bitmap bitmap = new Bitmap(filename))
181  {
182  //m_log.Debug("Size: " + m.PhysicalDimension.Height + "-" + m.PhysicalDimension.Width);
183  imageData = OpenJPEG.EncodeFromImage(bitmap, true);
184  }
185 
186  AssetBase ass = new AssetBase(UUID.Random(), "region " + name, (sbyte)AssetType.Texture, regionID.ToString());
187 
188  // !!! for now
189  //info.RegionSettings.TerrainImageID = ass.FullID;
190 
191  ass.Data = imageData;
192 
193  m_AssetService.Store(ass);
194 
195  // finally
196  mapTile = ass.FullID;
197  }
198  catch // LEGIT: Catching problems caused by OpenJPEG p/invoke
199  {
200  m_log.Info("[GATEKEEPER SERVICE CONNECTOR]: Failed getting/storing map image, because it is probably already in the cache");
201  }
202  return mapTile;
203  }
204 
205  public GridRegion GetHyperlinkRegion(GridRegion gatekeeper, UUID regionID, UUID agentID, string agentHomeURI, out string message)
206  {
207  Hashtable hash = new Hashtable();
208  hash["region_uuid"] = regionID.ToString();
209  if (agentID != UUID.Zero)
210  {
211  hash["agent_id"] = agentID.ToString();
212  if (agentHomeURI != null)
213  hash["agent_home_uri"] = agentHomeURI;
214  }
215 
216  IList paramList = new ArrayList();
217  paramList.Add(hash);
218 
219  XmlRpcRequest request = new XmlRpcRequest("get_region", paramList);
220  m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: contacting " + gatekeeper.ServerURI);
221  XmlRpcResponse response = null;
222  try
223  {
224  response = request.Send(gatekeeper.ServerURI, 10000);
225  }
226  catch (Exception e)
227  {
228  message = "Error contacting grid.";
229  m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
230  return null;
231  }
232 
233  if (response.IsFault)
234  {
235  message = "Error contacting grid.";
236  m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
237  return null;
238  }
239 
240  hash = (Hashtable)response.Value;
241  //foreach (Object o in hash)
242  // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
243  try
244  {
245  bool success = false;
246  Boolean.TryParse((string)hash["result"], out success);
247 
248  if (hash["message"] != null)
249  message = (string)hash["message"];
250  else if (success)
251  message = null;
252  else
253  message = "The teleport destination could not be found."; // probably the dest grid is old and doesn't send 'message', but the most common problem is that the region is unavailable
254 
255  if (success)
256  {
257  GridRegion region = new GridRegion();
258 
259  UUID.TryParse((string)hash["uuid"], out region.RegionID);
260  //m_log.Debug(">> HERE, uuid: " + region.RegionID);
261  int n = 0;
262  if (hash["x"] != null)
263  {
264  Int32.TryParse((string)hash["x"], out n);
265  region.RegionLocX = n;
266  //m_log.Debug(">> HERE, x: " + region.RegionLocX);
267  }
268  if (hash["y"] != null)
269  {
270  Int32.TryParse((string)hash["y"], out n);
271  region.RegionLocY = n;
272  //m_log.Debug(">> HERE, y: " + region.RegionLocY);
273  }
274  if (hash["size_x"] != null)
275  {
276  Int32.TryParse((string)hash["size_x"], out n);
277  region.RegionSizeX = n;
278  //m_log.Debug(">> HERE, x: " + region.RegionLocX);
279  }
280  if (hash["size_y"] != null)
281  {
282  Int32.TryParse((string)hash["size_y"], out n);
283  region.RegionSizeY = n;
284  //m_log.Debug(">> HERE, y: " + region.RegionLocY);
285  }
286  if (hash["region_name"] != null)
287  {
288  region.RegionName = (string)hash["region_name"];
289  //m_log.Debug(">> HERE, region_name: " + region.RegionName);
290  }
291  if (hash["hostname"] != null)
292  {
293  region.ExternalHostName = (string)hash["hostname"];
294  //m_log.Debug(">> HERE, hostname: " + region.ExternalHostName);
295  }
296  if (hash["http_port"] != null)
297  {
298  uint p = 0;
299  UInt32.TryParse((string)hash["http_port"], out p);
300  region.HttpPort = p;
301  //m_log.Debug(">> HERE, http_port: " + region.HttpPort);
302  }
303  if (hash["internal_port"] != null)
304  {
305  int p = 0;
306  Int32.TryParse((string)hash["internal_port"], out p);
307  region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
308  //m_log.Debug(">> HERE, internal_port: " + region.InternalEndPoint);
309  }
310 
311  if (hash["server_uri"] != null)
312  {
313  region.ServerURI = (string)hash["server_uri"];
314  //m_log.Debug(">> HERE, server_uri: " + region.ServerURI);
315  }
316 
317  // Successful return
318  return region;
319  }
320 
321  }
322  catch (Exception e)
323  {
324  message = "Error parsing response from grid.";
325  m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
326  return null;
327  }
328 
329  return null;
330  }
331  }
332 }
UUID GetMapImage(UUID regionID, string imageURL, string storagePath)
OpenSim.Services.Interfaces.GridRegion GridRegion
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
GridRegion GetHyperlinkRegion(GridRegion gatekeeper, UUID regionID, UUID agentID, string agentHomeURI, out string message)
bool LinkRegion(GridRegion info, out UUID regionID, out ulong realHandle, out string externalName, out string imageURL, out string reason)
Interactive OpenSim region server
Definition: OpenSim.cs:55