aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorMic Bowman2010-12-29 20:47:51 -0800
committerMic Bowman2010-12-29 20:47:51 -0800
commitdf5e4a1e5b8ef0f02b6b796a1f3fd72671a81a79 (patch)
tree479f3f3bff6d036e28176e308d0229884ed6afbb
parentFix up a prior fix (refix the fixed fix :) (diff)
downloadopensim-SC-df5e4a1e5b8ef0f02b6b796a1f3fd72671a81a79.zip
opensim-SC-df5e4a1e5b8ef0f02b6b796a1f3fd72671a81a79.tar.gz
opensim-SC-df5e4a1e5b8ef0f02b6b796a1f3fd72671a81a79.tar.bz2
opensim-SC-df5e4a1e5b8ef0f02b6b796a1f3fd72671a81a79.tar.xz
Standardize the way WebRequests are made in the SimulationServiceConnector. Added
debugging calls for tracking performance of web requests.
-rw-r--r--OpenSim/Framework/WebUtil.cs168
-rw-r--r--OpenSim/Services/Connectors/Hypergrid/GatekeeperServiceConnector.cs64
-rw-r--r--OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs553
3 files changed, 313 insertions, 472 deletions
diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs
index 1c856af..1cd9054 100644
--- a/OpenSim/Framework/WebUtil.cs
+++ b/OpenSim/Framework/WebUtil.cs
@@ -50,6 +50,8 @@ namespace OpenSim.Framework
50 LogManager.GetLogger( 50 LogManager.GetLogger(
51 MethodBase.GetCurrentMethod().DeclaringType); 51 MethodBase.GetCurrentMethod().DeclaringType);
52 52
53 private static int m_requestNumber = 0;
54
53 /// <summary> 55 /// <summary>
54 /// Send LLSD to an HTTP client in application/llsd+json form 56 /// Send LLSD to an HTTP client in application/llsd+json form
55 /// </summary> 57 /// </summary>
@@ -123,12 +125,145 @@ namespace OpenSim.Framework
123 } 125 }
124 126
125 /// <summary> 127 /// <summary>
128 /// PUT JSON-encoded data to a web service that returns LLSD or
129 /// JSON data
130 /// </summary>
131 public static OSDMap PutToService(string url, OSDMap data)
132 {
133 return ServiceOSDRequest(url,data,"PUT",10000);
134 }
135
136 public static OSDMap PostToService(string url, OSDMap data)
137 {
138 return ServiceOSDRequest(url,data,"POST",10000);
139 }
140
141 public static OSDMap GetFromService(string url)
142 {
143 return ServiceOSDRequest(url,null,"GET",10000);
144 }
145
146 public static OSDMap ServiceOSDRequest(string url, OSDMap data, string method, int timeout)
147 {
148 int reqnum = m_requestNumber++;
149 m_log.WarnFormat("[WEB UTIL]: <{0}> start osd request for {1}, method {2}",reqnum,url,method);
150
151 string errorMessage = "unknown error";
152 int tickstart = Util.EnvironmentTickCount();
153 try
154 {
155 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
156 request.Method = method;
157 request.Timeout = timeout;
158 //request.KeepAlive = false;
159
160 // If there is some input, write it into the request
161 if (data != null)
162 {
163 string strBuffer = OSDParser.SerializeJsonString(data);
164 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strBuffer);
165
166 request.ContentType = "application/json";
167 request.ContentLength = buffer.Length; //Count bytes to send
168 using (Stream requestStream = request.GetRequestStream())
169 {
170 requestStream.Write(buffer, 0, strBuffer.Length); //Send it
171 }
172 }
173
174 using (WebResponse webResponse = request.GetResponse())
175 {
176 using (Stream responseStream = webResponse.GetResponseStream())
177 {
178 string responseStr = null;
179 responseStr = responseStream.GetStreamString();
180 m_log.WarnFormat("[WEB UTIL]: <{0}> response is <{1}>",reqnum,responseStr);
181 return CanonicalizeResults(responseStr);
182 }
183 }
184 }
185 catch (WebException we)
186 {
187 errorMessage = we.Message;
188 if (we.Status == WebExceptionStatus.ProtocolError)
189 {
190 HttpWebResponse webResponse = (HttpWebResponse)we.Response;
191 errorMessage = String.Format("[{0}] {1}",webResponse.StatusCode,webResponse.StatusDescription);
192 }
193 }
194 catch (Exception ex)
195 {
196 errorMessage = ex.Message;
197 }
198 finally
199 {
200 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
201 if (tickdiff > 100)
202 m_log.WarnFormat("[WEB UTIL]: request <{0}> took {1} milliseconds",reqnum,tickdiff);
203 }
204
205 m_log.WarnFormat("[WEB UTIL] <{0}> request failed: {1}",reqnum,errorMessage);
206 return ErrorResponseMap(errorMessage);
207 }
208
209 /// <summary>
210 /// Since there are no consistencies in the way web requests are
211 /// formed, we need to do a little guessing about the result format.
212 /// Keys:
213 /// Success|success == the success fail of the request
214 /// _RawResult == the raw string that came back
215 /// _Result == the OSD unpacked string
216 /// </summary>
217 private static OSDMap CanonicalizeResults(string response)
218 {
219 OSDMap result = new OSDMap();
220
221 // Default values
222 result["Success"] = OSD.FromBoolean(true);
223 result["success"] = OSD.FromBoolean(true);
224 result["_RawResult"] = OSD.FromString(response);
225 result["_Result"] = new OSDMap();
226
227 if (response.Equals("true",System.StringComparison.OrdinalIgnoreCase))
228 return result;
229
230 if (response.Equals("false",System.StringComparison.OrdinalIgnoreCase))
231 {
232 result["Success"] = OSD.FromBoolean(false);
233 result["success"] = OSD.FromBoolean(false);
234 return result;
235 }
236
237 try
238 {
239 OSD responseOSD = OSDParser.Deserialize(response);
240 if (responseOSD.Type == OSDType.Map)
241 {
242 result["_Result"] = (OSDMap)responseOSD;
243 return result;
244 }
245 }
246 catch (Exception e)
247 {
248 // don't need to treat this as an error... we're just guessing anyway
249 m_log.DebugFormat("[WEB UTIL] couldn't decode result: <{0}>",response);
250 }
251
252 return result;
253 }
254
255 /// <summary>
126 /// POST URL-encoded form data to a web service that returns LLSD or 256 /// POST URL-encoded form data to a web service that returns LLSD or
127 /// JSON data 257 /// JSON data
128 /// </summary> 258 /// </summary>
129 public static OSDMap PostToService(string url, NameValueCollection data) 259 public static OSDMap PostToService(string url, NameValueCollection data)
130 { 260 {
261 int reqnum = m_requestNumber++;
262 string method = data["RequestMethod"] != null ? data["RequestMethod"] : "unknown";
263 m_log.WarnFormat("[WEB UTIL]: <{0}> start form request for {1}, method {2}",reqnum,url,method);
264
131 string errorMessage; 265 string errorMessage;
266 int tickstart = Util.EnvironmentTickCount();
132 267
133 try 268 try
134 { 269 {
@@ -139,7 +274,7 @@ namespace OpenSim.Framework
139 request.Method = "POST"; 274 request.Method = "POST";
140 request.ContentLength = requestData.Length; 275 request.ContentLength = requestData.Length;
141 request.ContentType = "application/x-www-form-urlencoded"; 276 request.ContentType = "application/x-www-form-urlencoded";
142 277
143 Stream requestStream = request.GetRequestStream(); 278 Stream requestStream = request.GetRequestStream();
144 requestStream.Write(requestData, 0, requestData.Length); 279 requestStream.Write(requestData, 0, requestData.Length);
145 requestStream.Close(); 280 requestStream.Close();
@@ -169,15 +304,42 @@ namespace OpenSim.Framework
169 } 304 }
170 } 305 }
171 } 306 }
307 catch (WebException we)
308 {
309 errorMessage = we.Message;
310 if (we.Status == WebExceptionStatus.ProtocolError)
311 {
312 HttpWebResponse webResponse = (HttpWebResponse)we.Response;
313 errorMessage = String.Format("[{0}] {1}",webResponse.StatusCode,webResponse.StatusDescription);
314 }
315 }
172 catch (Exception ex) 316 catch (Exception ex)
173 { 317 {
174 m_log.Warn("POST to URL " + url + " failed: " + ex);
175 errorMessage = ex.Message; 318 errorMessage = ex.Message;
176 } 319 }
320 finally
321 {
322 int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
323 if (tickdiff > 100)
324 m_log.WarnFormat("[WEB UTIL]: request <{0}> took {1} milliseconds",reqnum,tickdiff);
325 }
177 326
178 return new OSDMap { { "Message", OSD.FromString("Service request failed. " + errorMessage) } }; 327 m_log.WarnFormat("[WEB UTIL]: <{0}> request failed: {1}",reqnum,errorMessage);
328 return ErrorResponseMap(errorMessage);
179 } 329 }
180 330
331 /// <summary>
332 /// Create a response map for an error, trying to keep
333 /// the result formats consistent
334 /// </summary>
335 private static OSDMap ErrorResponseMap(string msg)
336 {
337 OSDMap result = new OSDMap();
338 result["Success"] = "False";
339 result["Message"] = OSD.FromString("Service request failed: " + msg);
340 return result;
341 }
342
181 #region Uri 343 #region Uri
182 344
183 /// <summary> 345 /// <summary>
diff --git a/OpenSim/Services/Connectors/Hypergrid/GatekeeperServiceConnector.cs b/OpenSim/Services/Connectors/Hypergrid/GatekeeperServiceConnector.cs
index a1d9167..6c69fec 100644
--- a/OpenSim/Services/Connectors/Hypergrid/GatekeeperServiceConnector.cs
+++ b/OpenSim/Services/Connectors/Hypergrid/GatekeeperServiceConnector.cs
@@ -283,46 +283,52 @@ namespace OpenSim.Services.Connectors.Hypergrid
283 283
284 public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string myipaddress, out string reason) 284 public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string myipaddress, out string reason)
285 { 285 {
286 HttpWebRequest AgentCreateRequest = null; 286 m_log.WarnFormat("[GATEKEEPER SERVICE CONNECTOR]: CreateAgent start");
287
287 myipaddress = String.Empty; 288 myipaddress = String.Empty;
288 reason = String.Empty; 289 reason = String.Empty;
289 290
290 if (SendRequest(destination, aCircuit, flags, out reason, out AgentCreateRequest)) 291 if (destination == null)
291 { 292 {
292 string response = GetResponse(AgentCreateRequest, out reason); 293 m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Given destination is null");
293 bool success = true; 294 return false;
294 UnpackResponse(response, out success, out reason, out myipaddress);
295 return success;
296 } 295 }
297 296
298 return false; 297 string uri = destination.ServerURI + AgentPath() + aCircuit.AgentID + "/";
299 }
300
301 protected void UnpackResponse(string response, out bool result, out string reason, out string ipaddress)
302 {
303 result = true;
304 reason = string.Empty;
305 ipaddress = string.Empty;
306 298
307 if (!String.IsNullOrEmpty(response)) 299 try
308 { 300 {
309 try 301 OSDMap args = aCircuit.PackAgentCircuitData();
310 { 302
311 // we assume we got an OSDMap back 303 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
312 OSDMap r = Util.GetOSDMap(response); 304 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
313 result = r["success"].AsBoolean(); 305 args["destination_name"] = OSD.FromString(destination.RegionName);
314 reason = r["reason"].AsString(); 306 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
315 ipaddress = r["your_ip"].AsString(); 307 args["teleport_flags"] = OSD.FromString(flags.ToString());
316 } 308
317 catch (NullReferenceException e) 309 OSDMap result = WebUtil.PostToService(uri,args);
310 if (result["Success"].AsBoolean())
318 { 311 {
319 m_log.InfoFormat("[GATEKEEPER SERVICE CONNECTOR]: exception on UnpackResponse of DoCreateChildAgentCall {0}", e.Message); 312 OSDMap unpacked = (OSDMap)result["_Result"];
320 reason = "Internal error"; 313
321 result = false; 314 if (unpacked != null)
315 {
316 reason = unpacked["reason"].AsString();
317 myipaddress = unpacked["your_ip"].AsString();
318 return unpacked["success"].AsBoolean();
319 }
322 } 320 }
321
322 reason = result["Message"] != null ? result["Message"].AsString() : "error";
323 return false;
324 }
325 catch (Exception e)
326 {
327 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: CreateAgent failed with exception: " + e.ToString());
328 reason = e.Message;
323 } 329 }
324 }
325
326 330
331 return false;
332 }
327 } 333 }
328} 334}
diff --git a/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs b/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
index 4e3cfa5..4facc4a 100644
--- a/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
+++ b/OpenSim/Services/Connectors/Simulation/SimulationServiceConnector.cs
@@ -75,469 +75,193 @@ namespace OpenSim.Services.Connectors.Simulation
75 return "agent/"; 75 return "agent/";
76 } 76 }
77 77
78 /// <summary>
79 ///
80 /// </summary>
78 public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason) 81 public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason)
79 { 82 {
80 HttpWebRequest AgentCreateRequest = null; 83 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: CreateAgent start");
81 reason = String.Empty; 84
82
83 if (SendRequest(destination, aCircuit, flags, out reason, out AgentCreateRequest))
84 {
85 string response = GetResponse(AgentCreateRequest, out reason);
86 bool success = true;
87 UnpackResponse(response, out success, out reason);
88 return success;
89 }
90
91 return false;
92 }
93
94
95 protected bool SendRequest(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason, out HttpWebRequest AgentCreateRequest)
96 {
97 reason = String.Empty; 85 reason = String.Empty;
98 AgentCreateRequest = null;
99
100 if (destination == null) 86 if (destination == null)
101 { 87 {
102 reason = "Destination is null";
103 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Given destination is null"); 88 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Given destination is null");
104 return false; 89 return false;
105 } 90 }
106 91
107 string uri = destination.ServerURI + AgentPath() + aCircuit.AgentID + "/"; 92 string uri = destination.ServerURI + AgentPath() + aCircuit.AgentID + "/";
108 93
109 AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri);
110 AgentCreateRequest.Method = "POST";
111 AgentCreateRequest.ContentType = "application/json";
112 AgentCreateRequest.Timeout = 10000;
113 //AgentCreateRequest.KeepAlive = false;
114 //AgentCreateRequest.Headers.Add("Authorization", authKey);
115
116 // Fill it in
117 OSDMap args = PackCreateAgentArguments(aCircuit, destination, flags);
118 if (args == null)
119 return false;
120
121 string strBuffer = "";
122 byte[] buffer = new byte[1];
123 try 94 try
124 { 95 {
125 strBuffer = OSDParser.SerializeJsonString(args); 96 OSDMap args = aCircuit.PackAgentCircuitData();
126 Encoding str = Util.UTF8;
127 buffer = str.GetBytes(strBuffer);
128 97
129 } 98 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
130 catch (Exception e) 99 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
131 { 100 args["destination_name"] = OSD.FromString(destination.RegionName);
132 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of ChildCreate: {0}", e.Message); 101 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
133 // ignore. buffer will be empty, caller should check. 102 args["teleport_flags"] = OSD.FromString(flags.ToString());
134 }
135 103
136 Stream os = null; 104 OSDMap result = WebUtil.PostToService(uri,args);
137 try 105 if (result["Success"].AsBoolean())
138 { // send the Post 106 return true;
139 AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send 107
140 os = AgentCreateRequest.GetRequestStream(); 108 reason = result["Message"] != null ? result["Message"].AsString() : "error";
141 os.Write(buffer, 0, strBuffer.Length); //Send it
142 m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: Posted CreateAgent request to remote sim {0}, region {1}, x={2} y={3}",
143 uri, destination.RegionName, destination.RegionLocX, destination.RegionLocY);
144 }
145 //catch (WebException ex)
146 catch
147 {
148 //m_log.ErrorFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on ChildAgentUpdate {0}", ex.Message);
149 reason = "cannot contact remote region";
150 return false; 109 return false;
151 } 110 }
152 finally
153 {
154 if (os != null)
155 os.Close();
156 }
157
158 return true;
159 }
160
161 protected string GetResponse(HttpWebRequest AgentCreateRequest, out string reason)
162 {
163 // Let's wait for the response
164 //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall");
165 reason = string.Empty;
166
167 WebResponse webResponse = null;
168 StreamReader sr = null;
169 string response = string.Empty;
170 try
171 {
172 webResponse = AgentCreateRequest.GetResponse();
173 if (webResponse == null)
174 {
175 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Null reply on DoCreateChildAgentCall post");
176 }
177 else
178 {
179
180 sr = new StreamReader(webResponse.GetResponseStream());
181 response = sr.ReadToEnd().Trim();
182 m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: DoCreateChildAgentCall reply was {0} ", response);
183 }
184 }
185 catch (WebException ex)
186 {
187 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", ex.Message);
188 reason = "Destination did not reply";
189 return string.Empty;
190 }
191 finally
192 {
193 if (sr != null)
194 sr.Close();
195 }
196
197 return response;
198 }
199
200 protected void UnpackResponse(string response, out bool result, out string reason)
201 {
202 result = true;
203 reason = string.Empty;
204 if (!String.IsNullOrEmpty(response))
205 {
206 try
207 {
208 // we assume we got an OSDMap back
209 OSDMap r = Util.GetOSDMap(response);
210 result = r["success"].AsBoolean();
211 reason = r["reason"].AsString();
212 }
213 catch (NullReferenceException e)
214 {
215 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", e.Message);
216
217 // check for old style response
218 if (response.ToLower().StartsWith("true"))
219 result = true;
220
221 result = false;
222 }
223 }
224 }
225
226 protected virtual OSDMap PackCreateAgentArguments(AgentCircuitData aCircuit, GridRegion destination, uint flags)
227 {
228 OSDMap args = null;
229 try
230 {
231 args = aCircuit.PackAgentCircuitData();
232 }
233 catch (Exception e) 111 catch (Exception e)
234 { 112 {
235 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: PackAgentCircuitData failed with exception: " + e.Message); 113 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: CreateAgent failed with exception: " + e.ToString());
236 return null; 114 reason = e.Message;
237 } 115 }
238 116
239 // Add the input arguments 117 return false;
240 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
241 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
242 args["destination_name"] = OSD.FromString(destination.RegionName);
243 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
244 args["teleport_flags"] = OSD.FromString(flags.ToString());
245
246 return args;
247 } 118 }
248 119
120 /// <summary>
121 /// Send complete data about an agent in this region to a neighbor
122 /// </summary>
249 public bool UpdateAgent(GridRegion destination, AgentData data) 123 public bool UpdateAgent(GridRegion destination, AgentData data)
250 { 124 {
251 return UpdateAgent(destination, (IAgentData)data); 125 return UpdateAgent(destination, (IAgentData)data);
252 } 126 }
253 127
128 /// <summary>
129 /// Send updated position information about an agent in this region to a neighbor
130 /// This operation may be called very frequently if an avatar is moving about in
131 /// the region.
132 /// </summary>
254 public bool UpdateAgent(GridRegion destination, AgentPosition data) 133 public bool UpdateAgent(GridRegion destination, AgentPosition data)
255 { 134 {
135 // we need a better throttle for these
136 return false;
137
256 return UpdateAgent(destination, (IAgentData)data); 138 return UpdateAgent(destination, (IAgentData)data);
257 } 139 }
258 140
141 /// <summary>
142 /// This is the worker function to send AgentData to a neighbor region
143 /// </summary>
259 private bool UpdateAgent(GridRegion destination, IAgentData cAgentData) 144 private bool UpdateAgent(GridRegion destination, IAgentData cAgentData)
260 { 145 {
261 // Eventually, we want to use a caps url instead of the agentID 146 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: UpdateAgent start");
262 147
148 // Eventually, we want to use a caps url instead of the agentID
263 string uri = destination.ServerURI + AgentPath() + cAgentData.AgentID + "/"; 149 string uri = destination.ServerURI + AgentPath() + cAgentData.AgentID + "/";
264 150
265 HttpWebRequest ChildUpdateRequest = (HttpWebRequest)WebRequest.Create(uri);
266 ChildUpdateRequest.Method = "PUT";
267 ChildUpdateRequest.ContentType = "application/json";
268 ChildUpdateRequest.Timeout = 30000;
269 //ChildUpdateRequest.KeepAlive = false;
270
271 // Fill it in
272 OSDMap args = null;
273 try 151 try
274 { 152 {
275 args = cAgentData.Pack(); 153 OSDMap args = cAgentData.Pack();
276 }
277 catch (Exception e)
278 {
279 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: PackUpdateMessage failed with exception: " + e.Message);
280 }
281 // Add the input arguments
282 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
283 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
284 args["destination_name"] = OSD.FromString(destination.RegionName);
285 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
286
287 string strBuffer = "";
288 byte[] buffer = new byte[1];
289 try
290 {
291 strBuffer = OSDParser.SerializeJsonString(args);
292 Encoding str = Util.UTF8;
293 buffer = str.GetBytes(strBuffer);
294
295 }
296 catch (Exception e)
297 {
298 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of ChildUpdate: {0}", e.Message);
299 // ignore. buffer will be empty, caller should check.
300 }
301 154
302 Stream os = null; 155 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
303 try 156 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
304 { // send the Post 157 args["destination_name"] = OSD.FromString(destination.RegionName);
305 ChildUpdateRequest.ContentLength = buffer.Length; //Count bytes to send 158 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
306 os = ChildUpdateRequest.GetRequestStream();
307 os.Write(buffer, 0, strBuffer.Length); //Send it
308 //m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: Posted AgentUpdate request to remote sim {0}", uri);
309 }
310 catch (WebException ex)
311 //catch
312 {
313 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on AgentUpdate {0}", ex.Message);
314 159
315 return false; 160 OSDMap result = WebUtil.PutToService(uri,args);
161 return result["Success"].AsBoolean();
316 } 162 }
317 finally 163 catch (Exception e)
318 {
319 if (os != null)
320 os.Close();
321 }
322
323 // Let's wait for the response
324 //m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after ChildAgentUpdate");
325
326 WebResponse webResponse = null;
327 StreamReader sr = null;
328 try
329 {
330 webResponse = ChildUpdateRequest.GetResponse();
331 if (webResponse == null)
332 {
333 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Null reply on ChilAgentUpdate post");
334 }
335
336 sr = new StreamReader(webResponse.GetResponseStream());
337 //reply = sr.ReadToEnd().Trim();
338 sr.ReadToEnd().Trim();
339 sr.Close();
340 //m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
341
342 }
343 catch (WebException ex)
344 {
345 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of ChilAgentUpdate from {0}: {1}", uri, ex.Message);
346 // ignore, really
347 }
348 finally
349 { 164 {
350 if (sr != null) 165 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: UpdateAgent failed with exception: " + e.ToString());
351 sr.Close();
352 } 166 }
353 167
354 return true; 168 return false;
355 } 169 }
356 170
171 /// <summary>
172 /// Not sure what sequence causes this function to be invoked. The only calling
173 /// path is through the GET method
174 /// </summary>
357 public bool RetrieveAgent(GridRegion destination, UUID id, out IAgentData agent) 175 public bool RetrieveAgent(GridRegion destination, UUID id, out IAgentData agent)
358 { 176 {
177 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: RetrieveAgent start");
178
359 agent = null; 179 agent = null;
180
360 // Eventually, we want to use a caps url instead of the agentID 181 // Eventually, we want to use a caps url instead of the agentID
361 string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/"; 182 string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
362 183
363 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
364 request.Method = "GET";
365 request.Timeout = 10000;
366 //request.Headers.Add("authorization", ""); // coming soon
367
368 HttpWebResponse webResponse = null;
369 string reply = string.Empty;
370 StreamReader sr = null;
371 try 184 try
372 { 185 {
373 webResponse = (HttpWebResponse)request.GetResponse(); 186 OSDMap result = WebUtil.GetFromService(uri);
374 if (webResponse == null) 187 if (result["Success"].AsBoolean())
375 { 188 {
376 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Null reply on agent get "); 189 // OSDMap args = Util.GetOSDMap(result["_RawResult"].AsString());
190 OSDMap args = (OSDMap)result["_Result"];
191 if (args != null)
192 {
193 agent = new CompleteAgentData();
194 agent.Unpack(args);
195 return true;
196 }
377 } 197 }
378
379 sr = new StreamReader(webResponse.GetResponseStream());
380 reply = sr.ReadToEnd().Trim();
381
382
383 } 198 }
384 catch (WebException ex) 199 catch (Exception e)
385 {
386 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of agent get {0}", ex.Message);
387 // ignore, really
388 return false;
389 }
390 finally
391 {
392 if (sr != null)
393 sr.Close();
394 }
395
396 if (webResponse.StatusCode == HttpStatusCode.OK)
397 { 200 {
398 // we know it's jason 201 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: UpdateAgent failed with exception: " + e.ToString());
399 OSDMap args = Util.GetOSDMap(reply);
400 if (args == null)
401 {
402 return false;
403 }
404
405 agent = new CompleteAgentData();
406 agent.Unpack(args);
407 return true;
408 } 202 }
409 203
410 return false; 204 return false;
411 } 205 }
412 206
207 /// <summary>
208 /// </summary>
413 public bool QueryAccess(GridRegion destination, UUID id) 209 public bool QueryAccess(GridRegion destination, UUID id)
414 { 210 {
211 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: QueryAccess start");
212
415 IPEndPoint ext = destination.ExternalEndPoint; 213 IPEndPoint ext = destination.ExternalEndPoint;
416 if (ext == null) return false; 214 if (ext == null) return false;
215
417 // Eventually, we want to use a caps url instead of the agentID 216 // Eventually, we want to use a caps url instead of the agentID
418 string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/"; 217 string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
419 218
420 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
421 request.Method = "QUERYACCESS";
422 request.Timeout = 10000;
423 //request.Headers.Add("authorization", ""); // coming soon
424
425 HttpWebResponse webResponse = null;
426 string reply = string.Empty;
427 StreamReader sr = null;
428 try 219 try
429 { 220 {
430 webResponse = (HttpWebResponse)request.GetResponse(); 221 OSDMap result = WebUtil.ServiceOSDRequest(uri,null,"QUERYACCESS",10000);
431 if (webResponse == null) 222 return result["Success"].AsBoolean();
432 {
433 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Null reply on agent query ");
434 }
435
436 sr = new StreamReader(webResponse.GetResponseStream());
437 reply = sr.ReadToEnd().Trim();
438
439
440 }
441 catch (WebException ex)
442 {
443 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of agent query {0}", ex.Message);
444 // ignore, really
445 return false;
446 } 223 }
447 finally 224 catch (Exception e)
448 { 225 {
449 if (sr != null) 226 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] QueryAcess failed with exception; {0}",e.ToString());
450 sr.Close();
451 } 227 }
452 228
453 if (webResponse.StatusCode == HttpStatusCode.OK)
454 {
455 try
456 {
457 bool result;
458
459 result = bool.Parse(reply);
460
461 return result;
462 }
463 catch
464 {
465 return false;
466 }
467 }
468
469 return false; 229 return false;
470 } 230 }
471 231
232 /// <summary>
233 /// </summary>
472 public bool ReleaseAgent(UUID origin, UUID id, string uri) 234 public bool ReleaseAgent(UUID origin, UUID id, string uri)
473 { 235 {
474 WebRequest request = WebRequest.Create(uri); 236 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: ReleaseAgent start");
475 request.Method = "DELETE";
476 request.Timeout = 10000;
477 237
478 StreamReader sr = null;
479 try 238 try
480 { 239 {
481 WebResponse webResponse = request.GetResponse(); 240 OSDMap result = WebUtil.ServiceOSDRequest(uri,null,"DELETE",10000);
482 if (webResponse == null)
483 {
484 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Null reply on ReleaseAgent");
485 }
486
487 sr = new StreamReader(webResponse.GetResponseStream());
488 //reply = sr.ReadToEnd().Trim();
489 sr.ReadToEnd().Trim();
490 sr.Close();
491 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
492
493 }
494 catch (WebException ex)
495 {
496 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of ReleaseAgent {0}", ex.Message);
497 return false;
498 } 241 }
499 finally 242 catch (Exception e)
500 { 243 {
501 if (sr != null) 244 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] ReleaseAgent failed with exception; {0}",e.ToString());
502 sr.Close();
503 } 245 }
504 246
505 return true; 247 return true;
506 } 248 }
507 249
250 /// <summary>
251 /// </summary>
508 public bool CloseAgent(GridRegion destination, UUID id) 252 public bool CloseAgent(GridRegion destination, UUID id)
509 { 253 {
510 string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/"; 254 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: CloseAgent start");
511 255
512 WebRequest request = WebRequest.Create(uri); 256 string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
513 request.Method = "DELETE";
514 request.Timeout = 10000;
515 257
516 StreamReader sr = null;
517 try 258 try
518 { 259 {
519 WebResponse webResponse = request.GetResponse(); 260 OSDMap result = WebUtil.ServiceOSDRequest(uri,null,"DELETE",10000);
520 if (webResponse == null)
521 {
522 m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Null reply on agent delete ");
523 }
524
525 sr = new StreamReader(webResponse.GetResponseStream());
526 //reply = sr.ReadToEnd().Trim();
527 sr.ReadToEnd().Trim();
528 sr.Close();
529 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
530
531 }
532 catch (WebException ex)
533 {
534 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of agent delete from {0}: {1}", destination.RegionName, ex.Message);
535 return false;
536 } 261 }
537 finally 262 catch (Exception e)
538 { 263 {
539 if (sr != null) 264 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] CloseAgent failed with exception; {0}",e.ToString());
540 sr.Close();
541 } 265 }
542 266
543 return true; 267 return true;
@@ -552,97 +276,46 @@ namespace OpenSim.Services.Connectors.Simulation
552 return "object/"; 276 return "object/";
553 } 277 }
554 278
279 /// <summary>
280 ///
281 /// </summary>
555 public bool CreateObject(GridRegion destination, ISceneObject sog, bool isLocalCall) 282 public bool CreateObject(GridRegion destination, ISceneObject sog, bool isLocalCall)
556 { 283 {
557 string uri 284 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: CreateObject start");
558 = destination.ServerURI + ObjectPath() + sog.UUID + "/";
559 //m_log.Debug(" >>> DoCreateObjectCall <<< " + uri);
560
561 WebRequest ObjectCreateRequest = WebRequest.Create(uri);
562 ObjectCreateRequest.Method = "POST";
563 ObjectCreateRequest.ContentType = "application/json";
564 ObjectCreateRequest.Timeout = 10000;
565
566 OSDMap args = new OSDMap(2);
567 args["sog"] = OSD.FromString(sog.ToXml2());
568 args["extra"] = OSD.FromString(sog.ExtraToXmlString());
569 args["modified"] = OSD.FromBoolean(sog.HasGroupChanged);
570 string state = sog.GetStateSnapshot();
571 if (state.Length > 0)
572 args["state"] = OSD.FromString(state);
573 // Add the input general arguments
574 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
575 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
576 args["destination_name"] = OSD.FromString(destination.RegionName);
577 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
578
579 string strBuffer = "";
580 byte[] buffer = new byte[1];
581 try
582 {
583 strBuffer = OSDParser.SerializeJsonString(args);
584 Encoding str = Util.UTF8;
585 buffer = str.GetBytes(strBuffer);
586 285
587 } 286 string uri = destination.ServerURI + ObjectPath() + sog.UUID + "/";
588 catch (Exception e)
589 {
590 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of CreateObject: {0}", e.Message);
591 // ignore. buffer will be empty, caller should check.
592 }
593 287
594 Stream os = null;
595 try 288 try
596 { // send the Post
597 ObjectCreateRequest.ContentLength = buffer.Length; //Count bytes to send
598 os = ObjectCreateRequest.GetRequestStream();
599 os.Write(buffer, 0, strBuffer.Length); //Send it
600 m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: Posted CreateObject request to remote sim {0}", uri);
601 }
602 catch (WebException ex)
603 {
604 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on CreateObject {0}", ex.Message);
605 return false;
606 }
607 finally
608 { 289 {
609 if (os != null) 290 OSDMap args = new OSDMap(2);
610 os.Close();
611 }
612 291
613 // Let's wait for the response 292 args["sog"] = OSD.FromString(sog.ToXml2());
614 //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall"); 293 args["extra"] = OSD.FromString(sog.ExtraToXmlString());
294 args["modified"] = OSD.FromBoolean(sog.HasGroupChanged);
615 295
616 StreamReader sr = null; 296 string state = sog.GetStateSnapshot();
617 try 297 if (state.Length > 0)
618 { 298 args["state"] = OSD.FromString(state);
619 WebResponse webResponse = ObjectCreateRequest.GetResponse();
620 if (webResponse == null)
621 {
622 m_log.Warn("[REMOTE SIMULATION CONNECTOR]: Null reply on CreateObject post");
623 return false;
624 }
625 299
626 sr = new StreamReader(webResponse.GetResponseStream()); 300 // Add the input general arguments
627 //reply = sr.ReadToEnd().Trim(); 301 args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
628 sr.ReadToEnd().Trim(); 302 args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
629 //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: DoCreateChildAgentCall reply was {0} ", reply); 303 args["destination_name"] = OSD.FromString(destination.RegionName);
304 args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
630 305
306 OSDMap result = WebUtil.PostToService(uri,args);
631 } 307 }
632 catch (WebException ex) 308 catch (Exception e)
633 {
634 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of CreateObject {0}", ex.Message);
635 return false;
636 }
637 finally
638 { 309 {
639 if (sr != null) 310 m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] CreateObject failed with exception; {0}",e.ToString());
640 sr.Close();
641 } 311 }
642 312
643 return true; 313 return true;
644 } 314 }
645 315
316 /// <summary>
317 ///
318 /// </summary>
646 public bool CreateObject(GridRegion destination, UUID userID, UUID itemID) 319 public bool CreateObject(GridRegion destination, UUID userID, UUID itemID)
647 { 320 {
648 // TODO, not that urgent 321 // TODO, not that urgent