Exemple #1
0
    def run(self):
        # wait until the zhttp_server exist in socket_map
        # because TimerService has to be started after the Zope HTTPServer
        from asyncore import socket_map
        ip = port = ''
        while 1:
            time.sleep(5)
            for k, v in socket_map.items():
                if hasattr(v, 'addr'):
                    # see Zope/lib/python/App/ApplicationManager.py: def getServers(self)
                    type = str(getattr(v, '__class__', 'unknown'))
                    if type == 'ZServer.HTTPServer.zhttp_server':
                        ip, port = v.addr
                        break
            if port:
                break

        if ip == '0.0.0.0':
            ip = socket.gethostbyname(socket.gethostname())

        # To be very sure, try to connect to the HTTPServer
        # and only start after we are able to connect and got a response
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(None)
        while 1:
            try:
                s.connect((ip, port))
            except socket.error:
                time.sleep(5)
                continue
            s.send('GET / HTTP/1.1\r\n\r\n')
            s.recv(4096)  # blocks until a response is received
            break
        s.close()

        module = self.module
        interval = self.interval

        # minutes = time.gmtime(time.time()[4], seconds = time.gmtime(time.time()[5]
        # max interval is therefore 59*60 + 59 = 208919 seconds

        wait = ((time.gmtime(time.time())[4] * 60) +
                time.gmtime(time.time())[5]) % interval
        sleep = interval - wait

        if sleep > 0:
            time.sleep(sleep)

        LOG('ZServer', INFO, 'Timerserver ready, starting timer services.')

        while 1:
            time.sleep(interval)
            # send message to zope
            try:
                out = StringIO()
                err = StringIO()
                response = TimerResponse(out, err)
                handle(module, TimerRequest(response, interval), response)
            except:
                pass
Exemple #2
0
    def run(self):
        # wait until the zhttp_server exist in socket_map
        # because TimerService has to be started after the Zope HTTPServer
        from asyncore import socket_map
        ip = port = ''
        while 1:
            time.sleep(5)
            for k, v in socket_map.items():
                if hasattr(v, 'addr'):
                        # see Zope/lib/python/App/ApplicationManager.py: def getServers(self)
                        type = str(getattr(v, '__class__', 'unknown'))
                        if type == 'ZServer.HTTPServer.zhttp_server':
                            ip, port = v.addr
                            break
            if port:
                break

        if ip == '0.0.0.0':
          ip = socket.gethostbyname(socket.gethostname())

        # To be very sure, try to connect to the HTTPServer
        # and only start after we are able to connect and got a response
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(None)
        while 1:
            try:
                s.connect((ip, port))
            except socket.error:
                time.sleep(5)
                continue
            s.send('GET / HTTP/1.1\r\n\r\n')
            s.recv(4096) # blocks until a response is received
            break
        s.close()

        module = self.module
        interval = self.interval

        # minutes = time.gmtime(time.time()[4], seconds = time.gmtime(time.time()[5]
        # max interval is therefore 59*60 + 59 = 208919 seconds

        wait = ((time.gmtime(time.time())[4] * 60) + time.gmtime(time.time())[5]) % interval
        sleep = interval - wait

        if sleep > 0:
            time.sleep(sleep)

        LOG('ZServer', INFO, 'Timerserver ready, starting timer services.')

        while 1:
            time.sleep(interval)
            # send message to zope
            try:
                out = StringIO()
                err = StringIO()
                response = TimerResponse(out, err)
                handle(module, TimerRequest(response, interval), response)
            except:
                pass
Exemple #3
0
    def getPort(self):
        """ return zope port """

        from asyncore import socket_map
        for k,v in socket_map.items():
            # this is only an approximation
            if hasattr(v, 'port'):
                type = str(getattr(v, '__class__', 'unknown'))
                port = v.port
        return port
def doit(consumer = None):
        while True:
                if consumer:
                        if consumer.is_done():
                                for fd, obj in list(map.items()):
                                        obj.handle_close()
                                break
                if not len(map):
                        break
                poll()
Exemple #5
0
def getWebdavServerPort():
    webdavport = None
    from asyncore import socket_map
    bFound = False
    for k,v in socket_map.items():
        if hasattr(v, 'port') and hasattr(v,'server_protocol') and v.server_protocol.lower() == 'webdav':
            webdavport = v.port
            bFound = True
        if bFound:
            break;
    return webdavport
Exemple #6
0
def poll(timeout = 0.1, map = asyncore.socket_map):

        tm = time.time()
        for fd, obj in list(map.items()):

                if (tm - obj.start) > obj.timeout:
                        obj.handle_timeout()

        if len(map) == 0:
                time.sleep(timeout)
        else:
                asyncore.poll(timeout, map)
        return
 def getServers(self):
     # used only for display purposes
     # return a sequence of two-tuples.  The first element of
     # each tuple is the service name, the second is a string repr. of
     # the port/socket/other on which it listens
     from asyncore import socket_map
     l = []
     for k,v in socket_map.items():
         # this is only an approximation
         if hasattr(v, 'port'):
             type = str(getattr(v, '__class__', 'unknown'))
             port = v.port
             l.append((str(type), 'Port: %s' % port))
     return l
Exemple #8
0
 def getServers(self):
     # used only for display purposes
     # return a sequence of two-tuples.  The first element of
     # each tuple is the service name, the second is a string repr. of
     # the port/socket/other on which it listens
     from asyncore import socket_map
     l = []
     for k,v in socket_map.items():
         # this is only an approximation
         if hasattr(v, 'port'):
             type = str(getattr(v, '__class__', 'unknown'))
             port = v.port
             l.append((str(type), 'Port: %s' % port))
     return l
Exemple #9
0
 def getCurrentNode(self):
     """ Return current node in form ip:port """
     global current_node
     if current_node is None:
       ip = port = ''
       from asyncore import socket_map
       for k, v in socket_map.items():
           if hasattr(v, 'addr'):
               # see Zope/lib/python/App/ApplicationManager.py: def getServers(self)
               type = str(getattr(v, '__class__', 'unknown'))
               if type == 'ZServer.HTTPServer.zhttp_server':
                   ip, port = v.addr
                   break
       if ip == '0.0.0.0':
         ip = socket.gethostbyname(socket.gethostname())
       current_node = '%s:%s' %(ip, port)
     return current_node
Exemple #10
0
    def run(self):
        try:
            zopewsgi = sys.modules['Products.ERP5.bin.zopewsgi']
        except KeyError:
            # wait until the zhttp_server exist in socket_map
            # because TimerService has to be started after the Zope HTTPServer
            from asyncore import socket_map
            ip = port = ''
            while 1:
                time.sleep(5)
                for k, v in socket_map.items():
                    if hasattr(v, 'addr'):
                        # see Zope/lib/python/App/ApplicationManager.py: def getServers(self)
                        type = str(getattr(v, '__class__', 'unknown'))
                        if type == 'ZServer.HTTPServer.zhttp_server':
                            ip, port = v.addr
                            break
                if port:
                    break
            from ZServer.PubCore import handle
        else:
            while 1:
                time.sleep(5)
                try:
                    server = zopewsgi.server
                    break
                except AttributeError:
                    pass

            ip, port = server.addr
            start_response = lambda *_: None

            class handle(object):
                def __init__(self, module_name, request, response):
                    self.service = partial(zopewsgi.publish_module,
                                           request.environ,
                                           start_response,
                                           _module_name=module_name,
                                           _request=request,
                                           _response=response)
                    server.add_task(self)

                def cancel(self):
                    pass

        if ip == '0.0.0.0':
            ip = socket.gethostbyname(socket.gethostname())

        # To be very sure, try to connect to the HTTPServer
        # and only start after we are able to connect and got a response
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(None)
        while 1:
            try:
                s.connect((ip, port))
            except socket.error:
                time.sleep(5)
                continue
            s.send(b'GET / HTTP/1.1\r\n\r\n')
            s.recv(4096)  # blocks until a response is received
            break
        s.close()

        module = self.module
        interval = self.interval

        logger.info('Service ready.')

        while 1:
            time.sleep(interval)
            # send message to zope
            try:
                out = StringIO()
                err = StringIO()
                response = TimerResponse(out, err)
                handle(module, TimerRequest(response, interval), response)
            except Exception:
                logger.warn("Ignoring exception in run loop", exc_info=True)