2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.access.client;
10 import akka.actor.ActorRef;
11 import com.google.common.annotations.Beta;
12 import javax.annotation.concurrent.NotThreadSafe;
13 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
19 public final class ConnectedClientConnection<T extends BackendInfo> extends AbstractReceivingClientConnection<T> {
20 private static final Logger LOG = LoggerFactory.getLogger(ConnectedClientConnection.class);
22 private long nextTxSequence;
24 public ConnectedClientConnection(final ClientActorContext context, final Long cookie, final T backend) {
25 super(context, cookie, backend);
28 private void transmit(final ConnectionEntry entry) {
29 final long txSequence = nextTxSequence++;
31 final RequestEnvelope toSend = new RequestEnvelope(entry.getRequest().toVersion(remoteVersion()), sessionId(),
34 // We need to enqueue the request before we send it to the actor, as we may be executing on a different thread
35 // than the client actor thread, in which case the round-trip could be made faster than we can enqueue --
36 // in which case the receive routine would not find the entry.
37 final TransmittedConnectionEntry txEntry = new TransmittedConnectionEntry(entry, sessionId(), txSequence,
39 appendToInflight(txEntry);
41 final ActorRef actor = remoteActor();
42 LOG.trace("Transmitting request {} as {} to {}", entry.getRequest(), toSend, actor);
43 actor.tell(toSend, ActorRef.noSender());
47 void enqueueEntry(final ConnectionEntry entry) {
48 if (inflightSize() < remoteMaxMessages()) {
50 LOG.debug("Enqueued request {} to queue {}", entry.getRequest(), this);
52 LOG.debug("Queue is at capacity, delayed sending of request {}", entry.getRequest());
53 super.enqueueEntry(entry);
58 void sendMessages(final int count) {
62 final ConnectionEntry e = dequeEntry();
67 LOG.debug("Transmitting entry {}", e);
74 ClientActorBehavior<T> reconnectConnection(final ClientActorBehavior<T> current) {
75 final ReconnectingClientConnection<T> next = new ReconnectingClientConnection<>(this);
76 setForwarder(new SimpleReconnectForwarder(next));
77 current.reconnectConnection(this, next);