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