You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2019/10/10 11:14:52 UTC
[plc4x] branch develop updated: - Fine tuned the simulator core
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new dfb3353 - Fine tuned the simulator core
dfb3353 is described below
commit dfb33532918dd5a5255deb1166daa6fdf9359c6e
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Thu Oct 10 13:14:44 2019 +0200
- Fine tuned the simulator core
---
.../org/apache/plc4x/simulator/PlcSimulator.java | 46 ++++++++++++++---
.../apache/plc4x/simulator/server/s7/S7Server.java | 58 ++++++++++++----------
.../plc4x/simulator/server/s7/S7ServerModule.java | 17 ++++++-
3 files changed, 87 insertions(+), 34 deletions(-)
diff --git a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/PlcSimulator.java b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/PlcSimulator.java
index 9553c02..aa55608 100644
--- a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/PlcSimulator.java
+++ b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/PlcSimulator.java
@@ -20,6 +20,8 @@ package org.apache.plc4x.simulator;
import org.apache.plc4x.simulator.server.ServerModule;
import org.apache.plc4x.simulator.simulation.SimulationModule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.ServiceLoader;
@@ -28,61 +30,89 @@ import java.util.concurrent.TimeUnit;
public class PlcSimulator {
+ private static final Logger LOGGER = LoggerFactory.getLogger(PlcSimulator.class);
+
private boolean running;
- private static Map<String, ServerModule> serverModules;
- private static Map<String, SimulationModule> simulationModules;
+ private final Map<String, ServerModule> serverModules;
+ private final Map<String, SimulationModule> simulationModules;
- public PlcSimulator() {
+ private PlcSimulator() {
this(Thread.currentThread().getContextClassLoader());
}
- public PlcSimulator(ClassLoader classLoader) {
+ private PlcSimulator(ClassLoader classLoader) {
+
// Initialize all the server modules.
+ LOGGER.info("Initializing Server Modules:");
serverModules = new TreeMap<>();
ServiceLoader<ServerModule> serverModuleLoader = ServiceLoader.load(ServerModule.class, classLoader);
for (ServerModule serverModule : serverModuleLoader) {
+ LOGGER.info(String.format("Initializing server module: %s ...", serverModule.getName()));
serverModules.put(serverModule.getName(), serverModule);
+ LOGGER.info("Initialized");
}
+ LOGGER.info("Finished Initializing Server Modules\n");
// Initialize all the simulation modules.
+ LOGGER.info("Initializing Simulation Modules:");
simulationModules = new TreeMap<>();
ServiceLoader<SimulationModule> simulationModuleLoader = ServiceLoader.load(SimulationModule.class, classLoader);
for (SimulationModule simulationModule : simulationModuleLoader) {
+ LOGGER.info(String.format("Initializing simulation module: %s ...", simulationModule.getName()));
simulationModules.put(simulationModule.getName(), simulationModule);
+ LOGGER.info("Initialized");
}
+ LOGGER.info("Finished Initializing Simulation Modules\n");
running = true;
}
- public void stop() {
+ private void stop() {
running = false;
}
- public void run() throws Exception {
+ private void run() throws Exception {
// Start all server modules.
+ LOGGER.info("Starting Server Modules:");
for (ServerModule serverModule : serverModules.values()) {
+ LOGGER.info(String.format("Starting server module: %s ...", serverModule.getName()));
serverModule.start();
+ LOGGER.info("Started");
}
+ LOGGER.info("Finished Starting Server Modules\n");
try {
+ LOGGER.info("Starting simulations ...");
while (running) {
// Give all the simulation modules the chance to do something.
for (SimulationModule simulationModule : simulationModules.values()) {
- simulationModule.loop();
+ try {
+ simulationModule.loop();
+ } catch(Exception e) {
+ LOGGER.error("Caught error while executing loop() method of " + simulationModule.getName() +
+ " simulation.", e);
+ }
}
// Sleep 100 ms to not run the simulation too eagerly.
TimeUnit.MILLISECONDS.sleep(100);
}
} finally {
+ LOGGER.info("Simulations ended");
// Start all server modules.
for (ServerModule serverModule : serverModules.values()) {
+ LOGGER.info(String.format("Stopping server module %s ...", serverModule.getName()));
serverModule.stop();
+ LOGGER.info("Stopped");
}
}
}
public static void main(String[] args) throws Exception {
- new PlcSimulator().run();
+ final PlcSimulator simulator = new PlcSimulator();
+ // Make sure we stop everything correctly.
+ Runtime.getRuntime().addShutdownHook(new Thread(simulator::stop));
+ // Start the simulator.
+ simulator.run();
}
}
diff --git a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/S7Server.java b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/S7Server.java
index 16cb8a8..ca7d8e4 100644
--- a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/S7Server.java
+++ b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/S7Server.java
@@ -30,32 +30,40 @@ public class S7Server {
private static final int ISO_ON_TCP_PORT = 102;
- public void run() throws Exception {
- EventLoopGroup loopGroup = new NioEventLoopGroup();
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- 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 S7Step7Protocol());
- pipeline.addLast(new S7Step7ServerProtocol());
- }
- }).option(ChannelOption.SO_BACKLOG, 128)
- .childOption(ChannelOption.SO_KEEPALIVE, true);
-
- ChannelFuture future = bootstrap.bind(ISO_ON_TCP_PORT).sync();
-
- // Wait till it ends ...
- // TODO: Remove this ...
- future.channel().closeFuture().sync();
- } finally {
- workerGroup.shutdownGracefully();
- loopGroup.shutdownGracefully();
+ private EventLoopGroup loopGroup;
+ private EventLoopGroup workerGroup;
+
+ public void start() throws Exception {
+ if(loopGroup != null) {
+ return;
+ }
+
+ 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 S7Step7Protocol());
+ pipeline.addLast(new S7Step7ServerProtocol());
+ }
+ }).option(ChannelOption.SO_BACKLOG, 128)
+ .childOption(ChannelOption.SO_KEEPALIVE, true);
+
+ bootstrap.bind(ISO_ON_TCP_PORT).sync();
+ }
+
+ public void stop() {
+ if(workerGroup == null) {
+ return;
}
+
+ workerGroup.shutdownGracefully();
+ loopGroup.shutdownGracefully();
}
}
diff --git a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/S7ServerModule.java b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/S7ServerModule.java
index e9ca944..b067305 100644
--- a/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/S7ServerModule.java
+++ b/sandbox/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/s7/S7ServerModule.java
@@ -22,6 +22,12 @@ import org.apache.plc4x.simulator.server.ServerModule;
public class S7ServerModule implements ServerModule {
+ private S7Server server;
+
+ public S7ServerModule() {
+ this.server = new S7Server();
+ }
+
@Override
public String getName() {
return "S7-STEP7";
@@ -30,11 +36,20 @@ public class S7ServerModule implements ServerModule {
@Override
public void start() {
+ try {
+ server.start();
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
}
@Override
public void stop() {
-
+ try {
+ server.stop();
+ } catch(Exception e) {
+ e.printStackTrace();
+ }
}
}