Python Manual User Manual

Page of 124
Service Reports
52
Although "
purgeReports()
" exists specifically to deal with potential performance issues in a very
limited number of cases, the "
expireReports()
" and "
expireAllReports()
" member func-
tions are useful where a service may have reset itself and it was necessary to discard all cached reports
so that new subscribers didn’t receive them.
Identity of Subscribers
In most circumstances the identity of a subscriber is not important, however, such information can be
quite useful in a few circumstances. At present this information is available by overriding a method in
the service agent base class.
class Publisher(netsvc.Service):
def __init__(self):
netsvc.Service.__init__(self,"publisher")
def handleSubscription(self,subscription):
subscriber = subscription.subscriber()
if subscription.status() == netsvc.SUBSCRIPTION_REQUESTED:
if self.matchSubject(subscription.subject(),"system.time"):
self.sendReport(subscriber,"system.time",netsvc.DateTime())
One can use this feature in preference to caching reports when they are published. That is, rather than
caching a report when it is published so that a new subscriber automatically receives it, generate the
report only when the subscription arrives. Obviously however, in this approach we would only want
to have the report sent to the particular subscriber and not to all subscribers as they would potentially
get duplicates otherwise.
To cater for this scenario, the member function "
sendReport()
" is supplied. In this variant of report
publishing, the first argument is the service binding object of the subscriber obtained from the sub-
scription notification. This report will only be sent to the subscriber in question and will not be cached.
Note that if a report was also cached against the subject in question, the subscriber would still receive
it as well. Both anonymous publishing and targeted reports should therefore not be used in combination
for a specific subject as it may give undesired results.
The member function "
matchSubject()
" is supplied to assist in determining if the subject pattern
contained in the subscription matches that of a particular subject. The first argument to "
matchSub-
ject()
" should be the pattern and the second the actual subject. Although not used here, the opposite
to the status value "
SUBSCRIPTION_REQUESTED
" is the value "
SUBSCRIPTION_WITHDRAWN
".
Note that if the "
sendReport()
" member function is used to send a report and the recipient has a
subscription against the publishing service, but doesn’t have a subscription against that service match-
ing that subject, the report will not be delivered via the callback it originally supplied with its subscrip-
tion. A similar situation is where a service receives an unsolicited report, or had since unsubscribed
from the reports. In these cases there is no current callback in place for reception of the report. When
this occurs the member function "
unexpectedReport()
" will be called. A service agent may if it
desires override this member function so as to deal with any such unexpected reports.