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 local side has to change to start the Gateway.

Local

In this example variant, the local 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.

  1. import java.util.Map;
  2.  
  3. import javax.servlet.http.HttpServlet;
  4.  
  5. import org.eclipse.basyx.vab.gateway.DelegatingModelProvider;
  6. import org.eclipse.basyx.vab.modelprovider.api.IModelProvider;
  7. import org.eclipse.basyx.vab.modelprovider.lambda.VABLambdaProvider;
  8. import org.eclipse.basyx.vab.protocol.basyx.connector.BaSyxConnectorFactory;
  9. import org.eclipse.basyx.vab.protocol.basyx.server.BaSyxTCPServer;
  10. import org.eclipse.basyx.vab.protocol.http.server.BaSyxContext;
  11. import org.eclipse.basyx.vab.protocol.http.server.BaSyxHTTPServer;
  12. import org.eclipse.basyx.vab.protocol.http.server.VABHTTPInterface;
  13. import org.eclipse.basyx.vab.registry.api.IVABRegistryService;
  14. import org.eclipse.basyx.vab.registry.memory.VABInMemoryRegistry;
  15. import org.eclipse.basyx.vab.registry.restapi.VABRegistryModelProvider;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18.  
  19. /**
  20.  * Expected console output in this HandsOn:
  21.  * - the heater id
  22.  * - oven is activated and deactivated multiple times
  23.  * - temperature values between 30 and 40
  24.  */
  25. public class Scenario2Gateway {
  26. 	// Initializes a logger for the output
  27. 	private static final Logger logger = LoggerFactory.getLogger(Scenario2Gateway.class);
  28.  
  29. 	public static void main(String[] args) throws Exception {
  30. 		// First, create a tcp server that hold the model, exactly as is the Example 2a
  31. 		BaSyxTCPServer<IModelProvider> tcpModelServer = createOvenTCPServer();
  32. 		// And start this tcp server
  33. 		tcpModelServer.start();
  34.  
  35. 		// Second, create a gateway servlet for storing endpoints for VAB models
  36. 		HttpServlet gatewayServlet = createGatewayServlet();
  37. 		// Third, create a VAB registry servlet for storing endpoints for VAB models
  38. 		HttpServlet registryServlet = createRegistryServlet();
  39. 		// Now combine the VAB registry and gateway servlets in one BaSyx HTTP server
  40. 		BaSyxHTTPServer httpServer = createHttpServerFromServlets(gatewayServlet, registryServlet);
  41. 		// And start this http server
  42. 		httpServer.start();
  43.  
  44. 		logger.info("Server started");
  45. 	}
  46.  
  47.  
  48. 	private static BaSyxHTTPServer createHttpServerFromServlets(HttpServlet gatewayServlet,
  49. 			HttpServlet directoryServlet) {
  50. 		// Define a context to which multiple servlets can be added
  51. 		BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001);
  52. 		// => Every servlet contained in this context is available at http://localhost:4001/handson/
  53. 		context.addServletMapping("/gateway/*", gatewayServlet);
  54. 		// The model will be available at http://localhost:4001/handson/oven/
  55. 		context.addServletMapping("/directory/*", directoryServlet);
  56. 		// The directory will be available at http://localhost:4001/handson/directory/
  57. 		return new BaSyxHTTPServer(context);
  58. 	}
  59.  
  60. 	private static HttpServlet createRegistryServlet() {
  61. 		// Create a registry that can store endpoints for VAB models
  62. 		// => The registry will be published using an HTTP-REST interface
  63. 		IVABRegistryService registry = new VABInMemoryRegistry();
  64. 		// Similar to the IModelProvider for the local oven model, a IModelProvider for the registry is created
  65. 		IModelProvider registryProvider = new VABRegistryModelProvider(registry);
  66. 		// Next, this model provider is given to a HTTP servlet that gives access to the VAB registry
  67. 		VABHTTPInterface<IModelProvider> registryServlet = new VABHTTPInterface<>(registryProvider);
  68. 		logger.info("Created a servlet for the directory");
  69.  
  70. 		// Register the VAB model at the directory (locally in this case)
  71. 		registry.addMapping("oven", "http://localhost:4001/handson/gateway//basyx://127.0.0.1:6999");
  72. 		logger.info("Oven model registered!");
  73.  
  74. 		return registryServlet;
  75. 	}
  76.  
  77. 	private static HttpServlet createGatewayServlet() {
  78. 		// Now, the model provider is given to a HTTP servlet that gives access to the
  79. 		// model in the next steps
  80. 		// => The model will be published using an HTTP-REST interface
  81. 		VABHTTPInterface<IModelProvider> gatewayServlet = new VABHTTPInterface<>(new DelegatingModelProvider(new BaSyxConnectorFactory()));
  82. 		logger.info("Created a servlet for the gateway");
  83. 		return gatewayServlet;
  84. 	}
  85.  
  86. 	private static BaSyxTCPServer<IModelProvider> createOvenTCPServer() {
  87. 		// First, a local model is created that is wrapped by a model provider (see first HandsOn)
  88. 		Map<String, Object> model = Scenario1.createMyOvenModel(new Oven());
  89. 		IModelProvider modelProvider = new VABLambdaProvider(model);
  90. 		// Up to this point, everything is known from the previous HandsOn
  91.  
  92. 		// Creates a tcp server providing the oven model on port 7000
  93. 		return new BaSyxTCPServer<>(modelProvider, 6999);
  94. 	}
  95. }

Remote

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

Expected Output

You will have different outputs for the local and remote code.

Local

The output states, that a servlet has ben created for the gateway as wel as the directory before the oven model has been registered. After this there will be some info (maybe in a red text color) about the BaSyx Context and HTTP server (a Tomcat server) and the message from our logger, that the server has been started.

Only if you execute the remote code the output Heater: activated and Heater: deactivated will show up.


15:43:01.110 [main] INFO  i.Scenario2Gateway - Created a servlet for the gateway
15:43:01.112 [main] INFO  i.Scenario2Gateway - Created a servlet for the directory
15:43:01.112 [main] INFO  i.Scenario2Gateway - Oven model registered!
...
15:43:01.573 [main] INFO  i.Scenario2Gateway - Servers started
Heater: activated
Heater: deactivated
Heater: activated
Heater: deactivated
Heater: activated
...

Remote

Once you start the remote you will only see information about the current temperature which will fluctuate between ~30 and ~40. This time the information about the activation or deactivation of the heater will be part of the output of the local code.


15:44:43.221 [main] INFO  i.Scenario2Connected - Current temperature: 20.0
15:44:43.370 [main] INFO  i.Scenario2Connected - Current temperature: 23.0
15:44:43.479 [main] INFO  i.Scenario2Connected - Current temperature: 25.7
15:44:43.588 [main] INFO  i.Scenario2Connected - Current temperature: 28.13
15:44:43.695 [main] INFO  i.Scenario2Connected - Current temperature: 30.317
15:44:43.805 [main] INFO  i.Scenario2Connected - Current temperature: 32.2853
15:44:43.914 [main] INFO  i.Scenario2Connected - Current temperature: 34.05677
15:44:44.022 [main] INFO  i.Scenario2Connected - Current temperature: 35.651093
15:44:44.131 [main] INFO  i.Scenario2Connected - Current temperature: 37.08598370000001
15:44:44.241 [main] INFO  i.Scenario2Connected - Current temperature: 38.37738533000001
15:44:44.349 [main] INFO  i.Scenario2Connected - Current temperature: 39.53964679700001
15:44:44.458 [main] INFO  i.Scenario2Connected - Current temperature: 40.58568211730001
15:44:44.579 [main] INFO  i.Scenario2Connected - Current temperature: 36.674402515013014
15:44:44.689 [main] INFO  i.Scenario2Connected - Current temperature: 35.00696226351172
...

Back to the top