예제 #1
0
        /// <summary>
        /// Returns an action that capture the parameters closure
        /// </summary>
        private Action <MemoryStream> CreateResponseAction(FrameHeader header, Action <Exception, Response> callback)
        {
            var compressor = Compressor;

            return(delegate(MemoryStream stream)
            {
                Response response = null;
                Exception ex = null;
                var nextPosition = stream.Position + header.BodyLength;
                try
                {
                    Stream plainTextStream = stream;
                    if (header.Flags.HasFlag(FrameHeader.HeaderFlag.Compression))
                    {
                        plainTextStream = compressor.Decompress(new WrappedStream(stream, header.BodyLength));
                        plainTextStream.Position = 0;
                    }
                    response = FrameParser.Parse(new Frame(header, plainTextStream, _serializer));
                }
                catch (Exception catchedException)
                {
                    ex = catchedException;
                }
                if (response is ErrorResponse)
                {
                    //Create an exception from the response error
                    ex = ((ErrorResponse)response).Output.CreateException();
                    response = null;
                }
                //We must advance the position of the stream manually in case it was not correctly parsed
                stream.Position = nextPosition;
                callback(ex, response);
            });
        }
        public IAsyncResult BeginExecuteQuery(int _streamId, byte[] Id, string cql, RowSetMetadata Metadata, object[] values,
                                              AsyncCallback callback, object state, object owner,
                                              ConsistencyLevel consistency, bool isTracing)
        {
            var jar = SetupJob(_streamId, callback, state, owner, "EXECUTE");

            BeginJob(jar, SetupKeyspace(SetupPreparedQuery(Id, cql, (streamId) =>
            {
                Evaluate(new ExecuteRequest(streamId, Id, Metadata, values, consistency, isTracing), streamId,
                         new Action <ResponseFrame>((frame2) =>
                {
                    var response = FrameParser.Parse(frame2);
                    if (response is ResultResponse)
                    {
                        JobFinished(streamId, (response as ResultResponse).Output);
                    }
                    else
                    {
                        _protocolErrorHandlerAction(new ErrorActionParam()
                        {
                            AbstractResponse = response,
                            StreamId         = streamId
                        });
                    }
                }));
            })));

            return(jar);
        }
예제 #3
0
 public Action SetupPreparedQuery(AsyncResult <IOutput> jar, byte[] Id, string cql, Action dx)
 {
     return(new Action(() =>
     {
         if (!preparedQueries.ContainsKey(Id))
         {
             Evaluate(new PrepareRequest(jar.StreamId, cql), jar.StreamId, new Action <ResponseFrame>((frame2) =>
             {
                 var response = FrameParser.Parse(frame2);
                 if (response is ResultResponse)
                 {
                     preparedQueries.TryAdd(Id, cql);
                     dx();
                 }
                 else
                 {
                     _protocolErrorHandlerAction(new ErrorActionParam()
                     {
                         AbstractResponse = response, Jar = jar
                     });
                 }
             }));
         }
         else
         {
             dx();
         }
     }));
 }
예제 #4
0
        public IAsyncResult BeginPrepareQuery(int stramId, string cqlQuery, AsyncCallback callback, object state, object owner)
        {
            var jar = SetupJob(stramId, callback, state, owner, "PREPARE");

            BeginJob(jar, SetupKeyspace((streamId) =>
            {
                Evaluate(new PrepareRequest(streamId, cqlQuery), streamId, new Action <ResponseFrame>((frame2) =>
                {
                    var response = FrameParser.Parse(frame2);
                    if (response is ResultResponse)
                    {
                        var outp = (response as ResultResponse).Output;
                        if (outp is OutputPrepared)
                        {
                            preparedQueries[(outp as OutputPrepared).QueryID] = cqlQuery;
                        }
                        JobFinished(streamId, outp);
                    }
                    else
                    {
                        _protocolErrorHandlerAction(new ErrorActionParam()
                        {
                            AbstractResponse = response, StreamId = streamId
                        });
                    }
                }));
            }));
            return(jar);
        }
예제 #5
0
 public Action <int> SetupKeyspace(Action <int> dx)
 {
     return(new Action <int>((streamId) =>
     {
         if (currentKs != selectedKs)
         {
             Evaluate(new QueryRequest(streamId, "USE " + selectedKs, ConsistencyLevel.Default, false), streamId, new Action <ResponseFrame>((frame3) =>
             {
                 var response = FrameParser.Parse(frame3);
                 if (response is ResultResponse)
                 {
                     currentKs = selectedKs;
                     dx(streamId);
                 }
                 else
                 {
                     _protocolErrorHandlerAction(new ErrorActionParam()
                     {
                         AbstractResponse = response, StreamId = streamId
                     });
                 }
             }));
         }
         else
         {
             dx(streamId);
         }
     }));
 }
 public Action SetupKeyspace(AsyncResult <IOutput> jar, Action dx)
 {
     return(new Action(() =>
     {
         if (!currentKs.Value.Equals(selectedKs.Value))
         {
             Evaluate(new QueryRequest(jar.StreamId, CqlQueryTools.GetUseKeyspaceCQL(selectedKs.Value), ConsistencyLevel.Default, false), jar.StreamId, new Action <ResponseFrame>((frame3) =>
             {
                 var response = FrameParser.Parse(frame3);
                 if (response is ResultResponse)
                 {
                     currentKs.Value = selectedKs.Value;
                     dx();
                 }
                 else
                 {
                     _protocolErrorHandlerAction(new ErrorActionParam()
                     {
                         AbstractResponse = response, Jar = jar
                     });
                 }
             }));
         }
         else
         {
             dx();
         }
     }));
 }
예제 #7
0
        public IAsyncResult BeginExecuteQueryOptions(int _streamId, AsyncCallback callback, object state, object owner)
        {
            var jar = SetupJob(_streamId, callback, state, owner, "OPTIONS");

            BeginJob(jar, new Action(() =>
            {
                Evaluate(new OptionsRequest(jar.StreamId), jar.StreamId, new Action <ResponseFrame>((frame2) =>
                {
                    var response = FrameParser.Parse(frame2);
                    if (response is SupportedResponse)
                    {
                        JobFinished(jar, (response as SupportedResponse).Output);
                    }
                    else
                    {
                        _protocolErrorHandlerAction(new ErrorActionParam()
                        {
                            AbstractResponse = response, Jar = jar
                        });
                    }
                }));
            }), true);

            return(jar);
        }
 public Action <int> SetupPreparedQuery(byte[] Id, string cql, Action <int> dx)
 {
     return(new Action <int>((streamId) =>
     {
         if (!preparedQueries.ContainsKey(Id))
         {
             Evaluate(new PrepareRequest(streamId, cql), streamId, new Action <ResponseFrame>((frame2) =>
             {
                 var response = FrameParser.Parse(frame2);
                 if (response is ResultResponse)
                 {
                     preparedQueries[Id] = cql;
                     dx(streamId);
                 }
                 else
                 {
                     _protocolErrorHandlerAction(new ErrorActionParam()
                     {
                         AbstractResponse = response, StreamId = streamId
                     });
                 }
             }));
         }
         else
         {
             dx(streamId);
         }
     }));
 }
예제 #9
0
        private AbstractResponse ReadParseResponse(FrameHeader header, Stream body)
        {
            //Start at the first byte
            body.Position = 0;
            if ((header.Flags & 0x01) > 0)
            {
                body = Compressor.Decompress(body);
            }
            var frame    = new ResponseFrame(header, body);
            var response = FrameParser.Parse(frame);

            return(response);
        }
예제 #10
0
        private void EventOccured(ResponseFrame frame)
        {
            var response = FrameParser.Parse(frame);

            if (response is EventResponse)
            {
                if (CassandraEvent != null)
                {
                    CassandraEvent.Invoke(this, (response as EventResponse).CassandraEventArgs);
                }
                return;
            }
            throw new DriverInternalError("Unexpected response frame");
        }
 public IAsyncResult BeginExecuteQueryOptions(AsyncCallback callback, object state, object owner)
 {
     return(BeginJob(callback, state, owner, "OPTIONS", new Action <int>((streamId) =>
     {
         Evaluate(new OptionsRequest(streamId), streamId, new Action <ResponseFrame>((frame2) =>
         {
             var response = FrameParser.Parse(frame2);
             if (response is SupportedResponse)
             {
                 JobFinished(streamId, (response as SupportedResponse).Output);
             }
             else
             {
                 _protocolErrorHandlerAction(new ErrorActionParam()
                 {
                     AbstractResponse = response, StreamId = streamId
                 });
             }
         }));
     }), true));
 }
 public IAsyncResult BeginExecuteQueryCredentials(IDictionary <string, string> credentials, AsyncCallback callback, object state, object owner)
 {
     return(BeginJob(callback, state, owner, "CREDENTIALS", new Action <int>((streamId) =>
     {
         Evaluate(new CredentialsRequest(streamId, credentials), streamId, new Action <ResponseFrame>((frame2) =>
         {
             var response = FrameParser.Parse(frame2);
             if (response is ReadyResponse)
             {
                 JobFinished(streamId, new OutputVoid(null));
             }
             else
             {
                 _protocolErrorHandlerAction(new ErrorActionParam()
                 {
                     AbstractResponse = response, StreamId = streamId
                 });
             }
         }));
     })));
 }
예제 #13
0
 public IAsyncResult BeginQuery(string cqlQuery, AsyncCallback callback, object state, object owner, ConsistencyLevel consistency, bool tracingEnabled)
 {
     return(BeginJob(callback, state, owner, "QUERY", SetupKeyspace((streamId) =>
     {
         Evaluate(new QueryRequest(streamId, cqlQuery, consistency, tracingEnabled), streamId, (frame2) =>
         {
             var response = FrameParser.Parse(frame2);
             if (response is ResultResponse)
             {
                 JobFinished(streamId, (response as ResultResponse).Output);
             }
             else
             {
                 _protocolErrorHandlerAction(new ErrorActionParam()
                 {
                     AbstractResponse = response, StreamId = streamId
                 });
             }
         });
     })));
 }
예제 #14
0
 public IAsyncResult BeginRegisterForCassandraEvent(CassandraEventType eventTypes, AsyncCallback callback, object state, object owner)
 {
     return(BeginJob(callback, state, owner, "REGISTER", new Action <int>((streamId) =>
     {
         Evaluate(new RegisterForEventRequest(streamId, eventTypes), streamId, new Action <ResponseFrame>((frame2) =>
         {
             var response = FrameParser.Parse(frame2);
             if (response is ReadyResponse)
             {
                 JobFinished(streamId, new OutputVoid(null));
             }
             else
             {
                 _protocolErrorHandlerAction(new ErrorActionParam()
                 {
                     AbstractResponse = response, StreamId = streamId
                 });
             }
         }));
     })));
 }
예제 #15
0
        /// <summary>
        /// Returns an action that capture the parameters closure
        /// </summary>
        private Action <MemoryStream> CreateResponseAction(FrameHeader header, Action <Exception, Response> callback)
        {
            var compressor         = Compressor;
            var bufferPool         = Configuration.BufferPool;
            var decompressorBuffer = _decompressorBuffer;

            return(stream =>
            {
                Response response = null;
                Exception ex = null;
                var nextPosition = stream.Position + header.BodyLength;
                try
                {
                    Stream plainTextStream = stream;
                    if (header.Flags.HasFlag(FrameHeader.HeaderFlag.Compression))
                    {
                        var compressedBodyStream = bufferPool.GetStream(typeof(Connection) + "/Decompress", header.BodyLength);
                        Utils.CopyStream(stream, compressedBodyStream, header.BodyLength, decompressorBuffer);
                        compressedBodyStream.Position = 0;
                        plainTextStream = compressor.Decompress(compressedBodyStream);
                        plainTextStream.Position = 0;
                    }
                    response = FrameParser.Parse(new Frame(header, plainTextStream));
                }
                catch (Exception catchedException)
                {
                    ex = catchedException;
                }
                if (response is ErrorResponse)
                {
                    //Create an exception from the response error
                    ex = ((ErrorResponse)response).Output.CreateException();
                    response = null;
                }
                //We must advance the position of the stream manually in case it was not correctly parsed
                stream.Position = nextPosition;
                callback(ex, response);
            });
        }
        public IAsyncResult BeginExecuteQueryCredentials(int _streamId, IDictionary <string, string> credentials, AsyncCallback callback, object state, object owner)
        {
            var jar = SetupJob(_streamId, callback, state, owner, "CREDENTIALS");

            BeginJob(jar, new Action(() =>
            {
                Evaluate(new CredentialsRequest(jar.StreamId, credentials), jar.StreamId, new Action <ResponseFrame>((frame2) =>
                {
                    var response = FrameParser.Parse(frame2);
                    if (response is ReadyResponse)
                    {
                        JobFinished(jar, new OutputVoid(null));
                    }
                    else
                    {
                        _protocolErrorHandlerAction(new ErrorActionParam()
                        {
                            AbstractResponse = response, Jar = jar
                        });
                    }
                }));
            }));
            return(jar);
        }
예제 #17
0
        public IAsyncResult BeginRegisterForCassandraEvent(int _streamId, CassandraEventType eventTypes, AsyncCallback callback, object state, object owner)
        {
            var jar = SetupJob(_streamId, callback, state, owner, "REGISTER");

            BeginJob(jar, new Action(() =>
            {
                Evaluate(new RegisterForEventRequest(jar.StreamId, eventTypes), jar.StreamId, new Action <ResponseFrame>((frame2) =>
                {
                    var response = FrameParser.Parse(frame2);
                    if (response is ReadyResponse)
                    {
                        JobFinished(jar, new OutputVoid(null));
                    }
                    else
                    {
                        _protocolErrorHandlerAction(new ErrorActionParam()
                        {
                            AbstractResponse = response, Jar = jar
                        });
                    }
                }));
            }));
            return(jar);
        }