Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

UdpClientProtocol now functional (basic test complete)

Rich Turner992fafb21682
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
index 8163b2a1c..3cb2b622c 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -379,10 +379,12 @@ public abstract class AbstractProtocol implements Protocol {
if (value != null && attributeValueType.isPresent()) {
if (attributeValueType.get() != value.getType()) {
- LOG.fine("Converting value: " + value.getType() + " -> " + attributeValueType.get());
+ LOG.fine("Trying to convert value: " + value.getType() + " -> " + attributeValueType.get());
Optional<Value> convertedValue = Values.convert(value, attributeValueType.get());
if (!convertedValue.isPresent()) {
LOG.warning("Failed to convert value: " + value.getType() + " -> " + attributeValueType.get());
+ LOG.warning("Cannot send linked attribute update");
+ return;
} else {
value = convertedValue.get();
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
index 276d0930e..82964ea93 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
@@ -552,7 +552,7 @@ public class HttpClientProtocol extends AbstractProtocol {
META_FAILURE_CODES,
META_PAGING_ENABLED);
- protected static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
META_PROTOCOL_BASE_URI,
META_PROTOCOL_USERNAME,
META_PROTOCOL_PASSWORD,
diff --git a/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
index fd80c8ed2..e2eae32ac 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
@@ -34,9 +34,7 @@ import org.openremote.model.value.Value;
import org.openremote.model.value.Values;
import java.nio.charset.Charset;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
@@ -78,17 +76,118 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
*/
public class UdpClientProtocol extends AbstractProtocol {
- private static class AttributeInfo {
+ private class ClientAndQueue {
+ IoClient<String> client;
+ AttributeRef protocolRef;
+ Deque<Runnable> actionQueue = new ArrayDeque<>();
+ int retries;
+ int timeoutMillis;
+ Consumer<String> currentResponseConsumer;
+ ScheduledFuture responseMonitor;
+
+ protected ClientAndQueue(IoClient<String> client, AttributeRef protocolRef) {
+ this.client = client;
+ this.protocolRef = protocolRef;
+ }
+
+ protected void connect() {
+ LOG.info("Connecting UDP client");
+ client.addConnectionStatusConsumer(status -> UdpClientProtocol.this.onConnectionStatusChanged(protocolRef, status));
+ client.addMessageConsumer(this::onMessageReceived);
+ client.connect();
+ }
+
+ protected void disconnect() {
+ LOG.info("Disconnecting UDP client");
+ client.removeAllMessageConsumers();
+ client.removeAllConnectionStatusConsumers();
+ client.disconnect();
+ }
+
+ protected synchronized void send(String str, Consumer<String> responseConsumer, AttributeInfo attributeInfo) {
+ if (currentResponseConsumer != null) {
+ actionQueue.add(() -> processNextCommand(str, responseConsumer, attributeInfo));
+ return;
+ }
+
+ executorService.schedule(() -> processNextCommand(str, responseConsumer, attributeInfo), 0);
+ }
+
+ protected void onMessageReceived(String msg) {
+ if (currentResponseConsumer != null) {
+ currentResponseConsumer.accept(msg);
+ }
+ }
+
+ protected synchronized void processNextCommand(String str, Consumer<String> responseConsumer, AttributeInfo attributeInfo) {
+ currentResponseConsumer = null;
+
+ LOG.fine("Executing send for attribute: " + attributeInfo.attributeRef);
+
+ if (responseConsumer == null) {
+ LOG.finer("No response consumer so fire and forget");
+ client.sendMessage(str);
+
+ // Move straight onto next command
+ if (!actionQueue.isEmpty()) {
+ executorService.schedule(actionQueue.pop(), 0);
+ }
+ return;
+ }
+
+ LOG.finer("Send expects a response [retries=" + attributeInfo.retries + ", timeout=" + attributeInfo.responseTimeoutMillis + "]");
+ this.retries = attributeInfo.retries;
+ this.timeoutMillis = attributeInfo.responseTimeoutMillis;
+ this.currentResponseConsumer = responseStr -> {
+ responseConsumer.accept(responseStr);
+ synchronized (ClientAndQueue.this) {
+ if (this.responseMonitor != null) {
+ this.responseMonitor.cancel(true);
+ currentResponseConsumer = null;
+ }
+
+ if (!actionQueue.isEmpty()) {
+ executorService.schedule(actionQueue.pop(), 0);
+ }
+ }
+ };
+
+ Runnable failureAction = () -> {
+ synchronized (ClientAndQueue.this) {
+ LOG.fine("Send failed: response timeout reached");
+
+ if (retries > 0) {
+ LOG.fine("Sending message again");
+ retries--;
+ client.sendMessage(str);
+ } else {
+ LOG.fine("No more retries left so abandoning send");
+ responseMonitor.cancel(false);
+ currentResponseConsumer = null;
+ if (!actionQueue.isEmpty()) {
+ executorService.schedule(actionQueue.pop(), 0);
+ }
+ }
+ }
+ };
+
+
+ client.sendMessage(str);
+ responseMonitor = executorService.scheduleWithFixedDelay(failureAction, 0, timeoutMillis);
+ }
+ }
+
+ protected static class AttributeInfo {
+ AttributeRef attributeRef;
Consumer<Value> sendConsumer;
ScheduledFuture pollingTask;
int retries;
- boolean expectResponse;
+ int responseTimeoutMillis;
- public AttributeInfo(Consumer<Value> sendConsumer, ScheduledFuture pollingTask, int retries, boolean expectResponse) {
- this.sendConsumer = sendConsumer;
- this.pollingTask = pollingTask;
+ protected AttributeInfo(AttributeRef attributeRef, int retries, int responseTimeoutMillis) {
+ this.attributeRef = attributeRef;
this.retries = retries;
- this.expectResponse = expectResponse;
+ this.responseTimeoutMillis = responseTimeoutMillis;
}
}
@@ -101,7 +200,7 @@ public class UdpClientProtocol extends AbstractProtocol {
private static int DEFAULT_SEND_RETRIES = 1;
private static boolean DEFAULT_SERVER_ALWAYS_RESPONDS = false;
private static int MIN_POLLING_MILLIS = 1000;
- protected final Map<AttributeRef, IoClient<String>> clientMap = new HashMap<>();
+ protected final Map<AttributeRef, ClientAndQueue> clientMap = new HashMap<>();
protected final Map<AttributeRef, AttributeInfo> attributeInfoMap = new HashMap<>();
/**
@@ -138,7 +237,7 @@ public class UdpClientProtocol extends AbstractProtocol {
* Indicates whether the server replies to each packet not just polling requests (need this to successfully match
* request and response packets)
*/
- MetaItemDescriptor META_SERVER_ALWAYS_RESPONDS = metaItemFixedBoolean(PROTOCOL_NAMESPACE + ":serverAlwaysResponds", ACCESS_PRIVATE, false);
+ public static final MetaItemDescriptor META_SERVER_ALWAYS_RESPONDS = metaItemFixedBoolean(PROTOCOL_NAMESPACE + ":serverAlwaysResponds", ACCESS_PRIVATE, false);
/**
* Indicates how long to wait for a response from the server before re-attempting {@link #META_SEND_RETRIES} times
@@ -160,6 +259,27 @@ public class UdpClientProtocol extends AbstractProtocol {
1,
10);
+ public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_ATTRIBUTE_WRITE_VALUE,
+ META_VALUE_FILTERS,
+ META_POLLING_MILLIS,
+ META_RESPONSE_TIMEOUT_MILLIS,
+ META_SEND_RETRIES,
+ META_SERVER_ALWAYS_RESPONDS
+ );
+
+ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(
+ META_PROTOCOL_HOST,
+ META_PROTOCOL_PORT,
+ META_PROTOCOL_BIND_PORT,
+ META_PROTOCOL_CHARSET,
+ META_PROTOCOL_CONVERT_BINARY,
+ META_PROTOCOL_CONVERT_HEX,
+ META_RESPONSE_TIMEOUT_MILLIS,
+ META_SEND_RETRIES,
+ META_SERVER_ALWAYS_RESPONDS
+ );
+
@Override
public String getProtocolName() {
return PROTOCOL_NAME;
@@ -175,6 +295,16 @@ public class UdpClientProtocol extends AbstractProtocol {
return PROTOCOL_VERSION;
}
+ @Override
+ protected List<MetaItemDescriptor> getProtocolConfigurationMetaItemDescriptors() {
+ return PROTOCOL_META_ITEM_DESCRIPTORS;
+ }
+
+ @Override
+ protected List<MetaItemDescriptor> getLinkedAttributeMetaItemDescriptors() {
+ return ATTRIBUTE_META_ITEM_DESCRIPTORS;
+ }
+
@Override
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
@@ -227,24 +357,21 @@ public class UdpClientProtocol extends AbstractProtocol {
true
).flatMap(Values::getString).map(Charset::forName).orElse(CharsetUtil.UTF_8);
- Consumer<ConnectionStatus> connectionStatusConsumer = connectionStatus ->
- onConnectionStatusChanged(protocolRef, connectionStatus);
-
- Consumer<String> messageConsumer = message -> onClientMessageReceived(protocolRef, message);
-
- IoClient client = addClient(host, port, bindPort, charset, binaryMode, hexMode, messageConsumer, connectionStatusConsumer);
- clientMap.put(protocolRef, client);
- connectClient(protocolRef, client);
+ IoClient client = addClient(host, port, bindPort, charset, binaryMode, hexMode);
+ ClientAndQueue clientAndQueue = new ClientAndQueue(client, protocolRef);
+ clientMap.put(protocolRef, clientAndQueue);
+ clientAndQueue.connect();
}
@Override
protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
- IoClient<String> client = clientMap.remove(protocolRef);
- removeClient(client);
- disconnectClient(protocolRef, client);
- updateStatus(protocolRef, ConnectionStatus.DISCONNECTED);
+ ClientAndQueue clientAndQueue = clientMap.remove(protocolRef);
+ if (clientAndQueue != null) {
+ clientAndQueue.disconnect();
+ updateStatus(protocolRef, ConnectionStatus.DISCONNECTED);
+ }
}
@Override
@@ -255,9 +382,9 @@ public class UdpClientProtocol extends AbstractProtocol {
return;
}
- IoClient<String> client = clientMap.get(protocolConfiguration.getReferenceOrThrow());
+ ClientAndQueue clientAndQueue = clientMap.get(protocolConfiguration.getReferenceOrThrow());
- if (client == null) {
+ if (clientAndQueue == null) {
return;
}
@@ -297,9 +424,16 @@ public class UdpClientProtocol extends AbstractProtocol {
Consumer<Value> sendConsumer = null;
ScheduledFuture pollingTask = null;
+ AttributeInfo info = new AttributeInfo(attribute.getReferenceOrThrow(), sendRetries, responseTimeoutMillis);
if (!attribute.isReadOnly()) {
- sendConsumer = createWriteConsumer(client, attribute.getReferenceOrThrow(), writeValue, null);
+ sendConsumer = createWriteConsumer(
+ clientAndQueue,
+ info,
+ writeValue,
+ serverAlwaysResponds ? str -> {
+ // Just drop the response; something in the future could be used to verify send was successful
+ } : null);
}
if (pollingMillis != null && pollingMillis < MIN_POLLING_MILLIS) {
@@ -313,18 +447,21 @@ public class UdpClientProtocol extends AbstractProtocol {
}
if (pollingMillis != null) {
- Consumer<String> responseConsumer = str ->
- updateLinkedAttribute(
- new AttributeState(
- attribute.getReferenceOrThrow(),
- str != null ? Values.create(str) : null));
-
- Consumer<Value> pollingWriter = createWriteConsumer(client, attribute.getReferenceOrThrow(), writeValue, responseConsumer);
- Runnable pollingRunnable = () -> pollingWriter.accept(null);
- pollingTask = schedulePollingRequest(client, attribute.getReferenceOrThrow(), pollingRunnable, pollingMillis);
+ Consumer<String> responseConsumer = str -> {
+ LOG.fine("Polling response received updating attribute: " + attribute.getReferenceOrThrow());
+ updateLinkedAttribute(
+ new AttributeState(
+ attribute.getReferenceOrThrow(),
+ str != null ? Values.create(str) : null));
+ };
+ Consumer<Value> pollingWriter = createWriteConsumer(clientAndQueue, info, null, responseConsumer);
+ Runnable pollingRunnable = () -> pollingWriter.accept(Values.create(writeValue));
+ pollingTask = schedulePollingRequest(clientAndQueue, attribute.getReferenceOrThrow(), pollingRunnable, pollingMillis);
}
- attributeInfoMap.put(attribute.getReferenceOrThrow(), new AttributeInfo(sendConsumer, pollingTask, sendRetries, serverAlwaysResponds));
+ attributeInfoMap.put(attribute.getReferenceOrThrow(), info);
+ info.pollingTask = pollingTask;
+ info.sendConsumer = sendConsumer;
}
@Override
@@ -348,7 +485,11 @@ public class UdpClientProtocol extends AbstractProtocol {
info.sendConsumer.accept(event.getValue().orElse(null));
}
- protected Consumer<Value> createWriteConsumer(IoClient<String> client, AttributeRef attributeRef, String writeValue, Consumer<String> responseConsumer) {
+ protected void onConnectionStatusChanged(AttributeRef protocolRef, ConnectionStatus connectionStatus) {
+ updateStatus(protocolRef, connectionStatus);
+ }
+
+ protected Consumer<Value> createWriteConsumer(ClientAndQueue clientAndQueue, AttributeInfo attributeInfo, String writeValue, Consumer<String> responseConsumer) {
return value -> {
String str = value != null ? value.toString() : "";
@@ -357,33 +498,21 @@ public class UdpClientProtocol extends AbstractProtocol {
str = writeValue.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, str);
}
- onClientWriteRequest(client, attributeRef, str, responseConsumer);
+ clientAndQueue.send(str, responseConsumer, attributeInfo);
};
}
- protected void onConnectionStatusChanged(AttributeRef protocolRef, ConnectionStatus connectionStatus) {
- updateStatus(protocolRef, connectionStatus);
- }
-
- protected void onClientWriteRequest(IoClient<String> client, AttributeRef attributeRef, String value, Consumer<String> responseConsumer) {
-
- }
-
- protected void onClientMessageReceived(AttributeRef protocolRef, String message) {
-
- }
-
- protected ScheduledFuture schedulePollingRequest(IoClient<String> client,
+ protected ScheduledFuture schedulePollingRequest(ClientAndQueue clientAndQueue,
AttributeRef attributeRef,
Runnable pollingTask,
int pollingMillis) {
- LOG.fine("Scheduling polling request on client '" + client + "' to execute every " + pollingMillis + " seconds for attribute: " + attributeRef);
+ LOG.fine("Scheduling polling request on client '" + clientAndQueue.client + "' to execute every " + pollingMillis + " ms for attribute: " + attributeRef);
return executorService.scheduleWithFixedDelay(pollingTask, 0, pollingMillis, TimeUnit.MILLISECONDS);
}
- protected IoClient<String> addClient(String host, int port, Integer bindPort, Charset charset, boolean binaryMode, boolean hexMode, Consumer<String> messageConsumer, Consumer<ConnectionStatus> statusConsumer) {
+ protected IoClient<String> addClient(String host, int port, Integer bindPort, Charset charset, boolean binaryMode, boolean hexMode) {
LOG.info("Adding UDP client");
@@ -395,7 +524,6 @@ public class UdpClientProtocol extends AbstractProtocol {
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
String msg = Protocol.bytesToHexString(bytes);
- buf.release();
messages.add(msg);
};
encoder = (message, buf) -> {
@@ -407,7 +535,6 @@ public class UdpClientProtocol extends AbstractProtocol {
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
String msg = Protocol.bytesToBinaryString(bytes);
- buf.release();
messages.add(msg);
};
encoder = (message, buf) -> {
@@ -418,8 +545,8 @@ public class UdpClientProtocol extends AbstractProtocol {
final Charset finalCharset = charset != null ? charset : CharsetUtil.UTF_8;
decoder = (buf, messages) -> {
String msg = buf.toString(finalCharset);
- buf.release();
messages.add(msg);
+ buf.readerIndex(buf.readerIndex() + buf.readableBytes());
};
encoder = (message, buf) -> buf.writeBytes(message.getBytes(finalCharset));
}
@@ -427,7 +554,7 @@ public class UdpClientProtocol extends AbstractProtocol {
BiConsumer<ByteBuf, List<String>> finalDecoder = decoder;
BiConsumer<String, ByteBuf> finalEncoder = encoder;
- final IoClient<String> client = new AbstractUdpClient<String>(host, port, bindPort, executorService) {
+ return new AbstractUdpClient<String>(host, port, bindPort, executorService) {
@Override
protected void decode(ByteBuf buf, List<String> messages) {
@@ -439,35 +566,5 @@ public class UdpClientProtocol extends AbstractProtocol {
finalEncoder.accept(message, buf);
}
};
-
- client.addMessageConsumer(messageConsumer);
- client.addConnectionStatusConsumer(statusConsumer);
- return client;
- }
-
- protected void removeClient(IoClient client) {
- if (client == null) {
- return;
- }
-
- LOG.info("Removing UDP client");
- client.removeAllMessageConsumers();
- client.removeAllConnectionStatusConsumers();
- }
-
- protected void connectClient(AttributeRef protocolRef, IoClient client) {
- if (client == null) {
- return;
- }
- LOG.info("Connecting UDP client");
- client.connect();
- }
-
- protected void disconnectClient(AttributeRef protocolRef, IoClient client) {
- if (client == null) {
- return;
- }
- LOG.info("Disconnecting UDP client");
- client.disconnect();
}
}
diff --git a/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService b/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
index f89460af8..c1d8da283 100644
--- a/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
+++ b/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
@@ -5,4 +5,5 @@ org.openremote.agent.protocol.knx.KNXProtocol
org.openremote.agent.protocol.velbus.VelbusTcpProtocol
org.openremote.agent.protocol.velbus.VelbusSerialProtocol
org.openremote.agent.protocol.http.HttpClientProtocol
-org.openremote.agent.protocol.controller.ControllerProtocol
\ No newline at end of file
+org.openremote.agent.protocol.controller.ControllerProtocol
+org.openremote.agent.protocol.udp.UdpClientProtocol
\ No newline at end of file
diff --git a/model/src/main/java/org/openremote/model/value/Values.java b/model/src/main/java/org/openremote/model/value/Values.java
index 3cdbdb0ce..4ddc664b6 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -214,12 +214,11 @@ public class Values {
return Optional.empty();
}
- if (value.get().getType().getModelType() != valueClazz) {
+ if (valueClazz != Value.class && value.get().getType().getModelType() != valueClazz) {
throw new IllegalArgumentException("Meta item value is of incorrect type: expected="
+ valueClazz.getName() + "; actual=" + value.get().getType().getModelType().getName());
}
-
return Optional.of((T)value.get());
}
diff --git a/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy b/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
index f91d2c14c..318fd1f0b 100644
--- a/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
@@ -99,7 +99,7 @@ class ClientEventTest extends Specification implements ManagerContainerTrait, Gw
then: "the agent status change event should have been received"
conditions.eventually {
- assert collectedSharedEvents.size() == 3
+ assert collectedSharedEvents.size() == 4
assert collectedSharedEvents[0] instanceof AgentStatusEvent
assert (collectedSharedEvents[0] as AgentStatusEvent).protocolConfiguration.entityId == managerDemoSetup.agentId
assert (collectedSharedEvents[0] as AgentStatusEvent).protocolConfiguration.attributeName == managerDemoSetup.agentProtocolConfigName
@@ -111,7 +111,11 @@ class ClientEventTest extends Specification implements ManagerContainerTrait, Gw
assert collectedSharedEvents[2] instanceof AgentStatusEvent
assert (collectedSharedEvents[2] as AgentStatusEvent).protocolConfiguration.entityId == managerDemoSetup.agentId
assert (collectedSharedEvents[2] as AgentStatusEvent).protocolConfiguration.attributeName == managerDemoSetup.agentProtocolConfigName
- assert (collectedSharedEvents[2] as AgentStatusEvent).connectionStatus == ConnectionStatus.DISABLED
+ assert (collectedSharedEvents[2] as AgentStatusEvent).connectionStatus == ConnectionStatus.CONNECTING
+ assert collectedSharedEvents[3] instanceof AgentStatusEvent
+ assert (collectedSharedEvents[3] as AgentStatusEvent).protocolConfiguration.entityId == managerDemoSetup.agentId
+ assert (collectedSharedEvents[3] as AgentStatusEvent).protocolConfiguration.attributeName == managerDemoSetup.agentProtocolConfigName
+ assert (collectedSharedEvents[3] as AgentStatusEvent).connectionStatus == ConnectionStatus.DISABLED
}
when: "the subscription is removed"
diff --git a/test/src/test/groovy/org/openremote/test/protocol/http/HttpClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/http/HttpClientProtocolTest.groovy
index 04a416577..8a99d52f4 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/http/HttpClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/http/HttpClientProtocolTest.groovy
@@ -328,7 +328,7 @@ class HttpClientProtocolTest extends Specification implements ManagerContainerTr
),
new MetaItem(
HttpClientProtocol.META_PROTOCOL_PING_MILLIS,
- Values.create(100) // Note for testing time unit is set to milliseconds
+ Values.create(100)
),
new MetaItem(
HttpClientProtocol.META_HEADERS,
diff --git a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
new file mode 100644
index 000000000..c91e07671
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
@@ -0,0 +1,407 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.test.protocol.udp
+
+import org.jboss.resteasy.spi.ResteasyUriInfo
+import org.jboss.resteasy.util.BasicAuthHelper
+import org.openremote.agent.protocol.Protocol
+import org.openremote.agent.protocol.ProtocolExecutorService
+import org.openremote.agent.protocol.filter.RegexFilter
+import org.openremote.agent.protocol.http.*
+import org.openremote.agent.protocol.udp.UdpClientProtocol
+import org.openremote.agent.protocol.udp.UdpStringServer
+import org.openremote.manager.agent.AgentService
+import org.openremote.manager.asset.AssetProcessingService
+import org.openremote.manager.asset.AssetStorageService
+import org.openremote.model.Constants
+import org.openremote.model.asset.Asset
+import org.openremote.model.asset.AssetAttribute
+import org.openremote.model.asset.AssetType
+import org.openremote.model.asset.agent.ConnectionStatus
+import org.openremote.model.attribute.*
+import org.openremote.model.value.ObjectValue
+import org.openremote.model.value.Value
+import org.openremote.model.value.Values
+import org.openremote.test.ManagerContainerTrait
+import spock.lang.Shared
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+import javax.ws.rs.HttpMethod
+import javax.ws.rs.client.ClientRequestContext
+import javax.ws.rs.client.ClientRequestFilter
+import javax.ws.rs.core.*
+
+import static org.openremote.model.asset.agent.ProtocolConfiguration.initProtocolConfiguration
+
+class UdpClientProtocolTest extends Specification implements ManagerContainerTrait {
+
+ @Shared
+ def mockServer = new ClientRequestFilter() {
+
+ private boolean supportsRefresh
+ private int accessTokenCount
+ private int refreshTokenCount
+ private String accessToken = null
+ private String refreshToken = null
+ private int pingCount = 0
+ private int pollCountFast = 0
+ private int pollCountSlow = 0
+ private boolean putRequestWithHeadersCalled = false
+ private int successFailureCount = 0
+
+ @Override
+ void filter(ClientRequestContext requestContext) throws IOException {
+ def requestUri = requestContext.uri
+ def requestPath = requestUri.scheme + "://" + requestUri.host + requestUri.path
+
+ switch (requestPath) {
+ case "https://mockapi/basicauth":
+ def authHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)
+ if (authHeader != null) {
+ def usernameAndPassword = BasicAuthHelper.parseHeader(authHeader)
+ if (usernameAndPassword != null
+ && usernameAndPassword[0] == "testuser"
+ && usernameAndPassword[1] == "password1") {
+ requestContext.abortWith(Response.ok().build())
+ return
+ }
+ }
+ break
+ case "https://mockapi/token":
+ // OAuth token request extract the grant info
+ def grant = ((Form) requestContext.getEntity()).asMap()
+ if (grant.getFirst(OAuthGrant.VALUE_KEY_GRANT_TYPE) == "password"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_ID) == "TestClient"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_SECRET) == "TestSecret"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_SCOPE) == "scope1 scope2"
+ && grant.getFirst(OAuthPasswordGrant.VALUE_KEY_USERNAME) == "testuser"
+ && grant.getFirst(OAuthPasswordGrant.VALUE_KEY_PASSWORD) == "password") {
+ accessToken = "accesstoken" + accessTokenCount++
+ def response = new OAuthServerResponse()
+ response.accessToken = accessToken
+ response.expiresIn = 100
+ response.tokenType = "Bearer"
+
+ // Include refresh token if configured to support it
+ if (supportsRefresh) {
+ refreshToken = "refreshtoken" + accessTokenCount
+ response.refreshToken = refreshToken
+ }
+
+ requestContext.abortWith(
+ Response.ok(response, MediaType.APPLICATION_JSON_TYPE).build()
+ )
+ return
+ } else if (grant.getFirst(OAuthGrant.VALUE_KEY_GRANT_TYPE) == "refresh_token"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_ID) == "TestClient"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_CLIENT_SECRET) == "TestSecret"
+ && grant.getFirst(OAuthGrant.VALUE_KEY_SCOPE) == "scope1 scope2"
+ && grant.getFirst(OAuthRefreshTokenGrant.VALUE_KEY_REFRESH_TOKEN) == refreshToken) {
+ refreshTokenCount++
+ accessToken = "accesstoken" + accessTokenCount++
+ refreshToken = "refreshtoken" + accessTokenCount
+ def response = new OAuthServerResponse()
+ response.accessToken = accessToken
+ response.refreshToken = refreshToken
+ response.expiresIn = 100
+ response.tokenType = "Bearer"
+
+ requestContext.abortWith(
+ Response.ok(response, MediaType.APPLICATION_JSON_TYPE).build()
+ )
+ return
+ }
+ break
+ default:
+ // Check access token is valid
+ def authHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)
+ def accessToken = authHeader == null ? null : authHeader.substring(7)
+ if (accessToken == null || this.accessToken == null || accessToken != this.accessToken) {
+ requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build())
+ return
+ }
+ break
+ }
+
+ switch (requestPath) {
+ case "https://mockapi/pingGet":
+ UriInfo uriInfo = new ResteasyUriInfo(requestContext.uri)
+ def queryParams = uriInfo.getQueryParameters(true)
+ if (queryParams.get("param1").size() == 1
+ && queryParams.getFirst("param1") == "param1Value1"
+ && queryParams.get("param2").size() == 2
+ && queryParams.get("param2").get(0) == "param2Value1"
+ && queryParams.get("param2").get(1) == "param2Value2"
+ && requestContext.getHeaderString("header1") == "header1Value1"
+ && requestContext.getHeaderString("header2") == "header2Value1,header2Value2") {
+ pingCount++
+ requestContext.abortWith(
+ Response.ok().build()
+ )
+ return
+ }
+ break
+ case "https://mockapi/pingPost":
+ UriInfo uriInfo = new ResteasyUriInfo(requestContext.uri)
+ def queryParams = uriInfo.getQueryParameters(true)
+ if (queryParams.get("param1").size() == 2
+ && queryParams.getFirst("param1") == "param1Value1"
+ && queryParams.get("param1").get(1) == "param1Value2"
+ && queryParams.get("param2").size() == 2
+ && queryParams.get("param2").get(0) == "param2Value1"
+ && queryParams.get("param2").get(1) == "param2Value2"
+ && queryParams.get("param3").size() == 1
+ && queryParams.getFirst("param3") == "param3Value1"
+ && requestContext.method == HttpMethod.POST
+ && requestContext.getHeaderString("header1") == "header1Value1"
+ && requestContext.getHeaderString("header2") == "header2Value1,header2Value2"
+ && (requestContext.getHeaderString("Content-type") == MediaType.APPLICATION_JSON
+ || requestContext.getHeaderString("Content-type") == MediaType.APPLICATION_XML)) {
+
+ String bodyStr = (String) requestContext.getEntity()
+ ObjectValue body = Values.<ObjectValue> parse(bodyStr).orElse(null)
+ if (body.get("prop1").isPresent() && body.get("prop2").isPresent()) {
+ pingCount++
+ requestContext.abortWith(Response.ok().build())
+ return
+ }
+ }
+ break
+ case "https://mockapi/put_request_with_headers":
+ UriInfo uriInfo = new ResteasyUriInfo(requestContext.uri)
+ def queryParams = uriInfo.getQueryParameters(true)
+ if (queryParams.get("param1").size() == 1
+ && queryParams.getFirst("param1") == "param1Value1"
+ && queryParams.get("param2").size() == 3
+ && queryParams.get("param2").get(0) == "param2Value1"
+ && queryParams.get("param2").get(1) == "param2Value2"
+ && queryParams.get("param2").get(2) == "param2Value3"
+ && queryParams.get("param3").size() == 1
+ && queryParams.getFirst("param3") == "param3Value1"
+ && requestContext.method == HttpMethod.PUT
+ && requestContext.getHeaderString("header1") == null
+ && requestContext.getHeaderString("header2") == "header2Value1,header2Value2"
+ && requestContext.getHeaderString("Content-type") == MediaType.APPLICATION_JSON) {
+
+ String bodyStr = (String) requestContext.getEntity()
+ ObjectValue body = Values.<ObjectValue> parse(bodyStr).orElse(null)
+ if (body.get("prop1").isPresent()
+ && body.get("prop1").get().toString() == /{"myProp1":123,"myProp2":true}/
+ && body.get("prop2").isPresent() && body.get("prop2").get().toString() == "prop2Value") {
+ putRequestWithHeadersCalled = true
+ requestContext.abortWith(Response.ok().build())
+ return
+ }
+ }
+ break
+ case "https://mockapi/value/50/set":
+ requestContext.abortWith(Response.ok().build())
+ return
+ case "https://mockapi/get_poll_slow":
+ pollCountSlow++
+ requestContext.abortWith(
+ Response
+ .ok("This is an example response where the value of 100% is in the body of the message.", MediaType.TEXT_PLAIN)
+ .build()
+ )
+ return
+ case "https://mockapi/get_poll_fast":
+ pollCountFast++
+ requestContext.abortWith(
+ Response
+ .ok("This is an example response where there are multiple values of 100% 60% in the body of the message.", MediaType.TEXT_PLAIN)
+ .build()
+ )
+ return
+ case "https://mockapi/get_success_200":
+ case "https://redirected.mockapi/get_success_200":
+ successFailureCount++
+ requestContext.abortWith(Response.ok().build())
+ return
+ case "https://mockapi/get_failure_401":
+ successFailureCount++
+ requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build())
+ return
+ case "https://mockapi/redirect":
+ requestContext.abortWith(Response.temporaryRedirect(new URI("https://redirected.mockapi/get_success_200")).build())
+ return
+ }
+
+ requestContext.abortWith(Response.serverError().build())
+ }
+ }
+
+ def cleanup() {
+ mockServer.supportsRefresh = false
+ mockServer.accessToken = null
+ mockServer.refreshToken = null
+ mockServer.accessTokenCount = 0
+ mockServer.refreshTokenCount = 0
+ mockServer.pingCount = 0
+ mockServer.pollCountSlow = 0
+ mockServer.pollCountFast = 0
+ mockServer.successFailureCount = 0
+ mockServer.putRequestWithHeadersCalled = false
+ }
+
+ def "Check UDP client protocol configuration and linked attribute deployment"() {
+
+ given: "expected conditions"
+ def conditions = new PollingConditions(timeout: 10, delay: 1)
+
+ and: "the UDP client protocol min times are adjusted for testing"
+ UdpClientProtocol.MIN_POLLING_MILLIS = 10
+
+ when: "the container starts"
+ def serverPort = findEphemeralPort()
+ def container = startContainerNoDemoImport(defaultConfig(serverPort), defaultServices())
+ def protocolExecutorService = container.getService(ProtocolExecutorService.class)
+ def udpClientProtocol = container.getService(UdpClientProtocol.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def agentService = container.getService(AgentService.class)
+
+ then: "the container should be running"
+ conditions.eventually {
+ assert container.isRunning()
+ }
+
+ when: "a simple UDP echo server is started"
+ def echoServerPort = findEphemeralPort()
+ def clientPort = findEphemeralPort()
+ def echoServer = new UdpStringServer(protocolExecutorService, new InetSocketAddress(echoServerPort), ";", Integer.MAX_VALUE, true)
+ def doEcho = true
+ def clientActualPort = null
+ def lastCommand = null
+ def receivedMessages = []
+ echoServer.addMessageConsumer({
+ message, channel, sender ->
+ clientActualPort = sender.port
+ lastCommand = message
+ receivedMessages.add(message)
+
+ if (doEcho) {
+ echoServer.sendMessage(message, sender)
+ }
+ })
+ echoServer.start()
+
+ then: "the UDP echo server should be connected"
+ conditions.eventually {
+ echoServer.connectionStatus == ConnectionStatus.CONNECTED
+ }
+
+ when: "an agent with a UDP client protocol configuration is created"
+ def agent = new Asset()
+ agent.setRealm(Constants.MASTER_REALM)
+ agent.setName("Test Agent")
+ agent.setType(AssetType.AGENT)
+ agent.setAttributes(
+ initProtocolConfiguration(new AssetAttribute("protocolConfig"), UdpClientProtocol.PROTOCOL_NAME)
+ .addMeta(
+ new MetaItem(
+ UdpClientProtocol.META_PROTOCOL_HOST,
+ Values.create("255.255.255.255")
+ ),
+ new MetaItem(
+ UdpClientProtocol.META_PROTOCOL_PORT,
+ Values.create(echoServerPort)
+ ),
+ new MetaItem(
+ UdpClientProtocol.META_PROTOCOL_BIND_PORT,
+ Values.create(clientPort)
+ ),
+ new MetaItem(
+ UdpClientProtocol.META_RESPONSE_TIMEOUT_MILLIS,
+ Values.create(200)
+ )
+ )
+ )
+
+ and: "the agent is added to the asset service"
+ agent = assetStorageService.merge(agent)
+
+ then: "the protocol should authenticate and start pinging the server and the connection status should become CONNECTED"
+ conditions.eventually {
+ def status = agentService.getProtocolConnectionStatus(new AttributeRef(agent.id, "protocolConfig"))
+ assert status == ConnectionStatus.CONNECTED
+ }
+
+ when: "an asset is created with attributes linked to the protocol configuration"
+ def asset = new Asset("Test Asset", AssetType.THING, agent)
+ asset.setAttributes(
+ // simple send with no retries
+ new AssetAttribute("echoHello", AttributeValueType.STRING)
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
+ new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create('Hello {$value};')),
+ new MetaItem(UdpClientProtocol.META_SEND_RETRIES, Values.create(0)),
+ new MetaItem(UdpClientProtocol.META_SERVER_ALWAYS_RESPONDS, Values.create(true))
+ ),
+ // attribute send with 10 retries
+ new AssetAttribute("echoWorld", AttributeValueType.STRING)
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
+ new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create("World;")),
+ new MetaItem(UdpClientProtocol.META_SEND_RETRIES, Values.create(10)),
+ new MetaItem(UdpClientProtocol.META_SERVER_ALWAYS_RESPONDS, Values.create(true))
+ ),
+ // attribute poll with 3 retries
+ new AssetAttribute("pollTest", AttributeValueType.STRING)
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),
+ new MetaItem(UdpClientProtocol.META_ATTRIBUTE_WRITE_VALUE, Values.create("Test;")),
+ new MetaItem(UdpClientProtocol.META_POLLING_MILLIS, Values.create(100)),
+ new MetaItem(UdpClientProtocol.META_SEND_RETRIES, Values.create(3))
+ ),
+ )
+
+ and: "the asset is merged into the asset service"
+ asset = assetStorageService.merge(asset)
+
+ then: "the polling attribute should have the correct values"
+ def messageCount = 0
+ conditions.eventually {
+ asset = assetStorageService.find(asset.getId(), true)
+ assert asset.getAttribute("pollTest").flatMap({ it.getValueAsString() }).orElse(null) == "Test"
+ messageCount = receivedMessages.size()
+ }
+
+ and: "the attribute should continue sending to the server"
+ conditions.eventually {
+ receivedMessages.size() > messageCount + 1
+ }
+
+ when: "a linked attribute value is updated"
+ def attributeEvent = new AttributeEvent(asset.id,
+ "echoHello",
+ Values.create("there"))
+ assetProcessingService.sendAttributeEvent(attributeEvent)
+
+ then: "the server should have received the request"
+ conditions.eventually {
+ assert receivedMessages.indexOf("Hello there") >= 0
+ }
+
+ cleanup: "the server should be stopped"
+ stopContainer(container)
+ }
+}
\ No newline at end of file
diff --git a/test/src/test/groovy/org/openremote/test/protocol/UdpClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
similarity index 97%
rename from test/src/test/groovy/org/openremote/test/protocol/UdpClientTest.groovy
rename to test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
index f8c2027ca..9e40777f7 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/UdpClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
@@ -17,11 +17,12 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-package org.openremote.test.protocol
+package org.openremote.test.protocol.udp
import io.netty.buffer.ByteBuf
import io.netty.util.CharsetUtil
import io.netty.util.internal.SocketUtils
+import org.openremote.agent.protocol.ProtocolExecutorService
import org.openremote.agent.protocol.udp.AbstractUdpClient
import org.openremote.agent.protocol.udp.UdpStringServer
import org.openremote.manager.concurrent.ManagerExecutorService
@@ -46,7 +47,7 @@ class UdpClientTest extends Specification implements ManagerContainerTrait {
def serverPort = findEphemeralPort()
def clientPort = findEphemeralPort()
def container = startContainer(defaultConfig(serverPort), Collections.singletonList(new ManagerExecutorService()))
- def protocolExecutorService = container.getService(ManagerExecutorService.class)
+ def protocolExecutorService = container.getService(ProtocolExecutorService.class)
and: "a simple UDP echo server"
def echoServerPort = findEphemeralPort()