Python Manual Benutzerhandbuch

Seite von 124
Service Requests
68
Note that "
suspendResponse()
" and "
resumeResponse()
" were only added in OSE 7.0pl5
and are a layer on top of the "
delayResponse()
" method which only performed the single opera-
tion of raising the exception of type
DelayedResponse
. The newer functions should make the task
of implementing a delayed responese easier, so if you are using "
delayResponse()
" you should
change your code to use the newer functions.
Identity of the Sender
Normally it is not necessary to know the identity of the sender of a request. If a means of identifying
who has initiated the request is required however, the details of the current request can be queried to
obtain the address of the sender. This can be useful where a separate session object in the form of a
new service is created to manage interaction with a particular client. To obtain the request object for
the current request the "
currentRequest()
" member function is used.
By calling the "
sender()
" member function of a request object, the service address of the service
agent initiating the request can be obtained. Having created a separate session for that client, all re-
quests for that session can be authenticated as being from the same service agent. Such a scheme may
even have as a prelude a log in mechanism to ensure that a service agent making the request has suffi-
cient privileges to initiate a separate session.
Whether or not a login and password is required, the idea is that the method used to initiate the session
returns the name of the service created to manage the session. Such a session object should monitor
the existence of the service agent who initiated the session such that the session can be destroyed au-
tomatically when the owner is withdrawn.
class Session(netsvc.Service):
def __init__(self,name,client):
netsvc.Service.__init__(self,name)
self._client = client
self.subscribeServiceAddress(self.announce,client)
self.exportMethod(self.close)
def announce(self,binding,status):
if status = netsvc.SERVICE_WITHDRAWN:
self.destroyReferences()
def close(self):
client = self.currentRequest().sender()
if client != self._client:
self.abortResponse(1,"Not Owner of Session")
self.destroyReferences()
class Service(netsvc.Service):
def __init__(self,name="service"):
netsvc.Service.__init__(self,name)
self._count = 0
self.exportMethod(self.login)
def login(self,login,passwd):
# authorise login/passwd