diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Services/Connectors/Hypergrid/HypergridServiceConnector.cs | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/Hypergrid/HypergridServiceConnector.cs b/OpenSim/Services/Connectors/Hypergrid/HypergridServiceConnector.cs new file mode 100644 index 0000000..460acae --- /dev/null +++ b/OpenSim/Services/Connectors/Hypergrid/HypergridServiceConnector.cs | |||
@@ -0,0 +1,245 @@ | |||
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.Text; | ||
32 | using System.Drawing; | ||
33 | using System.Net; | ||
34 | using System.Reflection; | ||
35 | using OpenSim.Services.Interfaces; | ||
36 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
37 | |||
38 | using OpenSim.Framework; | ||
39 | |||
40 | using OpenMetaverse; | ||
41 | using OpenMetaverse.Imaging; | ||
42 | using log4net; | ||
43 | using Nwc.XmlRpc; | ||
44 | |||
45 | namespace OpenSim.Services.Connectors.Hypergrid | ||
46 | { | ||
47 | public class HypergridServiceConnector | ||
48 | { | ||
49 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
50 | |||
51 | private IAssetService m_AssetService; | ||
52 | |||
53 | public HypergridServiceConnector(IAssetService assService) | ||
54 | { | ||
55 | m_AssetService = assService; | ||
56 | } | ||
57 | |||
58 | public bool LinkRegion(GridRegion info, out UUID regionID, out ulong realHandle, out string imageURL, out string reason) | ||
59 | { | ||
60 | regionID = UUID.Zero; | ||
61 | imageURL = string.Empty; | ||
62 | realHandle = 0; | ||
63 | reason = string.Empty; | ||
64 | |||
65 | Hashtable hash = new Hashtable(); | ||
66 | hash["region_name"] = info.RegionName; | ||
67 | |||
68 | IList paramList = new ArrayList(); | ||
69 | paramList.Add(hash); | ||
70 | |||
71 | XmlRpcRequest request = new XmlRpcRequest("link_region", paramList); | ||
72 | string uri = "http://" + info.ExternalEndPoint.Address + ":" + info.HttpPort + "/"; | ||
73 | m_log.Debug("[HGrid]: Linking to " + uri); | ||
74 | XmlRpcResponse response = null; | ||
75 | try | ||
76 | { | ||
77 | response = request.Send(uri, 10000); | ||
78 | } | ||
79 | catch (Exception e) | ||
80 | { | ||
81 | m_log.Debug("[HGrid]: Exception " + e.Message); | ||
82 | reason = "Error contacting remote server"; | ||
83 | return false; | ||
84 | } | ||
85 | |||
86 | if (response.IsFault) | ||
87 | { | ||
88 | reason = response.FaultString; | ||
89 | m_log.ErrorFormat("[HGrid]: remote call returned an error: {0}", response.FaultString); | ||
90 | return false; | ||
91 | } | ||
92 | |||
93 | hash = (Hashtable)response.Value; | ||
94 | //foreach (Object o in hash) | ||
95 | // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value); | ||
96 | try | ||
97 | { | ||
98 | bool success = false; | ||
99 | Boolean.TryParse((string)hash["result"], out success); | ||
100 | if (success) | ||
101 | { | ||
102 | UUID.TryParse((string)hash["uuid"], out regionID); | ||
103 | //m_log.Debug(">> HERE, uuid: " + uuid); | ||
104 | if ((string)hash["handle"] != null) | ||
105 | { | ||
106 | realHandle = Convert.ToUInt64((string)hash["handle"]); | ||
107 | //m_log.Debug(">> HERE, realHandle: " + realHandle); | ||
108 | } | ||
109 | if (hash["region_image"] != null) | ||
110 | { | ||
111 | imageURL = (string)hash["region_image"]; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | } | ||
116 | catch (Exception e) | ||
117 | { | ||
118 | reason = "Error parsing return arguments"; | ||
119 | m_log.Error("[HGrid]: Got exception while parsing hyperlink response " + e.StackTrace); | ||
120 | return false; | ||
121 | } | ||
122 | |||
123 | return true; | ||
124 | } | ||
125 | |||
126 | public UUID GetMapImage(UUID regionID, string imageURL) | ||
127 | { | ||
128 | try | ||
129 | { | ||
130 | |||
131 | WebClient c = new WebClient(); | ||
132 | //m_log.Debug("JPEG: " + uri); | ||
133 | string filename = regionID.ToString(); | ||
134 | c.DownloadFile(imageURL, filename + ".jpg"); | ||
135 | Bitmap m = new Bitmap(filename + ".jpg"); | ||
136 | //m_log.Debug("Size: " + m.PhysicalDimension.Height + "-" + m.PhysicalDimension.Width); | ||
137 | byte[] imageData = OpenJPEG.EncodeFromImage(m, true); | ||
138 | AssetBase ass = new AssetBase(UUID.Random(), "region " + filename, (sbyte)AssetType.Texture); | ||
139 | |||
140 | // !!! for now | ||
141 | //info.RegionSettings.TerrainImageID = ass.FullID; | ||
142 | |||
143 | ass.Temporary = true; | ||
144 | ass.Local = true; | ||
145 | ass.Data = imageData; | ||
146 | |||
147 | m_AssetService.Store(ass); | ||
148 | |||
149 | // finally | ||
150 | return ass.FullID; | ||
151 | |||
152 | } | ||
153 | catch // LEGIT: Catching problems caused by OpenJPEG p/invoke | ||
154 | { | ||
155 | m_log.Warn("[HGrid]: Failed getting/storing map image, because it is probably already in the cache"); | ||
156 | } | ||
157 | return UUID.Zero; | ||
158 | } | ||
159 | |||
160 | public GridRegion GetHyperlinkRegion(GridRegion gatekeeper, UUID regionID) | ||
161 | { | ||
162 | Hashtable hash = new Hashtable(); | ||
163 | hash["region_uuid"] = regionID.ToString(); | ||
164 | |||
165 | IList paramList = new ArrayList(); | ||
166 | paramList.Add(hash); | ||
167 | |||
168 | XmlRpcRequest request = new XmlRpcRequest("get_region", paramList); | ||
169 | string uri = "http://" + gatekeeper.ExternalEndPoint.Address + ":" + gatekeeper.HttpPort + "/"; | ||
170 | m_log.Debug("[HGrid]: contacting " + uri); | ||
171 | XmlRpcResponse response = null; | ||
172 | try | ||
173 | { | ||
174 | response = request.Send(uri, 10000); | ||
175 | } | ||
176 | catch (Exception e) | ||
177 | { | ||
178 | m_log.Debug("[HGrid]: Exception " + e.Message); | ||
179 | return null; | ||
180 | } | ||
181 | |||
182 | if (response.IsFault) | ||
183 | { | ||
184 | m_log.ErrorFormat("[HGrid]: remote call returned an error: {0}", response.FaultString); | ||
185 | return null; | ||
186 | } | ||
187 | |||
188 | hash = (Hashtable)response.Value; | ||
189 | //foreach (Object o in hash) | ||
190 | // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value); | ||
191 | try | ||
192 | { | ||
193 | bool success = false; | ||
194 | Boolean.TryParse((string)hash["result"], out success); | ||
195 | if (success) | ||
196 | { | ||
197 | GridRegion region = new GridRegion(); | ||
198 | |||
199 | UUID.TryParse((string)hash["uuid"], out region.RegionID); | ||
200 | //m_log.Debug(">> HERE, uuid: " + uuid); | ||
201 | int n = 0; | ||
202 | if (hash["x"] != null) | ||
203 | { | ||
204 | Int32.TryParse((string)hash["x"], out n); | ||
205 | region.RegionLocX = n; | ||
206 | } | ||
207 | if (hash["y"] != null) | ||
208 | { | ||
209 | Int32.TryParse((string)hash["y"], out n); | ||
210 | region.RegionLocY = n; | ||
211 | } | ||
212 | if (hash["region_name"] != null) | ||
213 | { | ||
214 | region.RegionName = (string)hash["region_name"]; | ||
215 | } | ||
216 | if (hash["hostname"] != null) | ||
217 | region.ExternalHostName = (string)hash["hostname"]; | ||
218 | if (hash["http_port"] != null) | ||
219 | { | ||
220 | uint p = 0; | ||
221 | UInt32.TryParse((string)hash["http_port"], out p); | ||
222 | region.HttpPort = p; | ||
223 | } | ||
224 | if (hash["internal_port"] != null) | ||
225 | { | ||
226 | int p = 0; | ||
227 | Int32.TryParse((string)hash["internal_port"], out p); | ||
228 | region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p); | ||
229 | } | ||
230 | |||
231 | // Successful return | ||
232 | return region; | ||
233 | } | ||
234 | |||
235 | } | ||
236 | catch (Exception e) | ||
237 | { | ||
238 | m_log.Error("[HGrid]: Got exception while parsing hyperlink response " + e.StackTrace); | ||
239 | return null; | ||
240 | } | ||
241 | |||
242 | return null; | ||
243 | } | ||
244 | } | ||
245 | } | ||