server/server.py
branchtls-sprint
changeset 1802 d628defebc17
parent 0 b97547f5f1fa
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
     1 """Pyro RQL server
     1 """Pyro RQL server
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 import os
     9 import os
    20 
    20 
    21 class TimeEvent:
    21 class TimeEvent:
    22     """base event"""
    22     """base event"""
    23     # timefunc = staticmethod(localtime)
    23     # timefunc = staticmethod(localtime)
    24     timefunc = localtime
    24     timefunc = localtime
    25     
    25 
    26     def __init__(self, absolute=None, period=None):
    26     def __init__(self, absolute=None, period=None):
    27         # local time tuple
    27         # local time tuple
    28         if absolute is None:
    28         if absolute is None:
    29             absolute = self.timefunc()
    29             absolute = self.timefunc()
    30         self.absolute = absolute
    30         self.absolute = absolute
    55 class QuitEvent(TimeEvent):
    55 class QuitEvent(TimeEvent):
    56     """stop the server"""
    56     """stop the server"""
    57     def fire(self, server):
    57     def fire(self, server):
    58         server.repo.shutdown()
    58         server.repo.shutdown()
    59         server.quiting = True
    59         server.quiting = True
    60         
    60 
    61 
    61 
    62 class RepositoryServer(object):
    62 class RepositoryServer(object):
    63     
    63 
    64     def __init__(self, config, debug=False):
    64     def __init__(self, config, debug=False):
    65         """make the repository available as a PyRO object"""
    65         """make the repository available as a PyRO object"""
    66         self.config = config
    66         self.config = config
    67         self.repo = Repository(config, debug=debug)
    67         self.repo = Repository(config, debug=debug)
    68         self.ns = None
    68         self.ns = None
    84                 event.fire(self)
    84                 event.fire(self)
    85                 try:
    85                 try:
    86                     event.update()
    86                     event.update()
    87                 except Finished:
    87                 except Finished:
    88                     self.events.remove(event)
    88                     self.events.remove(event)
    89             
    89 
    90     def run(self, req_timeout=5.0):
    90     def run(self, req_timeout=5.0):
    91         """enter the service loop"""
    91         """enter the service loop"""
    92         while self.quiting is None:
    92         while self.quiting is None:
    93             try:
    93             try:
    94                 self.daemon.handleRequests(req_timeout)
    94                 self.daemon.handleRequests(req_timeout)
    95             except select.error:
    95             except select.error:
    96                 continue
    96                 continue
    97             self.trigger_events()
    97             self.trigger_events()
    98     
    98 
    99     def quit(self):
    99     def quit(self):
   100         """stop the server"""
   100         """stop the server"""
   101         self.add_event(QuitEvent())
   101         self.add_event(QuitEvent())
   102 
   102 
   103     def connect(self, host='', port=0):
   103     def connect(self, host='', port=0):
   104         """the connect method on the repository only register to pyro if
   104         """the connect method on the repository only register to pyro if
   105         necessary
   105         necessary
   106         """
   106         """
   107         self.daemon = self.repo.pyro_register(host)
   107         self.daemon = self.repo.pyro_register(host)
   108             
   108 
   109     # server utilitities ######################################################
   109     # server utilitities ######################################################
   110     
   110 
   111     def install_sig_handlers(self):
   111     def install_sig_handlers(self):
   112         """install signal handlers"""
   112         """install signal handlers"""
   113         import signal
   113         import signal
   114         self.info('installing signal handlers')
   114         self.info('installing signal handlers')
   115         signal.signal(signal.SIGINT, lambda x, y, s=self: s.quit())
   115         signal.signal(signal.SIGINT, lambda x, y, s=self: s.quit())
   116         signal.signal(signal.SIGTERM, lambda x, y, s=self: s.quit())
   116         signal.signal(signal.SIGTERM, lambda x, y, s=self: s.quit())
   117         
   117 
   118     def daemonize(self, pid_file=None):
   118     def daemonize(self, pid_file=None):
   119         """daemonize the process"""
   119         """daemonize the process"""
   120         # fork so the parent can exist
   120         # fork so the parent can exist
   121         if (os.fork()):
   121         if (os.fork()):
   122             return -1
   122             return -1