Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.openems.backend.b2brest;

import java.net.InetSocketAddress;
import org.eclipse.jetty.server.Server;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
Expand All @@ -26,6 +27,7 @@
public class Backend2BackendRest extends AbstractOpenemsBackendComponent {

public static final int DEFAULT_PORT = 8075;
public static final String DEFAULT_IP = "127.0.0.1";

private final Logger log = LoggerFactory.getLogger(Backend2BackendRest.class);

Expand All @@ -43,7 +45,7 @@ public Backend2BackendRest() {

@Activate
private void activate(Config config) throws OpenemsException {
this.startServer(config.port());
this.startServer(config.ip(), config.port());
}

@Deactivate
Expand All @@ -54,17 +56,18 @@ private void deactivate() {
/**
* Create and start new server.
*
* @param ip the IP address
* @param port the port
* @throws OpenemsException on error
*/
private synchronized void startServer(int port) throws OpenemsException {
private synchronized void startServer(String ip, int port) throws OpenemsException {
try {
this.server = new Server(port);
this.server = new Server(new InetSocketAddress(ip, port));
this.server.setHandler(new RestHandler(this));
this.server.start();
this.logInfo(this.log, "Backend2Backend.Rest started on port [" + port + "].");
this.logInfo(this.log, "Backend2Backend.Rest started on [" + ip + ":" + port + "].");
} catch (Exception e) {
throw new OpenemsException("Backend2Backend.Rest failed on port [" + port + "].", e);
throw new OpenemsException("Backend2Backend.Rest failed on [" + ip + ":" + port + "].", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
@AttributeDefinition(name = "Port", description = "The port of the REST server.")
int port() default Backend2BackendRest.DEFAULT_PORT;

@AttributeDefinition(name = "IP Address", description = "The IP address to listen on.")
String ip() default Backend2BackendRest.DEFAULT_IP;

String webconsole_configurationFactory_nameHint() default "Backend2Backend Rest";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Backend2BackendWebsocket extends AbstractOpenemsBackendComponent im
private static final String COMPONENT_ID = "b2bwebsocket0";

public static final int DEFAULT_PORT = 8076;
public static final String DEFAULT_IP = io.openems.common.websocket.AbstractWebsocketServer.DEFAULT_IP;

protected final ScheduledExecutorService executor = Executors.newScheduledThreadPool(10,
new ThreadFactoryBuilder().setNameFormat("B2bWebsocket-%d").build());
Expand Down Expand Up @@ -88,7 +89,7 @@ private void deactivate() {
*/
private synchronized void startServer() {
if (this.server == null) {
this.server = new WebsocketServer(this, this.getName(), this.config.port(), this.config.poolSize());
this.server = new WebsocketServer(this, this.getName(), this.config.ip(), this.config.port(), this.config.poolSize());
this.server.start();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
@AttributeDefinition(name = "Port", description = "The port of the websocket server.")
int port() default Backend2BackendWebsocket.DEFAULT_PORT;

@AttributeDefinition(name = "IP Address", description = "The IP address to listen on.")
String ip() default io.openems.common.websocket.AbstractWebsocketServer.DEFAULT_IP;

@AttributeDefinition(name = "Number of Threads", description = "Pool-Size: the number of threads dedicated to handle the tasks")
int poolSize() default 10;

String webconsole_configurationFactory_nameHint() default "Backend2Backend Websocket";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class WebsocketServer extends AbstractWebsocketServer<WsData> {
private final OnError onError;
private final OnClose onClose;

public WebsocketServer(Backend2BackendWebsocket parent, String name, int port, int poolSize) {
super(name, port, poolSize);
public WebsocketServer(Backend2BackendWebsocket parent, String name, String ip, int port, int poolSize) {
super(name, ip, port, poolSize);
this.parent = parent;
this.onOpen = new OnOpen(//
() -> parent.metadata, //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
@AttributeDefinition(name = "Port", description = "The port of the websocket server.")
int port() default 8081;

@AttributeDefinition(name = "IP Address", description = "The IP address to listen on.")
String ip() default io.openems.common.websocket.AbstractWebsocketServer.DEFAULT_IP;

@AttributeDefinition(name = "Number of Threads", description = "Pool-Size: the number of threads dedicated to handle the tasks")
int poolSize() default 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void deactivate() {
*/
private synchronized void startServer() {
if (this.server == null) {
this.server = new WebsocketServer(this, this.getName(), this.config.port(), this.config.poolSize());
this.server = new WebsocketServer(this, this.getName(), this.config.ip(), this.config.port(), this.config.poolSize());
this.server.start();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class WebsocketServer extends AbstractWebsocketServer<WsData> {
private final OnError onError;
private final OnClose onClose;

public WebsocketServer(EdgeWebsocketImpl parent, String name, int port, int poolSize) {
super(name, port, poolSize);
public WebsocketServer(EdgeWebsocketImpl parent, String name, String ip, int port, int poolSize) {
super(name, ip, port, poolSize);
this.parent = parent;
this.onOpen = new OnOpen(parent);
this.onRequest = new OnRequest(//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
@AttributeDefinition(name = "Port", description = "The port of the websocket server.")
int port() default 8082;

@AttributeDefinition(name = "IP Address", description = "The IP address to listen on.")
String ip() default io.openems.common.websocket.AbstractWebsocketServer.DEFAULT_IP;

@AttributeDefinition(name = "Number of Threads", description = "Pool-Size: the number of threads dedicated to handle the tasks")
int poolSize() default 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void deactivate() {
*/
private synchronized void startServer() {
if (this.server == null) {
this.server = new WebsocketServer(this, this.getName(), this.config.port(), this.config.poolSize(), this.config.requestLimit());
this.server = new WebsocketServer(this, this.getName(), this.config.ip(), this.config.port(), this.config.poolSize(), this.config.requestLimit());
this.server.start();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class WebsocketServer extends AbstractWebsocketServer<WsData> {
private final OnError onError;
private final int requestLimit;

public WebsocketServer(UiWebsocketImpl parent, String name, int port, int poolSize, int requestLimit) {
super(name, port, poolSize);
public WebsocketServer(UiWebsocketImpl parent, String name, String ip, int port, int poolSize, int requestLimit) {
super(name, ip, port, poolSize);
this.parent = parent;
this.onRequest = new OnRequest(parent);
this.onNotification = new OnNotification(parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@

public abstract class AbstractWebsocketServer<T extends WsData> extends AbstractWebsocket<T> {

public static final String DEFAULT_IP = "127.0.0.1";

/**
* Shared {@link ExecutorService}.
*/
private final ThreadPoolExecutor executor;

private final Logger log = LoggerFactory.getLogger(AbstractWebsocketServer.class);
private final int port;
private final String ip;
private final WebSocketServer ws;
private final Collection<WebSocket> connections = ConcurrentHashMap.newKeySet();

Expand All @@ -43,16 +46,18 @@ public abstract class AbstractWebsocketServer<T extends WsData> extends Abstract
* Construct an {@link AbstractWebsocketServer}.
*
* @param name to identify this server
* @param ip to listen on
* @param port to listen on
* @param poolSize number of threads dedicated to handle the tasks
*/
protected AbstractWebsocketServer(String name, int port, int poolSize) {
protected AbstractWebsocketServer(String name, String ip, int port, int poolSize) {
super(name);
this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(poolSize,
new ThreadFactoryBuilder().setNameFormat(name + "-%d").build());

this.port = port;
this.ws = new WebSocketServer(new InetSocketAddress(port),
this.ip = ip;
this.ws = new WebSocketServer(new InetSocketAddress(ip, port),
/* AVAILABLE_PROCESSORS */ Runtime.getRuntime().availableProcessors(), //
/* drafts, no filter */ List.of(new MyDraft6455()), //
this.connections) {
Expand Down Expand Up @@ -172,7 +177,7 @@ protected OnInternalError getOnInternalError() {
return (t, wsDataString) -> {
switch (t) {
case BindException be //
-> this.logError(this.log, "Unable to Bind to port [" + this.port + "]");
-> this.logError(this.log, "Unable to Bind to [" + this.ip + ":" + this.port + "]");
default //
-> this.logError(this.log, new StringBuilder() //
.append("OnInternalError for ").append(wsDataString).append(". ") //
Expand Down Expand Up @@ -223,6 +228,10 @@ public void broadcastMessage(JsonrpcMessage message, Predicate<T> matcher) {
public int getPort() {
return this.ws.getPort();
}

public String getIp() {
return this.ip;
}

/**
* Starts the {@link WebSocketServer}.
Expand All @@ -234,7 +243,7 @@ public synchronized void start() {
}
this.isStarted = true;
super.start();
this.logInfo(this.log, "Starting websocket server [port=" + this.port + "]");
this.logInfo(this.log, "Starting websocket server [" + this.ip + ":" + this.port + "]");
this.ws.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static DummyWebsocketServer.Builder create() {
private final DummyWebsocketServer.Builder builder;

private DummyWebsocketServer(DummyWebsocketServer.Builder builder) {
super("DummyWebsocketServer", 0 /* auto-select port */, 1 /* pool size */);
super("DummyWebsocketServer", "127.0.0.1", 0 /* auto-select port */, 1 /* pool size */);
this.builder = builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ public AbstractRestApi(String implementationName, io.openems.edge.common.channel
* @param enabled enable component?
* @param isDebugModeEnabled enable debug mode?
* @param apiTimeout the API timeout in seconds
* @param ip the listen ip
* @param port the port; if '0', the port is automatically
* assigned
* @param connectionlimit the connection limit
*/
protected void activate(ComponentContext context, String id, String alias, boolean enabled,
boolean isDebugModeEnabled, int apiTimeout, int port, int connectionlimit) {
boolean isDebugModeEnabled, int apiTimeout, String ip, int port, int connectionlimit) {
super.activate(context, id, alias, enabled);
this.isDebugModeEnabled = isDebugModeEnabled;

Expand All @@ -78,18 +79,18 @@ protected void activate(ComponentContext context, String id, String alias, boole
UriCompliance.Violation.SUSPICIOUS_PATH_CHARACTERS, //
UriCompliance.Violation.ILLEGAL_PATH_CHARACTERS)));
final var connector = new ServerConnector(this.server, new HttpConnectionFactory(httpConfig));
connector.setHost(ip);
connector.setPort(port);
this.server.addConnector(connector);
this.server.setHandler(new RestHandler(this));
this.server.addBean(new AcceptRateLimit(10, 5, TimeUnit.SECONDS, this.server));
this.server.addBean(new NetworkConnectionLimit(connectionlimit, this.server));
this.server.start();
this.logInfo(this.log, this.implementationName + " started on port [" + port + "].");
this.logInfo(this.log, this.implementationName + " started on [" + ip + ":" + port + "].");
this._setUnableToStart(false);

} catch (Exception e) {
this.logError(this.log,
"Unable to start " + this.implementationName + " on port [" + port + "]: " + e.getMessage());
"Unable to start " + this.implementationName + " on [" + ip + ":" + port + "]: " + e.getMessage());
this._setUnableToStart(true);
}
this.getRpcRestHandler().setOnCall(call -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
@AttributeDefinition(name = "Port", description = "Port on which the webserver should listen.")
int port() default 8084;

@AttributeDefinition(name = "IP Address", description = "The IP address to listen on.")
String ip() default io.openems.common.websocket.AbstractWebsocketServer.DEFAULT_IP;

@AttributeDefinition(name = "Connection limit", description = "Maximum number of connections")
int connectionlimit() default 5;

Expand All @@ -30,4 +33,4 @@

String webconsole_configurationFactory_nameHint() default "Controller Api REST/JSON Read-Only [{id}]";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void activate(ComponentContext context, Config config) throws OpenemsExc
this.restHandler = this.restHandlerFactory.get();

super.activate(context, config.id(), config.alias(), config.enabled(), config.debugMode(), 0, /* no timeout */
config.port(), config.connectionlimit());
config.ip(), config.port(), config.connectionlimit());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
@AttributeDefinition(name = "Port", description = "Port on which the webserver should listen.")
int port() default 8084;

@AttributeDefinition(name = "IP Address", description = "The IP address to listen on.")
String ip() default io.openems.common.websocket.AbstractWebsocketServer.DEFAULT_IP;

@AttributeDefinition(name = "Connection limit", description = "Maximum number of connections")
int connectionlimit() default 5;

Expand All @@ -33,4 +36,4 @@

String webconsole_configurationFactory_nameHint() default "Controller Api REST/JSON Read-Write [{id}]";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public ControllerApiRestReadWriteImpl() {
private void activate(ComponentContext context, Config config) throws OpenemsException {
this.restHandler = this.restHandlerFactory.get();

super.activate(context, config.id(), config.alias(), config.enabled(), config.debugMode(), config.apiTimeout(),
config.port(), config.connectionlimit());
super.activate(context, config.id(), config.alias(), config.enabled(), config.debugMode(),
config.apiTimeout(), config.ip(), config.port(), config.connectionlimit());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static void beforeClass() throws Exception {
.setEnabled(true) //
.setConnectionlimit(5) //
.setDebugMode(false) //
.setIp("127.0.0.1") //
.setPort(port) //
.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ protected static class Builder {
private int port;
private int connectionlimit;
private boolean debugMode;
private String ip;

private Builder() {
}
Expand Down Expand Up @@ -39,6 +40,11 @@ public Builder setDebugMode(boolean debugMode) {
this.debugMode = debugMode;
return this;
}

public Builder setIp(String ip) {
this.ip = ip;
return this;
}

public MyConfig build() {
return new MyConfig(this);
Expand Down Expand Up @@ -75,5 +81,10 @@ public int connectionlimit() {
public boolean debugMode() {
return this.builder.debugMode;
}

@Override
public String ip() {
return this.builder.ip;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void test() throws OpenemsException, Exception {
.setApiTimeout(60) //
.setConnectionlimit(5) //
.setDebugMode(false) //
.setIp("127.0.0.1") //
.setPort(port) //
.build());

Expand Down
Loading