metaclient.py
上传用户:ghyvgy
上传日期:2009-05-26
资源大小:547k
文件大小:2k
源码类别:

其他游戏

开发平台:

Python

  1. from twisted.spread.pb import authIdentity, getObjectAt, portno
  2. from twisted.spread.flavors import Referenceable
  3. from twisted.internet import reactor
  4. from twisted.internet.defer import DeferredList
  5. class BugClient(Referenceable):
  6.     def gotPerspective(self, pref):
  7.         print 'got bug'
  8.         pref.callRemote("goToPsychologist","The Tick").addCallback(self.notify)
  9.         pref.callRemote("psychoanalyze").addCallback(self.notify)
  10.     def notify(self, text):
  11.         print 'bug:', text
  12.     def remote_healed(self, healedMessage):
  13.         print 'Healed:', healedMessage
  14. class CellClient(Referenceable):
  15.     def gotPerspective(self, pref):
  16.         print 'got cell'
  17.         # pref.callRemote()
  18.     def remote_connected(self):
  19.         print 'Cell Connected'
  20.     def remote_hear(self, text):
  21.         print 'Phone:', text
  22.     def remote_ring(self, callerID):
  23.         print "Your phone is ringing.  It's a call from", callerID
  24. bugClient = BugClient()
  25. phoneClient = CellClient()
  26. # Log-In Information
  27. username = "bob"
  28. password = "bobpass"
  29. bugName = "franz"
  30. phoneNumber = "222-303-8485"
  31.     
  32. # A little magic to get us connected...
  33. getObjectAt("localhost", portno).addCallback(
  34.     # challenge-response authentication
  35.     lambda r: authIdentity(r, username, password)).addCallback(
  36.     # connecting to each perspective with 'attach' method of remote identity
  37.     lambda i: DeferredList([
  38.     i.callRemote("attach", "metamorph", bugName, bugClient),
  39.     i.callRemote("attach", "cellphone", phoneNumber, phoneClient)])
  40.     # connecting perspectives to client-side objects
  41.     ).addCallback(lambda l: (bugClient.gotPerspective(l[0][1]),
  42.                              phoneClient.gotPerspective(l[1][1])))
  43. reactor.callLater(5, reactor.stop) # In five seconds, log out.
  44. reactor.run() # Start the main loop.