ListenerSet-class {Proc4}R Documentation

Class "ListenerSet"

Description

This is a “mix-in” class that adds a speaker protocol to an object, which is complementary to the Listener protocol. This object maintains a list of listeners. When the notifyListeners method is called, it notifies each of the listeners by calling the receiveMessage method on the listener.

Extends

All reference classes extend and inherit methods from "envRefClass".

Methods

isListener

signature(x = "ListenerSet"): Returns true, as the ListenerSet follows the listener protocol.

receiveMessage

signature(x = "ListenerSet"): A synonym for notifyListeners.

notifyListeners

signature(sender = "ListenerSet"): A synonym for the notifyListeners internal method.

Protocol

The key to this class is the notifyListeners method. This method should receive as its argument a P4Message object. (The protocol is fairly robust to the type of message and the type is not enforced. In fact, any object which has a as.jlist method should work.)

When the notifier is called it performs the following functions:

  1. It saves the message to the collection represented by messdb(). If messdb() is NULL (dburi is the empty string) then the messages is not saved.

  2. It calls the receiveMessage method on each of the objects in the listener list.

  3. It logs the messages sent using the flog.logger, in the "Proc4" logger. The sending of the messages is logged a the “INFO” level, and the actual message at the “DEBUG” level.

In addition, the ListenerSet maintains a named list of Listener objects (that is, objects that have a receiveMessage method). The methods addListener and removeListener maintain this list.

Fields

sender:

Object of class character:the name of the source of the messages.

dburi:

Object of class character: the URI for the mongo database. If null, then no recording of messages to a database is done (except possibly in the listeners).

dbname:

Object of class character: the name of the database in which messages should be logged.

colname:

Object of class character: the name of the collection in which messages should be logged.

listeners:

A named list of Listener objects, that is objects for which isListener is true.

db:

Object of class MongoDB which is a handle to the collection where messages are logged, or NULL if the log database has not been initialized. As the database may have not been initialized, programs should call the messdb() method which will open the database connection if it is not yet open.

Class-Based Methods

notifyListeners(mess):

This method calls receiveMessage on all of the listeners. See Protocol section above.

addListener(name, listener):

This method addes a lsitener to the list.

initialize(sender, dburi, listeners, colname, ...):

This creates the listener. Note, this does not initialize the database collection. Call messdb() to initialize the collection.

removeListener(name):

This removes a listener from the collection by its name.

messdb

signature(): Returns the mongo database collection to which to log messages. Creates the column if it has not been initialized.

Note

The notifyListeners method uses the flog.logger protocol. In particular, it logs sending the message at the “INFO” level, and the actual message sent at the “DEBUG” level. In particular, setting flog.threshold(DEBUG,name="Proc4") will turn on logging of the actual message and flog.threshold(WARN,name="Proc4") will turn off logging of the message sent messages.

It is often useful to redirect the Proc4 logger to a log file. In addition, changing the logging format to JSON, will allow the message to be recovered. Thus, try flog.layout(layout.json,name="Proc4" to activate logging in JSON format.

Author(s)

Russell Almond

References

https://en.wikipedia.org/wiki/Observer_pattern

See Also

Listener, receiveMessage, notifyListeners, flog.logger, mongo, P4Message

Listener Classes. CaptureListener, UpdateListener, UpsertListener, InjectionListener, TableListener

Examples

showClass("ListenerSet")

[Package Proc4 version 0.4-7 Index]