2007-12-06 00:58:28 +00:00
|
|
|
/*
|
2008-07-21 21:12:59 +00:00
|
|
|
* Copyright (c) 2007-2008, openmetaverse.org
|
2007-12-06 00:58:28 +00:00
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* - Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* - Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
2008-07-21 21:12:59 +00:00
|
|
|
* - Neither the name of the openmetaverse.org nor the names
|
2007-12-06 00:58:28 +00:00
|
|
|
* of its contributors may be used to endorse or promote products derived from
|
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
2008-04-22 18:59:39 +00:00
|
|
|
using System.Threading;
|
2008-07-21 21:12:59 +00:00
|
|
|
using OpenMetaverse.StructuredData;
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2008-12-29 20:44:28 +00:00
|
|
|
namespace OpenMetaverse.Http
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
|
|
|
|
public class EventQueueClient
|
|
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
/// <summary>=</summary>
|
|
|
|
|
public const int REQUEST_TIMEOUT = 1000 * 120;
|
|
|
|
|
|
2007-12-06 00:58:28 +00:00
|
|
|
public delegate void ConnectedCallback();
|
2008-10-30 01:50:59 +00:00
|
|
|
public delegate void EventCallback(string eventName, OSDMap body);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
public ConnectedCallback OnConnected;
|
|
|
|
|
public EventCallback OnEvent;
|
|
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
public bool Running { get { return _Running; } }
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
protected Uri _Address;
|
2007-12-06 00:58:28 +00:00
|
|
|
protected bool _Dead;
|
|
|
|
|
protected bool _Running;
|
2009-05-01 06:04:32 +00:00
|
|
|
protected HttpWebRequest _Request;
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
public EventQueueClient(Uri eventQueueLocation)
|
|
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
_Address = eventQueueLocation;
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
_Dead = false;
|
2009-05-01 06:04:32 +00:00
|
|
|
|
|
|
|
|
// Create an EventQueueGet request
|
|
|
|
|
OSDMap request = new OSDMap();
|
|
|
|
|
request["ack"] = new OSD();
|
|
|
|
|
request["done"] = OSD.FromBoolean(false);
|
|
|
|
|
|
|
|
|
|
byte[] postData = OSDParser.SerializeLLSDXmlBytes(request);
|
|
|
|
|
|
|
|
|
|
_Request = CapsBase.UploadDataAsync(_Address, null, "application/xml", postData, REQUEST_TIMEOUT, OpenWriteHandler, null, RequestCompletedHandler);
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop(bool immediate)
|
|
|
|
|
{
|
|
|
|
|
_Dead = true;
|
|
|
|
|
|
|
|
|
|
if (immediate)
|
|
|
|
|
_Running = false;
|
|
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
if (_Request != null)
|
|
|
|
|
_Request.Abort();
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
void OpenWriteHandler(HttpWebRequest request)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
_Running = true;
|
|
|
|
|
_Request = request;
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
Logger.Log.Debug("Capabilities event queue connected");
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
// The event queue is starting up for the first time
|
|
|
|
|
if (OnConnected != null)
|
|
|
|
|
{
|
|
|
|
|
try { OnConnected(); }
|
|
|
|
|
catch (Exception ex) { Logger.Log.Error(ex.Message, ex); }
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
2009-05-01 06:04:32 +00:00
|
|
|
}
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
void RequestCompletedHandler(HttpWebRequest request, HttpWebResponse response, byte[] responseData, Exception error)
|
|
|
|
|
{
|
|
|
|
|
// We don't care about this request now that it has completed
|
|
|
|
|
_Request = null;
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
OSDArray events = null;
|
|
|
|
|
int ack = 0;
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
if (responseData != null)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
// Got a response
|
|
|
|
|
OSDMap result = OSDParser.DeserializeLLSDXml(responseData) as OSDMap;
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
if (result != null)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
events = result["events"] as OSDArray;
|
|
|
|
|
ack = result["id"].AsInteger();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Log.Warn("Got an unparseable response from the event queue: \"" +
|
|
|
|
|
System.Text.Encoding.UTF8.GetString(responseData) + "\"");
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-05-01 06:04:32 +00:00
|
|
|
else if (error != null)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
#region Error handling
|
|
|
|
|
|
2008-08-12 22:38:02 +00:00
|
|
|
HttpStatusCode code = HttpStatusCode.OK;
|
2009-05-01 06:04:32 +00:00
|
|
|
|
|
|
|
|
if (error is WebException)
|
|
|
|
|
{
|
|
|
|
|
WebException webException = (WebException)error;
|
|
|
|
|
|
|
|
|
|
if (webException.Response != null)
|
|
|
|
|
code = ((HttpWebResponse)webException.Response).StatusCode;
|
|
|
|
|
else if (webException.Status == WebExceptionStatus.RequestCanceled)
|
|
|
|
|
goto HandlingDone;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error is WebException && ((WebException)error).Response != null)
|
|
|
|
|
code = ((HttpWebResponse)((WebException)error).Response).StatusCode;
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2008-08-12 22:38:02 +00:00
|
|
|
if (code == HttpStatusCode.NotFound || code == HttpStatusCode.Gone)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
Logger.Log.InfoFormat("Closing event queue at {0} due to missing caps URI", _Address);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
_Running = false;
|
|
|
|
|
_Dead = true;
|
|
|
|
|
}
|
2008-08-12 22:38:02 +00:00
|
|
|
else if (code == HttpStatusCode.BadGateway)
|
|
|
|
|
{
|
|
|
|
|
// This is not good (server) protocol design, but it's normal.
|
|
|
|
|
// The EventQueue server is a proxy that connects to a Squid
|
|
|
|
|
// cache which will time out periodically. The EventQueue server
|
|
|
|
|
// interprets this as a generic error and returns a 502 to us
|
|
|
|
|
// that we ignore
|
|
|
|
|
}
|
2009-05-01 06:04:32 +00:00
|
|
|
else
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
2008-08-12 22:38:02 +00:00
|
|
|
// Try to log a meaningful error message
|
|
|
|
|
if (code != HttpStatusCode.OK)
|
2008-07-11 05:51:56 +00:00
|
|
|
{
|
2008-12-29 20:44:28 +00:00
|
|
|
Logger.Log.WarnFormat("Unrecognized caps connection problem from {0}: {1}",
|
2009-05-01 06:04:32 +00:00
|
|
|
_Address, code);
|
2008-05-06 23:57:26 +00:00
|
|
|
}
|
2009-05-01 06:04:32 +00:00
|
|
|
else if (error.InnerException != null)
|
2008-05-06 23:57:26 +00:00
|
|
|
{
|
2008-12-29 20:44:28 +00:00
|
|
|
Logger.Log.WarnFormat("Unrecognized caps exception from {0}: {1}",
|
2009-05-01 06:04:32 +00:00
|
|
|
_Address, error.InnerException.Message);
|
2008-04-22 18:59:39 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-12-29 20:44:28 +00:00
|
|
|
Logger.Log.WarnFormat("Unrecognized caps exception from {0}: {1}",
|
2009-05-01 06:04:32 +00:00
|
|
|
_Address, error.Message);
|
2008-04-22 18:59:39 +00:00
|
|
|
}
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
#endregion Error handling
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
2009-05-01 06:04:32 +00:00
|
|
|
else
|
2007-12-21 06:25:13 +00:00
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
Logger.Log.Warn("No response from the event queue but no reported error either");
|
2007-12-21 06:25:13 +00:00
|
|
|
}
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
HandlingDone:
|
|
|
|
|
|
|
|
|
|
#region Resume the connection
|
|
|
|
|
|
2007-12-06 00:58:28 +00:00
|
|
|
if (_Running)
|
|
|
|
|
{
|
2009-05-01 06:04:32 +00:00
|
|
|
OSDMap osdRequest = new OSDMap();
|
|
|
|
|
if (ack != 0) osdRequest["ack"] = OSD.FromInteger(ack);
|
|
|
|
|
else osdRequest["ack"] = new OSD();
|
|
|
|
|
osdRequest["done"] = OSD.FromBoolean(_Dead);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
byte[] postData = OSDParser.SerializeLLSDXmlBytes(osdRequest);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
// Resume the connection. The event handler for the connection opening
|
|
|
|
|
// just sets class _Request variable to the current HttpWebRequest
|
|
|
|
|
CapsBase.UploadDataAsync(_Address, null, "application/xml", postData, REQUEST_TIMEOUT,
|
|
|
|
|
delegate(HttpWebRequest newRequest) { _Request = newRequest; }, null, RequestCompletedHandler);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
// If the event queue is dead at this point, turn it off since
|
|
|
|
|
// that was the last thing we want to do
|
|
|
|
|
if (_Dead)
|
|
|
|
|
{
|
|
|
|
|
_Running = false;
|
2008-12-29 20:44:28 +00:00
|
|
|
Logger.Log.Debug("Sent event queue shutdown message");
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
#endregion Resume the connection
|
|
|
|
|
|
|
|
|
|
#region Handle incoming events
|
|
|
|
|
|
2007-12-06 00:58:28 +00:00
|
|
|
if (OnEvent != null && events != null && events.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// Fire callbacks for each event received
|
2008-10-30 01:50:59 +00:00
|
|
|
foreach (OSDMap evt in events)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
|
|
|
|
string msg = evt["message"].AsString();
|
2008-10-30 01:50:59 +00:00
|
|
|
OSDMap body = (OSDMap)evt["body"];
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
try { OnEvent(msg, body); }
|
2008-12-29 20:44:28 +00:00
|
|
|
catch (Exception ex) { Logger.Log.Error(ex.Message, ex); }
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-01 06:04:32 +00:00
|
|
|
#endregion Handle incoming events
|
|
|
|
|
}
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
}
|