Package VisionEgg :: Module PyroHelpers |
[frames] | no frames]

# Source Code for Module VisionEgg.PyroHelpers

      1  # The Vision Egg: PyroHelpers
      2  #
      3  # Copyright (C) 2001-2003 Andrew Straw.
      4  # Author: Andrew Straw <astraw@users.sourceforge.net>
      5  # URL: <http://www.visionegg.org/>
      6  #
      7  # Distributed under the terms of the GNU Lesser General Public License
      8  # (LGPL). See LICENSE.TXT that came with this file.
      9  #
     10  # $Id$
     11
     12  """
     13  Python Remote Objects support.
     14
     15  Use this class if you don't want to deal with TCP directly and Python
     16  is the program on both ends of the network.
     17
     18  The module provides some Vision Egg specific code for Pyro.  Pyro
     19  allows you to call python objects on remote machines just like they
     20  are on the local machine.  This makes the task of writing a two
     21  computer Vision Egg application quite easy, because one can mostly
     22  ignore the network-based intermediate stage.
     23
     24  PyroControllers are run on the computer performing the presentation.
     25  The PyroServer class also runs on this computer, and allows these
     26  controllers to be changed from a computer running PyroClient. To
     27  listen to the network PyroListenerController must be instantiated by
     28  the PyroServer -- this checks for any requests coming over the
     29  network, but only at times specified because it is a subclass of
     30  VisionEgg.FlowControl.Controller.
     31
     32  Just like TCPControllers, don't use this class for realtime control
     33  unless you think your network is that fast and reliable.  It's great
     34  for setting up parameters in advance and sending a trigger pulse,
     35  though!"""
     36
     37  import VisionEgg
     38  import VisionEgg.Core
     39  import VisionEgg.FlowControl
     40  import VisionEgg.ParameterTypes as ve_types
     41
     42  __version__ = VisionEgg.release_name
     43  __cvs__ = '$Revision$'.split()[1]
     44  __date__ = ' '.join('$Date$'.split()[1:3])
     45  __author__ = 'Andrew Straw <astraw@users.sourceforge.net>'
     46
     47  import Pyro.core
     48  import Pyro.errors
     49
     50  Pyro.config.PYRO_MULTITHREADED = 0 # No multithreading!
     51
     52 -class PyroServer:
     53      """Set up a Pyro server for your PyroControllers and PyroGoClass.
     54
     55      This class is analagous to VisionEgg.TCPController.TCPServer.
     56
     57      """
     58 -    def __init__(self):
     59          # Start Pyro
     60          Pyro.core.initServer()
     61          self.daemon = Pyro.core.Daemon()
     62          self.ok_to_run = 1
     63
     64 -    def get_hostname_and_port(self):
     65          return self.daemon.hostname, self.daemon.port
     66
     67 -    def connect(self,object,name):
     68          """Serve an object under a name"""
     69          URI=self.daemon.connect(object,name)
     70          return URI
     71
     72 -    def disconnect(self,object):
     73          if Pyro.core.constants.VERSION >= '3.2':
     74              self.daemon.disconnect(object)
     75          else:
     76              # workaround bug in Pyro pre-3.2
     77              del self.daemon.implementations[object.GUID()]
     78              object.setDaemon(None)
     79
     80 -    def create_listener_controller(self):
     81          if hasattr(self,'listen_controller'):
     82              raise RuntimeError("Only one pyro listen controller allowed per server!")
     83          self.listen_controller = PyroListenController(self)
     84          return self.listen_controller
     85
     86 -    def handleRequests(self, timeout=0):
     87          """Only use this if you don't use the PyroListenerController.
     88
     89          A timeout of 0 specifies return immediately."""
     90          self.daemon.handleRequests(timeout)
     91
     92 -class PyroConstantController(VisionEgg.FlowControl.ConstantController,Pyro.core.ObjBase):
     93 -    def __init__(self, **kw):
     94          VisionEgg.FlowControl.ConstantController.__init__(self,**kw)
     95          Pyro.core.ObjBase.__init__(self)
     96
     97 -class PyroEvalStringController(VisionEgg.FlowControl.EvalStringController,Pyro.core.ObjBase):
     98 -    def __init__(self, **kw):
     99          VisionEgg.FlowControl.EvalStringController.__init__(self,**kw)
    100          Pyro.core.ObjBase.__init__(self)
    101
    102 -class PyroExecStringController(VisionEgg.FlowControl.ExecStringController,Pyro.core.ObjBase):
    103 -    def __init__(self, **kw):
    104          VisionEgg.FlowControl.ExecStringController.__init__(self,**kw)
    105          Pyro.core.ObjBase.__init__(self)
    106
    107 -class PyroEncapsulatedController(VisionEgg.FlowControl.EncapsulatedController,Pyro.core.ObjBase):
    108      """Create the instance of Controller on client, and send it to server.
    109
    110      This class is analagous to VisionEgg.TCPController.TCPController.
    111      """
    112 -    def __init__(self,initial_controller=None,**kw):
    113          VisionEgg.FlowControl.EncapsulatedController.__init__(self,initial_controller)
    114          Pyro.core.ObjBase.__init__(self)
    115
    116 -class PyroLocalDictController(VisionEgg.FlowControl.EncapsulatedController,Pyro.core.ObjBase):
    117      """Contain several dictionary entries, set controller accordingly.
    118      """
    119 -    def __init__(self, dict=None, key=None, **kw):
    120          if dict is None:
    121              self.dict = {}
    122              initial_controller = VisionEgg.FlowControl.ConstantController(during_go_value=0,
    123                                                                     between_go_value=0,
    124                                                                     eval_frequency=VisionEgg.FlowControl.Controller.NEVER)
    125          else:
    126              self.dict = dict
    127          if key is None:
    128              if len(self.dict.keys()):
    129                  key = self.dict.keys()[0]
    130                  initial_controller = self.dict[key]
    131              else:
    132                  initial_controller = VisionEgg.FlowControl.ConstantController(during_go_value=0,
    133                                                                         between_go_value=0,
    134                                                                         eval_frequency=VisionEgg.FlowControl.Controller.NEVER)
    135          else:
    136              initial_controller = dict[key]
    137          VisionEgg.FlowControl.EncapsulatedController.__init__(self,initial_controller)
    138          Pyro.core.ObjBase.__init__(self)
    139 -    def use_controller(self,key):
    140          self.set_new_controller(self.dict[key])
    141 -    def add_controller(self,key,new_controller):
    142          self.dict[key] = new_controller
    143
    144 -class PyroListenController(VisionEgg.FlowControl.Controller):
    145      """Handle connection from remote machine, control PyroControllers.
    146
    147      This meta controller handles a Pyro daemon, which checks the TCP
    148      socket for new input and acts accordingly.
    149
    150      This class is analagous to VisionEgg.TCPController.SocketListenController.
    151
    152      """
    153
    154 -    def __init__(self,server=None,**kw):
    155          """Called by PyroServer. Creates a PyroListenerController instance."""
    156          if not isinstance(server,PyroServer):
    157              raise ValueError("Must specify a Pyro Server.")
    158          if 'eval_frequency' not in kw.keys():
    159              kw['eval_frequency'] = VisionEgg.FlowControl.Controller.EVERY_FRAME
    160          if 'return_type' not in kw.keys():
    161              kw['return_type'] = ve_types.get_type(None)
    162          VisionEgg.FlowControl.Controller.__init__(self,**kw)
    163          self.server=server
    164
    165 -    def during_go_eval(self):
    166          # setting timeout = 0 means return ASAP
    167          self.server.daemon.handleRequests(timeout=0)
    168
    169 -    def between_go_eval(self):
    170          # setting timeout = 0 means return ASAP
    171          self.server.daemon.handleRequests(timeout=0)
    172