use of org.apache.plc4x.java.s7.readwrite.TPKTPacket in project plc4x by apache.
the class BenchmarkGeneratedS7 method main.
public static void main(String[] args) throws Exception {
byte[] rData = Hex.decodeHex("0300001611e00000000f00c2020100c1020311c0010a");
long start = System.currentTimeMillis();
int numRunsParse = 2000000;
// Benchmark the parsing code
TPKTPacket packet = null;
for (int i = 0; i < numRunsParse; i++) {
ReadBuffer rBuf = new ReadBufferByteBased(rData);
packet = TPKTPacket.staticParse(rBuf);
}
long endParsing = System.currentTimeMillis();
System.out.println("Parsed " + numRunsParse + " packets in " + (endParsing - start) + "ms");
System.out.println("That's " + ((float) (endParsing - start) / numRunsParse) + "ms per packet");
// Benchmark the serializing code
int numRunsSerialize = 2000000;
byte[] oData = null;
for (int i = 0; i < numRunsSerialize; i++) {
WriteBufferByteBased wBuf = new WriteBufferByteBased(packet.getLengthInBytes());
packet.serialize(wBuf);
oData = wBuf.getData();
}
long endSerializing = System.currentTimeMillis();
System.out.println("Serialized " + numRunsSerialize + " packets in " + (endSerializing - endParsing) + "ms");
System.out.println("That's " + ((float) (endSerializing - endParsing) / numRunsSerialize) + "ms per packet");
if (!Arrays.equals(rData, oData)) {
for (int i = 0; i < rData.length; i++) {
if (rData[i] != oData[i]) {
System.out.println("Difference in byte " + i);
}
}
System.out.println("Not equals");
} else {
System.out.println("Bytes equal");
}
}
use of org.apache.plc4x.java.s7.readwrite.TPKTPacket in project plc4x by apache.
the class S7ServerModule method start.
@Override
public void start() throws SimulatorExcepiton {
if (loopGroup != null) {
return;
}
try {
loopGroup = new NioEventLoopGroup();
workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(loopGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new GeneratedProtocolMessageCodec<>(TPKTPacket.class, TPKTPacket::staticParse, ByteOrder.BIG_ENDIAN, null, new S7Driver.ByteLengthEstimator(), new S7Driver.CorruptPackageCleaner()));
pipeline.addLast(new S7Step7ServerAdapter(context));
}
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
bootstrap.bind(ISO_ON_TCP_PORT).sync();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new SimulatorExcepiton(e);
}
}
use of org.apache.plc4x.java.s7.readwrite.TPKTPacket in project plc4x by apache.
the class S7ProtocolLogic method write.
@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
CompletableFuture<PlcWriteResponse> future = new CompletableFuture<>();
DefaultPlcWriteRequest request = (DefaultPlcWriteRequest) writeRequest;
List<S7VarRequestParameterItem> parameterItems = new ArrayList<>(request.getNumberOfFields());
List<S7VarPayloadDataItem> payloadItems = new ArrayList<>(request.getNumberOfFields());
for (String fieldName : request.getFieldNames()) {
final S7Field field = (S7Field) request.getField(fieldName);
final PlcValue plcValue = request.getPlcValue(fieldName);
parameterItems.add(new S7VarRequestParameterItemAddress(encodeS7Address(field)));
payloadItems.add(serializePlcValue(field, plcValue));
}
final int tpduId = tpduGenerator.getAndIncrement();
// If we've reached the max value for a 16 bit transaction identifier, reset back to 1
if (tpduGenerator.get() == 0xFFFF) {
tpduGenerator.set(1);
}
TPKTPacket tpktPacket = new TPKTPacket(new COTPPacketData(null, new S7MessageRequest(tpduId, new S7ParameterWriteVarRequest(parameterItems), new S7PayloadWriteVarRequest(payloadItems, null)), true, (short) tpduId, null));
// Start a new request-transaction (Is ended in the response-handler)
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
transaction.submit(() -> context.sendRequest(tpktPacket).onTimeout(new TransactionErrorCallback<>(future, transaction)).onError(new TransactionErrorCallback<>(future, transaction)).expectResponse(TPKTPacket.class, REQUEST_TIMEOUT).check(p -> p.getPayload() instanceof COTPPacketData).unwrap(p -> ((COTPPacketData) p.getPayload())).unwrap(COTPPacket::getPayload).check(p -> p.getTpduReference() == tpduId).handle(p -> {
try {
future.complete(((PlcWriteResponse) decodeWriteResponse(p, writeRequest)));
} catch (PlcProtocolException e) {
logger.warn("Error sending 'write' message: '{}'", e.getMessage(), e);
}
// Finish the request-transaction.
transaction.endRequest();
}));
return future;
}
use of org.apache.plc4x.java.s7.readwrite.TPKTPacket in project plc4x by apache.
the class S7ProtocolLogic method subscribe.
@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
CompletableFuture<PlcSubscriptionResponse> future = new CompletableFuture<>();
DefaultPlcSubscriptionRequest request = (DefaultPlcSubscriptionRequest) subscriptionRequest;
List<S7ParameterUserDataItem> parameterItems = new ArrayList<>(request.getNumberOfFields());
List<S7PayloadUserDataItem> payloadItems = new ArrayList<>(request.getNumberOfFields());
for (String fieldName : request.getFieldNames()) {
final DefaultPlcSubscriptionField sf = (DefaultPlcSubscriptionField) request.getField(fieldName);
final S7SubscriptionField field = (S7SubscriptionField) sf.getPlcField();
switch(field.getFieldType()) {
case EVENT_SUBSCRIPTION:
;
encodeEventSubcriptionRequest(request, parameterItems, payloadItems);
break;
case EVENT_UNSUBSCRIPTION:
;
// encodeEventUnSubcriptionRequest(msg, out);
break;
case ALARM_ACK:
;
// encodeAlarmAckRequest(msg, out);
break;
case ALARM_QUERY:
;
// encodeAlarmQueryRequest(msg, out);
break;
case CYCLIC_SUBSCRIPTION:
;
// encodeCycledSubscriptionRequest(msg, out);
break;
case CYCLIC_UNSUBSCRIPTION:
;
// encodeCycledUnSubscriptionRequest(msg, out);
break;
default:
;
}
;
// final PlcValue plcValue = request.getPlcValue(fieldName);
// parameterItems.add(new S7VarRequestParameterItemAddress(encodeS7Address(field)));
// payloadItems.add(serializePlcValue(field, plcValue));
}
final int tpduId = tpduGenerator.getAndIncrement();
// If we've reached the max value for a 16 bit transaction identifier, reset back to 1
if (tpduGenerator.get() == 0xFFFF) {
tpduGenerator.set(1);
}
TPKTPacket tpktPacket = new TPKTPacket(new COTPPacketData(null, new S7MessageUserData(tpduId, new S7ParameterUserData(parameterItems), new S7PayloadUserData(payloadItems, null)), true, (short) tpduId, null));
// Start a new request-transaction (Is ended in the response-handler)
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
transaction.submit(() -> context.sendRequest(tpktPacket).onTimeout(new TransactionErrorCallback<>(future, transaction)).onError(new TransactionErrorCallback<>(future, transaction)).expectResponse(TPKTPacket.class, REQUEST_TIMEOUT).check(p -> p.getPayload() instanceof COTPPacketData).unwrap(p -> ((COTPPacketData) p.getPayload())).unwrap(COTPPacket::getPayload).check(p -> p.getTpduReference() == tpduId).handle(p -> {
try {
future.complete(((PlcSubscriptionResponse) decodeEventSubcriptionRequest(p, subscriptionRequest)));
} catch (PlcProtocolException e) {
logger.warn("Error sending 'write' message: '{}'", e.getMessage(), e);
}
// Finish the request-transaction.
transaction.endRequest();
}));
return future;
}
Aggregations