예제 #1
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);
        }