Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "BaSyx / Introductory Examples / Java / Example 2b"

m (Remote)
m
(One intermediate revision by the same user not shown)
Line 1: Line 1:
= Example 2b =
+
= Example 2b - Remote VAB Access with Gateway =
 
This is a variant of [[BaSyx_/_Introductory_Examples_/_Java_/_Example_2a | Example 2a]]. Instead of directly connecting to the the model via TCP, a HTTP/REST to TCP [[BaSyx.Gateway.Overview | Gateway]] is used.  
 
This is a variant of [[BaSyx_/_Introductory_Examples_/_Java_/_Example_2a | Example 2a]]. Instead of directly connecting to the the model via TCP, a HTTP/REST to TCP [[BaSyx.Gateway.Overview | Gateway]] is used.  
 
Thus, access to models only available via TCP are made possible for web based apps, e.g. a browser.
 
Thus, access to models only available via TCP are made possible for web based apps, e.g. a browser.
Line 13: Line 13:
  
 
<syntaxhighlight lang="java" style="margin-left: 4em">
 
<syntaxhighlight lang="java" style="margin-left: 4em">
/**  
+
/**
 
  * Expected console output in this HandsOn:
 
  * Expected console output in this HandsOn:
 
  * - the heater id
 
  * - the heater id
Line 22: Line 22:
 
// Initializes a logger for the output
 
// Initializes a logger for the output
 
private static final Logger logger = LoggerFactory.getLogger(Scenario2Gateway.class);
 
private static final Logger logger = LoggerFactory.getLogger(Scenario2Gateway.class);
+
 
 
public static void main(String[] args) throws Exception {
 
public static void main(String[] args) throws Exception {
// First, a local model is created that is wrapped by a model provider (see first HandsOn)
+
// First, create a tcp server that hold the model, exactly as is the Example 2a
Map<String, Object> model = Scenario1.createMyOvenModel(new Oven());
+
BaSyxTCPServer<IModelProvider> tcpModelServer = createOvenTCPServer();
IModelProvider modelProvider = new VABLambdaProvider(model);
+
// And start this tcp server
// Up to this point, everything is known from the previous HandsOn
+
tcpModelServer.start();
+
 
// Now, the model provider is given to a HTTP servlet that gives access to the
+
// Second, create a gateway servlet for storing endpoints for VAB models
// model in the next steps
+
HttpServlet gatewayServlet = createGatewayServlet();
// => The model will be published using an HTTP-REST interface
+
// Third, create a VAB registry servlet for storing endpoints for VAB models
HttpServlet gatewayServlet = new VABHTTPInterface<IModelProvider>(new DelegatingModelProvider(new BaSyxConnectorFactory()));
+
HttpServlet registryServlet = createRegistryServlet();
logger.info("Created a servlet for the gateway");
+
// Now combine the VAB registry and gateway servlets in one BaSyx HTTP server
+
BaSyxHTTPServer httpServer = createHttpServerFromServlets(gatewayServlet, registryServlet);
// Second, create a registry that can store endpoints for VAB models
+
// And start this http server
IVABRegistryService registry = new VABInMemoryRegistry();
+
httpServer.start();
IModelProvider registryProvider = new VABRegistryModelProvider(registry);
+
 
HttpServlet directoryServlet = new VABHTTPInterface<IModelProvider>(registryProvider);
+
logger.info("Servers started");
logger.info("Created a servlet for the directory");
+
}
+
 
// Now, define a context to which multiple servlets can be added
+
 
 +
private static BaSyxHTTPServer createHttpServerFromServlets(HttpServlet gatewayServlet,
 +
HttpServlet directoryServlet) {
 +
// Define a context to which multiple servlets can be added
 
BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001);
 
BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001);
 
// => Every servlet contained in this context is available at http://localhost:4001/handson/
 
// => Every servlet contained in this context is available at http://localhost:4001/handson/
Line 48: Line 51:
 
context.addServletMapping("/directory/*", directoryServlet);
 
context.addServletMapping("/directory/*", directoryServlet);
 
// The directory will be available at http://localhost:4001/handson/directory/
 
// The directory will be available at http://localhost:4001/handson/directory/
BaSyxHTTPServer server = new BaSyxHTTPServer(context);
+
return new BaSyxHTTPServer(context);
server.start();
+
}
+
 
// Creates a tcp server providing the oven model on port 7000
+
private static HttpServlet createRegistryServlet() {
BaSyxTCPServer<IModelProvider> tcpServer = new BaSyxTCPServer<>(modelProvider, 6999);
+
// Create a registry that can store endpoints for VAB models
tcpServer.start();
+
// => The registry will be published using an HTTP-REST interface
+
IVABRegistryService registry = new VABInMemoryRegistry();
+
// Similar to the IModelProvider for the local oven model, a IModelProvider for the registry is created
 +
IModelProvider registryProvider = new VABRegistryModelProvider(registry);
 +
// Next, this model provider is given to a HTTP servlet that gives access to the VAB registry
 +
VABHTTPInterface<IModelProvider> registryServlet = new VABHTTPInterface<>(registryProvider);
 +
logger.info("Created a servlet for the directory");
 +
 
 
// Register the VAB model at the directory (locally in this case)
 
// Register the VAB model at the directory (locally in this case)
 
registry.addMapping("oven", "http://localhost:4001/handson/gateway//basyx://127.0.0.1:6999");
 
registry.addMapping("oven", "http://localhost:4001/handson/gateway//basyx://127.0.0.1:6999");
 
logger.info("Oven model registered!");
 
logger.info("Oven model registered!");
+
 
logger.info("Server started");
+
return registryServlet;
 +
}
 +
 
 +
private static HttpServlet createGatewayServlet() {
 +
// Now, the model provider is given to a HTTP servlet that gives access to the
 +
// model in the next steps
 +
// => The model will be published using an HTTP-REST interface
 +
VABHTTPInterface<IModelProvider> gatewayServlet = new VABHTTPInterface<>(new DelegatingModelProvider(new BaSyxConnectorFactory()));
 +
logger.info("Created a servlet for the gateway");
 +
return gatewayServlet;
 +
}
 +
 
 +
private static BaSyxTCPServer<IModelProvider> createOvenTCPServer() {
 +
// First, a local model is created that is wrapped by a model provider (see first HandsOn)
 +
Map<String, Object> model = Scenario1.createMyOvenModel(new Oven());
 +
IModelProvider modelProvider = new VABLambdaProvider(model);
 +
// Up to this point, everything is known from the previous HandsOn
 +
 
 +
// Creates a tcp server providing the oven model on port 7000
 +
return new BaSyxTCPServer<>(modelProvider, 6999);
 
}
 
}
 
}
 
}

Revision as of 08:00, 2 July 2021

Example 2b - Remote VAB Access with Gateway

This is a variant of Example 2a. Instead of directly connecting to the the model via TCP, a HTTP/REST to TCP Gateway is used. Thus, access to models only available via TCP are made possible for web based apps, e.g. a browser.

Example Code

There's little change necessary to the already existing code. The local code can be reused from Example 2. Only the remote side has to change to start the Gateway.

Remote

In this example variant, the remote code is very similar to that of variant Example 2a. Additionally to the existing TCP server, the Gateway is started.

Due to the usage of the Gateway, the path to the oven model has to be changed.

/**
 * Expected console output in this HandsOn:
 * - the heater id
 * - oven is activated and deactivated multiple times
 * - temperature values between 30 and 40
 */
public class Scenario2Gateway {
	// Initializes a logger for the output
	private static final Logger logger = LoggerFactory.getLogger(Scenario2Gateway.class);
 
	public static void main(String[] args) throws Exception {
		// First, create a tcp server that hold the model, exactly as is the Example 2a
		BaSyxTCPServer<IModelProvider> tcpModelServer = createOvenTCPServer();
		// And start this tcp server
		tcpModelServer.start();
 
		// Second, create a gateway servlet for storing endpoints for VAB models
		HttpServlet gatewayServlet = createGatewayServlet();
		// Third, create a VAB registry servlet for storing endpoints for VAB models
		HttpServlet registryServlet = createRegistryServlet();
		// Now combine the VAB registry and gateway servlets in one BaSyx HTTP server
		BaSyxHTTPServer httpServer = createHttpServerFromServlets(gatewayServlet, registryServlet);
		// And start this http server
		httpServer.start();
 
		logger.info("Servers started");
	}
 
 
	private static BaSyxHTTPServer createHttpServerFromServlets(HttpServlet gatewayServlet,
			HttpServlet directoryServlet) {
		// Define a context to which multiple servlets can be added
		BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001);
		// => Every servlet contained in this context is available at http://localhost:4001/handson/
		context.addServletMapping("/gateway/*", gatewayServlet);
		// The model will be available at http://localhost:4001/handson/oven/
		context.addServletMapping("/directory/*", directoryServlet);
		// The directory will be available at http://localhost:4001/handson/directory/
		return new BaSyxHTTPServer(context);
	}
 
	private static HttpServlet createRegistryServlet() {
		// Create a registry that can store endpoints for VAB models
		// => The registry will be published using an HTTP-REST interface
		IVABRegistryService registry = new VABInMemoryRegistry();
		// Similar to the IModelProvider for the local oven model, a IModelProvider for the registry is created
		IModelProvider registryProvider = new VABRegistryModelProvider(registry);
		// Next, this model provider is given to a HTTP servlet that gives access to the VAB registry
		VABHTTPInterface<IModelProvider> registryServlet = new VABHTTPInterface<>(registryProvider);
		logger.info("Created a servlet for the directory");
 
		// Register the VAB model at the directory (locally in this case)
		registry.addMapping("oven", "http://localhost:4001/handson/gateway//basyx://127.0.0.1:6999");
		logger.info("Oven model registered!");
 
		return registryServlet;
	}
 
	private static HttpServlet createGatewayServlet() {
		// Now, the model provider is given to a HTTP servlet that gives access to the
		// model in the next steps
		// => The model will be published using an HTTP-REST interface
		VABHTTPInterface<IModelProvider> gatewayServlet = new VABHTTPInterface<>(new DelegatingModelProvider(new BaSyxConnectorFactory()));
		logger.info("Created a servlet for the gateway");
		return gatewayServlet;
	}
 
	private static BaSyxTCPServer<IModelProvider> createOvenTCPServer() {
		// First, a local model is created that is wrapped by a model provider (see first HandsOn)
		Map<String, Object> model = Scenario1.createMyOvenModel(new Oven());
		IModelProvider modelProvider = new VABLambdaProvider(model);
		// Up to this point, everything is known from the previous HandsOn
 
		// Creates a tcp server providing the oven model on port 7000
		return new BaSyxTCPServer<>(modelProvider, 6999);
	}
}

Local

As previously stated, the code for local from Example 2 can be reused.

Back to the top