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
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public delegate void ConnectedCallback();
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="eventName"></param>
|
|
|
|
|
/// <param name="body"></param>
|
2008-10-30 01:50:59 +00:00
|
|
|
public delegate void EventCallback(string eventName, OSDMap body);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
/// <summary></summary>
|
|
|
|
|
public ConnectedCallback OnConnected;
|
|
|
|
|
/// <summary></summary>
|
|
|
|
|
public EventCallback OnEvent;
|
|
|
|
|
|
|
|
|
|
public IWebProxy Proxy;
|
|
|
|
|
|
|
|
|
|
public bool Running { get { return _Running && _Client.IsBusy; } }
|
|
|
|
|
|
|
|
|
|
protected CapsBase _Client;
|
|
|
|
|
protected bool _Dead;
|
|
|
|
|
protected bool _Running;
|
|
|
|
|
|
|
|
|
|
public EventQueueClient(Uri eventQueueLocation)
|
|
|
|
|
{
|
2008-12-29 20:44:28 +00:00
|
|
|
_Client = new CapsBase(eventQueueLocation, null);
|
2008-05-05 23:41:41 +00:00
|
|
|
_Client.OpenWriteCompleted += new CapsBase.OpenWriteCompletedEventHandler(Client_OpenWriteCompleted);
|
|
|
|
|
_Client.UploadDataCompleted += new CapsBase.UploadDataCompletedEventHandler(Client_UploadDataCompleted);
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
_Dead = false;
|
|
|
|
|
_Client.OpenWriteAsync(_Client.Location);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop(bool immediate)
|
|
|
|
|
{
|
|
|
|
|
_Dead = true;
|
|
|
|
|
|
|
|
|
|
if (immediate)
|
|
|
|
|
_Running = false;
|
|
|
|
|
|
|
|
|
|
if (_Client.IsBusy)
|
|
|
|
|
_Client.CancelAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Callback Handlers
|
|
|
|
|
|
2008-05-05 23:41:41 +00:00
|
|
|
private void Client_OpenWriteCompleted(object sender, CapsBase.OpenWriteCompletedEventArgs e)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
|
|
|
|
bool raiseEvent = false;
|
|
|
|
|
|
|
|
|
|
if (!_Dead)
|
|
|
|
|
{
|
|
|
|
|
if (!_Running) raiseEvent = true;
|
|
|
|
|
|
|
|
|
|
// We are connected to the event queue
|
|
|
|
|
_Running = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create an EventQueueGet request
|
2008-10-30 01:50:59 +00:00
|
|
|
OSDMap request = new OSDMap();
|
|
|
|
|
request["ack"] = new OSD();
|
|
|
|
|
request["done"] = OSD.FromBoolean(false);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2008-10-30 16:54:17 +00:00
|
|
|
byte[] postData = OSDParser.SerializeLLSDXmlBytes(request);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
_Client.UploadDataAsync(_Client.Location, postData);
|
|
|
|
|
|
|
|
|
|
if (raiseEvent)
|
|
|
|
|
{
|
2008-12-29 20:44:28 +00:00
|
|
|
Logger.Log.Debug("Capabilities event queue connected");
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
// The event queue is starting up for the first time
|
|
|
|
|
if (OnConnected != null)
|
|
|
|
|
{
|
|
|
|
|
try { OnConnected(); }
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-05 23:41:41 +00:00
|
|
|
private void Client_UploadDataCompleted(object sender, CapsBase.UploadDataCompletedEventArgs e)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
2008-10-30 01:50:59 +00:00
|
|
|
OSDArray events = null;
|
2007-12-06 00:58:28 +00:00
|
|
|
int ack = 0;
|
|
|
|
|
|
|
|
|
|
if (e.Error != null)
|
|
|
|
|
{
|
2008-08-12 22:38:02 +00:00
|
|
|
HttpStatusCode code = HttpStatusCode.OK;
|
2008-08-15 22:10:54 +00:00
|
|
|
if (e.Error is WebException && ((WebException)e.Error).Response != null)
|
2008-08-12 22:38:02 +00:00
|
|
|
code = ((HttpWebResponse)((WebException)e.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
|
|
|
{
|
2008-12-29 20:44:28 +00:00
|
|
|
Logger.Log.InfoFormat("Closing event queue at {0} due to missing caps URI", _Client.Location);
|
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
|
|
|
|
|
}
|
2008-05-06 23:57:26 +00:00
|
|
|
else if (!e.Cancelled)
|
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}",
|
|
|
|
|
_Client.Location, code);
|
2008-05-06 23:57:26 +00:00
|
|
|
}
|
|
|
|
|
else if (e.Error.InnerException != null)
|
|
|
|
|
{
|
2008-12-29 20:44:28 +00:00
|
|
|
Logger.Log.WarnFormat("Unrecognized caps exception from {0}: {1}",
|
|
|
|
|
_Client.Location, e.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}",
|
|
|
|
|
_Client.Location, e.Error.Message);
|
2008-04-22 18:59:39 +00:00
|
|
|
}
|
2007-12-06 00:58:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!e.Cancelled && e.Result != null)
|
|
|
|
|
{
|
2007-12-21 06:25:13 +00:00
|
|
|
// Got a response
|
2008-10-30 16:54:17 +00:00
|
|
|
OSD result = OSDParser.DeserializeLLSDXml(e.Result);
|
2008-10-30 01:50:59 +00:00
|
|
|
if (result != null && result.Type == OSDType.Map)
|
2007-12-06 00:58:28 +00:00
|
|
|
{
|
|
|
|
|
// Parse any events returned by the event queue
|
2008-10-30 01:50:59 +00:00
|
|
|
OSDMap map = (OSDMap)result;
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2008-10-30 01:50:59 +00:00
|
|
|
events = (OSDArray)map["events"];
|
2007-12-06 00:58:28 +00:00
|
|
|
ack = map["id"].AsInteger();
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-12-21 06:25:13 +00:00
|
|
|
else if (e.Cancelled)
|
|
|
|
|
{
|
|
|
|
|
// Connection was cancelled
|
2008-12-29 20:44:28 +00:00
|
|
|
Logger.Log.Debug("Cancelled connection to event queue at " + _Client.Location);
|
2007-12-21 06:25:13 +00:00
|
|
|
}
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
if (_Running)
|
|
|
|
|
{
|
2008-10-30 01:50:59 +00:00
|
|
|
OSDMap request = new OSDMap();
|
|
|
|
|
if (ack != 0) request["ack"] = OSD.FromInteger(ack);
|
|
|
|
|
else request["ack"] = new OSD();
|
|
|
|
|
request["done"] = OSD.FromBoolean(_Dead);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
2008-10-30 16:54:17 +00:00
|
|
|
byte[] postData = OSDParser.SerializeLLSDXmlBytes(request);
|
2007-12-06 00:58:28 +00:00
|
|
|
|
|
|
|
|
_Client.UploadDataAsync(_Client.Location, postData);
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Callback Handlers
|
|
|
|
|
}
|
|
|
|
|
}
|