示例#1
0
    def run(self):
        
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        signal.signal(signal.SIGTERM, signal.SIG_IGN)
        
        # close inherited father pipes
        self.father_pipes[0].close()
        self.father_pipes[1].close()
        
        self.socks = Queue.Queue() #线程安全的queue

        self.clean_timer = threading.Timer(60, self.clean)
        self.clean_timer.start()
        
        self.f_control = ControlFatherProcess(self)
        self.f_control.start()
        
        while self.f_control.is_alive() or not self.socks.empty():
            try:
                sock = self.socks.get(timeout=0.01) #在timeout时间内,假如socks为空,返回Empty Exception
                sock.accept()
                ProtocolDetectDispatcher(sock, self.f_control)
            except (IOError, Queue.Empty):
                continue

            # reload asyncore if stopped
            if self.t_asyncore is None or not self.t_asyncore.is_alive():
                # timeout needed for more SSL layer reactivity
                self.t_asyncore = threading.Thread(target=lambda:asyncore.loop(timeout=0.01))
                self.t_asyncore.start()
        
        if self.t_asyncore is not None and self.t_asyncore.is_alive():
            asyncore.close_all()
            self.t_asyncore.join()
示例#2
0
 def run(self):
     cfg = self.session.config.sys.smtpd
     if cfg.host and cfg.port:
         server = SMTPServer(self.session, (cfg.host, cfg.port))
         while not self.quitting:
             asyncore.poll(timeout=1.0)
         asyncore.close_all()
示例#3
0
    def closeall_check(self, usedefault):
        # Check that close_all() closes everything in a given map

        l = []
        testmap = {}
        for i in range(10):
            c = dummychannel()
            l.append(c)
            self.assertEqual(c.socket.closed, False)
            testmap[i] = c

        if usedefault:
            socketmap = asyncore.socket_map
            try:
                asyncore.socket_map = testmap
                asyncore.close_all()
            finally:
                testmap, asyncore.socket_map = asyncore.socket_map, socketmap
        else:
            asyncore.close_all(testmap)

        self.assertEqual(len(testmap), 0)

        for c in l:
            self.assertEqual(c.socket.closed, True)
示例#4
0
文件: tests.py 项目: 6ft/django
 def run(self):
     self.active = True
     self.__flag.set()
     while self.active and asyncore.socket_map:
         with self.active_lock:
             asyncore.loop(timeout=0.1, count=1)
     asyncore.close_all()
示例#5
0
def debugging_server(serv, serv_evt, client_evt):
    serv_evt.set()

    try:
        if hasattr(select, 'poll'):
            poll_fun = asyncore.poll2
        else:
            poll_fun = asyncore.poll

        n = 1000
        while asyncore.socket_map and n > 0:
            poll_fun(0.01, asyncore.socket_map)

            # when the client conversation is finished, it will
            # set client_evt, and it's then ok to kill the server
            if client_evt.is_set():
                serv.close()
                break

            n -= 1

    except socket.timeout:
        pass
    finally:
        if not client_evt.is_set():
            # allow some time for the client to read the result
            time.sleep(0.5)
            serv.close()
        asyncore.close_all()
        serv_evt.set()
示例#6
0
 def stop_server(self):
     self._ftp_server.close()
     asyncore.close_all()
     self._async_thread.join()
     if 'threads' in tests.selftest_debug_flags:
         sys.stderr.write('Thread  joined: %s\n'
                          % (self._async_thread.ident,))
	def _HandleCommand(self, cmdtext):
		"""Handle system control commands which arrive via the web 
		interface.
		Parameters: cmdtext (string) = The command JSON string.
		"""
		# Decode the JSON.
		try:
			cmd = MonUtils.JSONDecode(cmdtext)
			cmdcode = cmd['mblogicsyscmd']
		except:
			self._SendError(404, MonUtils.JSONEncode({'mblogicsyscmd' : 'error'}))
			return

		timestamp = time.mktime(time.gmtime())
		respjson = MonUtils.JSONEncode({'mblogicsyscmd' : 'ok'})

		# Now, see what the command was.
		if cmdcode == 'reloadhmiconfig':
			print('\nRemote config reload command at %s\n' % time.ctime())
			# Send the headers and content.
			self._SendOKResponse('application/json', len(respjson), timestamp, respjson)

			HMIConf.LoadHMIConfig()

		elif cmdcode == 'shutdown':
			print('\nRemote shutdown command at %s\n' % time.ctime())
			# Send the headers and content.
			self._SendOKResponse('application/json', len(respjson), timestamp, respjson)

			asyncore.close_all()

		# We don't recognise the command.
		else:
			self._SendError(404, MonUtils.JSONEncode({'mblogicsyscmd' : 'error'}))
def FindFastest(varname, sitelist):
  latencies = {}

  # Ignore duplicates
  sitelist = set(sitelist)

  def callback(url, duration):
    latencies.setdefault(url, 0)
    latencies[url] += duration

  for url in sitelist:
    AsyncConnect(url, callback)
  count = len(sitelist)

  # We probaly don't need more than 10 results, so if there are more than 10
  # servers, lets only track the first few that return quickly:
  if count > 10:
    count = 10
  asyncore.loop(timeout=1, count=count)

  if not latencies:
    # No data has come back yet, let's wait
    print >>sys.stderr, " => Still waiting on data for %s" % varname
    asyncore.loop(timeout=30)

  # Close any still-open connections...
  asyncore.close_all()

  # Return the latency dict sorted by latency as list of (key, value)
  latency_list = sorted(latencies.iteritems(), key=lambda (a,b): (b,a))
  return latency_list
示例#9
0
 def run_client(self, command):
     time.sleep(1)  # TODO need a blocking queue
     s = socket.create_connection(('localhost', PORT))
     s.send(command)
     data = s.recv(1024)
     s.close()
     asyncore.close_all()
     self.assertEquals(data, command)
示例#10
0
 def run(self):
     self.active = True
     self.__flag.set()
     while self.active and asyncore.socket_map:
         self.active_lock.acquire()
         asyncore.loop(timeout=0.1, count=1)
         self.active_lock.release()
     asyncore.close_all()
示例#11
0
文件: bot.py 项目: TZer0/botmily
	def drop(self, calling_irc_handler):
		# Prevent accidental rogue irc timers?
		print("DC message from " + calling_irc_handler.__str__())
		if self.irc is calling_irc_handler:
			self.socket.close()
			asyncore.close_all()
		else:
			print("Bot is ignoring out of date handler. Masti is bad at threading!")
示例#12
0
 def start_server(self, port=2199, **kw):
     """Starts an asyncore server reading commands for clients and opening
     a new interpreter for each connection."""
     _AsynServer(self, _AsynHandler, port)  # register the server
     try:
         asyncore.loop(**kw)
     except KeyboardInterrupt:
         pass
     finally:
         asyncore.close_all()
示例#13
0
 def stop(self, signum=None, frame=None):
     if signum is not None:
         log(1, "caught %s" % STOP_SIGNALS[signum])
     log(1, "shutting down...")
     for app in self.config.apps:
         if hasattr(app, 'close') and inspect.ismethod(app.close):
             app.close()
     self.task_dispatcher.shutdown()
     asyncore.close_all()
     log(1, "httpy stopped")
示例#14
0
 def found_terminator(self):
     data, self.buffer = self.buffer, ''
     if not data:
         asyncore.close_all()
         print("done")
         return
     n = string.atoi(string.split(data)[0])
     tf = trigger_file(self)
     self.count = self.count + 1
     thread.start_new_thread(thread_function, (tf, self.count, n))
示例#15
0
 def test(port='8021'):
     fs = ftp_server(unix_authorizer(), port=string.atoi(port))
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         fs.log_info('FTP server shutting down. (received SIGINT)',
                     'warning')
         # close everything down on SIGINT.
         # of course this should be a cleaner shutdown.
         asyncore.close_all()
示例#16
0
 def found_terminator (self):
     data, self.buffer = self.buffer, ''
     if not data:
         asyncore.close_all()
         print "done"
         return
     n = string.atoi (string.split (data)[0])
     tf = trigger_file (self)
     self.count = self.count + 1
     thread.start_new_thread (thread_function, (tf, self.count, n))
    def handle_close(self):
        """When closed, this method will be call. clean itself."""
        LOGGER.debug("server closed")
        self.clear()
        data = {}
        self.update_notify_state(data, STAGE_SERVER_UNCONNECTED)
        self.close()

        # close all and restart
        asyncore.close_all()
示例#18
0
 def start_server(self, port=2199, **kw):
     """Starts an asyncore server reading commands for clients and opening
     a new interpreter for each connection."""
     _AsynServer(self, _AsynHandler, port)  # register the server
     try:
         asyncore.loop(**kw)
     except (KeyboardInterrupt, TerminatedProcess):
         pass
     finally:
         asyncore.close_all()
示例#19
0
def shutdown():
    global SHUTDOWN_PERFORMED

    if not SHUTDOWN_PERFORMED:
        l.warn('Passing out...')
        l.debug("Socket map: %s" % repr(asyncore.socket_map))
        l.info("Closing %d socket(s)" % len(asyncore.socket_map))
        asyncore.close_all()
        l.debug("Socket map: %s" % repr(asyncore.socket_map))
        l.warn('Gone')
示例#20
0
 def stop_and_close(self):
     '''To check the correctness of connection closing - we need to close
     it before stopping sniffer and analyzing sniffer's output (and throwing
     an exception in case of failure); so, we need to close Deproxy client
     and server connections in test_* function (not in tearDown).
     '''
     asyncore.close_all()
     self.client.stop()
     self.tempesta.stop()
     self.tester.stop()
示例#21
0
 def stop(self):
     global IS_FINISH
     IS_FINISH = True
     self.__task_handler.join()
     self.__logger.log("Debug", "Closing all connections...")
     asyncore.close_all()
     self.__logger.log("Debug", "Waiting for service"
                       " dispatcher to close...")
     self.__service.close()
     self.__logger.log("Debug", "Service dispatcher closed...")
     self.join()
示例#22
0
 def stop(self):
    if self.running:
        self.running = False
        self.commitTimer.cancel()
        for service in self.services:
            try:
                service.stop()
            except Exception as e:
                self.handler.stop_error(service, e)
                pass
        asyncore.close_all()
        self.asyncLoop.join()
    def shutdown(self, arg1=None, arg2=None):
        print('Initiating shutdown...')
        runtime.logger.info('Initiating shutdown...')
        self.running = False

        # stop services
        runtime.logger.info('Stopping services...')
        services.stop()

        # join asyn thread
        asyncore.close_all()
        self._asyn_thread.join()
示例#24
0
 def test (port='8021'):
     fs = ftp_server (
             unix_authorizer(),
             port=string.atoi (port)
             )
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         fs.log_info('FTP server shutting down. (received SIGINT)', 'warning')
         # close everything down on SIGINT.
         # of course this should be a cleaner shutdown.
         asyncore.close_all()
示例#25
0
    def shutdown(self, arg1=None, arg2=None):
        print('Initiating shutdown...')
        runtime.logger.info('Initiating shutdown...')
        self.running = False

        # stop services
        runtime.logger.info('Stopping services...')
        services.stop()

        # join asyn thread
        asyncore.close_all()
        self._asyn_thread.join()
示例#26
0
    def run_server(self, password="", port=DEFAULT_PORT):
        self.password = password
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.bind(("", port))
        self.listen(1)
        try:
            asyncore.loop(map=self.socket_map)
        except:
            asyncore.close_all()
            raise

        return self.taskmanager.results
示例#27
0
    def run_server(self, password="", port=DEFAULT_PORT):
        self.password = password
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.bind(("", port))
        self.listen(1)
        try:
            asyncore.loop(map=self.socket_map)
        except:
            asyncore.close_all()
            raise

        return self.taskmanager.results
示例#28
0
 def close(self, cancel_async_services = False):
     if cancel_async_services:
         logger.info('Cancelling all running asynchronous requests...')
         ResponseCallback.cancel_all()
         self.executor.cancel_all()
     else:
         logger.info('Waiting for all asynchronous requests to complete...')
     self.executor.shutdown(wait = True)
     self.simulator_service.close()
     # Close all other asyncore sockets (StreanJSON)
     asyncore.close_all()
     Morse._asyncore_thread.join(TIMEOUT)
     Morse._asyncore_thread = None # in case we want to re-create
     logger.info('Done. Bye bye!')
示例#29
0
 def close(self, cancel_async_services = False):
     if cancel_async_services:
         logger.info('Cancelling all running asynchronous requests...')
         ResponseCallback.cancel_all()
         self.executor.cancel_all()
     else:
         logger.info('Waiting for all asynchronous requests to complete...')
     self.executor.shutdown(wait = True)
     self.simulator_service.close()
     # Close all other asyncore sockets (StreanJSON)
     asyncore.close_all()
     Morse._asyncore_thread.join(TIMEOUT)
     Morse._asyncore_thread = None # in case we want to re-create
     logger.info('Done. Bye bye!')
示例#30
0
 def run(self):
     self.active = True
     self.__flag.set()
     try:
         while self.active and asyncore.socket_map:
             self.active_lock.acquire()
             try:
                 asyncore.loop(timeout=0.1, count=1)
             except:
                 self.active_lock.release()
                 raise
             self.active_lock.release()
     finally:
         asyncore.close_all(ignore_all=True)
示例#31
0
 def exit_parent(self, sig, frame):
     self.zk.stop()
     self.close()
     asyncore.close_all()
     pids = []
     for pid in self.child_pids:
         print 'before kill'
         try:
             os.kill(pid, signal.SIGINT)
             pids.append(pid)
         except OSError, ex:
             if ex.args[0] == errno.ECHILD:
                 continue
             raise ex
         print 'after kill', pid
示例#32
0
 def _wait_data(self, clients, queue):
     result_data = [None] * self.num_workers
     ready_flags = [False] * self.num_workers
     all_ready_flags = [True] * self.num_workers
     while True:
         time.sleep(0.2)
         for index in range(self.num_workers):
             if clients[index].ready() and clients[index] is not None:
                 result_data[index] = clients[index].get()
                 ready_flags[index] = True
         if ready_flags == all_ready_flags:
             raveled_data = ravel_array(result_data)
             queue.put(raveled_data)
             break
     asyncore.close_all()
示例#33
0
文件: rpc_server.py 项目: b1uema/pRPC
 def exit_parent(self, sig, frame):
     self.zk.stop()  # 关闭 zk 客户端
     self.close()  # 关闭 serversocket
     asyncore.close_all()  # 关闭所有 clientsocket
     pids = []
     for pid in self.child_pids:
         print 'before kill'
         try:
             os.kill(pid, signal.SIGINT)  # 关闭子进程
             pids.append(pid)
         except OSError, ex:
             if ex.args[0] == errno.ECHILD:  # 目标子进程已经提前挂了
                 continue
             raise ex
         print 'after kill', pid
示例#34
0
 def exit_parent(self, sig, frame):
     self.zk.stop()  # 关闭zk客户端
     self.close()  # 关闭serversocket
     asyncore.close_all()  # 关闭所有clientsocket
     pids = []
     for pid in self.child_pids:
         print 'before kill'
         try:
             os.kill(pid, signal.SIGINT)  # 关闭子进程
             pids.append(pid)
         except OSError, ex:
             if ex.args[0] == errno.ECHILD:  # 目标子进程已经提前挂了
                 continue
             raise ex
         print 'after kill', pid
示例#35
0
    def close(self):
        DebugLogger.log('A', id(self._request),
                '%s %s' % (self._request.reply_code, self._bytes))
        if not self._channel.closed:
            self._channel.push(LoggingProducer(self._request, self._bytes), 0)
            self._channel.push(CallbackProducer(self._channel.done), 0)
            self._channel.push(CallbackProducer(
                lambda t=('E', id(self._request)): apply(DebugLogger.log, t)), 0)
            if self._shutdown:
                self._channel.push(ShutdownProducer(), 0)
                Wakeup()
            else:
                if self._close: self._channel.push(None, 0)
            Wakeup()
        else:
            # channel closed too soon

            self._request.log(self._bytes)
            DebugLogger.log('E', id(self._request))

            if self._shutdown:
                Wakeup(lambda: asyncore.close_all())
            else:
                Wakeup()

        self._channel=None #need to break cycles?
        self._request=None
示例#36
0
 def run_server(self):
     logging.info("Starting server.")
     self.datasource = mapreducer.READER(FLAGS.reader)().read(FLAGS.input)
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
     logging.info("Number of input key value pairs: %d " % \
                  (len(self.datasource.keys())))
     self.bind(("", FLAGS.port))
     self.listen(1)
     logging.info("Starting listening on %d" % (FLAGS.port))
     try:
         asyncore.loop()
     except:
         asyncore.close_all()
         raise
     logging.info("Mapreduce done.")
     mapreducer.WRITER(FLAGS.writer)().write(self.taskmanager.results)
示例#37
0
    def _finish(self):
        self.channel.reply_code=self.status

        DebugLogger.log('A', id(self.channel), '%d %d' % (
                self.status, self.stdout.length))

        t=self._tempfile
        if t is not None:
            self.stdout.write((file_close_producer(t), 0))
        self._tempfile=None

        self.channel.sendStreamTerminator(FCGI_STDOUT)
        self.channel.sendEndRecord()
        self.stdout.close()
        self.stderr.close()

        if not self.channel.closed:
            self.channel.push_with_producer(LoggingProducer(self.channel,
                                                            self.stdout.length,
                                                            'log_request'), 0)
        if self._shutdownRequested():
            self.channel.push(ShutdownProducer(), 0)
            Wakeup(lambda: asyncore.close_all())
        else:
            self.channel.push(None,0)
            Wakeup()
        self.channel=None
示例#38
0
def startSocketServer_clearAll(closeAllSockets=True, stopAllPolling=True):
    """
    use this to stop all prior started servers
    """
    if closeAllSockets:
        asyncore.close_all()
    if stopAllPolling:
        global _pollers
        for p in _pollers:
            try:
                p.Stop
            except:
                import traceback
                traceback.print_exc()

        _pollers = []
示例#39
0
def socketServer_clearAll(closeAllSockets=True, stopAllPolling=True):
    """
    use this to stop all prior started servers
    """
    if closeAllSockets:
        asyncore.close_all()
    if stopAllPolling:
        global _pollers
        for p in _pollers:
            try:
                p.Stop
            except:
                import traceback
                traceback.print_exc()

        _pollers = []
示例#40
0
    def close(self):
        if not self._channel.closed:
            data = self._data.getvalue()
            l = len(data)
            DebugLogger.log('A', id(self._channel),
                            '%s %s' % (self._channel.reply_code, l))
            self._channel.push('%010d%s%010d' % (l, data, 0), 0)
            self._channel.push(
                LoggingProducer(self._channel, l, 'log_request'), 0)
            self._channel.push(
                CallbackProducer(lambda t=('E', id(self._channel)): apply(
                    DebugLogger.log, t)), 0)

            if self._shutdown:
                try:
                    r = self._shutdown[0]
                except:
                    r = 0
                config.ZSERVER_EXIT_CODE = r
                self._channel.push(ShutdownProducer(), 0)
                Wakeup(lambda: asyncore.close_all())
            else:
                self._channel.push(None, 0)
                Wakeup()
        self._data = None
        self._channel = None
示例#41
0
 def run_server(self):
     logging.info("Starting server.")
     self.datasource = mapreducer.READER(FLAGS.reader)().read(FLAGS.input)
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
     logging.info("Number of input key value pairs: %d " % \
                  (len(self.datasource.keys())))
     self.bind(("", FLAGS.port))
     self.listen(1)
     logging.info("Starting listening on %d" % (FLAGS.port))
     try:
         asyncore.loop()
     except:
         asyncore.close_all()
         raise
     logging.info("Mapreduce done.")
     mapreducer.WRITER(FLAGS.writer)().write(self.taskmanager.results)
示例#42
0
    def close(self):
        log('A', id(self._request),
            '%s %s' % (self._request.reply_code, self._bytes))
        if not self._channel.closed:
            self._channel.push(LoggingProducer(self._request, self._bytes), 0)
            self._channel.push(CallbackProducer(self._channel.done), 0)
            self._channel.push(
                CallbackProducer(lambda t=
                                 ('E', id(self._request)): apply(log, t)), 0)
            if self._shutdown:
                self._channel.push(ShutdownProducer(), 0)
                Wakeup()
            else:
                if self._close: self._channel.push(None, 0)
            Wakeup()
        else:
            # channel closed too soon

            self._request.log(self._bytes)
            log('E', id(self._request))

            if self._shutdown:
                Wakeup(lambda: asyncore.close_all())
            else:
                Wakeup()

        self._channel = None  #need to break cycles?
        self._request = None
示例#43
0
    def _finish(self):
        self.channel.reply_code=self.status

        DebugLogger.log('A', id(self.channel), '%d %d' % (
                self.status, self.stdout.length))

        t=self._tempfile
        if t is not None:
            self.stdout.write((file_close_producer(t), 0))
        self._tempfile=None

        self.channel.sendStreamTerminator(FCGI_STDOUT)
        self.channel.sendEndRecord()
        self.stdout.close()
        self.stderr.close()

        if not self.channel.closed:
            self.channel.push_with_producer(LoggingProducer(self.channel,
                                                            self.stdout.length,
                                                            'log_request'), 0)
        if self._shutdownRequested():
            self.channel.push(ShutdownProducer(), 0)
            Wakeup(lambda: asyncore.close_all())
        else:
            self.channel.push(None,0)
            Wakeup()
        self.channel=None
示例#44
0
    def proxy_loop(self):
        """Handle main loop and reconnection."""
        self.molo_client.sock_connect()
        while not self.is_exited:
            try:
                asyncore.loop()
            except asyncore.ExitNow as exc:
                logging.exception(exc)
                LOGGER.error("asyncore.loop exception")

            if not self.is_exited:
                asyncore.close_all()
                self.molo_client.sock_connect()
                time.sleep(RECONNECT_INTERVAL)
                LOGGER.info("moloserver reconnecting...")
        asyncore.close_all()
        LOGGER.debug("proxy exited")
示例#45
0
    def onShuttingDown(self):
        if self.isRunning:
            self.logger.info('Shutting down bot')
            if self.cmdHandler.monitor_thread:
                self.cmdHandler.monitor_thread.cancel()
            for user in self.users.values():
                if user.dccSocket:
                    user.dccSocket.handle_close()

            self.isRunning = False
            self.isReady = False
            self.isIdentified = False
            self.dccSocket.handle_close()
            self.socket.handle_close()

            # Just in case we missed any somehow
            asyncore.close_all()
示例#46
0
def mockup_smtp_server(bind=('localhost', 0)):
    SMTP_SERVER_LOCK = threading.Event()

    class MockupSMTPServer(smtpd.SMTPServer):
        def __init__(self, bind):
            super().__init__(bind, None, decode_data=False)
            self.server_address = self.socket.getsockname()[:2]
            SMTP_SERVER_LOCK.set()

        def process_message(*args, **kwargs):
            pass

    server = MockupSMTPServer(bind)
    thread = threading.Thread(target=asyncore.loop, daemon=True)
    thread.start()
    SMTP_SERVER_LOCK.wait()
    yield server, server.server_address
    asyncore.close_all()
示例#47
0
文件: pymorse.py 项目: chgrand/morse
 def close(self, cancel_async_services = False, wait_publishers = True):
     if wait_publishers:
         import time
         for stream in self.get_publisher_streams():
             while len(stream.producer_fifo) > 0:
                 time.sleep(0.001)
     if cancel_async_services:
         logger.info('Cancelling all running asynchronous requests...')
         ResponseCallback.cancel_all()
         self.executor.cancel_all()
     else:
         logger.info('Waiting for all asynchronous requests to complete...')
     self.executor.shutdown(wait = True)
     # Close all other asyncore sockets (StreanJSON)
     if Morse.poll_thread:
         Morse.poll_thread.syncstop(TIMEOUT)
     asyncore.close_all()
     Morse.poll_thread = None # in case we want to re-create
     logger.info('Done. Bye bye!')
示例#48
0
def main(argv):
    if '-d' in argv[1:]:
        logger.setLevel(logging.DEBUG)

    logger.debug("Hybrid node started...")
    hn = HybridNode()

    try:
        while 1:
            hn.synchronize()
    except KeyboardInterrupt:
        logger.info("Quit (Ctrl+C)")
    finally:
        del hn
        logger.info("Closing all connections")
        asyncore.close_all()

    logger.info("Bye!")
    return 0
示例#49
0
def main(argv):
    if '-d' in argv[1:]:
        logger.setLevel(logging.DEBUG)

    logger.debug("Hybrid node started...")
    hn = HybridNode()

    try:
        while 1:
            hn.synchronize()
    except KeyboardInterrupt:
        logger.info("Quit (Ctrl+C)")
    finally:
        del hn
        logger.info("Closing all connections")
        asyncore.close_all()

    logger.info("Bye!")
    return 0
示例#50
0
文件: pymorse.py 项目: hawesie/morse
 def close(self, cancel_async_services=False, wait_publishers=True):
     if wait_publishers:
         import time
         for stream in self.get_publisher_streams():
             while len(stream.producer_fifo) > 0:
                 time.sleep(0.001)
     if cancel_async_services:
         logger.info('Cancelling all running asynchronous requests...')
         ResponseCallback.cancel_all()
         self.executor.cancel_all()
     else:
         logger.info('Waiting for all asynchronous requests to complete...')
     self.executor.shutdown(wait=True)
     # Close all other asyncore sockets (StreanJSON)
     if Morse.poll_thread:
         Morse.poll_thread.syncstop(TIMEOUT)
     asyncore.close_all()
     Morse.poll_thread = None  # in case we want to re-create
     logger.info('Done. Bye bye!')
	def run(self):
		Logger.info("[WebApps] new process started")
		
		signal.signal(signal.SIGINT, signal.SIG_IGN)
		signal.signal(signal.SIGTERM, signal.SIG_IGN)
		
		# close inherited father pipes
		self.father_pipes[0].close()
		self.father_pipes[1].close()
		
		self.socks = Queue.Queue()
		
		self.clean_timer = threading.Timer(Config.process_timeout, self.clean)
		self.clean_timer.start()
		
		self.f_control = ControlFatherProcess(self)
		self.f_control.start()
		
		while self.f_control.is_alive() or not self.socks.empty():
			try:
				sock = self.socks.get(timeout=0.01)
				if Config.connection_secure:
					ssl_conn = SSL.Connection(self.ssl_ctx, sock)
					Logger.debug("[WebApps] new connection => %s" % str(ssl_conn.getpeername()))
					ssl_conn.set_accept_state()
					ProtocolDetectDispatcher(ssl_conn, self.f_control, self.ssl_ctx)
				else:
					HttpProtocolDetectDispatcher(sock, self.f_control)
			except (IOError, Queue.Empty):
				continue
			
			# reload asyncore if stopped
			if self.is_sleeping():
				# timeout needed for more SSL layer reactivity
				self.t_asyncore = threading.Thread(target=lambda:asyncore.loop(timeout=0.01))
				self.t_asyncore.start()
		
		if not self.is_sleeping():
			asyncore.close_all()
			self.t_asyncore.join()
		
		Logger.info("[WebApps] child process stopped")
示例#52
0
    def run(self):
        Logger.info("[WebApps] new process started")

        signal.signal(signal.SIGINT, signal.SIG_IGN)
        signal.signal(signal.SIGTERM, signal.SIG_IGN)

        # close inherited father pipes
        self.father_pipes[0].close()
        self.father_pipes[1].close()

        self.socks = Queue.Queue()

        self.clean_timer = threading.Timer(Config.process_timeout, self.clean)
        self.clean_timer.start()

        self.f_control = ControlFatherProcess(self)
        self.f_control.start()

        while self.f_control.is_alive() or not self.socks.empty():
            try:
                sock = self.socks.get(timeout=0.01)
                if Config.connection_secure:
                    ssl_conn = SSL.Connection(self.ssl_ctx, sock)
                    Logger.debug("[WebApps] new connection => %s" % str(ssl_conn.getpeername()))
                    ssl_conn.set_accept_state()
                    ProtocolDetectDispatcher(ssl_conn, self.f_control, self.ssl_ctx)
                else:
                    HttpProtocolDetectDispatcher(sock, self.f_control)
            except (IOError, Queue.Empty):
                continue

                # reload asyncore if stopped
            if self.is_sleeping():
                # timeout needed for more SSL layer reactivity
                self.t_asyncore = threading.Thread(target=lambda: asyncore.loop(timeout=0.01))
                self.t_asyncore.start()

        if not self.is_sleeping():
            asyncore.close_all()
            self.t_asyncore.join()

        Logger.info("[WebApps] child process stopped")
示例#53
0
    def stop(self, signum=None, frame=None):
        """Stop the server.
        """
        if signum is not None:
            msg = "caught %s, shutting down..." % STOP_SIGNALS[signum]
            sys.stderr.write(msg)
            sys.stderr.flush()

        if hasattr(self.responder, 'stop'):
            if inspect.ismethod(self.responder.stop):
                self.responder.stop()

        self.task_dispatcher.shutdown()
        asyncore.close_all()

        if (not WINDOWS) and (self.sockfam == socket.AF_UNIX):
            if os.path.exists(self.address):
                os.unlink(self.address)
        sys.stderr.write("done\n")
        sys.stderr.flush()
示例#54
0
 def closeall_check(self, usedefault):
     l = []
     testmap = {}
     for i in range(10):
         c = dummychannel()
         l.append(c)
         self.assertEqual(c.socket.closed, False)
         testmap[i] = c
     if usedefault:
         socketmap = asyncore.socket_map
         try:
             asyncore.socket_map = testmap
             asyncore.close_all()
         finally:
             testmap, asyncore.socket_map = asyncore.socket_map, socketmap
     else:
         asyncore.close_all(testmap)
     self.assertEqual(len(testmap), 0)
     for c in l:
         self.assertEqual(c.socket.closed, True)
示例#55
0
文件: main.py 项目: klange/sppoker
def HandleExit(soft=False):
    global client, thread, keepal, renderer, eventh, exit, promptWait, console
    try: client.send("DROP")
    except: pass
    if soft:
        try: console.visible = False
        except: pass
        exit = True
        return
    try: server.server.stop()
    except: pass
    client.close()
    asyncore.close_all()
    exit = True
    kill = True
    pscreen.UnloadScreen()
    pygame.quit()
    promptWait = False
    print "EXITING"
    os._exit(1)
示例#56
0
    def stop(self, signum=None, frame=None):
        """Stop the server.
        """
        if signum is not None:
            logger.debug("caught %s" % STOP_SIGNALS[signum])
        logger.info("shutting down...")

        if hasattr(self.responder, 'stop'):
            if inspect.ismethod(self.responder.stop):
                self.responder.stop()

        self.task_dispatcher.shutdown()
        asyncore.close_all()

        if (not WINDOWS) and (self.config.sockfam == socket.AF_UNIX):
            if not os.path.exists(self.config.address):
                logger.debug("what happened to our socket?")
            os.unlink(self.config.address)

        logger.info("httpy stopped")
    def ping_server(self):
        """Send ping to server every ping_interval."""
        while not self.is_exited:
            try:
                if self.molo_client:
                    self.set_ping_buffer(self.molo_client.ping_server_buffer())
                time.sleep(self.ping_interval)

                time_interval = time.time() - self.last_activate_time
                LOGGER.debug("data interval: %f", time_interval)
                if time_interval > PROXY_TCP_CONNECTION_ACTIVATE_TIME:
                    LOGGER.info("connection timeout, reconnecting server")
                    self.molo_client.handle_close()
                    self.reset_activate_time()

            except Exception as exc:
                print("ping_server(): " + str(exc))
                asyncore.close_all()
                self.molo_client.sock_connect()
                time.sleep(RECONNECT_INTERVAL)
                LOGGER.info("moloserver reconnecting...")
示例#58
0
    def main_loop(self):
        """Handle main loop and reconnection."""
        self.molo_client.sock_connect()
        while not self.is_exited:
            try:
                asyncore.loop(map=self.async_map)
            except asyncore.ExitNow as exc:
                logging.exception(exc)
                LOGGER.error("asyncore.loop exception")

            if not self.is_exited:
                try:
                    asyncore.close_all()
                    self.molo_client.sock_connect()
                    time.sleep(RECONNECT_INTERVAL)
                    LOGGER.info("moloserver reconnecting...")
                except Exception as exc:
                    print("main_loop(): " + str(exc))
                    LOGGER.info("reconnect failed, retry...")
                    time.sleep(RECONNECT_INTERVAL)
        asyncore.close_all()
        LOGGER.debug("main loop exited")
示例#59
0
def debugging_server(serv, serv_evt, client_evt):
    serv_evt.set()
    try:
        if hasattr(select, 'poll'):
            poll_fun = asyncore.poll2
        else:
            poll_fun = asyncore.poll
        n = 1000
        while asyncore.socket_map and n > 0:
            poll_fun(0.01, asyncore.socket_map)
            if client_evt.is_set():
                serv.close()
                break
            n -= 1
    except socket.timeout:
        pass
    finally:
        if not client_evt.is_set():
            time.sleep(0.5)
            serv.close()
        asyncore.close_all()
        serv_evt.set()
示例#60
0
 def stop(self, signum=None, frame=None):
     self.alive = False
     logger.info("Number of Threads: {0}".format(threading.activeCount()))
     try:
         asyncore.close_all(self.socket_map)
     except:
         pass
     try:
         self.scheduler.stop()
     except:
         pass
     try:
         self._plugins.stop()
     except:
         pass
     time.sleep(0.5)
     if threading.active_count() > 1:
         for thread in threading.enumerate():
             logger.info("Thread: %s, still alive" % thread.name)
     try:
         os.remove(PID_FILE)
     except OSError, e:
         logger.critical("Could not remove pid file: {0} - {1}".format(PID_FILE, e))