Ejemplo n.º 1
0
        // helper method for beacon sending with retries
        private StatusResponse SendBeaconRequest(IHTTPClient httpClient, byte[] beaconData, int numRetries)
        {
            StatusResponse response         = null;
            var            retry            = 0;
            var            retrySleepMillis = INITIAL_RETRY_SLEEP_TIME_MILLISECONDS;

            while (true)
            {
                response = httpClient.SendBeaconRequest(clientIPAddress, beaconData);
                if (response != null || (retry >= numRetries))
                {
                    // success OR max retry count reached
                    break;
                }

                timingProvider.Sleep(retrySleepMillis);
                retrySleepMillis *= 2;
                retry++;
            }

            return(response);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// send current state of Beacon
        /// </summary>
        /// <param name="httpClientProvider"></param>
        /// <param name="numRetries"></param>
        /// <returns></returns>
        public StatusResponse Send(IHTTPClientProvider httpClientProvider)
        {
            var            httpClient = httpClientProvider.CreateClient(httpConfiguration);
            StatusResponse response   = null;

            while (true)
            {
                // prefix for this chunk - must be built up newly, due to changing timestamps
                var prefix = basicBeaconData + BEACON_DATA_DELIMITER + CreateTimestampData();
                // subtract 1024 to ensure that the chunk does not exceed the send size configured on server side?
                // i guess that was the original intention, but i'm not sure about this
                // TODO stefan.eberl - This is a quite uncool algorithm and should be improved, avoid subtracting some "magic" number
                var chunk = beaconCache.GetNextBeaconChunk(sessionNumber, prefix, configuration.MaxBeaconSize - 1024, BEACON_DATA_DELIMITER);
                if (string.IsNullOrEmpty(chunk))
                {
                    // no data added so far or no data to send
                    return(response);
                }

                byte[] encodedBeacon = Encoding.UTF8.GetBytes(chunk);

                // send the request
                response = httpClient.SendBeaconRequest(clientIPAddress, encodedBeacon);
                if (response == null)
                {
                    // error happened - but don't know what exactly
                    // reset the previously retrieved chunk (restore it in internal cache) & retry another time
                    beaconCache.ResetChunkedData(sessionNumber);
                    break;
                }
                else
                {
                    // worked -> remove previously retrieved chunk from cache
                    beaconCache.RemoveChunkedData(sessionNumber);
                }
            }

            return(response);
        }
 public void IsErroneousResponseGivesTrueForErrorCodeGreaterThan400()
 {
     // when, then
     Assert.That(StatusResponse.CreateErrorResponse(logger, 401).IsErroneousResponse, Is.True);
 }
Ejemplo n.º 4
0
        // generic internal request send
        protected IStatusResponse SendRequestInternal(RequestType requestType, string url, string clientIpAddress,
                                                      byte[] data, string method)
        {
            int retry = 1;

            while (true)
            {
                try
                {
                    // if beacon data is available gzip it
                    byte[] gzippedData = null;
                    if ((data != null) && (data.Length > 0))
                    {
                        gzippedData = CompressByteArray(data);

                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug(GetType().Name + " Beacon Payload: " +
                                         Encoding.UTF8.GetString(data, 0, data.Length));
                        }
                    }

                    HttpResponse httpResponse;
                    if (method == "GET")
                    {
                        httpResponse = GetRequest(url, clientIpAddress);
                    }
                    else if (method == "POST")
                    {
                        httpResponse = PostRequest(url, clientIpAddress, gzippedData);
                    }
                    else
                    {
                        return(UnknownErrorResponse(requestType));
                    }

                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug(GetType().Name + " HTTP Response: " + httpResponse.Response);
                        logger.Debug(GetType().Name + " HTTP Response Code: " + httpResponse.ResponseCode);
                    }

                    // create typed response based on request type and response content
                    if ((requestType.RequestName == RequestType.Beacon.RequestName) ||
                        (requestType.RequestName == RequestType.Status.RequestName) ||
                        (requestType.RequestName == RequestType.NewSession.RequestName))
                    {
                        return(httpResponse.Response == null || httpResponse.ResponseCode >= 400
                            ? StatusResponse.CreateErrorResponse(logger, httpResponse.ResponseCode, httpResponse.Headers)
                            : ParseStatusResponse(httpResponse));
                    }

                    logger.Warn(GetType().Name + " Unknown request type " + requestType + " - ignoring response");
                    return(UnknownErrorResponse(requestType));
                }
                catch (Exception)
                {
                    retry++;
                    if (retry > MaxSendRetries)
                    {
                        throw;
                    }

                    threadSuspender.Sleep(RetrySleepTime);
                }
            }
        }
 public void ResponseCodeIsSet()
 {
     // given
     Assert.That(StatusResponse.CreateErrorResponse(logger, 418).ResponseCode, Is.EqualTo(418));
 }