void requestCompletedHandler(HttpRequestBase request, HttpResponse response, byte[] responseData, Exception error)
{
// We don't care about this request now that it has completed
_Request = null;
OSDArray events = null;
int ack = 0;
if (responseData != null)
{
_errorCount = 0;
// Got a response
OSDMap result = (OSDMap)XmlLLSDOSDParser.DeserializeLLSDXml(responseData);
if (result != null)
{
events = (OSDArray)result.get("events");
ack = result.get("id").asInteger();
}
else
{
try {
logger.warning("Got an unparseable response from the event queue: \"" +
Utils.bytesToString(responseData) + "\"");
} catch (UnsupportedEncodingException e) {
logger.warning("Got an unparseable response from the event queue: \"" +
Utils.getExceptionStackTraceAsString(e) + "\"");
}
}
}
else if (error != null)
{
//TODO Handle the case when the request is aborted or there is some client side error
if(response !=null)
{
//region Error handling
int code = response.getStatusLine().getStatusCode();
if(code == HttpStatus.SC_GONE || code == HttpStatus.SC_NOT_FOUND)
{
logger.info(String.format("Closing event queue at {0} due to missing caps URI", _Address));
_Running = false;
_Dead = true;
}
else if(code == HttpStatus.SC_BAD_GATEWAY)
{
// 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
}
else
{
++_errorCount;
// Try to log a meaningful error message
if (code != HttpStatus.SC_OK)
{
logger.warning(String.format("Unrecognized caps connection problem from %s: %s",
_Address, code));
}
else
{
logger.warning(String.format("Unrecognized caps exception from %s: %s",
_Address, error.getMessage()));
}
}
}
else
{
++_errorCount;
logger.warning("Some client Side Error" + Utils.getExceptionStackTraceAsString(error));
}
}
else
{
++_errorCount;
logger.warning("No response from the event queue but no reported error either");
}
// if (error instanceof HttpException)
// {
// HttpException webException = (HttpException)error;
//
// if (response != null)
// code = response.getStatusLine().getStatusCode();
// else if (webException.Status == WebExceptionStatus.RequestCanceled)
// goto HandlingDone;
// }
//
// if (error is WebException && ((WebException)error).Response != null)
// code = ((HttpWebResponse)((WebException)error).Response).StatusCode;
//
// if (code == HttpStatusCode.NotFound || code == HttpStatusCode.Gone)
// {
// Logger.Log(String.format("Closing event queue at {0} due to missing caps URI", _Address), Helpers.LogLevel.Info);
//
// _Running = false;
// _Dead = true;
// }
// 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
// }
// else
// {
// ++_errorCount;
//
// // Try to log a meaningful error message
// if (code != HttpStatusCode.OK)
// {
// Logger.Log(String.format("Unrecognized caps connection problem from {0}: {1}",
// _Address, code), Helpers.LogLevel.Warning);
// }
// else if (error.InnerException != null)
// {
// Logger.Log(String.Format("Unrecognized internal caps exception from {0}: {1}",
// _Address, error.InnerException.Message), Helpers.LogLevel.Warning);
// }
// else
// {
// Logger.Log(String.Format("Unrecognized caps exception from {0}: {1}",
// _Address, error.Message), Helpers.LogLevel.Warning);
// }
// }
//
// //endregion Error handling
// }
// else
// {
// ++_errorCount;
//
// Logger.Log("No response from the event queue but no reported error either", Helpers.LogLevel.Warning);
// }
//
// HandlingDone:
//
// //region Resume the connection
//
// if (_Running)
// {
// OSDMap osdRequest = new OSDMap();
// if (ack != 0) osdRequest["ack"] = OSD.FromInteger(ack);
// else osdRequest["ack"] = new OSD();
// osdRequest["done"] = OSD.FromBoolean(_Dead);
//
// byte[] postData = OSDParser.SerializeLLSDXmlBytes(osdRequest);
//
// if (_errorCount > 0) // Exponentially back off, so we don't hammer the CPU
// Thread.Sleep(_random.Next(500 + (int)Math.Pow(2, _errorCount)));
//
// // Resume the connection. The event handler for the connection opening
// // just sets class _Request variable to the current HttpRequest
// CapsBase.UploadDataAsync(_Address, null, "application/xml", postData, REQUEST_TIMEOUT,
// delegate(HttpRequest newRequest) { _Request = newRequest; }, null, RequestCompletedHandler);
//
// // 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;
// Logger.DebugLog("Sent event queue shutdown message");
// }
// }
//
// //endregion Resume the connection
//
// //region Handle incoming events
//
// if (OnEvent != null && events != null && events.Count > 0)
// {
// // Fire callbacks for each event received
// foreach (OSDMap evt in events)
// {
// string msg = evt["message"].AsString();
// OSDMap body = (OSDMap)evt["body"];
//
// try { OnEvent(msg, body); }
// catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); }
// }
// }
//
//// endregion Handle incoming events
//region Handle incoming events
if (eventObservable != null && events != null && events.count() > 0)
{
//Fire callbacks for each event received
Iterator<OSD> itr = events.iterator();
while(itr.hasNext())
{
OSDMap evt = (OSDMap)itr.next();
String msg = evt.get("message").asString();
OSDMap body = (OSDMap)evt.get("body");