diff options
Diffstat (limited to 'OpenSim/Services/Connectors/Hypergrid/UserAgentServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/Hypergrid/UserAgentServiceConnector.cs | 397 |
1 files changed, 397 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/Hypergrid/UserAgentServiceConnector.cs b/OpenSim/Services/Connectors/Hypergrid/UserAgentServiceConnector.cs new file mode 100644 index 0000000..3e91e3a --- /dev/null +++ b/OpenSim/Services/Connectors/Hypergrid/UserAgentServiceConnector.cs | |||
@@ -0,0 +1,397 @@ | |||
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.IO; | ||
32 | using System.Net; | ||
33 | using System.Reflection; | ||
34 | using System.Text; | ||
35 | |||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Services.Interfaces; | ||
38 | using OpenSim.Services.Connectors.Simulation; | ||
39 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
40 | |||
41 | using OpenMetaverse; | ||
42 | using OpenMetaverse.StructuredData; | ||
43 | using log4net; | ||
44 | using Nwc.XmlRpc; | ||
45 | using Nini.Config; | ||
46 | |||
47 | namespace OpenSim.Services.Connectors.Hypergrid | ||
48 | { | ||
49 | public class UserAgentServiceConnector : IUserAgentService | ||
50 | { | ||
51 | private static readonly ILog m_log = | ||
52 | LogManager.GetLogger( | ||
53 | MethodBase.GetCurrentMethod().DeclaringType); | ||
54 | |||
55 | string m_ServerURL; | ||
56 | public UserAgentServiceConnector(string url) | ||
57 | { | ||
58 | m_ServerURL = url; | ||
59 | } | ||
60 | |||
61 | public UserAgentServiceConnector(IConfigSource config) | ||
62 | { | ||
63 | } | ||
64 | |||
65 | public bool LoginAgentToGrid(AgentCircuitData aCircuit, GridRegion gatekeeper, GridRegion destination, out string reason) | ||
66 | { | ||
67 | reason = String.Empty; | ||
68 | |||
69 | if (destination == null) | ||
70 | { | ||
71 | reason = "Destination is null"; | ||
72 | m_log.Debug("[USER AGENT CONNECTOR]: Given destination is null"); | ||
73 | return false; | ||
74 | } | ||
75 | |||
76 | string uri = m_ServerURL + "/homeagent/" + aCircuit.AgentID + "/"; | ||
77 | |||
78 | Console.WriteLine(" >>> LoginAgentToGrid <<< " + uri); | ||
79 | |||
80 | HttpWebRequest AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri); | ||
81 | AgentCreateRequest.Method = "POST"; | ||
82 | AgentCreateRequest.ContentType = "application/json"; | ||
83 | AgentCreateRequest.Timeout = 10000; | ||
84 | //AgentCreateRequest.KeepAlive = false; | ||
85 | //AgentCreateRequest.Headers.Add("Authorization", authKey); | ||
86 | |||
87 | // Fill it in | ||
88 | OSDMap args = PackCreateAgentArguments(aCircuit, gatekeeper, destination); | ||
89 | |||
90 | string strBuffer = ""; | ||
91 | byte[] buffer = new byte[1]; | ||
92 | try | ||
93 | { | ||
94 | strBuffer = OSDParser.SerializeJsonString(args); | ||
95 | Encoding str = Util.UTF8; | ||
96 | buffer = str.GetBytes(strBuffer); | ||
97 | |||
98 | } | ||
99 | catch (Exception e) | ||
100 | { | ||
101 | m_log.WarnFormat("[USER AGENT CONNECTOR]: Exception thrown on serialization of ChildCreate: {0}", e.Message); | ||
102 | // ignore. buffer will be empty, caller should check. | ||
103 | } | ||
104 | |||
105 | Stream os = null; | ||
106 | try | ||
107 | { // send the Post | ||
108 | AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send | ||
109 | os = AgentCreateRequest.GetRequestStream(); | ||
110 | os.Write(buffer, 0, strBuffer.Length); //Send it | ||
111 | m_log.InfoFormat("[USER AGENT CONNECTOR]: Posted CreateAgent request to remote sim {0}, region {1}, x={2} y={3}", | ||
112 | uri, destination.RegionName, destination.RegionLocX, destination.RegionLocY); | ||
113 | } | ||
114 | //catch (WebException ex) | ||
115 | catch | ||
116 | { | ||
117 | //m_log.InfoFormat("[USER AGENT CONNECTOR]: Bad send on ChildAgentUpdate {0}", ex.Message); | ||
118 | reason = "cannot contact remote region"; | ||
119 | return false; | ||
120 | } | ||
121 | finally | ||
122 | { | ||
123 | if (os != null) | ||
124 | os.Close(); | ||
125 | } | ||
126 | |||
127 | // Let's wait for the response | ||
128 | //m_log.Info("[USER AGENT CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall"); | ||
129 | |||
130 | WebResponse webResponse = null; | ||
131 | StreamReader sr = null; | ||
132 | try | ||
133 | { | ||
134 | webResponse = AgentCreateRequest.GetResponse(); | ||
135 | if (webResponse == null) | ||
136 | { | ||
137 | m_log.Info("[USER AGENT CONNECTOR]: Null reply on DoCreateChildAgentCall post"); | ||
138 | } | ||
139 | else | ||
140 | { | ||
141 | |||
142 | sr = new StreamReader(webResponse.GetResponseStream()); | ||
143 | string response = sr.ReadToEnd().Trim(); | ||
144 | m_log.InfoFormat("[USER AGENT CONNECTOR]: DoCreateChildAgentCall reply was {0} ", response); | ||
145 | |||
146 | if (!String.IsNullOrEmpty(response)) | ||
147 | { | ||
148 | try | ||
149 | { | ||
150 | // we assume we got an OSDMap back | ||
151 | OSDMap r = Util.GetOSDMap(response); | ||
152 | bool success = r["success"].AsBoolean(); | ||
153 | reason = r["reason"].AsString(); | ||
154 | return success; | ||
155 | } | ||
156 | catch (NullReferenceException e) | ||
157 | { | ||
158 | m_log.InfoFormat("[USER AGENT CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", e.Message); | ||
159 | |||
160 | // check for old style response | ||
161 | if (response.ToLower().StartsWith("true")) | ||
162 | return true; | ||
163 | |||
164 | return false; | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | } | ||
169 | catch (WebException ex) | ||
170 | { | ||
171 | m_log.InfoFormat("[USER AGENT CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", ex.Message); | ||
172 | reason = "Destination did not reply"; | ||
173 | return false; | ||
174 | } | ||
175 | finally | ||
176 | { | ||
177 | if (sr != null) | ||
178 | sr.Close(); | ||
179 | } | ||
180 | |||
181 | return true; | ||
182 | |||
183 | } | ||
184 | |||
185 | protected OSDMap PackCreateAgentArguments(AgentCircuitData aCircuit, GridRegion gatekeeper, GridRegion destination) | ||
186 | { | ||
187 | OSDMap args = null; | ||
188 | try | ||
189 | { | ||
190 | args = aCircuit.PackAgentCircuitData(); | ||
191 | } | ||
192 | catch (Exception e) | ||
193 | { | ||
194 | m_log.Debug("[USER AGENT CONNECTOR]: PackAgentCircuitData failed with exception: " + e.Message); | ||
195 | } | ||
196 | // Add the input arguments | ||
197 | args["gatekeeper_host"] = OSD.FromString(gatekeeper.ExternalHostName); | ||
198 | args["gatekeeper_port"] = OSD.FromString(gatekeeper.HttpPort.ToString()); | ||
199 | args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString()); | ||
200 | args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString()); | ||
201 | args["destination_name"] = OSD.FromString(destination.RegionName); | ||
202 | args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString()); | ||
203 | |||
204 | return args; | ||
205 | } | ||
206 | |||
207 | public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt) | ||
208 | { | ||
209 | position = Vector3.UnitY; lookAt = Vector3.UnitY; | ||
210 | |||
211 | Hashtable hash = new Hashtable(); | ||
212 | hash["userID"] = userID.ToString(); | ||
213 | |||
214 | IList paramList = new ArrayList(); | ||
215 | paramList.Add(hash); | ||
216 | |||
217 | XmlRpcRequest request = new XmlRpcRequest("get_home_region", paramList); | ||
218 | XmlRpcResponse response = null; | ||
219 | try | ||
220 | { | ||
221 | response = request.Send(m_ServerURL, 10000); | ||
222 | } | ||
223 | catch (Exception e) | ||
224 | { | ||
225 | return null; | ||
226 | } | ||
227 | |||
228 | if (response.IsFault) | ||
229 | { | ||
230 | return null; | ||
231 | } | ||
232 | |||
233 | hash = (Hashtable)response.Value; | ||
234 | //foreach (Object o in hash) | ||
235 | // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value); | ||
236 | try | ||
237 | { | ||
238 | bool success = false; | ||
239 | Boolean.TryParse((string)hash["result"], out success); | ||
240 | if (success) | ||
241 | { | ||
242 | GridRegion region = new GridRegion(); | ||
243 | |||
244 | UUID.TryParse((string)hash["uuid"], out region.RegionID); | ||
245 | //m_log.Debug(">> HERE, uuid: " + region.RegionID); | ||
246 | int n = 0; | ||
247 | if (hash["x"] != null) | ||
248 | { | ||
249 | Int32.TryParse((string)hash["x"], out n); | ||
250 | region.RegionLocX = n; | ||
251 | //m_log.Debug(">> HERE, x: " + region.RegionLocX); | ||
252 | } | ||
253 | if (hash["y"] != null) | ||
254 | { | ||
255 | Int32.TryParse((string)hash["y"], out n); | ||
256 | region.RegionLocY = n; | ||
257 | //m_log.Debug(">> HERE, y: " + region.RegionLocY); | ||
258 | } | ||
259 | if (hash["region_name"] != null) | ||
260 | { | ||
261 | region.RegionName = (string)hash["region_name"]; | ||
262 | //m_log.Debug(">> HERE, name: " + region.RegionName); | ||
263 | } | ||
264 | if (hash["hostname"] != null) | ||
265 | region.ExternalHostName = (string)hash["hostname"]; | ||
266 | if (hash["http_port"] != null) | ||
267 | { | ||
268 | uint p = 0; | ||
269 | UInt32.TryParse((string)hash["http_port"], out p); | ||
270 | region.HttpPort = p; | ||
271 | } | ||
272 | if (hash["internal_port"] != null) | ||
273 | { | ||
274 | int p = 0; | ||
275 | Int32.TryParse((string)hash["internal_port"], out p); | ||
276 | region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p); | ||
277 | } | ||
278 | if (hash["position"] != null) | ||
279 | Vector3.TryParse((string)hash["position"], out position); | ||
280 | if (hash["lookAt"] != null) | ||
281 | Vector3.TryParse((string)hash["lookAt"], out lookAt); | ||
282 | |||
283 | // Successful return | ||
284 | return region; | ||
285 | } | ||
286 | |||
287 | } | ||
288 | catch (Exception e) | ||
289 | { | ||
290 | return null; | ||
291 | } | ||
292 | |||
293 | return null; | ||
294 | |||
295 | } | ||
296 | |||
297 | public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName) | ||
298 | { | ||
299 | Hashtable hash = new Hashtable(); | ||
300 | hash["sessionID"] = sessionID.ToString(); | ||
301 | hash["externalName"] = thisGridExternalName; | ||
302 | |||
303 | IList paramList = new ArrayList(); | ||
304 | paramList.Add(hash); | ||
305 | |||
306 | XmlRpcRequest request = new XmlRpcRequest("agent_is_coming_home", paramList); | ||
307 | string reason = string.Empty; | ||
308 | return GetBoolResponse(request, out reason); | ||
309 | } | ||
310 | |||
311 | public bool VerifyAgent(UUID sessionID, string token) | ||
312 | { | ||
313 | Hashtable hash = new Hashtable(); | ||
314 | hash["sessionID"] = sessionID.ToString(); | ||
315 | hash["token"] = token; | ||
316 | |||
317 | IList paramList = new ArrayList(); | ||
318 | paramList.Add(hash); | ||
319 | |||
320 | XmlRpcRequest request = new XmlRpcRequest("verify_agent", paramList); | ||
321 | string reason = string.Empty; | ||
322 | return GetBoolResponse(request, out reason); | ||
323 | } | ||
324 | |||
325 | public bool VerifyClient(UUID sessionID, string token) | ||
326 | { | ||
327 | Hashtable hash = new Hashtable(); | ||
328 | hash["sessionID"] = sessionID.ToString(); | ||
329 | hash["token"] = token; | ||
330 | |||
331 | IList paramList = new ArrayList(); | ||
332 | paramList.Add(hash); | ||
333 | |||
334 | XmlRpcRequest request = new XmlRpcRequest("verify_client", paramList); | ||
335 | string reason = string.Empty; | ||
336 | return GetBoolResponse(request, out reason); | ||
337 | } | ||
338 | |||
339 | public void LogoutAgent(UUID userID, UUID sessionID) | ||
340 | { | ||
341 | Hashtable hash = new Hashtable(); | ||
342 | hash["sessionID"] = sessionID.ToString(); | ||
343 | hash["userID"] = userID.ToString(); | ||
344 | |||
345 | IList paramList = new ArrayList(); | ||
346 | paramList.Add(hash); | ||
347 | |||
348 | XmlRpcRequest request = new XmlRpcRequest("logout_agent", paramList); | ||
349 | string reason = string.Empty; | ||
350 | GetBoolResponse(request, out reason); | ||
351 | } | ||
352 | |||
353 | |||
354 | private bool GetBoolResponse(XmlRpcRequest request, out string reason) | ||
355 | { | ||
356 | //m_log.Debug("[HGrid]: Linking to " + uri); | ||
357 | XmlRpcResponse response = null; | ||
358 | try | ||
359 | { | ||
360 | response = request.Send(m_ServerURL, 10000); | ||
361 | } | ||
362 | catch (Exception e) | ||
363 | { | ||
364 | m_log.Debug("[USER AGENT CONNECTOR]: Unable to contact remote server "); | ||
365 | reason = "Exception: " + e.Message; | ||
366 | return false; | ||
367 | } | ||
368 | |||
369 | if (response.IsFault) | ||
370 | { | ||
371 | m_log.ErrorFormat("[HGrid]: remote call returned an error: {0}", response.FaultString); | ||
372 | reason = "XMLRPC Fault"; | ||
373 | return false; | ||
374 | } | ||
375 | |||
376 | Hashtable hash = (Hashtable)response.Value; | ||
377 | //foreach (Object o in hash) | ||
378 | // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value); | ||
379 | try | ||
380 | { | ||
381 | bool success = false; | ||
382 | reason = string.Empty; | ||
383 | Boolean.TryParse((string)hash["result"], out success); | ||
384 | |||
385 | return success; | ||
386 | } | ||
387 | catch (Exception e) | ||
388 | { | ||
389 | m_log.Error("[HGrid]: Got exception while parsing GetEndPoint response " + e.StackTrace); | ||
390 | reason = "Exception: " + e.Message; | ||
391 | return false; | ||
392 | } | ||
393 | |||
394 | } | ||
395 | |||
396 | } | ||
397 | } | ||