Skip to main content
Jump to: navigation, search

BaSyx / Introductory Examples / Java / Example 2b

Example 2b

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.


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, 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
		// 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
		HttpServlet gatewayServlet = new VABHTTPInterface<IModelProvider>(new DelegatingModelProvider(new BaSyxConnectorFactory()));"Created a servlet for the gateway");
		// Second, create a registry that can store endpoints for VAB models
		IVABRegistryService registry = new VABInMemoryRegistry();
		IModelProvider registryProvider = new VABRegistryModelProvider(registry);
		HttpServlet directoryServlet = new VABHTTPInterface<IModelProvider>(registryProvider);"Created a servlet for the directory");
		// Now, 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/
		BaSyxHTTPServer server = new BaSyxHTTPServer(context);
		// Creates a tcp server providing the oven model on port 7000
		BaSyxTCPServer<IModelProvider> tcpServer = new BaSyxTCPServer<>(modelProvider, 6999);
		// Register the VAB model at the directory (locally in this case)
		registry.addMapping("oven", "http://localhost:4001/handson/gateway//basyx://");"Oven model registered!");"Server started");


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

Copyright © Eclipse Foundation, Inc. All Rights Reserved.