You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2019/08/02 16:32:44 UTC
[plc4x] 01/01: MWE.
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch too-many-open-files
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 5ddc7595c0b49ecfe552fb5bcd7b95acba01617c
Author: julian <j....@pragmaticminds.de>
AuthorDate: Fri Aug 2 18:32:36 2019 +0200
MWE.
---
.../plc4x/java/examples/helloplc4x/HelloPlc4x.java | 77 ++++------------------
1 file changed, 11 insertions(+), 66 deletions(-)
diff --git a/plc4j/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java b/plc4j/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
index 9a236ae..8759322 100644
--- a/plc4j/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
+++ b/plc4j/examples/hello-world-plc4x/src/main/java/org/apache/plc4x/java/examples/helloplc4x/HelloPlc4x.java
@@ -20,6 +20,8 @@ package org.apache.plc4x.java.examples.helloplc4x;
import org.apache.plc4x.java.PlcDriverManager;
import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.messages.PlcReadRequest;
import org.apache.plc4x.java.api.messages.PlcReadResponse;
import org.apache.plc4x.java.api.types.PlcResponseCode;
@@ -27,6 +29,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletableFuture;
+import java.util.stream.IntStream;
public class HelloPlc4x {
@@ -38,74 +41,16 @@ public class HelloPlc4x {
* @param args ignored.
*/
public static void main(String[] args) throws Exception {
- CliOptions options = CliOptions.fromArgs(args);
- if (options == null) {
- CliOptions.printHelp();
- // Could not parse.
- System.exit(1);
- }
-
- // Establish a connection to the plc using the url provided as first argument
- try (PlcConnection plcConnection = new PlcDriverManager().getConnection(options.getConnectionString())) {
-
- // Check if this connection support reading of data.
- if (!plcConnection.getMetadata().canRead()) {
- logger.error("This connection doesn't support reading.");
- return;
- }
-
- // Create a new read request:
- // - Give the single item requested the alias name "value"
- PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
- for (int i = 0; i < options.getFieldAddress().length; i++) {
- builder.addItem("value-" + i, options.getFieldAddress()[i]);
- }
- PlcReadRequest readRequest = builder.build();
-
- //////////////////////////////////////////////////////////
- // Read synchronously ...
- // NOTICE: the ".get()" immediately lets this thread pause until
- // the response is processed and available.
- logger.info("Synchronous request ...");
- PlcReadResponse syncResponse = readRequest.execute().get();
- // Simply iterating over the field names returned in the response.
- printResponse(syncResponse);
-
- //////////////////////////////////////////////////////////
- // Read asynchronously ...
- // Register a callback executed as soon as a response arrives.
- logger.info("Asynchronous request ...");
- CompletableFuture<? extends PlcReadResponse> asyncResponse = readRequest.execute();
- asyncResponse.whenComplete((readResponse, throwable) -> {
- if (readResponse != null) {
- printResponse(readResponse);
- } else {
- logger.error("An error occurred: " + throwable.getMessage(), throwable);
- }
- });
- }
+ IntStream.range(0, 1000).parallel().forEach(i -> createConnection(i));
}
- private static void printResponse(PlcReadResponse response) {
- for (String fieldName : response.getFieldNames()) {
- if(response.getResponseCode(fieldName) == PlcResponseCode.OK) {
- int numValues = response.getNumberOfValues(fieldName);
- // If it's just one element, output just one single line.
- if(numValues == 1) {
- logger.info("Value[" + fieldName + "]: " + response.getObject(fieldName));
- }
- // If it's more than one element, output each in a single row.
- else {
- logger.info("Value[" + fieldName + "]:");
- for(int i = 0; i < numValues; i++) {
- logger.info(" - " + response.getObject(fieldName, i));
- }
- }
- }
- // Something went wrong, to output an error message instead.
- else {
- logger.error("Error[" + fieldName + "]: " + response.getResponseCode(fieldName).name());
- }
+ private static void createConnection(int i) {
+ System.out.println("trying " + i);
+ try (PlcConnection plcConnection = new PlcDriverManager().getConnection("s7://192.168.167.211/0/0")) {
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ logger.error("Kann keine Verbindung zu der angegebenen Adresse aufbauen");
}
}