use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage in project milo by eclipse.
the class UaStackClient method sendRequest.
/**
* Send a {@link UaRequestMessage} to the connected server.
* <p>
* The {@link RequestHeader} of {@code request} must have a unique request handle. Use the
* {@code newRequestHeader} helper functions to create headers for {@link UaRequestMessage}s.
*
* @param request the {@link UaRequestMessage} to send.
* @return a {@link CompletableFuture} containing the eventual {@link UaResponseMessage} from the server.
* @see #newRequestHeader()
* @see #newRequestHeader(NodeId)
* @see #newRequestHeader(NodeId, UInteger)
*/
public CompletableFuture<UaResponseMessage> sendRequest(UaRequestMessage request) {
RequestHeader requestHeader = request.getRequestHeader();
UInteger requestHandle = requestHeader.getRequestHandle();
final CompletableFuture<UaResponseMessage> future = new CompletableFuture<>();
pending.put(requestHandle, future);
transport.sendRequest(request).whenComplete((response, ex) -> {
pending.remove(requestHandle);
deliverResponse(request, response, ex, future);
});
return future;
}
use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage 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.serialization.UaResponseMessage in project milo by eclipse.
the class ClientServerTest method testClientStateMachine.
@Test
public void testClientStateMachine() 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);
for (int i = 0; i < 1000; i++) {
client.connect().get();
RequestHeader header = new RequestHeader(NodeId.NULL_VALUE, DateTime.now(), uint(i), 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.debug("sending request: {}", request);
UaResponseMessage response = client.sendRequest(request).get();
logger.debug("got response: {}", response);
client.disconnect().get();
}
}
use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage in project milo by eclipse.
the class DataTypeTreeBuilder method readNamespaceTable.
private static CompletableFuture<NamespaceTable> readNamespaceTable(UaStackClient client, OpcUaSession session) {
RequestHeader requestHeader = client.newRequestHeader(session.getAuthenticationToken(), client.getConfig().getRequestTimeout());
CompletableFuture<UaResponseMessage> readFuture = client.sendRequest(new ReadRequest(requestHeader, 0.0, TimestampsToReturn.Neither, new ReadValueId[] { new ReadValueId(Identifiers.Server_NamespaceArray, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE) }));
return readFuture.thenApply(ReadResponse.class::cast).thenApply(response -> {
DataValue dataValue = response.getResults()[0];
String[] namespaceUris = (String[]) dataValue.getValue().getValue();
NamespaceTable namespaceTable = new NamespaceTable();
for (String namespaceUri : namespaceUris) {
namespaceTable.addUri(namespaceUri);
}
return namespaceTable;
});
}
use of org.eclipse.milo.opcua.stack.core.serialization.UaResponseMessage in project milo by eclipse.
the class OpcUaClient method maybeHandleServiceFault.
private void maybeHandleServiceFault(UaResponseMessage response, Throwable ex) {
if (faultListeners.isEmpty())
return;
if (ex != null) {
if (ex instanceof UaServiceFaultException) {
UaServiceFaultException faultException = (UaServiceFaultException) ex;
ServiceFault serviceFault = faultException.getServiceFault();
logger.debug("Notifying {} ServiceFaultListeners", faultListeners.size());
faultNotificationQueue.submit(() -> faultListeners.forEach(h -> h.onServiceFault(serviceFault)));
} else if (ex.getCause() instanceof UaServiceFaultException) {
UaServiceFaultException faultException = (UaServiceFaultException) ex.getCause();
ServiceFault serviceFault = faultException.getServiceFault();
logger.debug("Notifying {} ServiceFaultListeners", faultListeners.size());
faultNotificationQueue.submit(() -> faultListeners.forEach(h -> h.onServiceFault(serviceFault)));
}
}
}
Aggregations