C-Uebung

Networking

boost::asio | | Ausblick

Excercising Boost.asio:

We use boost.asio to setup a client/server system for the following purpose:

We want to implement a weather server, from which we can receive the actual weather conditions at the server location.

On the server we open a http connection to a weather REST service of your choice, for example:

When a client sends a request to the weather server on port 10000, then the server sends the received actual weather conditons back to its clients.

A boost client class and a server base class is available here:

We need to specialize the server base class according to the following strategy:

  • We implement the abstract method make_response_string() to return the actual weather status.
  • To keep the weather status up to date, we use an asynchronous strategy:
    • When the server delivers a response, we check the time stamp of the weather status
    • If the weather status is out-dated (e.g. older than a minute or so), we start a new asynchronous http request to the weather REST web server to receive a new status.
    • When the new weather status is available it replaces the old one for delivery.

More about asynchronous http requests in this boost example and the following base class:

A running weather server, as described above, is available at schorsch.efi.fh-nuernberg.de, port 14. On 25.04.2013 it reported the following weather conditions:

Thu Apr 25 12:46:01 2013
{"weatherObservation":{"weatherCondition":"n/a","clouds":"clouds and visibility OK","observation":"EDDN 251020Z 24004KT 100V260 CAVOK 21/08 Q1025 NOSIG","windDirection":240,"ICAO":"EDDN","elevation":312,"countryCode":"DE","cloudsCode":"CAVOK","lng":11.066666666666666,"temperature":"21","dewPoint":"8","windSpeed":"04","humidity":43,"stationName":"Nuernberg","datetime":"2013-04-25 10:20:00","lat":49.5,"hectoPascAltimeter":1025}}

Implementation Approach:

  • First we get a test server running locally at your computer that just sends a test string, e.g. by returning “hey, there!” in make_response_string(). The server needs to run with root permissions (sudo). The address of the locally running server is “localhost”.
  • We test the test server with an instance of the client contained in a separate program.
  • Then we update the server response string to contain the actual weather conditions by instantiating an async client to connect to the weather conditions provider. We have to call the run() method on the io_service object, which is necessary for asynchronous requests to be processed!

Side Note: Ideally, the asynchronous http request should be run in a different thread. More about boost.asio threads in this boost tutorial. When using threads, the access of shared resources has to be locked with a mutex (e.g. with a scoped lock via boost::mutex::scoped_lock or boost::lock_guard<boost::mutex>).

boost::asio | | Ausblick

Options: