A Database Server - Client model with Callback


Introduction

       CAVERNsoft classes can be used to set up a simple client server model. We will try to establish a database server which looks out for clients wanting to connect to it. In this demo the clients tell the server to store a piece of data in its database. When the server receives this message it stores it in its database and then reflects the update to all the other connected clients.
            The demo also depicts two ways of notifying when new data is arriving - firing a user defined callback function using the trigger method and the contemporary object oriented alternative to do the same. The second technique is based on the Observer/Observable class as used in Java.


CAVERNsoft classes used

   CAVERNdb_server_c

                This class is used to instantiate a small database server. It is designed to handle small datasets. Once the server is set up using the init() method, it can be used to handle connections and process incoming requests using the process() method. This class also provides functions to display performance statistics.

    CAVERNdb_client_c

                Clients which are created using this class can connect to the server set up using CAVERNdb_server_c class. The client can perform commands like put(), fetch() and commit()
or remoteCommit()  (for the server database). The process() method is used to keep the client running and handle messages and reconnect() is used to connect back to the server if the connection is broken. The trigger() method is used to execute a user defined function each time a data arrives (in client.cxx the user defined function is incomingData() ). This is the traditional way of firing a callback.

    CAVERNmisc_observer_c and CAVERNmisc_subject_c

                    These classes are used to implement the Observer/Subject technique -  we do not instantiate this class, but the we create a 'myObserver' class (see client.cxx) which inherits from the CAVERNmisc_observer_c class and overload the update() function. The myObserver object is attached to the client using the attach() method and each time the notify() method is called the update() method is executed. All clients are then informed about the last update made.


Usage

 1. To run the server
      server

 2. To run a client
      client <server-address>

Output

       When a client connects to the server, performance data is displayed. Note the following sample display seen at the client. It shows that a callback was fired in two ways.
 CAVERN DB client connected to: whitted:9500
 DONE PUT
 OBSERVER NOTIFIED path1 key1 hello
 CALLBACK FIRED    path1 key1 hello

    If another client is connected to the server, then a similar message is relayed to all the clients. You can check if the message 'hello' was inserted into the server database.


for more information email : cavern@evl.uic.edu