示例#1
0
        /// <summary>
        /// Attempts the next transaction.
        /// </summary>
        public void AttemptNextTransaction()
        {
            HttpStatusCode statusCode;
            bool           exitLoop = false;

            lock (syncLock)
            {
                Device.Log.Debug(string.Format("AttemptNextTransaction:  count  {0}   exitloop: {1} ", this.Count, exitLoop));

                int qCount = this.Count;
                if (qCount <= 0)
                {
                    return;
                }

                while (this.Count > 0 && !exitLoop)
                {
                    RestfulObject <T> nextItem = Peek();

                    if (nextItem == null || nextItem.Verb == HttpVerb.None)
                    {
                        Device.Log.Debug(string.Format("AttemptNextTransaction:  next item is null or Verb == None"));
                        Dequeue();
                        continue;
                    }

                    try
                    {
                        Device.Log.Debug("Make Post Request: item: " + nextItem.Object.ToString() + " type: " + typeof(T).ToString());

                        RestfulObject <T> responseItem;
                        statusCode = MakeRequest(nextItem, out responseItem);

                        switch (statusCode)
                        {
                        case HttpStatusCode.OK:
                        case HttpStatusCode.Created:
                        case HttpStatusCode.Accepted:
                        case HttpStatusCode.NoContent:
                            Device.Log.Debug("Queue Processing Success: result status " + statusCode.ToString() + " item: " + nextItem.ToString() + " type: " + typeof(T).ToString());
                            Dequeue();
                            SerializeQueue();
                            if (OnRequestComplete != null && responseItem != null)
                            {
                                OnRequestComplete(responseItem, responseItem.Verb);
                            }
                            break;

                        case HttpStatusCode.Unauthorized:
                        case HttpStatusCode.ServiceUnavailable:
                        case HttpStatusCode.RequestTimeout:
                        case HttpStatusCode.BadGateway:
                        case HttpStatusCode.GatewayTimeout:
                        case (HttpStatusCode)(-1):       // application exception from post
                        case (HttpStatusCode)(-2):       // no response object from post
                            Device.Log.Debug("Halt Queue Processing: item: " + nextItem.ToString() + " type: " + typeof(T).ToString());
                            // do not remove from queue but halt processing to prevent repeated calls to server until authorized
                            exitLoop = true;
                            if (OnRequestError != null)
                            {
                                OnRequestError(nextItem, nextItem.Verb, statusCode);
                            }
                            if (DequeueOnError)
                            {
                                Device.Log.Debug("Removing Object From Queue: item: " + nextItem.ToString() + " type: " + typeof(T).ToString());
                                Dequeue();
                                SerializeQueue();
                            }
                            continue;

                        //break;
                        default:
                            Device.Log.Debug(string.Format("Restful Queue<{0}>: Received Status {1} for {2} to {3}.  Removing from Queue.", typeof(T).Name, statusCode, nextItem.Verb, nextItem.TransactionEndpoint));
                            if (OnRequestError != null)
                            {
                                OnRequestError(nextItem, nextItem.Verb, statusCode);
                            }
                            Dequeue();
                            SerializeQueue();
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        if (OnRequestFailed != null)
                        {
                            OnRequestFailed(nextItem, nextItem.Verb, exc);
                        }
                    }
                }
            }
        }