opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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;
37 using GridRegion = OpenSim.Services.Interfaces.GridRegion;
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 {
48 public class GatekeeperServiceConnector : SimulationServiceConnector
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  
56 public GatekeeperServiceConnector()
57 : base()
58 {
59 }
60  
61 public GatekeeperServiceConnector(IAssetService assService)
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 string name = regionID.ToString();
165 filename = Path.Combine(storagePath, name + ".jpg");
166 m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: Map image at {0}, cached at {1}", imageURL, filename);
167 if (!File.Exists(filename))
168 {
169 m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: downloading...");
170 c.DownloadFile(imageURL, filename);
171 }
172 else
173 {
174 m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: using cached image");
175 }
176  
177 byte[] imageData = null;
178  
179 using (Bitmap bitmap = new Bitmap(filename))
180 {
181 //m_log.Debug("Size: " + m.PhysicalDimension.Height + "-" + m.PhysicalDimension.Width);
182 imageData = OpenJPEG.EncodeFromImage(bitmap, true);
183 }
184  
185 AssetBase ass = new AssetBase(UUID.Random(), "region " + name, (sbyte)AssetType.Texture, regionID.ToString());
186  
187 // !!! for now
188 //info.RegionSettings.TerrainImageID = ass.FullID;
189  
190 ass.Data = imageData;
191  
192 mapTile = ass.FullID;
193  
194 // finally
195 m_AssetService.Store(ass);
196  
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)
206 {
207 Hashtable hash = new Hashtable();
208 hash["region_uuid"] = regionID.ToString();
209  
210 IList paramList = new ArrayList();
211 paramList.Add(hash);
212  
213 XmlRpcRequest request = new XmlRpcRequest("get_region", paramList);
214 m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: contacting " + gatekeeper.ServerURI);
215 XmlRpcResponse response = null;
216 try
217 {
218 response = request.Send(gatekeeper.ServerURI, 10000);
219 }
220 catch (Exception e)
221 {
222 m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
223 return null;
224 }
225  
226 if (response.IsFault)
227 {
228 m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
229 return null;
230 }
231  
232 hash = (Hashtable)response.Value;
233 //foreach (Object o in hash)
234 // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
235 try
236 {
237 bool success = false;
238 Boolean.TryParse((string)hash["result"], out success);
239 if (success)
240 {
241 GridRegion region = new GridRegion();
242  
243 UUID.TryParse((string)hash["uuid"], out region.RegionID);
244 //m_log.Debug(">> HERE, uuid: " + region.RegionID);
245 int n = 0;
246 if (hash["x"] != null)
247 {
248 Int32.TryParse((string)hash["x"], out n);
249 region.RegionLocX = n;
250 //m_log.Debug(">> HERE, x: " + region.RegionLocX);
251 }
252 if (hash["y"] != null)
253 {
254 Int32.TryParse((string)hash["y"], out n);
255 region.RegionLocY = n;
256 //m_log.Debug(">> HERE, y: " + region.RegionLocY);
257 }
258 if (hash["size_x"] != null)
259 {
260 Int32.TryParse((string)hash["size_x"], out n);
261 region.RegionSizeX = n;
262 //m_log.Debug(">> HERE, x: " + region.RegionLocX);
263 }
264 if (hash["size_y"] != null)
265 {
266 Int32.TryParse((string)hash["size_y"], out n);
267 region.RegionSizeY = n;
268 //m_log.Debug(">> HERE, y: " + region.RegionLocY);
269 }
270 if (hash["region_name"] != null)
271 {
272 region.RegionName = (string)hash["region_name"];
273 //m_log.Debug(">> HERE, region_name: " + region.RegionName);
274 }
275 if (hash["hostname"] != null)
276 {
277 region.ExternalHostName = (string)hash["hostname"];
278 //m_log.Debug(">> HERE, hostname: " + region.ExternalHostName);
279 }
280 if (hash["http_port"] != null)
281 {
282 uint p = 0;
283 UInt32.TryParse((string)hash["http_port"], out p);
284 region.HttpPort = p;
285 //m_log.Debug(">> HERE, http_port: " + region.HttpPort);
286 }
287 if (hash["internal_port"] != null)
288 {
289 int p = 0;
290 Int32.TryParse((string)hash["internal_port"], out p);
291 region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
292 //m_log.Debug(">> HERE, internal_port: " + region.InternalEndPoint);
293 }
294  
295 if (hash["server_uri"] != null)
296 {
297 region.ServerURI = (string)hash["server_uri"];
298 //m_log.Debug(">> HERE, server_uri: " + region.ServerURI);
299 }
300  
301 // Successful return
302 return region;
303 }
304  
305 }
306 catch (Exception e)
307 {
308 m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
309 return null;
310 }
311  
312 return null;
313 }
314 }
315 }