public override ConsumerStatus RegisterCommand(AbstractCommand command)
 {
     if (command == null)
         return ConsumerStatus.Running;
     lock (this)
     {
         _commandsQueue.Enqueue(command);
         if (_alive)
             return ConsumerStatus.Running;
         return ConsumerStatus.Idle;
     }
 }
        private byte[] BuildTextResponse(AbstractCommand command)
        {
            _logManager.Debug("TextResponseManager.BuildTextResponse", "Building response for command : " + command.Opcode);

            if (command.NoReply)
                return new byte[]{};

            if(command.ExceptionOccured)
                return MemcachedEncoding.BinaryConverter.GetBytes("SERVER_ERROR " + command.ErrorMessage + "\r\n");
            if (command.ErrorMessage != null)
                return MemcachedEncoding.BinaryConverter.GetBytes(command.ErrorMessage + "\r\n");

            DataStream resultStream = new DataStream();

            switch (command.Opcode)
            {
                case Opcode.Set:
                case Opcode.Add:
                case Opcode.Replace:
                case Opcode.Append:
                case Opcode.Prepend:
                    if (command.OperationResult.ReturnResult == Result.SUCCESS)
                    {
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("STORED\r\n"));
                    }
                    else
                    {
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_STORED\r\n"));
                    }
                    break;
                case Opcode.CAS:
                    switch (command.OperationResult.ReturnResult)
                    {
                        case Result.SUCCESS:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("STORED\r\n"));
                            break;
                        case Result.ITEM_MODIFIED:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("EXISTS\r\n"));
                            break;
                        case Result.ITEM_NOT_FOUND:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_FOUND\r\n"));
                            break;
                        default:
                            break;
                    }
                    break;
                case Opcode.Get:
                case Opcode.Gets:
                    List<GetOpResult> results = (command as GetCommand).Results;
                    foreach (GetOpResult result in results)
                    {
                        if (result == null)
                            continue;
                        byte[] value=result.Value as byte[];
                        string valueString = null;
                        if(command.Opcode==Opcode.Get)
                            valueString = string.Format("VALUE {0} {1} {2}\r\n", result.Key, result.Flag, value.Length);
                        else
                            valueString = string.Format("VALUE {0} {1} {2} {3}\r\n", result.Key, result.Flag, value.Length, result.Version);
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes(valueString));
                        resultStream.Write(value);
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("\r\n"));
                    }
                    resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes( "END\r\n"));
                    break;
                case Opcode.Increment:
                case Opcode.Decrement:
                    switch (command.OperationResult.ReturnResult)
                    {
                        case Result.SUCCESS:
                            long value = (long) (command.OperationResult as MutateOpResult).MutateResult;
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes(value.ToString() + "\r\n"));
                            break;
                        case Result.ITEM_TYPE_MISMATCHED:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("CLIENT_ERROR cannot increment or decrement non-numeric value\r\n"));
                            break;
                        case Result.ITEM_NOT_FOUND:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_FOUND\r\n"));
                            break;
                        default:
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("ERROR\r\n"));
                            break;
                    }
                    break;
                case Opcode.Delete:
                    if (command.OperationResult.ReturnResult == Result.SUCCESS)
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("DELETED\r\n"));
                    else
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_FOUND\r\n"));
                    break;
                case Opcode.Touch:
                    if (command.OperationResult.ReturnResult == Result.SUCCESS)
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("TOUCHED\r\n"));
                    else
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("NOT_FOUND\r\n"));
                    break;
                case Opcode.Flush:
                    resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("OK\r\n"));
                        break;
                case Opcode.Version:
                        string version = command.OperationResult.Value as string;
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes(version + "\r\n"));
                        break;
                case Opcode.Verbosity:
                case Opcode.Slabs_Reassign:
                case Opcode.Slabs_Automove:
                        if (command.OperationResult.ReturnResult == Result.SUCCESS)
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("OK\r\n"));
                    else
                            resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("ERROR\r\n"));
                    break;
                case Opcode.Stat:
                    Hashtable stats = command.OperationResult.Value as Hashtable;
                    if (stats == null)
                    {
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("END\r\n"));
                        break;
                    }
                    IDictionaryEnumerator ie = stats.GetEnumerator();
                    string statString = null;
                    while (ie.MoveNext())
                    {
                        statString = string.Format("STAT {0} {1}\r\n",ie.Key,ie.Value);
                        resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes(statString));
                    }
                    resultStream.Write(MemcachedEncoding.BinaryConverter.GetBytes("END\r\n"));
                    break;
                case Opcode.Quit:
                    TcpNetworkGateway.DisposeClient(_memTcpClient);
                    break;
            }

            return resultStream.ReadAll();
        }
        private byte[] BuildBinaryResponse(AbstractCommand command)
        {
            _logManager.Debug("BinaryResponseManager.BuildBinaryResponse()", "Building response for command : " + command.Opcode);

            switch (command.Opcode)
            {
                case Opcode.Set:
                case Opcode.Add:
                case Opcode.Replace:
                case Opcode.Append:
                case Opcode.Prepend:
                    _responseStream.Write(BinaryResponseBuilder.BuildStorageResponse(command as StorageCommand));
                    break;
                case Opcode.SetQ:
                case Opcode.AddQ:
                case Opcode.ReplaceQ:
                case Opcode.AppendQ:
                case Opcode.PrependQ:
                    _responseStream.Write(BinaryResponseBuilder.BuildStorageResponse(command as StorageCommand));
                    if(!command.ExceptionOccured)
                        return null;
                    break;

                case Opcode.Get:
                case Opcode.GetK:
                    _responseStream.Write(BinaryResponseBuilder.BuildGetResponse(command as GetCommand));
                    break;

                case Opcode.GetQ:
                case Opcode.GetKQ:
                    _responseStream.Write(BinaryResponseBuilder.BuildGetResponse(command as GetCommand));
                    if (!command.ExceptionOccured)
                        return null;
                    break;

                case Opcode.No_op:
                    _responseStream.Write(BinaryResponseBuilder.BuildNoOpResponse(command as NoOperationCommand));
                    break;

                case Opcode.Delete:
                    _responseStream.Write(BinaryResponseBuilder.BuildDeleteResponse(command as DeleteCommand));
                    break;

                case Opcode.DeleteQ: 
                    _responseStream.Write(BinaryResponseBuilder.BuildDeleteResponse(command as DeleteCommand));
                    if (!command.ExceptionOccured)
                        return null;
                    break;

                case Opcode.Flush:
                    _responseStream.Write(BinaryResponseBuilder.BuildFlushResponse(command as FlushCommand));
                    break;
                case Opcode.FlushQ:
                    _responseStream.Write(BinaryResponseBuilder.BuildFlushResponse(command as FlushCommand));
                    if (!command.ExceptionOccured)
                        return null;
                    break;

                case Opcode.Increment:
                case Opcode.Decrement:
                    _responseStream.Write(BinaryResponseBuilder.BuildCounterResponse(command as CounterCommand));
                    break;
                case Opcode.IncrementQ:
                case Opcode.DecrementQ:
                    _responseStream.Write(BinaryResponseBuilder.BuildCounterResponse(command as CounterCommand));
                    if (!command.ExceptionOccured)
                        return null;
                    break;

                case Opcode.Stat:
                    _responseStream.Write(BinaryResponseBuilder.BuildStatsResponse(command as StatsCommand));
                    break;

                case Opcode.Quit:
                    _responseStream.Write(BinaryResponseBuilder.BuildQuitResponse(command as QuitCommand));
                    break;
                case Opcode.QuitQ:
                    _responseStream.Write(BinaryResponseBuilder.BuildQuitResponse(command as QuitCommand));
                    if (!command.ExceptionOccured)
                        return null;
                    break;

                case Opcode.Version:
                    _responseStream.Write(BinaryResponseBuilder.BuildVersionResponse(command as VersionCommand));
                    break;
                default:
                    _responseStream.Write(BinaryResponseBuilder.BuildInvalidResponse(command as InvalidCommand));
                    break;
            }
            return _responseStream.ReadAll();
        }
Example #4
0
 public abstract ConsumerStatus RegisterCommand(AbstractCommand command);