Skip to main content
Jump to: navigation, search

BaSyx / Introductory Examples / Java / Example 2b

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