use of org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest in project milo by eclipse.
the class OpcUaClient method readNamespaceTableAsync.
/**
* Read the server's NamespaceTable and update the local copy.
* <p>
* This call completes asynchronously.
*
* @return a {@link CompletableFuture} that completes successfully with the updated
* {@link NamespaceTable} or completes exceptionally if a service- or operation-level error
* occurs.
*/
public CompletableFuture<NamespaceTable> readNamespaceTableAsync() {
return getSession().thenCompose(session -> {
RequestHeader requestHeader = newRequestHeader(session.getAuthenticationToken());
ReadRequest readRequest = new ReadRequest(requestHeader, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(Identifiers.Server_NamespaceArray, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE) });
CompletableFuture<String[]> namespaceArray = sendRequest(readRequest).thenApply(ReadResponse.class::cast).thenApply(response -> Objects.requireNonNull(response.getResults())).thenApply(results -> (String[]) results[0].getValue().getValue());
return namespaceArray.thenAccept(this::updateNamespaceTable).thenApply(v -> getNamespaceTable());
});
}
use of org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest in project milo by eclipse.
the class DefaultAttributeServiceSet method onRead.
@Override
public void onRead(ServiceRequest service) {
ReadRequest request = (ReadRequest) service.getRequest();
OpcUaServer server = service.attr(ServiceAttributes.SERVER_KEY).get();
Session session = service.attr(ServiceAttributes.SESSION_KEY).get();
List<ReadValueId> nodesToRead = l(request.getNodesToRead());
if (nodesToRead.isEmpty()) {
service.setServiceFault(StatusCodes.Bad_NothingToDo);
return;
}
if (nodesToRead.size() > server.getConfig().getLimits().getMaxNodesPerRead().longValue()) {
service.setServiceFault(StatusCodes.Bad_TooManyOperations);
return;
}
if (request.getMaxAge() < 0d) {
service.setServiceFault(StatusCodes.Bad_MaxAgeInvalid);
return;
}
if (request.getTimestampsToReturn() == null) {
service.setServiceFault(StatusCodes.Bad_TimestampsToReturnInvalid);
return;
}
DiagnosticsContext<ReadValueId> diagnosticsContext = new DiagnosticsContext<>();
ReadContext context = new ReadContext(server, session, diagnosticsContext);
server.getAddressSpaceManager().read(context, request.getMaxAge(), request.getTimestampsToReturn(), nodesToRead);
context.getFuture().thenAccept(values -> {
ResponseHeader header = service.createResponseHeader();
DiagnosticInfo[] diagnosticInfos = diagnosticsContext.getDiagnosticInfos(nodesToRead);
ReadResponse response = new ReadResponse(header, values.toArray(new DataValue[0]), diagnosticInfos);
service.setResponse(response);
});
}
use of org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest in project milo by eclipse.
the class ClientServerTest method connectAndTest.
private void connectAndTest(Variant input, UaStackClient client) throws InterruptedException, java.util.concurrent.ExecutionException {
setReadRequestHandler(input);
client.connect().get();
List<CompletableFuture<ReadResponse>> responses = Lists.newArrayList();
for (int i = 0; i < 100; i++) {
RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(i), uint(0), null, uint(10000), null);
ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
responses.add(client.sendRequest(request).thenApply(ReadResponse.class::cast));
}
CompletableFuture.allOf(responses.toArray(new CompletableFuture[0])).get();
FutureUtils.sequence(responses).get().forEach(response -> {
Variant value = l(response.getResults()).get(0).getValue();
assertEquals(value, input);
});
client.disconnect().get();
}
use of org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest in project milo by eclipse.
the class ClientServerTest method testClientDisconnect.
@Test
public void testClientDisconnect() throws Exception {
EndpointDescription endpoint = endpoints[0];
Variant input = new Variant(42);
logger.info("SecurityPolicy={}, MessageSecurityMode={}, input={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode(), input);
UaStackClient client = createClient(endpoint);
client.connect().get();
RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(0), uint(0), null, DEFAULT_TIMEOUT_HINT, null);
ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
logger.info("sending request: {}", request);
UaResponseMessage response0 = client.sendRequest(request).get();
logger.info("got response: {}", response0);
client.disconnect().get();
assertThrows(() -> client.sendRequest(request).get());
}
use of org.eclipse.milo.opcua.stack.core.types.structured.ReadRequest in project milo by eclipse.
the class ClientServerTest method testClientTimeout.
@Test
public void testClientTimeout() throws Exception {
EndpointDescription endpoint = endpoints[0];
logger.info("SecurityPolicy={}, MessageSecurityMode={}", SecurityPolicy.fromUri(endpoint.getSecurityPolicyUri()), endpoint.getSecurityMode());
UaStackClientConfig config = UaStackClientConfig.builder().setEndpoint(endpoint).setKeyPair(clientKeyPair).setCertificate(clientCertificate).build();
UaStackClient client = UaStackClient.create(config);
client.connect().get();
server.addServiceHandler("/test", ReadRequest.TYPE_ID, service -> {
// intentionally do nothing so the request can timeout
logger.info("received {}; ignoring...", service.getRequest());
});
RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(0), uint(0), null, uint(1000), null);
ReadRequest request = new ReadRequest(header, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(NodeId.NULL_VALUE, AttributeId.Value.uid(), null, null) });
try {
client.sendRequest(request).get();
fail("expected response to timeout");
} catch (Throwable t) {
StatusCode statusCode = UaException.extractStatusCode(t).orElse(StatusCode.BAD);
assertEquals(statusCode.getValue(), StatusCodes.Bad_Timeout);
}
}
Aggregations