예제 #1
0
        private void Client_UploadDataCompleted(object sender, CapsBase.UploadDataCompletedEventArgs e)
        {
            if (OnComplete != null && !e.Cancelled)
            {
                if (e.Error == null)
                {
                    OSD result = OSDParser.DeserializeLLSDXml(e.Result);

                    try { OnComplete(this, result, e.Error); }
                    catch (Exception ex) { Logger.Log.Error(ex.Message, ex); }
                }
                else
                {
                    // Some error occurred, try to figure out what happened
                    HttpStatusCode code = HttpStatusCode.OK;
                    if (e.Error is WebException && ((WebException)e.Error).Response != null)
                    {
                        code = ((HttpWebResponse)((WebException)e.Error).Response).StatusCode;
                    }

                    if (code == HttpStatusCode.BadGateway)
                    {
                        // This is not good (server) protocol design, but it's normal.
                        // The CAPS server is a proxy that connects to a Squid
                        // cache which will time out periodically. The CAPS server
                        // interprets this as a generic error and returns a 502 to us
                        // that we ignore
                        StartRequest(_PostData, _ContentType);
                    }
                    else if (code != HttpStatusCode.OK)
                    {
                        // Status code was set to something unknown, this is a failure
                        Logger.Log.DebugFormat("Caps error at {0}: {1}", _Client.Location, code);

                        try { OnComplete(this, null, e.Error); }
                        catch (Exception ex) { Logger.Log.Error(ex.Message, ex); }
                    }
                    else
                    {
                        // Status code was not set, some other error occurred. This is a failure
                        Logger.Log.DebugFormat("Caps error at {0}: {1}", _Client.Location, e.Error.Message);

                        try { OnComplete(this, null, e.Error); }
                        catch (Exception ex) { Logger.Log.Error(ex.Message, ex); }
                    }
                }
            }
            else if (e.Cancelled)
            {
                Logger.Log.Debug("Capability action at " + _Client.Location + " cancelled");
            }
        }
예제 #2
0
        private void Client_UploadDataCompleted(object sender, CapsBase.UploadDataCompletedEventArgs e)
        {
            OSDArray events = null;
            int      ack    = 0;

            if (e.Error != null)
            {
                HttpStatusCode code = HttpStatusCode.OK;
                if (e.Error is WebException && ((WebException)e.Error).Response != null)
                {
                    code = ((HttpWebResponse)((WebException)e.Error).Response).StatusCode;
                }

                if (code == HttpStatusCode.NotFound || code == HttpStatusCode.Gone)
                {
                    Logger.Log.InfoFormat("Closing event queue at {0} due to missing caps URI", _Client.Location);

                    _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 if (!e.Cancelled)
                {
                    // Try to log a meaningful error message
                    if (code != HttpStatusCode.OK)
                    {
                        Logger.Log.WarnFormat("Unrecognized caps connection problem from {0}: {1}",
                                              _Client.Location, code);
                    }
                    else if (e.Error.InnerException != null)
                    {
                        Logger.Log.WarnFormat("Unrecognized caps exception from {0}: {1}",
                                              _Client.Location, e.Error.InnerException.Message);
                    }
                    else
                    {
                        Logger.Log.WarnFormat("Unrecognized caps exception from {0}: {1}",
                                              _Client.Location, e.Error.Message);
                    }
                }
            }
            else if (!e.Cancelled && e.Result != null)
            {
                // Got a response
                OSD result = OSDParser.DeserializeLLSDXml(e.Result);
                if (result != null && result.Type == OSDType.Map)
                {
                    // Parse any events returned by the event queue
                    OSDMap map = (OSDMap)result;

                    events = (OSDArray)map["events"];
                    ack    = map["id"].AsInteger();
                }
            }
            else if (e.Cancelled)
            {
                // Connection was cancelled
                Logger.Log.Debug("Cancelled connection to event queue at " + _Client.Location);
            }

            if (_Running)
            {
                OSDMap request = new OSDMap();
                if (ack != 0)
                {
                    request["ack"] = OSD.FromInteger(ack);
                }
                else
                {
                    request["ack"] = new OSD();
                }
                request["done"] = OSD.FromBoolean(_Dead);

                byte[] postData = OSDParser.SerializeLLSDXmlBytes(request);

                _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;
                    Logger.Log.Debug("Sent event queue shutdown message");
                }
            }

            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.Error(ex.Message, ex); }
                }
            }
        }