Listener {Proc4}R Documentation

A listener is an object which can recieve a message.

Description

A listener an an object that takes on the observer or listerner role in the the listener (or observer) design pattern. A listener will register itself with a speaker, and when the speaker sends a message it will act accordingly. The receiveMessage generic function must be implemented by a listener. It is called when the speaker wants to send a message.

Usage

receiveMessage(x, mess)
isListener(x)
## S4 method for signature 'ANY'
isListener(x)

Arguments

x

A object of the virtual class Listner.

mess

A P4Message which is being transmitted.

Details

The Listener class is a virtual class. Any object can become a listener by giving it a method for receiveMessage. The message is intended to be a subclass of P4Message, but in practice, no restriction is placed on the type of the message.

As Listener is a virtual class, it does not have a formal definition. Instead the generic function isListner is used to test if the object is a proper listener or not. The default method checks for the presence of a receiveMessage method. As this might not work properly with S3 objects, an object can also register itself directly by setting a method for isListner which returns true.

Typically, a lister will register itself with the speaker objects. For example the ListenerSet$addListener method adds itself to a list of listeners maintained by the object. When the ListenerSet$notifyListeners method is called, the receiveMessage method is called on each listener in the list.

Value

The isListener function should return TRUE or FALSE, according to whether or not the object follows the listner protocol.

The receiveMessage function is typically invoked for side effects and it may have any return value.

Author(s)

Russell Almond

References

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

See Also

Implementing Classes: CaptureListener, UpdateListener, UpsertListener, InjectionListener, TableListener

Related Classes: ListenerSet, P4Message

Examples


## Not run: ## Requires Mongo database set up.
MyListener <- setClass("MyListener",slots=c("name"="character"))
setMethod("receiveMessage","MyListener",
   function(x,mess)
      cat("I (",x@name,") just got the message ",mess(mess),"\n"))


lset <-
ListenerSet$new(sender="Other",dburi="mongodb://localhost",
                colname="messages")
lset$addListener("me",MyListener())

mess1 <- P4Message("Fred","Task 1","Evidence ID","Scored Response",
         as.POSIXct("2018-11-04 21:15:25 EST"),
         list(correct=TRUE,seletion="D"))

mess2 <- P4Message("Fred","Task 2","Evidence ID","Scored Response",
         as.POSIXct("2018-11-04 21:17:25 EST"),
         list(correct=FALSE,seletion="D"))

lset$notifyListeners(mess1)

lset$removeListener("me")

notifyListeners(lset,mess2)


## End(Not run)

[Package Proc4 version 0.4-7 Index]