creating clients 3: notification system

using the notification system.

introduction
overview of this tutorial

notification in the service client
providing infomation on a notification basis

notification in the user client
receiving infomation via notifications

running the examples
instructions for running the example applications covered in this tutorial

introduction

So far we have had to explicitly request all our information from the service and do this every time we want to get some info. SVS clients, however, also support a notification system through which clients can register as 'listeners' for particular data or event messages on another client. In this tutorial we will extend the 'time service' client to provide notifications to all listeners whenever a client requests the time. the 'time user' client will be extended to register for this and handle the notification messages.

The source code for the examples in this tutorial are:

The source code for all tutorials can be downloaded with the tutorials-x.x.tar.gz package.

page contents

notification in the service client

source: timeservicenotify.py

The notification system is already built-in to svs_core.network.GenericClient. All that a client wishing to send data to listener clients needs to to do is call the notifyListeners and send a labelled data packet. We want to notify all listening clients when one client has requested the time, so we can overide the original getTime method to include a notification:

def getTime(self, requester):
    dataPacket = TimeService.getTime(self, requester)
    # notify listeners of time request
    self.notifyListeners(self.getTimeLog())
    # return time to requester
    return dataPacket

Example 1.) getTime method with notification call (in bold).

The data we send with the notification is the time log. The getTimeLog method provides this in a ready labelled data packet - the label is 'time_log'. Any clients which have registered to listen for data labelled 'time_log' will be sent this.

page contents

notification in the user client

source: timeuserlistener.py

We will extend the graphical time user client to register as a listener for this data. All we need to do here is overide the haveJoinedClusterGroup method (which is automatically called after the client has joined a group), and get it to register as a listener for 'time_log' data by calling the startListeningTo method with the name of the client we want to listen to (the time service) and the label for the type of data we are interested in ('time_log'):

def haveJoinedClusterGroup(self):
    GraphicTimeUser.haveJoinedClusterGroup(self)
    self.startListeningTo(self.timeService, listenFor='time_log')

Example 2.) registering as a listener (in bold).

Clients can start listening at any time during a network session and can also stop listening for particilat data by calling stopListeningTo and senind it the same parameters:

self.stopListeningTo(self.timeService, listenFor='time_log')

Example 3.) stop listening.

As with the notification functionality in the time service client, the basic listener functionality is built-in to GenericClient and so available to all SVS clients.

When notification data is received it is handled in the same way as other data packets as part of the handleDataPacket method. As we have already implemented code for handling 'time_log' data packets nothing more needs to be done. If we were listening for data with a different label, however, we would have to add in new code for handling this.

page contents

running the examples

Working version of the examples are in the 'creating_clients' directory of the 'tutorials'.

Step 1.) start the server:

./server.py

This will run as a command line application with no user interface.

Step 2.) in a new terminal, start the service client:

./timeservice.py

This will run as a command line application with no user interface.

Step 3.) in a new terminal, start a user client with the listener code:

./timeuserlistener.py time_user_1 p@ssw0rd

Step 4.) start a second user, again in a new terminal, but this time one without the listener code:

./timeusergui.py time_user_2 p@ssw0rd

Step 5.) enter the 'time' command on both clients. You will see that on the first client, the time log display will automatically update not only when this client requests the time (as in the previous tutorial) but also when the second client requests the time. The second client, however, will only update the time log when it request the time or enters the 'time_log' command.

Step 6.) enter 'quit' to close each client and disconnect from the network. Enter 'control-c' in the terminal to shutdown the time service and the server.

page contents

creating SVS clients
part 1: time service and user
part 2: interface components
part 3: notification system