Exemplo n.º 1
0
    def __init__(self,
                 ncpus="autodetect",
                 interface="0.0.0.0",
                 broadcast="255.255.255.255",
                 port=None,
                 secret=None,
                 timeout=None,
                 restart=False,
                 proto=2,
                 socket_timeout=3600,
                 pid_file=None):
        pp.Server.__init__(self, ncpus, (), secret, restart, proto,
                           socket_timeout)
        if pid_file:
            with open(pid_file, 'w') as pfile:
                six.print_(os.getpid(), file=pfile)
            atexit.register(os.remove, pid_file)
        self.host = interface
        self.bcast = broadcast
        if port is not None:
            self.port = port
        else:
            self.port = ppc.randomport()
        self.timeout = timeout
        self.ncon = 0
        self.last_con_time = time.time()
        self.ncon_lock = threading.Lock()

        self.logger.debug("Starting network server interface=%s port=%i" %
                          (self.host, self.port))
        if self.timeout is not None:
            self.logger.debug("ppserver will exit in %i seconds if no "\
                    "connections with clients exist" % (self.timeout))
            ppc.start_thread("timeout_check", self.check_timeout)
Exemplo n.º 2
0
    def listen(self):
        """Initiates listenting to incoming connections"""
        try:
            self.ssocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            # following allows ppserver to restart faster on the same port
            self.ssocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.ssocket.settimeout(LISTEN_SOCKET_TIMEOUT)
            self.ssocket.bind((self.host, self.port))
            self.ssocket.listen(5)
        except socket.error:
            e = sys.exc_info()[1]
            self.logger.error("Cannot create socket for %s:%s, %s", self.host,
                              self.port, e)

        try:
            while 1:
                csocket = None
                # accept connections from outside
                try:
                    (csocket, address) = self.ssocket.accept()
                except socket.timeout:
                    pass
                # don't exit on an interupt due to a signal
                except socket.error:
                    e = sys.exc_info()[1]
                    if e.errno == errno.EINTR:
                        pass
                if self._exiting:
                    return
                # now do something with the clientsocket
                # in this case, we'll pretend this is a threaded server
                if csocket:
                    ppc.start_thread("client_socket", self.crun, (csocket, ))
        except KeyboardInterrupt:
            pass
        except:
            self.logger.debug("Exception in listen method (possibly expected)",
                              exc_info=True)
        finally:
            self.logger.debug("Closing server socket")
            self.ssocket.close()
Exemplo n.º 3
0
 def broadcast(self):
     """Initiaates auto-discovery mechanism"""
     discover = ppauto.Discover(self)
     ppc.start_thread("server_broadcast", discover.run,
                      ((self.host, self.port), (self.bcast, self.port)))
Exemplo n.º 4
0
class _NetworkServer(pp.Server):
    """Network Server Class
    """
    def __init__(self,
                 ncpus="autodetect",
                 interface="0.0.0.0",
                 broadcast="255.255.255.255",
                 port=None,
                 secret=None,
                 timeout=None,
                 restart=False,
                 proto=2,
                 socket_timeout=3600,
                 pid_file=None):
        pp.Server.__init__(self, ncpus, (), secret, restart, proto,
                           socket_timeout)
        if pid_file:
            with open(pid_file, 'w') as pfile:
                print >> pfile, os.getpid()
            atexit.register(os.remove, pid_file)
        self.host = interface
        self.bcast = broadcast
        if port is not None:
            self.port = port
        else:
            self.port = ppcommon.randomport()
        self.timeout = timeout
        self.ncon = 0
        self.last_con_time = time.time()
        self.ncon_lock = threading.Lock()

        self.logger.debug("Strarting network server interface=%s port=%i" %
                          (self.host, self.port))
        if self.timeout is not None:
            self.logger.debug("ppserver will exit in %i seconds if no "\
                    "connections with clients exist" % (self.timeout))
            ppcommon.start_thread("timeout_check", self.check_timeout)

    def ncon_add(self, val):
        """Keeps track of the number of connections and time of the last one"""
        self.ncon_lock.acquire()
        self.ncon += val
        self.last_con_time = time.time()
        self.ncon_lock.release()

    def check_timeout(self):
        """Checks if timeout happened and shutdowns server if it did"""
        while True:
            if self.ncon == 0:
                idle_time = time.time() - self.last_con_time
                if idle_time < self.timeout:
                    time.sleep(self.timeout - idle_time)
                else:
                    self.logger.debug("exiting ppserver due to timeout (no client"\
                            " connections in last %i sec)", self.timeout)
                    os._exit(0)
            else:
                time.sleep(self.timeout)

    def listen(self):
        """Initiates listenting to incoming connections"""
        try:
            self.ssocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            # following allows ppserver to restart faster on the same port
            self.ssocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.ssocket.settimeout(LISTEN_SOCKET_TIMEOUT)
            self.ssocket.bind((self.host, self.port))
            self.ssocket.listen(5)
        except socket.error, e:
            self.logger.error("Cannot create socket for %s:%s, %s", self.host,
                              self.port, e)

        try:
            while 1:
                csocket = None
                # accept connections from outside
                try:
                    (csocket, address) = self.ssocket.accept()
                except socket.timeout:
                    pass
                # don't exit on an interupt due to a signal
                except socket.error:
                    e = sys.exc_info()[1]
                    if e.errno == errno.EINTR:
                        pass
                if self._exiting:
                    return
                # now do something with the clientsocket
                # in this case, we'll pretend this is a threaded server
                if csocket:
                    ppcommon.start_thread("client_socket", self.crun,
                                          (csocket, ))
        except KeyboardInterrupt:
            pass
        except:
            self.logger.debug("Exception in listen method (possibly expected)",
                              exc_info=True)
        finally:
            self.logger.debug("Closing server socket")
            self.ssocket.close()