Example #1
0
 internal GetMappingResponse(ConnectionStatus status, Dictionary<string, RootObjectMapping> dict)
 {
     this.IsValid = status.Success;
     if (dict == null || dict.Count <= 0) return;
     var mapping = dict.First();
     if (mapping.Value == null)
         return;
     mapping.Value.Name = mapping.Key;
     this.Mapping = mapping.Value;
 }
Example #2
0
 protected virtual Task<ConnectionStatus> DoAsyncRequest(HttpWebRequest request, string data = null)
 {
     return Task.Factory.StartNew<ConnectionStatus>(() =>
     {
         var status = new ConnectionStatus("{ \"status\" : \"USING NEST IN MEMORY CONNECTION\" }")
         {
             Request = data,
             RequestUrl = request.RequestUri.ToString(),
             RequestMethod = request.Method
         };
         return status;
     });
 }
Example #3
0
 private void ConnectionStatusDefaultHandler(ConnectionStatus status)
 {
     return;
 }
Example #4
0
        private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ConnectionStatus> tcs, string data = null)
        {
            using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
            {
                var timeout = this._ConnectionSettings.Timeout;

                var state = new ConnectionState { Connection = request };

                if (data != null)
                {
                    var getRequestStream = Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null);
                    //ThreadPool.RegisterWaitForSingleObject((getRequestStream as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                    yield return getRequestStream;

                    var requestStream = getRequestStream.Result;
                    try
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(data);
                        var writeToRequestStream = Task.Factory.FromAsync(requestStream.BeginWrite, requestStream.EndWrite, buffer, 0, buffer.Length, state);
                        yield return writeToRequestStream;
                    }
                    finally
                    {
                        requestStream.Close();
                    }
                }

                // Get the response
                var getResponse = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                //ThreadPool.RegisterWaitForSingleObject((getResponse as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                yield return getResponse;

                // Get the response stream
                using (var response = (HttpWebResponse)getResponse.Result)
                using (var responseStream = response.GetResponseStream())
                {
                    // Copy all data from the response stream
                    var output = new MemoryStream();
                    var buffer = new byte[BUFFER_SIZE];
                    while (responseStream != null)
                    {
                        var read = Task<int>.Factory.FromAsync(responseStream.BeginRead, responseStream.EndRead, buffer, 0, BUFFER_SIZE, null);
                        yield return read;
                        if (read.Result == 0) break;
                        output.Write(buffer, 0, read.Result);
                    }

                    // Decode the data and store the result
                    var result = Encoding.UTF8.GetString(output.ToArray());
                    var cs = new ConnectionStatus(result) { Request = data, RequestUrl = request.RequestUri.ToString(), RequestMethod = request.Method };
                    tcs.TrySetResult(cs);
                    tracer.SetResult(cs);
                    _ConnectionSettings.ConnectionStatusHandler(cs);
                }
                yield break;
            }
        }
Example #5
0
        protected virtual ConnectionStatus DoSynchronousRequest(HttpWebRequest request, string data = null)
        {
            using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
            {
                ConnectionStatus cs = null;
                if (data != null)
                {
                    using (var r = request.GetRequestStream())
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(data);
                        r.Write(buffer, 0, buffer.Length);
                    }
                }
                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    using (var responseStream = response.GetResponseStream())
                    using (var streamReader = new StreamReader(responseStream))
                    {
                        string result = streamReader.ReadToEnd();
                        cs = new ConnectionStatus(result)
                        {
                            Request = data,
                            RequestUrl = request.RequestUri.ToString(),
                            RequestMethod = request.Method
                        };
                        tracer.SetResult(cs);
                        return cs;
                    }
                }
                catch (WebException webException)
                {
                    cs = new ConnectionStatus(webException)
                    {
                        Request = data,
                        RequestUrl = request.RequestUri.ToString(),
                        RequestMethod = request.Method
                    };
                    tracer.SetResult(cs);

                    _ConnectionSettings.ConnectionStatusHandler(cs);

                    return cs;
                }
            }
        }
Example #6
0
        protected virtual Task<ConnectionStatus> DoAsyncRequest(HttpWebRequest request, string data = null)
        {
            var tcs = new TaskCompletionSource<ConnectionStatus>();
            if (this._ConnectionSettings.MaximumAsyncConnections <= 0)
                return this.CreateIterateTask(request, data, tcs);

            var timeout = this._ConnectionSettings.Timeout;
            if (!this._ResourceLock.WaitOne(timeout))
            {
                using (var tracer = new ConnectionStatusTracer(this._ConnectionSettings.TraceEnabled))
                {
                    var m = "Could not start the operation before the timeout of " + timeout +
                        "ms completed while waiting for the semaphore";
                    var cs = new ConnectionStatus(new TimeoutException(m));
                    tcs.SetResult(cs);
                    tracer.SetResult(cs);
                    return tcs.Task;
                }
            }
            try
            {
                return Task.Factory.StartNew(
                    () => this.CreateIterateTask(request, data, tcs).Result
                    , TaskCreationOptions.LongRunning
                );
            }
            finally
            {
                this._ResourceLock.Release();
            }
        }
Example #7
0
 public InMemoryConnection(IConnectionSettings settings, ConnectionStatus fixedResult)
     : base(settings)
 {
     this._fixedResult = fixedResult;
 }
Example #8
0
        private ConnectionStatus Execute(RestRequest restRequest)
        {
            //RestResponse result = GetClient().execute(restRequest);
            //

            if (!this._resourceLock.WaitOne(this._timeout))
            {
                var m = "Could not start the thrift operation before the timeout of " + this._timeout + "ms completed while waiting for the semaphore";
                return new ConnectionStatus(this._connectionSettings, new TimeoutException(m));
            }
            try
            {
                Rest.Client client = null;
                if (!this._clients.TryDequeue(out client))
                {
                    var m = string.Format("Could dequeue a thrift client from internal socket pool of size {0}", this._poolSize);
                    var status = new ConnectionStatus(this._connectionSettings, new Exception(m));
                    return status;
                }
                try
                {
                    if (!client.InputProtocol.Transport.IsOpen)
                        client.InputProtocol.Transport.Open();

                    var result = client.execute(restRequest);
                    if (result.Status == Status.OK || result.Status == Status.CREATED || result.Status == Status.ACCEPTED)
                        return new ConnectionStatus(this._connectionSettings, DecodeStr(result.Body));
                    else
                    {
                        var connectionException = new ConnectionException(
                            msg: Enum.GetName(typeof (Status), result.Status),
                            statusCode: (int)result.Status,
                            response: DecodeStr(result.Body)
                        );
                        return new ConnectionStatus(this._connectionSettings, connectionException);
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    //make sure we make the client available again.
                    this._clients.Enqueue(client);
                }

            }
            catch (Exception e)
            {
                return new ConnectionStatus(this._connectionSettings, e);
            }
            finally
            {
                this._resourceLock.Release();
            }
        }
Example #9
0
 private ConnectionStatus DoSynchronousRequest(HttpWebRequest request, string data = null)
 {
     request.Timeout = this._ConnectionSettings.TimeOut;
     Stream postStream = null;
     WebResponse response = null;
     try
     {
         if (!data.IsNullOrEmpty())
         {
             byte[] buffer = Encoding.UTF8.GetBytes(data);
             request.ContentLength = buffer.Length;
             postStream = request.GetRequestStream();
             postStream.Write(buffer, 0, buffer.Length);
             postStream.Close();
         }
         response = request.GetResponse();
         var result = new StreamReader(response.GetResponseStream()).ReadToEnd();
         response.Close();
         return new ConnectionStatus(result);
     }
     catch (WebException e)
     {
         string result;
         var error = this.GetConnectionErrorFromWebException(e, out result);
         var status = new ConnectionStatus(error);
         status.Result = result;
         return status;
     }
     catch (Exception e) { return new ConnectionStatus(new ConnectionError(e)); }
     finally
     {
         if (postStream != null)
             postStream.Close();
         if (response != null)
             response.Close();
     }
 }
Example #10
0
 internal IndexExistsResponse(ConnectionStatus connectionStatus)
 {
     this.ConnectionStatus = connectionStatus;
     this.IsValid = connectionStatus.Error == null || connectionStatus.Error.HttpStatusCode == HttpStatusCode.NotFound;
     this.Exists = connectionStatus.Error == null && connectionStatus.Success;
 }
Example #11
0
 public ReindexException(ConnectionStatus status, string message = null)
     : base(message)
 {
     this.Status = status;
 }
Example #12
0
        private IEnumerable<Task> _AsyncSteps(HttpWebRequest request, TaskCompletionSource<ConnectionStatus> tcs, string data = null)
        {
            var timeout = this._ConnectionSettings.Timeout;

            if (!this._ResourceLock.WaitOne(timeout))
            {
                var m = "Could not start the operation before the timeout of " + timeout + "ms completed while waiting for the semaphore";
                tcs.SetResult(new ConnectionStatus(new TimeoutException(m)));
                yield break;
            }
            try
            {
                var state = new ConnectionState { Connection = request };

                if (data != null)
                {
                    var getRequestStream = Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null);
                    ThreadPool.RegisterWaitForSingleObject((getRequestStream as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                    yield return getRequestStream;

                    var requestStream = getRequestStream.Result;
                    try
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(data);
                        var writeToRequestStream = Task.Factory.FromAsync(requestStream.BeginWrite, requestStream.EndWrite, buffer, 0, buffer.Length, state);
                        yield return writeToRequestStream;
                    }
                    finally
                    {
                        requestStream.Close();
                    }
                }

                // Get the response
                var getResponse = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
                ThreadPool.RegisterWaitForSingleObject((getResponse as IAsyncResult).AsyncWaitHandle, ThreadTimeoutCallback, request, timeout, true);
                yield return getResponse;

                // Get the response stream
                using (var response = (HttpWebResponse)getResponse.Result)
                using (var responseStream = response.GetResponseStream())
                {
                    // Copy all data from the response stream
                    var output = new MemoryStream();
                    var buffer = new byte[BUFFER_SIZE];
                    while (true)
                    {
                        var read = Task<int>.Factory.FromAsync(responseStream.BeginRead, responseStream.EndRead, buffer, 0, BUFFER_SIZE, null);
                        yield return read;
                        if (read.Result == 0) break;
                        output.Write(buffer, 0, read.Result);
                    }

                    // Decode the data and store the result
                    var result = Encoding.UTF8.GetString(output.ToArray());
                    var cs = new ConnectionStatus(result) { Request = data };
                    tcs.TrySetResult(cs);
                }
            }
            finally
            {
                this._ResourceLock.Release();
            }
        }