-
Notifications
You must be signed in to change notification settings - Fork 215
/
LiveResponseAndAcknowledgementForwarder.java
154 lines (134 loc) · 6.28 KB
/
LiveResponseAndAcknowledgementForwarder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.things.service.persistence.actors;
import java.time.Duration;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.acks.AcknowledgementLabel;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgements;
import org.eclipse.ditto.base.model.signals.commands.CommandResponse;
import org.eclipse.ditto.internal.models.signal.correlation.CommandAndCommandResponseMatchingValidator;
import org.eclipse.ditto.internal.utils.akka.logging.DittoDiagnosticLoggingAdapter;
import org.eclipse.ditto.internal.utils.akka.logging.DittoLoggerFactory;
import org.eclipse.ditto.policies.enforcement.PreEnforcer;
import org.eclipse.ditto.things.model.signals.commands.query.ThingQueryCommand;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.ReceiveTimeout;
import akka.japi.pf.ReceiveBuilder;
/**
* An actor to deal with a live thing query command expecting a response and requesting custom acknowledgements.
* The sender of the live command is an actor in Concierge in order to apply policy enforcement on the response.
* As a result, custom acknowledgements are also sent to Concierge, which must forward them to the acknowledgement
* aggregator actor.
*/
final class LiveResponseAndAcknowledgementForwarder extends AbstractActor {
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(60);
private final DittoDiagnosticLoggingAdapter log = DittoLoggerFactory.getDiagnosticLoggingAdapter(this);
private final ActorRef messageReceiver;
private final ActorRef acknowledgementReceiver;
private final Set<AcknowledgementLabel> pendingAcknowledgements;
private final ThingQueryCommand<?> thingQueryCommand;
private final CommandAndCommandResponseMatchingValidator responseValidator;
private boolean responseReceived = false;
@Nullable
private ActorRef messageSender = null;
@SuppressWarnings("unused")
private LiveResponseAndAcknowledgementForwarder(final ThingQueryCommand<?> thingQueryCommand,
final ActorRef messageReceiver,
final ActorRef acknowledgementReceiver) {
pendingAcknowledgements = new HashSet<>();
this.messageReceiver = messageReceiver;
this.acknowledgementReceiver = acknowledgementReceiver;
this.thingQueryCommand = thingQueryCommand;
responseValidator = CommandAndCommandResponseMatchingValidator.getInstance();
getContext().setReceiveTimeout(this.thingQueryCommand.getDittoHeaders().getTimeout().orElse(DEFAULT_TIMEOUT));
for (final var ackRequest : this.thingQueryCommand.getDittoHeaders().getAcknowledgementRequests()) {
pendingAcknowledgements.add(ackRequest.getLabel());
}
}
/**
* Create Props object for this actor.
*
* @param thingQueryCommand The live command whose acknowledgements and responses this actor listens for.
* @param messageReceiver Receiver of the message to publish.
* @param acknowledgementReceiver Receiver of acknowledgements.
* @return The Props object.
*/
public static Props props(final ThingQueryCommand<?> thingQueryCommand,
final ActorRef messageReceiver,
final ActorRef acknowledgementReceiver) {
return Props.create(LiveResponseAndAcknowledgementForwarder.class, thingQueryCommand, messageReceiver,
acknowledgementReceiver);
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(Acknowledgement.class, this::onAcknowledgement)
.match(Acknowledgements.class, this::onAcknowledgements)
.match(CommandResponse.class, this::onCommandResponse)
.match(ReceiveTimeout.class, this::stopSelf)
.matchAny(this::sendMessage)
.build();
}
private void sendMessage(final Object message) {
log.debug("Got message to send <{}>", message);
messageSender = getSender();
messageReceiver.tell(message, getSelf());
}
private void onAcknowledgement(final Acknowledgement ack) {
log.debug("Got <{}>", ack);
pendingAcknowledgements.remove(ack.getLabel());
acknowledgementReceiver.forward(ack, getContext());
}
private void onAcknowledgements(final Acknowledgements acks) {
log.debug("Got <{}>", acks);
for (final var ack : acks) {
pendingAcknowledgements.remove(ack.getLabel());
}
acknowledgementReceiver.forward(acks, getContext());
}
private void onCommandResponse(final CommandResponse<?> incomingResponse) {
final CommandResponse<?> response = PreEnforcer.setOriginatorHeader(incomingResponse);
final boolean validResponse = isValidResponse(response);
log.debug("Got <{}>, valid=<{}>", response, validResponse);
if (validResponse) {
responseReceived = true;
if (messageSender != null) {
messageSender.forward(response, getContext());
checkCompletion();
} else {
log.error("Got response without receiving command");
stopSelf("Message sender not found");
}
} else {
acknowledgementReceiver.forward(response, getContext());
}
}
private void checkCompletion() {
if (responseReceived && pendingAcknowledgements.isEmpty()) {
stopSelf("All responses and acknowledgements delivered");
}
}
private void stopSelf(final Object trigger) {
log.debug("Stopping due to <{}>", trigger);
getContext().cancelReceiveTimeout();
getContext().stop(getSelf());
}
private boolean isValidResponse(final CommandResponse<?> response) {
return responseValidator.apply(thingQueryCommand, response).isSuccess();
}
}