BUG-5280: add AbstractClientConnection
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / ConnectedClientConnection.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.access.client;
9
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;
16
17 @Beta
18 @NotThreadSafe
19 public final class ConnectedClientConnection<T extends BackendInfo> extends AbstractReceivingClientConnection<T> {
20     private static final Logger LOG = LoggerFactory.getLogger(ConnectedClientConnection.class);
21
22     private long nextTxSequence;
23
24     public ConnectedClientConnection(final ClientActorContext context, final Long cookie, final T backend) {
25         super(context, cookie, backend);
26     }
27
28     private TransmittedConnectionEntry transmit(final ConnectionEntry entry) {
29         final long txSequence = nextTxSequence++;
30
31         final RequestEnvelope toSend = new RequestEnvelope(entry.getRequest().toVersion(remoteVersion()), sessionId(),
32             txSequence);
33
34         final ActorRef actor = remoteActor();
35         LOG.trace("Transmitting request {} as {} to {}", entry.getRequest(), toSend, actor);
36         actor.tell(toSend, ActorRef.noSender());
37
38         return new TransmittedConnectionEntry(entry, sessionId(), txSequence, readTime());
39     }
40
41     @Override
42     void enqueueEntry(final ConnectionEntry entry) {
43         if (inflightSize() < remoteMaxMessages()) {
44             appendToInflight(transmit(entry));
45             LOG.debug("Enqueued request {} to queue {}", entry.getRequest(), this);
46         } else {
47             LOG.debug("Queue is at capacity, delayed sending of request {}", entry.getRequest());
48             super.enqueueEntry(entry);
49         }
50     }
51
52     @Override
53     void sendMessages(final int count) {
54         int toSend = count;
55
56         while (toSend > 0) {
57             final ConnectionEntry e = dequeEntry();
58             if (e == null) {
59                 break;
60             }
61
62             LOG.debug("Transmitting entry {}", e);
63             appendToInflight(transmit(e));
64             toSend--;
65         }
66     }
67
68     @Override
69     ClientActorBehavior<T> reconnectConnection(final ClientActorBehavior<T> current) {
70         final ReconnectingClientConnection<T> next = new ReconnectingClientConnection<>(this);
71         setForwarder(new SimpleReconnectForwarder(next));
72         current.reconnectConnection(this, next);
73         return current;
74     }
75 }