aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Communications/RestClient/RestClient.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Communications/RestClient/RestClient.cs')
-rw-r--r--OpenSim/Framework/Communications/RestClient/RestClient.cs101
1 files changed, 86 insertions, 15 deletions
diff --git a/OpenSim/Framework/Communications/RestClient/RestClient.cs b/OpenSim/Framework/Communications/RestClient/RestClient.cs
index 25fc61a..392669f 100644
--- a/OpenSim/Framework/Communications/RestClient/RestClient.cs
+++ b/OpenSim/Framework/Communications/RestClient/RestClient.cs
@@ -6,7 +6,9 @@ using System.Text;
6using System.Collections.Generic; 6using System.Collections.Generic;
7using System.Threading; 7using System.Threading;
8 8
9namespace OpenSim.Framework.RestClient 9using OpenSim.Framework.Console;
10
11namespace OpenSim.Framework.Communications
10{ 12{
11 /// <summary> 13 /// <summary>
12 /// Implementation of a generic REST client 14 /// Implementation of a generic REST client
@@ -25,8 +27,11 @@ namespace OpenSim.Framework.RestClient
25 /// other threads to execute, while it waits for a response from the web-service. RestClient it self, can be 27 /// other threads to execute, while it waits for a response from the web-service. RestClient it self, can be
26 /// invoked by the caller in either synchroneous mode or asynchroneous mode. 28 /// invoked by the caller in either synchroneous mode or asynchroneous mode.
27 /// </remarks> 29 /// </remarks>
28 public class RestClient 30 public class RestClient
29 { 31 {
32
33 string realuri;
34 #region member variables
30 /// <summary> 35 /// <summary>
31 /// The base Uri of the web-service e.g. http://www.google.com 36 /// The base Uri of the web-service e.g. http://www.google.com
32 /// </summary> 37 /// </summary>
@@ -55,7 +60,7 @@ namespace OpenSim.Framework.RestClient
55 /// <summary> 60 /// <summary>
56 /// MemoryStream representing the resultiong resource 61 /// MemoryStream representing the resultiong resource
57 /// </summary> 62 /// </summary>
58 MemoryStream _resource; 63 Stream _resource;
59 64
60 /// <summary> 65 /// <summary>
61 /// WebRequest object, held as a member variable 66 /// WebRequest object, held as a member variable
@@ -89,6 +94,9 @@ namespace OpenSim.Framework.RestClient
89 /// </summary> 94 /// </summary>
90 private Exception _asyncException; 95 private Exception _asyncException;
91 96
97 #endregion member variables
98
99 #region constructors
92 /// <summary> 100 /// <summary>
93 /// Instantiate a new RestClient 101 /// Instantiate a new RestClient
94 /// </summary> 102 /// </summary>
@@ -100,8 +108,12 @@ namespace OpenSim.Framework.RestClient
100 _resource = new MemoryStream(); 108 _resource = new MemoryStream();
101 _request = null; 109 _request = null;
102 _response = null; 110 _response = null;
111 _lock = new object();
103 } 112 }
104 113
114 object _lock;
115 #endregion constructors
116
105 /// <summary> 117 /// <summary>
106 /// Add a path element to the query, e.g. assets 118 /// Add a path element to the query, e.g. assets
107 /// </summary> 119 /// </summary>
@@ -125,6 +137,15 @@ namespace OpenSim.Framework.RestClient
125 } 137 }
126 138
127 /// <summary> 139 /// <summary>
140 /// Add a query parameter to the Url
141 /// </summary>
142 /// <param name="name">Name of the parameter, e.g. min</param>
143 public void AddQueryParameter(string name)
144 {
145 _parameterElements.Add(HttpUtility.UrlEncode(name), null);
146 }
147
148 /// <summary>
128 /// Web-Request method, e.g. GET, PUT, POST, DELETE 149 /// Web-Request method, e.g. GET, PUT, POST, DELETE
129 /// </summary> 150 /// </summary>
130 public string RequestMethod 151 public string RequestMethod
@@ -185,9 +206,10 @@ namespace OpenSim.Framework.RestClient
185 sb.Append(kv.Value); 206 sb.Append(kv.Value);
186 } 207 }
187 } 208 }
209 realuri = sb.ToString();
188 return new Uri(sb.ToString()); 210 return new Uri(sb.ToString());
189 } 211 }
190 212 #region Async communications with server
191 /// <summary> 213 /// <summary>
192 /// Async method, invoked when a block of data has been received from the service 214 /// Async method, invoked when a block of data has been received from the service
193 /// </summary> 215 /// </summary>
@@ -199,7 +221,6 @@ namespace OpenSim.Framework.RestClient
199 Stream s = (Stream)ar.AsyncState; 221 Stream s = (Stream)ar.AsyncState;
200 int read = s.EndRead(ar); 222 int read = s.EndRead(ar);
201 223
202 // Read the HTML page and then print it to the console.
203 if (read > 0) 224 if (read > 0)
204 { 225 {
205 _resource.Write(_readbuf, 0, read); 226 _resource.Write(_readbuf, 0, read);
@@ -207,7 +228,6 @@ namespace OpenSim.Framework.RestClient
207 228
208 // TODO! Implement timeout, without killing the server 229 // TODO! Implement timeout, without killing the server
209 //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true); 230 //ThreadPool.RegisterWaitForSingleObject(asynchronousResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
210 return;
211 } 231 }
212 else 232 else
213 { 233 {
@@ -261,32 +281,83 @@ namespace OpenSim.Framework.RestClient
261 } 281 }
262 } 282 }
263 } 283 }
284 #endregion Async communications with server
264 285
265 /// <summary> 286 /// <summary>
266 /// Perform synchroneous request 287 /// Perform synchroneous request
267 /// </summary> 288 /// </summary>
268 public Stream Request() 289 public Stream Request()
269 { 290 {
291 lock (_lock)
292 {
293 _request = (HttpWebRequest)WebRequest.Create(buildUri());
294 _request.KeepAlive = false;
295 _request.ContentType = "application/xml";
296 _request.Timeout = 200000;
297 _asyncException = null;
298
299// IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
300 _response = (HttpWebResponse)_request.GetResponse();
301 Stream src = _response.GetResponseStream();
302 int length = src.Read(_readbuf, 0, BufferSize);
303 while(length > 0)
304 {
305 _resource.Write(_readbuf, 0, length);
306 length = src.Read(_readbuf, 0, BufferSize);
307 }
308
309
310 // TODO! Implement timeout, without killing the server
311 // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
312 //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
313
314// _allDone.WaitOne();
315 if (_response != null)
316 _response.Close();
317 if (_asyncException != null)
318 throw _asyncException;
319
320 if (_resource != null)
321 {
322 _resource.Flush();
323 _resource.Seek(0, SeekOrigin.Begin);
324 }
325
326 return _resource;
327 }
328 }
329
330 public Stream Request(Stream src)
331 {
270 _request = (HttpWebRequest)WebRequest.Create(buildUri()); 332 _request = (HttpWebRequest)WebRequest.Create(buildUri());
271 _request.KeepAlive = false; 333 _request.KeepAlive = false;
272 _request.ContentType = "text/html"; 334 _request.ContentType = "application/xml";
273 _request.Timeout = 200; 335 _request.Timeout = 900000;
336 _request.Method = RequestMethod;
274 _asyncException = null; 337 _asyncException = null;
338 _request.ContentLength = src.Length;
275 339
276 IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request); 340 src.Seek(0, SeekOrigin.Begin);
341 Stream dst = _request.GetRequestStream();
342 byte[] buf = new byte[1024];
343 int length = src.Read(buf,0, 1024);
344 while (length > 0)
345 {
346 dst.Write(buf, 0, length);
347 length = src.Read(buf, 0, 1024);
348 }
349 _response = (HttpWebResponse)_request.GetResponse();
350
351// IAsyncResult responseAsyncResult = _request.BeginGetResponse(new AsyncCallback(ResponseIsReadyDelegate), _request);
277 352
278 // TODO! Implement timeout, without killing the server 353 // TODO! Implement timeout, without killing the server
279 // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted 354 // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted
280 //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true); 355 //ThreadPool.RegisterWaitForSingleObject(responseAsyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), _request, DefaultTimeout, true);
281 356
282 _allDone.WaitOne(); 357 return null;
283 if(_response != null)
284 _response.Close();
285 if (_asyncException != null)
286 throw _asyncException;
287 return _resource;
288 } 358 }
289 359
360
290 #region Async Invocation 361 #region Async Invocation
291 public IAsyncResult BeginRequest(AsyncCallback callback, object state) 362 public IAsyncResult BeginRequest(AsyncCallback callback, object state)
292 { 363 {