Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2014 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package com.couchbase.client.core.endpoint;
import com.couchbase.client.core.CouchbaseException;
import com.couchbase.client.core.RequestCancelledException;
import com.couchbase.client.core.ResponseEvent;
import com.couchbase.client.core.ResponseHandler;
import com.couchbase.client.core.env.CoreEnvironment;
import com.couchbase.client.core.logging.CouchbaseLogger;
import com.couchbase.client.core.logging.CouchbaseLoggerFactory;
import com.couchbase.client.core.message.CouchbaseRequest;
import com.couchbase.client.core.message.CouchbaseResponse;
import com.couchbase.client.core.message.ResponseStatus;
import com.couchbase.client.deps.com.lmax.disruptor.EventSink;
import com.couchbase.client.deps.io.netty.channel.Channel;
import com.couchbase.client.deps.io.netty.channel.ChannelHandlerContext;
import com.couchbase.client.deps.io.netty.handler.codec.MessageToMessageCodec;
import com.couchbase.client.deps.io.netty.handler.timeout.IdleState;
import com.couchbase.client.deps.io.netty.handler.timeout.IdleStateEvent;
import com.couchbase.client.deps.io.netty.util.CharsetUtil;
import rx.Scheduler;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.subjects.Subject;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
/**
* Generic handler which acts as the common base type for all implementing handlers.
*
* @author Michael Nitschinger
* @since 1.0
*/
public abstract class AbstractGenericHandler
extends MessageToMessageCodec {
/**
* The default charset to use for all requests and responses.
*/
protected static final Charset CHARSET = CharsetUtil.UTF_8;
/**
* The logger used.
*/
private static final CouchbaseLogger LOGGER = CouchbaseLoggerFactory.getInstance(AbstractGenericHandler.class);
/**
* The response buffer to push response events into.
*/
private final EventSink responseBuffer;
/**
* The endpoint held as a reference.
*/
private final AbstractEndpoint endpoint;
/**
* This queue keeps all currently outstanding requests.
*/
private final Queue sentRequestQueue;
private final boolean isTransient;
/**
* The request which is expected to return next.
*/
private REQUEST currentRequest;
private DecodingState currentDecodingState;
/**
* Creates a new {@link AbstractGenericHandler} with the default queue.
*
* @param endpoint the endpoint reference.
* @param responseBuffer the response buffer.
*/
protected AbstractGenericHandler(final AbstractEndpoint endpoint, final EventSink responseBuffer, final boolean isTransient) {
this(endpoint, responseBuffer, new ArrayDeque(), isTransient);
}
/**
* Creates a new {@link AbstractGenericHandler} with a custom queue.
*
* @param endpoint the endpoint reference.
* @param responseBuffer the response buffer.
* @param queue the queue.
*/
protected AbstractGenericHandler(final AbstractEndpoint endpoint, final EventSink responseBuffer,
final Queue queue, final boolean isTransient) {
this.endpoint = endpoint;
this.responseBuffer = responseBuffer;
this.sentRequestQueue = queue;
this.currentDecodingState = DecodingState.INITIAL;
this.isTransient = isTransient;
}
/**
* Encode the outgoing request and return it in encoded format.
*
* This method needs to be implemented by the child handler and is responsible for the actual conversion.
*
* @param ctx the context passed in.
* @param msg the outgoing message.
* @return the encoded request.
* @throws Exception as a generic error.
*/
protected abstract ENCODED encodeRequest(ChannelHandlerContext ctx, REQUEST msg) throws Exception;
/**
* Decodes the incoming response and transforms it into a {@link CouchbaseResponse}.
*
* Note that the actual notification is handled by this generic handler, the implementing class only is concerned
* about the conversion itself.
*
* @param ctx the context passed in.
* @param msg the incoming message.
* @return a response or null if nothing should be returned.
* @throws Exception as a generic error. It will be bubbled up to the user (wrapped in a CouchbaseException) in the
* onError of the request's Observable.
*/
protected abstract CouchbaseResponse decodeResponse(ChannelHandlerContext ctx, RESPONSE msg) throws Exception;
@Override
protected void encode(ChannelHandlerContext ctx, REQUEST msg, List