Module CSC5002—ASR6: Middleware and software architecture for distributed applications

Portail informatique

REST lab - Use vlibtour-visit-emulation-server as a REST component and move the users on the map

The vlibtour-visit-emulation-system module is able to emulate the move of group of users during a visit. A group of tourists (Daltons, named Joe and Averell) are moving during a tour . Those tourists have selected ”BigParisTour” tour instance made up of three POI (”Musée Grévin”, ”Pyramide du Louvres” and ”Les catacombes de Paris”). They are actually going from one POI to the next POI with different paths.

At the end of this lab, you will be able to:

  • Use the emulation server as a REST server.
  • Use the emulation servers to move the users forward step by step and get their positions
  • Finally display the positions of the users on an OpenStreetMap

Discover the emulation server

Discover the API of the emulation server with its javadoc

You probably think that you need a more detailed documentation of the API available with the emulation server. For this purpose, we will discover the API of this server through its javadoc. In the real life, the service should be detailed through a web page with language neutral explanations.

After the installation of the module through maven, you can discover the produced java documentation in a navigator. Study carefully this API.
Your objective is to know how you can move one user step by step from the first POI to the last POI.

cd vlibtour-microservices/vlibtour-visit-emulation-system/vlibtour-visit-emulation-server mvn clean install
Then the documentation is available. If your navigator is brave-browser, you can display the documentation with this command
brave-browser target/apidocs/index.html &
Then you can read the javadoc of the class vlibtour.vlibtour_visit_emulation_server

Make the emulation server available as a REST server

You have a server vlibtour.vlibtour_visit_emulation_server, that implements the interface vlibtour.vlibtour_visit_emulation_api. In this part, you have to make this service available through a REST API.

The class vlibtour_visit_emulation_server contains a main method that creates a graph of positions and starts a http Grizzly server.

// All the REST classes that are in the same package as the server will be served final ResourceConfig rc = new ResourceConfig().packages(VisitEmulationServer.class.getPackage().getName()); // Start the http server. // The server will be available at the URI defined in the class ExampleOfAVisitWithTwoTourists final HttpServer server = GrizzlyHttpServerFactory .createHttpServer(URI.create(ExampleOfAVisitWithTwoTourists.BASE_URI_WEB_SERVER), rc);
All the constants variables related to the emulation server are defined in the file ./vlibtour-libraries/vlibtour-common/src/main/java/vlibtour/vlibtour_common/ExampleOfAVisitWithTwoTourists.java. Especially:
  • The server URI
  • The name of the daltons (group of tourists which positions are emulated by the server).
  • Some positions in Paris (including POIs positions of the ParisBigTour)

Modify the class vlibtour_visit_emulation_server (look for all the TODO inside the code)

  • Add all the annotations necessary to make the service available through REST requests
    • Define the paths (class, methods and parameters)
    • Declare the representation format of the produced data (Json)

Discover the API of the emulation server when the server is started

You can discover the synthesis of the API when the emulation server is started through its wadl.

To obtain this image:

  • Start the server (commands given from the root of the project)
    cd csc-mw-project/vlibtour-microservices/vlibtour-visit-emulation-system/vlibtour-visit-emulation-server mvn clean install start_visit_emulation_server.sh Dalton ParisBigTour
  • Look at the server interface through the wadl page http://localhost:8888/VisitEmulation/application.wadl

Verfify and be aware of:

  • the path of the class ressource
  • the names of the four available methods
  • the names of their input parameters
  • the representation type for the responses you should have this in the wadl for each method : <method id="getCurrentPosition" name="GET"> ... <response> <representation mediaType="application/json"/> </response> </method>
You will need them for the next question.
Congratulations, you have made the emulation server available with REST requests.

Test the emulation server

Here we test that the server is operational with three methods.
  • Through a navigator or a REST client (e.g. Postman)
  • A simple test client that gets the first position of the user Joe.
  • Junit tests, that verify systematically all the methods of the server.

With the JUnit tests

JUnit tests may test the server methods. It is especially essential to provide JUnit tests for servers, before you put them to production.

A test class is provided VLibTourVisitEmulationServerIT.

When your methods are ready, in the test class, you have to remove the lines

@Disabled // FIXME

to test effectively the methods.

To run it

./run_integration_tests.sh

or

mvn failsafe:integration-test failsafe:verify

------------------------------------------------------- T E S T S ------------------------------------------------------- [INFO] Running vlibtour.vlibtour_visit_emulation_server.VLibTourVisitEmulationServerIT oct. 04, 2024 9:23:48 AM org.glassfish.grizzly.http.server.NetworkListener start INFOS: Started listener bound to [localhost:8888] oct. 04, 2024 9:23:48 AM org.glassfish.grizzly.http.server.HttpServer start INFOS: [HttpServer] Started. [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.521 s -- in vlibtour.vlibtour_visit_emulation_server.VLibTourVisitEmulationServerIT [INFO] [INFO] Results: [INFO] [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- maven-failsafe-plugin:3.5.0:verify (default-cli) @ vlibtour-visit-emulation-server --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS

With Postman or a navigator

When your unitary tests work perfectly, you can test the server with a REST client !

For example

you have to adapt to the paths you have chosen in the previous question.

http://localhost:8888/VisitEmulation/visitemulation/getNextPOIPosition/Joe Base address of the server: http://localhost:8888/VisitEmulation/ Path of the server class: visitemulation (the name you have given previously in the class path annotation) Path of the operation: getNextPOIPosition (the name you have given previously in the method path annotation) Path of the parameter: Joe

Write a proxy on the client side

In this step, you will implement a proxy (see the explanations of the proxy design pattern, for example in wikipedia).

Learn more on what is a proxy



The proxy on the client implements all the methods provided by the REST server. In those methods, the proxy delegates to the REST server (send a REST request to the server).
In our case the picture is :

The proxy allows to simplify the code on the client point of view.

With the proxy

With the use of a proxy, the service will be called as if it was a call to a local object. The developer simply has to write

// Client code with the proxy VLibTourVisitEmulationProxy proxy=new VLibTourVisitEmulationProxy(); Position position=proxy.getNextPOIPosition(ExampleOfAVisitWithTwoTourists.USER_ID_JOE);
Without the proxy

Instead of

// Client code without the proxy // 1) Initialize the target service Client client = ClientBuilder.newClient(); URI uri = UriBuilder.fromUri(ExampleOfAVisitWithTwoTourists.BASE_URI_WEB_SERVER).build(); WebTarget service = client.target(uri); // 2) Call the service Position position=service .path("visitemulation/getNextPOIPosition/" + ExampleOfAVisitWithTwoTourists.USER_ID_JOE).request() .accept(MediaType.APPLICATION_JSON).get().readEntity(Position.class);
ExampleOfAVisitWithTwoTourists.USER_ID_JOE is a String constant whose value is "Joe"

Find the proxy class

For this step, we will work on the client side, in the vlibtour-tourist-application module, in the class VisitEmulationProxy of the package vlibtour.vlibtour_tourist_application.visit_emulation_proxy. In this part of the lab, you will modify the class VisitEmulationProxy (NB this class is provided but nearly empty). Can you find this class ?

Write the constructor

You have to fill the constructor The constructor should initialize the WebTarget service attribute. This attribute has to be initialized in order to be connected to the server, and to be able to send requests to it. Look at a REST client example to know how to initialize it.

Write the four methods that access to the 4 methods defined in the emulator REST API

Each method makes a request on the created WebTarget and returns whatever is expected.

Test the proxy

In the tests, for the package vlibtour.vlibtour_visit_emulation_proxy modify the class VisitEmulationProxyIT to test the four methods now available in the proxy class. Don't forget that the server should be listening for this test.
cd vlibtour-applications/vlibtour-tourist-application mvn clean install # start the emulation server (if necessary) (cd ../../vlibtour-microservices/vlibtour-visit-emulation-system/vlibtour-visit-emulation-server; ./start_visit_emulation_server.sh Dalton ParisBigTour) # run the integration tests mvn failsafe:integration-test failsafe:verify

Move one user from the first POI to the last POI

Read carefully the subject of the microproject, to understand the methods available in the emulation server. You can also look at the code javadoc of the class vlibtour.vlibtour_visit_emulation_server

cd vlibtour-microservices/vlibtour-visit-emulation-system/vlibtour-visit-emulation-server mvn clean install brave-browser target/apidocs/index.html &

You can also take a look at the figure on the right.

With this knowledge build an algorithm to move the user from its first position to the last POI. Add a test method to test your algorithm. At the end of your test Joe should arrive on position "Les catacombes".

public static final Position POSITION47 = new Position("Les catacombes", new GPSPosition(48.833566, 2.332416), "description of Les catacombes...");

Use the proxy in the tourist application to visualize the user tour on the map

In this part of the lab, we will build the tourist application, that calls the emulation server, and displays the position of the user on a map.

For this step, we will work in the vlibtour-tourist-application module and we will complete the class VLibTourVisitTouristApplication in the package vlibtour.vlibtour_tourist_application

NB this class is provided, it displays a map of Paris.

To start the tourist application, go to the root of the csc-mw-project and
mvn clean install run_scenario_visitonly_w_mapviewer.sh # it starts and stop the emulated server

You have to modify the class, to see the user moving on the map till "Les catacombes". You will have to work on the TODOs that are in the main method.

Modify the tourist application (the main method)

  • TODO TOUR
    • You can get the POIs through the previously created database
    • Or you can copy the following code
      List pois = new ArrayList(); POI poi1 = new POI(ExampleOfAVisitWithTwoTourists.POSITION4.getName(), ExampleOfAVisitWithTwoTourists.POSITION4.getDescription()); pois.add(poi1); POI poi2 = new POI(ExampleOfAVisitWithTwoTourists.POSITION19.getName(), ExampleOfAVisitWithTwoTourists.POSITION19.getDescription()); pois.add(poi2); POI poi3 = new POI(ExampleOfAVisitWithTwoTourists.POSITION47.getName(), ExampleOfAVisitWithTwoTourists.POSITION47.getDescription()); pois.add(poi3); Tour tour = new Tour(tourId, "description of " + tourId, pois);
  • TODO VISITEMULATION instantiate the visit emulation proxy
  • TODO VISITEMULATION get the first position of the tourist With the proxy
  • TODO VISITEMULATION Show userId first position on the map
  • TODO GROUPCOMM and VISITEMULATION loop In this part you will have to include the algorithm you have found in the previous question.
    This loop will be completed in other labs concerning group communication.
  • TODO VISITEMULATION Close the proxy

Test the tourist application


Be careful, every time you make a change in the code, you have to produce the new java classes.
(cd vlibtour-applications/vlibtour-tourist-application;mvn clean install)
To start the tourist application, go to the root of the csc-mw-project and
run_scenario_visitonly_w_mapviewer.sh
You are able to see Avrell moving on the map ? Congratulations !
Your micro project is on the good track !
For this lab, only one tourist moves on the map. In the following weeks, the objective is to see all the tourists of the group moving on the map. For this purpose, you will start one tourist application by user. Users will exchange their locations through a publish/subscribe broker, and wait for each other at each POI.

 

 

 


$Date: 2019-09-09 00:31:57 +0200 (lun. 09 sept. 2019) $