public RiakResult Read(IRiakCommand command) { bool done = false; do { int size = ReadMessageSize(command.ExpectedCode); byte[] buffer = null; if (size > 0) { buffer = ReceiveAll(new byte[size]); } RiakResp response = command.DecodeResponse(buffer); command.OnSuccess(response); var streamingResponse = response as IRpbStreamingResp; if (streamingResponse == null) { done = true; } else { done = streamingResponse.done; } }while (done == false); return(RiakResult.Success()); }
public RiakResult Read(IRiakCommand command) { bool done = false; do { MessageCode expectedCode = command.ExpectedCode; Type expectedType = MessageCodeTypeMapBuilder.GetTypeFor(expectedCode); int size = DoRead(expectedCode, expectedType); RpbResp response = DeserializeInstance(expectedType, size); command.OnSuccess(response); var streamingResponse = response as IRpbStreamingResp; if (streamingResponse == null) { done = true; } else { done = streamingResponse.done; } }while (done == false); return(RiakResult.Success()); }
public RiakResult <TResult> PbcRead <TResult>() where TResult : class, new() { try { var result = socket.Read <TResult>(); return(RiakResult <TResult> .Success(result)); } catch (RiakException ex) { if (ex.NodeOffline) { Disconnect(); } if (ex.Message.Contains("Bucket cannot be zero-length") || ex.Message.Contains("Key cannot be zero-length")) { return(RiakResult <TResult> .FromException(ResultCode.InvalidRequest, ex, ex.NodeOffline)); } return(RiakResult <TResult> .FromException(ResultCode.CommunicationError, ex, ex.NodeOffline)); } catch (Exception ex) { Disconnect(); return(RiakResult <TResult> .FromException(ResultCode.CommunicationError, ex, true)); } }
public RiakResult <IEnumerable <RiakResult <TResult> > > PbcRepeatRead <TResult>(Func <RiakResult <TResult>, bool> repeatRead) where TResult : class, new() { var results = new List <RiakResult <TResult> >(); try { RiakResult <TResult> result; do { result = RiakResult <TResult> .Success(_socket.Read <TResult>()); results.Add(result); } while(repeatRead(result)); return(RiakResult <IEnumerable <RiakResult <TResult> > > .Success(results)); } catch (RiakException ex) { if (ex.NodeOffline) { Disconnect(); } return(RiakResult <IEnumerable <RiakResult <TResult> > > .Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline)); } catch (Exception ex) { Disconnect(); return(RiakResult <IEnumerable <RiakResult <TResult> > > .Error(ResultCode.CommunicationError, ex.Message, true)); } }
public RiakResult <IEnumerable <RiakResult <TResult> > > PbcStreamRead <TResult>(Func <RiakResult <TResult>, bool> repeatRead, Action onFinish) where TResult : class, new() { var streamer = PbcStreamReadIterator(repeatRead, onFinish); return(RiakResult <IEnumerable <RiakResult <TResult> > > .Success(streamer)); }
public void SetUp() { var result = RiakResult <RiakRestResponse> .Success(new RiakRestResponse { StatusCode = System.Net.HttpStatusCode.NoContent }); Cluster.ConnectionMock.Setup(m => m.RestRequest(It.IsAny <RiakRestRequest>())).Returns(result); Response = Client.SetBucketProperties("foo", new RiakBucketProperties().SetAllowMultiple(true).SetRVal("one")); }
public RiakResult <IEnumerable <RiakResult <TResult> > > PbcWriteStreamRead <TRequest, TResult>(TRequest request, Func <RiakResult <TResult>, bool> repeatRead, Action onFinish) where TResult : new() { var streamer = PbcWriteStreamReadIterator(request, repeatRead, onFinish); return(RiakResult <IEnumerable <RiakResult <TResult> > > .Success(streamer)); }
public void SetUp() { var result = RiakResult.Success(); Cluster.ConnectionMock.Setup(m => m.PbcWriteRead(It.IsAny <RpbSetBucketReq>(), MessageCode.SetBucketResp)).Returns(result); Response = Client.SetBucketProperties("foo", new RiakBucketProperties().SetAllowMultiple(true)); }
public RiakResult PbcWrite <TRequest>(TRequest request) { try { _socket.Write(request); return(RiakResult.Success()); } catch (Exception ex) { Disconnect(); return(RiakResult.Error(ResultCode.CommunicationError, ex.Message)); } }
public Task <RiakResult <IEnumerable <RiakResult <TResult> > > > PbcWriteStreamRead <TResult>(MessageCode messageCode, Func <RiakResult <TResult>, bool> repeatRead, Action onFinish) where TResult : class, new() { return(PbcWrite(messageCode) .ContinueWith((Task <RiakResult> writeTask) => { var writeResult = writeTask.Result; if (writeResult.IsSuccess) { var streamer = PbcStreamReadIterator(repeatRead, onFinish); return RiakResult <IEnumerable <RiakResult <TResult> > > .Success(streamer); } return RiakResult <IEnumerable <RiakResult <TResult> > > .Error(writeResult.ResultCode, writeResult.ErrorMessage, writeResult.NodeOffline); })); }
public RiakResult Read(IRiakCommand command) { MessageCode expectedCode = command.ExpectedCode; Type expectedType = MessageCodeTypeMapBuilder.GetTypeFor(expectedCode); int size = DoRead(expectedCode, expectedType); RpbResp response = DeserializeInstance(expectedType, size); command.OnSuccess(response); return(RiakResult.Success()); }
public RiakResult <TResult> PbcRead <TResult>() where TResult : new() { try { var result = _socket.Read <TResult>(); return(RiakResult <TResult> .Success(result)); } catch (Exception ex) { Disconnect(); return(RiakResult <TResult> .Error(ResultCode.CommunicationError, ex.Message)); } }
public Task <RiakResult> PbcWrite(MessageCode messageCode) { return(_socket.WriteAsync(messageCode) .ContinueWith((Task finishedTask) => { if (!finishedTask.IsFaulted) { return RiakResult.Success(); } else { return GetExceptionResult(finishedTask.Exception); } })); }
public Task <RiakResult> PbcWrite <TRequest>(TRequest request) where TRequest : class { return(_socket.WriteAsync <TRequest>(request) .ContinueWith((Task finishedTask) => { if (!finishedTask.IsFaulted) { return RiakResult.Success(); } else { return GetExceptionResult(finishedTask.Exception); } })); }
public Task <RiakResult> PbcRead(MessageCode expectedMessageCode) { return(_socket.ReadAsync(expectedMessageCode) .ContinueWith((Task <MessageCode> finishedTask) => { if (!finishedTask.IsFaulted) { return RiakResult.Success(); } else { return GetExceptionResult(finishedTask.Exception); } })); }
public Task <RiakResult <TResult> > PbcRead <TResult>() where TResult : class, new() { return(_socket.ReadAsync <TResult>() .ContinueWith((Task <TResult> finishedTask) => { if (!finishedTask.IsFaulted) { return RiakResult <TResult> .Success(finishedTask.Result); } else { return GetExceptionResult <TResult>(finishedTask.Exception); } })); }
public RiakResult Write(IRiakCommand command) { RpbReq request = command.ConstructPbRequest(); if (request.IsMessageCodeOnly) { Write(request.MessageCode); return(RiakResult.Success()); } else { return(DoWrite(s => Serializer.Serialize(s, request), request.GetType())); } }
public RiakResult Write(IRiakCommand command) { RiakReq request = command.ConstructRequest(useTtb); if (request.IsMessageCodeOnly) { Write(request.MessageCode); return(RiakResult.Success()); } else { return(DoWrite(s => request.WriteTo(s), request.MessageCode)); } }
private RiakResult DoWrite(Action <MemoryStream> serializer, MessageCode messageCode) { byte[] messageBody; int messageLength = 0; using (var memStream = new MemoryStream()) { // add a buffer to the start of the array to put the size and message code memStream.Position += PbMsgHeaderSize; serializer(memStream); messageBody = memStream.GetBuffer(); messageLength = (int)memStream.Position; } // check to make sure something was written, otherwise we'll have to create a new array if (messageLength == PbMsgHeaderSize) { messageBody = new byte[PbMsgHeaderSize]; } byte[] size = BitConverter.GetBytes(messageLength - PbMsgHeaderSize + 1); if (BitConverter.IsLittleEndian) { Array.Reverse(size); } Array.Copy(size, messageBody, SizeOfInt); messageBody[SizeOfInt] = (byte)messageCode; if (NetworkStream.CanWrite) { NetworkStream.Write(messageBody, 0, messageLength); } else { string errorMessage = string.Format("Failed to send data to server - Can't write: {0}:{1}", server, port); throw new RiakException(errorMessage, true); } return(RiakResult.Success()); }
public RiakResult PbcWrite(MessageCode messageCode) { try { _socket.Write(messageCode); return(RiakResult.Success()); } catch (RiakException ex) { if (ex.NodeOffline) { Disconnect(); } return(RiakResult.Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline)); } catch (Exception ex) { Disconnect(); return(RiakResult.Error(ResultCode.CommunicationError, ex.Message, true)); } }
public RiakResult PbcWrite <TRequest>(TRequest request) where TRequest : class { try { _socket.Write(request); return(RiakResult.Success()); } catch (RiakException ex) { if (ex.NodeOffline) { Disconnect(); } return(RiakResult.Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline)); } catch (Exception ex) { Disconnect(); return(RiakResult.Error(ResultCode.CommunicationError, ex.Message, true)); } }
public RiakResult <TResult> PbcRead <TResult>() where TResult : class, new() { try { var result = _socket.Read <TResult>(); return(RiakResult <TResult> .Success(result)); } catch (RiakException ex) { if (ex.NodeOffline) { Disconnect(); } return(RiakResult <TResult> .Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline)); } catch (Exception ex) { Disconnect(); return(RiakResult <TResult> .Error(ResultCode.CommunicationError, ex.Message, true)); } }
public RiakResult PbcRead(MessageCode expectedMessageCode) { try { socket.Read(expectedMessageCode); return(RiakResult.Success()); } catch (RiakException ex) { if (ex.NodeOffline) { Disconnect(); } return(RiakResult.FromException(ResultCode.CommunicationError, ex, ex.NodeOffline)); } catch (Exception ex) { Disconnect(); return(RiakResult.FromException(ResultCode.CommunicationError, ex, true)); } }
public Task <RiakResult <IEnumerable <RiakResult <TResult> > > > PbcRepeatRead <TResult>(Func <RiakResult <TResult>, bool> repeatRead) where TResult : class, new() { var source = new TaskCompletionSource <RiakResult <IEnumerable <RiakResult <TResult> > > >(); var resultsList = new List <RiakResult <TResult> >(); // repeat as a continuation Action readNext = null; readNext = (() => { _socket.ReadAsync <TResult>() .ContinueWith((Task <TResult> readTask) => { var result = RiakResult <TResult> .Success(readTask.Result); resultsList.Add(result); try { if (repeatRead(result)) { readNext(); } else { source.SetResult(RiakResult <IEnumerable <RiakResult <TResult> > > .Success(resultsList)); } } catch (Exception ex) { source.SetException(ex); } }); }); // begin reading and completion task readNext(); return(source.Task); }
public RiakResult <RiakRestResponse> RestRequest(RiakRestRequest request) { var baseUri = new StringBuilder(_restRootUrl).Append(request.Uri); if (request.QueryParams.Count > 0) { baseUri.Append("?"); var first = request.QueryParams.First(); baseUri.Append(first.Key.UrlEncoded()).Append("=").Append(first.Value.UrlEncoded()); request.QueryParams.Skip(1).ForEach(kv => baseUri.Append("&").Append(kv.Key.UrlEncoded()).Append("=").Append(kv.Value.UrlEncoded())); } var targetUri = new Uri(baseUri.ToString()); var req = (HttpWebRequest)WebRequest.Create(targetUri); req.KeepAlive = true; req.Method = request.Method; req.Credentials = CredentialCache.DefaultCredentials; if (!string.IsNullOrWhiteSpace(request.ContentType)) { req.ContentType = request.ContentType; } if (!request.Cache) { req.Headers.Set(RiakConstants.Rest.HttpHeaders.DisableCacheKey, RiakConstants.Rest.HttpHeaders.DisableCacheValue); } request.Headers.ForEach(h => req.Headers.Set(h.Key, h.Value)); if (request.Body != null && request.Body.Length > 0) { req.ContentLength = request.Body.Length; using (var writer = req.GetRequestStream()) { writer.Write(request.Body, 0, request.Body.Length); } } else { req.ContentLength = 0; } try { var response = (HttpWebResponse)req.GetResponse(); var result = new RiakRestResponse { ContentLength = response.ContentLength, ContentType = response.ContentType, StatusCode = response.StatusCode, Headers = response.Headers.AllKeys.ToDictionary(k => k, k => response.Headers[k]), ContentEncoding = !string.IsNullOrWhiteSpace(response.ContentEncoding) ? Encoding.GetEncoding(response.ContentEncoding) : Encoding.Default }; if (response.ContentLength > 0) { using (var responseStream = response.GetResponseStream()) { if (responseStream != null) { using (var reader = new StreamReader(responseStream, result.ContentEncoding)) { result.Body = reader.ReadToEnd(); } } } } return(RiakResult <RiakRestResponse> .Success(result)); } catch (RiakException ex) { return(RiakResult <RiakRestResponse> .Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline)); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { return(RiakResult <RiakRestResponse> .Error(ResultCode.HttpError, ex.Message, false)); } return(RiakResult <RiakRestResponse> .Error(ResultCode.HttpError, ex.Message, true)); } catch (Exception ex) { return(RiakResult <RiakRestResponse> .Error(ResultCode.CommunicationError, ex.Message, true)); } }
public void SetUp() { Result = RiakResult <RpbPingResp> .Success(new RpbPingResp()); SetUpInternal(); }
public Task <RiakResult <RiakRestResponse> > RestRequest(RiakRestRequest request) { var baseUri = new StringBuilder(_restRootUrl).Append(request.Uri); if (request.QueryParams.Count > 0) { baseUri.Append("?"); var first = request.QueryParams.First(); baseUri.Append(first.Key.UrlEncoded()).Append("=").Append(first.Value.UrlEncoded()); request.QueryParams.Skip(1).ForEach(kv => baseUri.Append("&").Append(kv.Key.UrlEncoded()).Append("=").Append(kv.Value.UrlEncoded())); } var targetUri = new Uri(baseUri.ToString()); var req = (HttpWebRequest)WebRequest.Create(targetUri); req.KeepAlive = true; req.Method = request.Method; req.Credentials = CredentialCache.DefaultCredentials; if (!string.IsNullOrWhiteSpace(request.ContentType)) { req.ContentType = request.ContentType; } if (!request.Cache) { req.Headers.Set(RiakConstants.Rest.HttpHeaders.DisableCacheKey, RiakConstants.Rest.HttpHeaders.DisableCacheValue); } request.Headers.ForEach(h => req.Headers.Set(h.Key, h.Value)); if (request.Body != null && request.Body.Length > 0) { req.ContentLength = request.Body.Length; using (var writer = req.GetRequestStream()) { writer.Write(request.Body, 0, request.Body.Length); } } else { req.ContentLength = 0; } // process response var asyncResult = req.BeginGetResponse(null, null); return(Task <WebResponse> .Factory.FromAsync(asyncResult, req.EndGetResponse) .ContinueWith((Task <WebResponse> responseTask) => { // rethrow exception on fault if (responseTask.IsFaulted) { throw responseTask.Exception; } // pull out response var response = (HttpWebResponse)responseTask.Result; var result = new RiakRestResponse() { ContentLength = response.ContentLength, ContentType = response.ContentType, StatusCode = response.StatusCode, Headers = response.Headers.AllKeys.ToDictionary(k => k, k => response.Headers[k]), ContentEncoding = !string.IsNullOrWhiteSpace(response.ContentEncoding) ? Encoding.GetEncoding(response.ContentEncoding) : Encoding.Default }; if (response.ContentLength > 0) { using (var responseStream = response.GetResponseStream()) { if (responseStream != null) { return ReadStreamAsync(responseStream, (int)response.ContentLength) .ContinueWith((Task <byte[]> readTask) => { result.Body = result.ContentEncoding.GetString(readTask.Result); return result; }); } } } // end of the line throw new RiakException("Couldn't read HTTP response", false); }).Unwrap() .ContinueWith((Task <RiakRestResponse> responseTask) => { if (!responseTask.IsFaulted) { return RiakResult <RiakRestResponse> .Success(responseTask.Result); } else { var exceptions = responseTask.Exception.Flatten().InnerExceptions; var rEx = exceptions.OfType <RiakException>().FirstOrDefault(); var wEx = exceptions.OfType <WebException>().FirstOrDefault(); // process exceptions if (rEx != null) { return RiakResult <RiakRestResponse> .Error(ResultCode.CommunicationError, rEx.Message, rEx.NodeOffline); } else if (wEx != null) { if (wEx.Status == WebExceptionStatus.ProtocolError) { return RiakResult <RiakRestResponse> .Error(ResultCode.HttpError, wEx.Message, false); } return RiakResult <RiakRestResponse> .Error(ResultCode.HttpError, wEx.Message, true); } else { var ex = exceptions.FirstOrDefault(); return RiakResult <RiakRestResponse> .Error(ResultCode.CommunicationError, ex.Message, true); } } })); }
public void SetUp() { ClientMock.Setup(m => m.Get(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <uint>())).Returns(RiakResult <RiakObject> .Success(new RiakObject("foo", "bar", "baz"))); Result = AsyncClient.Get("foo", "bar").Result; }
public void SetUp() { ClientMock.Setup(m => m.Get(It.IsAny <string>(), It.IsAny <string>(), 2)).Returns(RiakResult <RiakObject> .Success(new RiakObject("foo", "bar", "baz"))); AsyncClient.Get(new RiakObjectId("foo", "bar"), Tester.HandleResult); Result = Tester.Result; }
public override void SetUp() { Result = RiakResult <RpbPingResp> .Success(new RpbPingResp()); base.SetUp(); }