Module CSC7321—CSN: Middleware and software architecture for Internet distributed applications

Portail informatique

REST lab - write a client for the vlibtour-visit-emulation-system REST component

The vlibtour-visit-emulation-system component provides a REST API. It is specialized to emulate the microproject scenario: A group of tourists (Joe and Averell) are moving during a tour. Those tourists have selected ”The unusual Paris” 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 servers to move the users forward step by step and get their positions
  • Display the positions of the users on an OpenStreetMap

Discover the emulation server

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

You can discover the synthesis of the API of this component 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 vlibtour-visit-emulation-system/vlibtour-visit-emulation-server mvn clean install mvn exec:java@server

Discover the API of the emulation server with its javadoc

You probably think that you need a more detailed documentation to know how to write your own client. 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-visit-emulation-system/vlibtour-visit-emulation-server mvn clean install
Then the documentation is available. If your navigator is firefox, you can display the documentation with this command
firefox target/apidocs/index.html

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 Postman or a navigator

http://localhost:8888/VisitEmulation/visitemulation/getNextPOIPosition/Joe Base address of the server: http://localhost:8888/VisitEmulation/ Path of the server class: visitemulation Path of the operation: getNextPOIPosition Path of the parameter: Joe

With the test client

For this question go to the vlibtour-visit-emulation-server module directory. From the root of the project (csc-mw-project)
cd vlibtour-visit-emulation-system/vlibtour-visit-emulation-server
In one terminal run the server.mvn exec:java@server In an other terminal run a test client.mvn exec:java@clientand read the positions of Joe that are provided by the REST server:
Position [name=4, location is longitude=2.342355 and latitude=48.871799, description=Musée Grévin]

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.
When you do mvn clean install, the JUnit tests are executed. ------------------------------------------------------- T E S T S ------------------------------------------------------- Running vlibtour.vlibtour_emulation_visit.TestVLibTourGeoLocationEmulator Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.097 sec - in vlibtour.vlibtour_emulation_visit.TestVLibTourGeoLocationEmulator Running vlibtour.vlibtour_emulation_visit.TestDepthFirstSearch Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 sec - in vlibtour.vlibtour_emulation_visit.TestDepthFirstSearch Running vlibtour.vlibtour_emulation_visit.TestScenario sept. 10, 2019 11:35:15 PM org.glassfish.grizzly.http.server.NetworkListener start INFOS: Started listener bound to [localhost:8888] sept. 10, 2019 11:35:15 PM org.glassfish.grizzly.http.server.HttpServer start INFOS: [HttpServer] Started. 23:35:15,610 INFO main emulation:159 - Jersey app started with WADL available at http://localhost:8888/VisitEmulation/application.wadl 0 [main] INFO emulation - Jersey app started with WADL available at http://localhost:8888/VisitEmulation/application.wadl Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.723 sec - in vlibtour.vlibtour_emulation_visit.TestScenario Results : Tests run: 13, Failures: 0, Errors: 0, Skipped: 0 To execute the tests in eclipse :
  • Choose a class in src/test/java
  • With a Rigt click choose Run as ... Junit test

Write a proxy on the client side

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

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. We call the service as if it was a call to a local object. You simply has to write
// Client code with the proxy VLibTourVisitEmulationProxy proxy=new VLibTourVisitEmulationProxy(); Position position=proxy.getNextPOIPosition(ExampleOfAVisitWithTwoTourists.USER_ID_JOE);
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"
For this step, we will work in the vlibtour_visit_emulation_proxy module. In this module, you will write the class VLibTourVisitEmulationProxy (NB this class is provided but nearly empty).

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 package vlibtour.vlibtour_visit_emulation_proxy modify the class VisitEmulationTestClientWithProxy to use the proxy and test the four methods now available in the proxy class. Test the proxy client.Don't forget the mvn clean install and that the server should be listening.

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. With this knowledge build an algorithm to move the user from its first position to the last POI. And test the algorithm in the class VisitEmulationTestClientWithProxy.

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-scenario module and we will complete the class VlibTourVisitTouristApplication (NB this class is provided, it displays a map of Paris, prints the three POIs, you have to modify the class, to see the user (the user name is given in argument of the execution, Joe with mvn exec:java@touristapplijoe or Avrel with mvn exec:java@touristappliavrel) moving on the map).

Understand the map demo

Understand the code of the MapDemo class and run this class
mvn clean install mvn exec:java@map

Modify the constructor of VlibTourVisitTouristApplication

First of all you have to modify the constructor. Replace the raise of the exception by the initiation of the emulationVisitClient (which is an instance of the proxy we have created in the previous step).

Modify the tourist application

  • At the beginning of the main, initialize the client variable by creating an instance of VlibTourVisitTouristApplication, the parameters of the constructor are the following:
    • The tour Id (it may be modified when you will populate the tours in the database) for the moment you can write "The unusual Paris"
    • The group Id, for the moment just write Optional.empty()
    • The user Id, the user Id is provided as the first argument of the command line, it is copied in the userId variable at the beginning of the main
  • Then modify the while loop get the position of the user through the call the proxy methods, and modify the position of the user on the map.

Test the tourist application


Be careful, to test the tourist application, the emulation server has to be started. Keep an open terminal in the vlibtour-visit-emulation-server module for it.
Be careful, every time you make a change in the code, you have to produce the new java classes. mvn clean install
mvn exec:java@touristapplijoe mvn exec:java@touristappliavrel
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) $