| 1 | // Copyright (c) 2006 Dustin Sallings <dustin@spy.net> |
| 2 | |
| 3 | package net.spy.memcached.protocol.ascii; |
| 4 | |
| 5 | import java.io.ByteArrayOutputStream; |
| 6 | import java.io.IOException; |
| 7 | import java.nio.ByteBuffer; |
| 8 | |
| 9 | import net.spy.memcached.KeyUtil; |
| 10 | import net.spy.memcached.ops.Operation; |
| 11 | import net.spy.memcached.ops.OperationCallback; |
| 12 | import net.spy.memcached.ops.OperationErrorType; |
| 13 | import net.spy.memcached.ops.OperationState; |
| 14 | import net.spy.memcached.ops.OperationStatus; |
| 15 | import net.spy.memcached.protocol.BaseOperationImpl; |
| 16 | |
| 17 | /** |
| 18 | * Operations on a memcached connection. |
| 19 | */ |
| 20 | abstract class OperationImpl extends BaseOperationImpl implements Operation { |
| 21 | |
| 22 | protected static final byte[] CRLF={'\r', '\n'}; |
| 23 | private static final String CHARSET = "UTF-8"; |
| 24 | |
| 25 | private final ByteArrayOutputStream byteBuffer=new ByteArrayOutputStream(); |
| 26 | OperationReadType readType=OperationReadType.LINE; |
| 27 | boolean foundCr=false; |
| 28 | |
| 29 | protected OperationImpl() { |
| 30 | super(); |
| 31 | } |
| 32 | |
| 33 | protected OperationImpl(OperationCallback cb) { |
| 34 | super(); |
| 35 | callback=cb; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Match the status line provided against one of the given |
| 40 | * OperationStatus objects. If none match, return a failure status with |
| 41 | * the given line. |
| 42 | * |
| 43 | * @param line the current line |
| 44 | * @param statii several status objects |
| 45 | * @return the appropriate status object |
| 46 | */ |
| 47 | protected final OperationStatus matchStatus(String line, |
| 48 | OperationStatus... statii) { |
| 49 | OperationStatus rv=null; |
| 50 | for(OperationStatus status : statii) { |
| 51 | if(line.equals(status.getMessage())) { |
| 52 | rv=status; |
| 53 | } |
| 54 | } |
| 55 | if(rv == null) { |
| 56 | rv=new OperationStatus(false, line); |
| 57 | } |
| 58 | return rv; |
| 59 | } |
| 60 | |
| 61 | /* (non-Javadoc) |
| 62 | * @see net.spy.memcached.protocol.ascii.Operation#getReadType() |
| 63 | */ |
| 64 | protected final OperationReadType getReadType() { |
| 65 | return readType; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Set the read type of this operation. |
| 70 | */ |
| 71 | protected final void setReadType(OperationReadType to) { |
| 72 | readType=to; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Set some arguments for an operation into the given byte buffer. |
| 77 | */ |
| 78 | protected final void setArguments(ByteBuffer bb, Object... args) { |
| 79 | boolean wasFirst=true; |
| 80 | for(Object o : args) { |
| 81 | if(wasFirst) { |
| 82 | wasFirst=false; |
| 83 | } else { |
| 84 | bb.put((byte)' '); |
| 85 | } |
| 86 | bb.put(KeyUtil.getKeyBytes(String.valueOf(o))); |
| 87 | } |
| 88 | bb.put(CRLF); |
| 89 | } |
| 90 | |
| 91 | OperationErrorType classifyError(String line) { |
| 92 | OperationErrorType rv=null; |
| 93 | if(line.startsWith("ERROR")) { |
| 94 | rv=OperationErrorType.GENERAL; |
| 95 | } else if(line.startsWith("CLIENT_ERROR")) { |
| 96 | rv=OperationErrorType.CLIENT; |
| 97 | } else if(line.startsWith("SERVER_ERROR")) { |
| 98 | rv=OperationErrorType.SERVER; |
| 99 | } |
| 100 | return rv; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void readFromBuffer(ByteBuffer data) throws IOException { |
| 105 | // Loop while there's data remaining to get it all drained. |
| 106 | while(getState() != OperationState.COMPLETE && data.remaining() > 0) { |
| 107 | if(readType == OperationReadType.DATA) { |
| 108 | handleRead(data); |
| 109 | } else { |
| 110 | int offset=-1; |
| 111 | for(int i=0; data.remaining() > 0; i++) { |
| 112 | byte b=data.get(); |
| 113 | if(b == '\r') { |
| 114 | foundCr=true; |
| 115 | } else if(b == '\n') { |
| 116 | assert foundCr: "got a \\n without a \\r"; |
| 117 | offset=i; |
| 118 | foundCr=false; |
| 119 | break; |
| 120 | } else { |
| 121 | assert !foundCr : "got a \\r without a \\n"; |
| 122 | byteBuffer.write(b); |
| 123 | } |
| 124 | } |
| 125 | if(offset >= 0) { |
| 126 | String line=new String(byteBuffer.toByteArray(), CHARSET); |
| 127 | byteBuffer.reset(); |
| 128 | OperationErrorType eType=classifyError(line); |
| 129 | if(eType != null) { |
| 130 | handleError(eType, line); |
| 131 | } else { |
| 132 | handleLine(line); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* (non-Javadoc) |
| 140 | * @see net.spy.memcached.protocol.ascii.Operation#handleLine(java.lang.String) |
| 141 | */ |
| 142 | public abstract void handleLine(String line); |
| 143 | } |