Search in sources :

Example 1 with HotRodCounterEvent

use of org.infinispan.client.hotrod.counter.impl.HotRodCounterEvent in project infinispan by infinispan.

the class HeaderDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    try {
        switch(state()) {
            case READ_MESSAGE_ID:
                long messageId = codec.readMessageId(in);
                receivedOpCode = codec.readOpCode(in);
                switch(receivedOpCode) {
                    case CACHE_ENTRY_CREATED_EVENT_RESPONSE:
                    case CACHE_ENTRY_MODIFIED_EVENT_RESPONSE:
                    case CACHE_ENTRY_REMOVED_EVENT_RESPONSE:
                    case CACHE_ENTRY_EXPIRED_EVENT_RESPONSE:
                        if (codec.allowOperationsAndEvents()) {
                            operation = messageId == 0 ? null : incomplete.get(messageId);
                        } else if (incomplete.size() == 1) {
                            operation = incomplete.values().iterator().next();
                            messageId = operation.header().messageId();
                        } else if (incomplete.size() > 1) {
                            throw new IllegalStateException("Too many incomplete operations: " + incomplete);
                        } else {
                            operation = null;
                            messageId = 0;
                        }
                        // complete earlier.
                        if (operation != null && !(operation instanceof AddClientListenerOperation)) {
                            throw HOTROD.operationIsNotAddClientListener(messageId, operation.toString());
                        } else if (log.isTraceEnabled()) {
                            log.tracef("Received event for request %d", messageId, operation);
                        }
                        checkpoint(State.READ_CACHE_EVENT);
                        // the loop in HintedReplayingDecoder will call decode again
                        return;
                    case COUNTER_EVENT_RESPONSE:
                        checkpoint(State.READ_COUNTER_EVENT);
                        // the loop in HintedReplayingDecoder will call decode again
                        return;
                }
                if (messageId == 0) {
                    // let's read the header even at this stage; it should throw an error and the other throw statement
                    // won't be reached
                    codec.readHeader(in, receivedOpCode, null, channelFactory, ctx.channel().remoteAddress());
                    throw new IllegalStateException("Should be never reached");
                }
                // we can remove the operation at this point since we'll read no more in this state
                operation = incomplete.remove(messageId);
                if (operation == null) {
                    throw HOTROD.unknownMessageId(messageId);
                }
                if (log.isTraceEnabled()) {
                    log.tracef("Received response for request %d, %s", messageId, operation);
                }
                checkpoint(State.READ_HEADER);
            // fall through
            case READ_HEADER:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding header for message %s", HotRodConstants.Names.of(receivedOpCode));
                }
                status = codec.readHeader(in, receivedOpCode, operation.header(), channelFactory, ctx.channel().remoteAddress());
                checkpoint(State.READ_PAYLOAD);
            // fall through
            case READ_PAYLOAD:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding payload for message %s", HotRodConstants.Names.of(receivedOpCode));
                }
                operation.acceptResponse(in, status, this);
                checkpoint(State.READ_MESSAGE_ID);
                break;
            case READ_CACHE_EVENT:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding cache event %s", HotRodConstants.Names.of(receivedOpCode));
                }
                AbstractClientEvent cacheEvent;
                try {
                    cacheEvent = codec.readCacheEvent(in, listenerNotifier::getCacheDataFormat, receivedOpCode, configuration.getClassAllowList(), ctx.channel().remoteAddress());
                } catch (Signal signal) {
                    throw signal;
                } catch (Throwable t) {
                    log.unableToReadEventFromServer(t, ctx.channel().remoteAddress());
                    throw t;
                }
                if (operation != null) {
                    ((AddClientListenerOperation) operation).postponeTimeout(ctx.channel());
                }
                invokeEvent(cacheEvent.getListenerId(), cacheEvent);
                checkpoint(State.READ_MESSAGE_ID);
                break;
            case READ_COUNTER_EVENT:
                if (log.isTraceEnabled()) {
                    log.tracef("Decoding counter event %s", HotRodConstants.Names.of(receivedOpCode));
                }
                HotRodCounterEvent counterEvent;
                try {
                    counterEvent = codec.readCounterEvent(in);
                } catch (Signal signal) {
                    throw signal;
                } catch (Throwable t) {
                    HOTROD.unableToReadEventFromServer(t, ctx.channel().remoteAddress());
                    throw t;
                }
                invokeEvent(counterEvent.getListenerId(), counterEvent);
                checkpoint(State.READ_MESSAGE_ID);
                break;
        }
    } catch (Signal signal) {
        throw signal;
    } catch (Exception e) {
        // If this is server error make sure to restart the state of decoder
        checkpoint(State.READ_MESSAGE_ID);
        throw e;
    }
}
Also used : AbstractClientEvent(org.infinispan.client.hotrod.event.impl.AbstractClientEvent) Signal(io.netty.util.Signal) HotRodCounterEvent(org.infinispan.client.hotrod.counter.impl.HotRodCounterEvent) AddClientListenerOperation(org.infinispan.client.hotrod.impl.operations.AddClientListenerOperation) TransportException(org.infinispan.client.hotrod.exceptions.TransportException)

Aggregations

Signal (io.netty.util.Signal)1 HotRodCounterEvent (org.infinispan.client.hotrod.counter.impl.HotRodCounterEvent)1 AbstractClientEvent (org.infinispan.client.hotrod.event.impl.AbstractClientEvent)1 TransportException (org.infinispan.client.hotrod.exceptions.TransportException)1 AddClientListenerOperation (org.infinispan.client.hotrod.impl.operations.AddClientListenerOperation)1