def run(self):
		print "Service Started at: "+ self._address +":"+ str(self._port)
		self._server = LocalSMTPServer((self._address, self._port), None)
		self._server.set_parent(self)
		self._stopevent.clear()
		while not self._stopevent.isSet():
			asyncore.loop(timeout=0.01, count=1)
 def run(self):
     print "Starting " + self.name
     if self.name == 'socket':
         asyncore.loop()
     elif self.name == 'MySQLpoll':
         mysqlpoll()
     print "Exiting " + self.name
Example #3
0
    def _runImpl(self):
        self._server = NimbleServer(router=self._router)
        self._SERVER_THREAD = self
        self._ACTIVATING    = False

        NimbleEnvironment.log(NimbleServerThread._ACTIVE_MESSAGE)
        asyncore.loop()
 def run(self):
   """Drives the communication service with the remote inspector."""
   try:
     while asyncore.socket_map:
       asyncore.loop(timeout=1, count=1, use_poll=True)
   except KeyboardInterrupt:
     pass
Example #5
0
File: tests.py Project: 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()
Example #6
0
    def start(self):
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        try:
            self.bind((self.host, self.port))
        except socket.error:
            sys.stderr.write("Unable bind socket to %s:%s\n" % (self.host, self.port))
            sys.exit(1)

        self.listen(500)

        # Check for a pidfile to see if the daemon already runs
        try:
            pf = file(self.pidfile,'r')
            pid = int(pf.read().strip())
            pf.close()
        except IOError:
            pid = None

        if pid:
            message = "pidfile %s already exist. Daemon already running?\n"
            sys.stderr.write(message % self.pidfile)
            sys.exit(1)

        # Start the daemon
        self.daemonize()
        asyncore.loop(use_poll=True, timeout=0.1)
    def run(self):
        with shared.printLock:
            print "Asyncore thread started"

        while True:
            asyncore.loop(timeout=1) # Despite the horrible parameter name, this function will not timeout until all channels are closed.
            time.sleep(1)
Example #8
0
    def __init__(self, host, port):
        ''' Communicates between the GUI and server. '''

        async_chat.__init__(self)
        self.data = []
        self.ID = -1   
        self.ch = ClientHelper(self)
        
        while True:
            if host == None:
                host = raw_input("Enter the IP of the server (leave blank for localhost) >")
                if len(host.strip()) == 0:
                    host = "localhost"
            try:
                print "connecting to...",host
                self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
                self.connect((host,port))
                self.set_terminator("\n")
                self.register()
                
                try: asyncore.loop()
                except KeyboardInterrupt: print
                break
            except Exception:
                print "Cannot connect, ", Exception
                host = None
def setup_worker(fp, server_addr, port, counter=0, verbose=False,
                             error_profile=None):
    """ This routine starts the worker.

    fp: fp that should be used to store intermediate data

    server_addr: ip address of server
    
    port: port on server to connect to
    
    counter: counts each round of denoising

    verbose: verbose flag

    error_profile: path to error profile .dat file
    """
    if fp==None:
        raise ValueError, "setup_worker needs file path for worker"
    log_fh=None
    if verbose:
        dir = dirname(fp+".log")
        if not exists(dir):
            makedirs(dir)
        log_fh = open(fp+".log","a",0)

    #use local tmp if possible
    new_fp = fp
    if exists("/tmp"):
        new_fp = "/tmp/" + split(fp)[1]    
    
    #set up the workers and start the loop
    worker = DenoiseWorker(new_fp, server_addr, port, counter=counter,
                            log_fh=log_fh, error_profile=error_profile)
    #this asyncore loop will run until the server closes the connection
    loop()
def monitor(intent, project, dispatch, verbose):
    """ Monitor of mcaf observation files. 
    Scans that match intent and project are searched (unless --dispatch).
    Blocking function.
    """

    # Set up verbosity level for log
    if verbose:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    # Report start-up information
    logger.info('* * * * * * * * * * * * * * * * * * * * *')
    logger.info('* * * VLA Dispatcher is now running * * *')
    logger.info('* * * * * * * * * * * * * * * * * * * * *')
    logger.info('*   Looking for intent = %s, project = %s' % (intent, project))
    logger.debug('*   Running in verbose mode')
    if dispatch:
        logger.info('*   Running in dispatch mode. Will dispatch obs commands.')
    else:
        logger.info('*   Running in listening mode. Will not dispatch obs commands.')
    logger.info('* * * * * * * * * * * * * * * * * * * * *\n')

    # This starts the receiving/handling loop
    controller = FRBController(intent=intent, project=project, dispatch=dispatch, verbose=verbose)
    obsdoc_client = mcaf_library.ObsdocClient(controller)
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        # Just exit without the trace barf
        logger.info('Escaping mcaf_monitor')
Example #11
0
 def start(self):
     logging.info("Starting client...")
     self.local.start()
     logging.info("Awaiting local connections...")
     self.remote.start()
     logging.info("Client started. Connecting to [%s:%d]..." % (self.address, self.port))
     asyncore.loop()
Example #12
0
def start_smtp_server(queue):
    server = TestingSMTPServer(SMTP_SERVER, None, queue)
    try:
        asyncore.loop()
    finally:
        queue.close()
        server.close()
def unix_mainloop(repodir, port, logfilename):

    try:
        port = int(port)
    except ValueError:
        port = None

    if not os.path.isdir(repodir):
        sys.stderr.write("Specified directory, %s, is not a directory!\n" % repodir)
        usage()
    elif port is None:
        sys.stderr.write("Bad port number, %s, specified.\n", portnum)
        usage()

    try:
        repo = start_angel(repodir, port, logfilename)
    except:
        sys.stderr.write("%s: exception initializing angel:\n%s" % (
            time.ctime(), ''.join(traceback.format_exception(*sys.exc_info()))))
        sys.exit(1)

    # Finally, start up the server loop!  This loop will not exit until
    # all clients and servers are closed.  You may cleanly shut the system
    # down by sending SIGINT (a.k.a. KeyboardInterrupt).

    from uplib.plibUtil import note
    while True:
        try:
            asyncore.loop()
        except (KeyboardInterrupt, SystemExit), x:
            note(4, "Exited from main loop due to exception:\n%s", ''.join(traceback.format_exception(*sys.exc_info())))
            raise
        except:
Example #14
0
    def loop(self):
        """The main loop of bzrflag.

        Checks events, updates positions, and draws to the screen until
        the pygame window is closed, KeyboardInterrupt, or System Exit.
        """
        self.running = True
        self.start_servers()
        if not self.config['test']:
            self.display.setup()
        try:
            while self.running:
                if self.game.end_game:
                    break
                asyncore.loop(constants.LOOP_TIMEOUT, count=1)
                self.update_game()
                if not self.config['test']:
                    self.update_graphics()
                    self.display.update()
        except KeyboardInterrupt:
            pass
        finally:
            final_scores = '\nFinal Score\n'
            for team in self.game.teams:
                team_total = self.game.teams[team].score.total()
                final_scores += 'Team %s: %d\n' % (team, team_total)
            if not self.config['test']:
                print final_scores
Example #15
0
 def handle_noargs(self, **options):
     receiver = EmailReceiver('localhost', 7999)
     
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         pass
Example #16
0
	def start(self, sock, force):
		self.__sock = sock
		# Remove socket
		if os.path.exists(sock):
			logSys.error("Fail2ban seems to be already running")
			if force:
				logSys.warning("Forcing execution of the server")
				os.remove(sock)
			else:
				raise AsyncServerException("Server already running")
		# Creates the socket.
		self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM)
		self.set_reuse_addr()
		try:
			self.bind(sock)
		except Exception:
			raise AsyncServerException("Unable to bind socket %s" % self.__sock)
		AsyncServer.__markCloseOnExec(self.socket)
		self.listen(1)
		# Sets the init flag.
		self.__init = True
		# TODO Add try..catch
		# There's a bug report for Python 2.6/3.0 that use_poll=True yields some 2.5 incompatibilities:
		if (sys.version_info >= (2, 7) and sys.version_info < (2, 8)) \
		   or (sys.version_info >= (3, 4)): # if python 2.7 ...
			logSys.debug("Detected Python 2.7. asyncore.loop() using poll")
			asyncore.loop(use_poll=True) # workaround for the "Bad file descriptor" issue on Python 2.7, gh-161
		else:
			asyncore.loop(use_poll=False) # fixes the "Unexpected communication problem" issue on Python 2.6 and 3.0
Example #17
0
    def test_connection_attributes(self):
        server = BaseServer(self.family, self.addr)
        client = BaseClient(self.family, server.address)

        # we start disconnected
        self.assertFalse(server.connected)
        self.assertTrue(server.accepting)
        # this can't be taken for granted across all platforms
        #self.assertFalse(client.connected)
        self.assertFalse(client.accepting)

        # execute some loops so that client connects to server
        asyncore.loop(timeout=0.01, use_poll=self.use_poll, count=100)
        self.assertFalse(server.connected)
        self.assertTrue(server.accepting)
        self.assertTrue(client.connected)
        self.assertFalse(client.accepting)

        # disconnect the client
        client.close()
        self.assertFalse(server.connected)
        self.assertTrue(server.accepting)
        self.assertFalse(client.connected)
        self.assertFalse(client.accepting)

        # stop serving
        server.close()
        self.assertFalse(server.connected)
        self.assertFalse(server.accepting)
Example #18
0
def multi_ping_query(hosts, timeout=1, step=512, ignore_errors=False):
    """
    Sends multiple icmp echo requests at once.

    "hosts" is a list of ips or hostnames which should be pinged.
    "timeout" must be given and a integer or float greater than zero.
    "step" is the amount of sockets which should be watched at once.

    See the docstring of "PingQuery" for the meaning of "ignore_erros".

    """
    results, host_list, id = {}, [], 0
    for host in hosts:
        try:
            host_list.append(socket.gethostbyname(host))
        except socket.gaierror:
            results[host] = None
    while host_list:
        sock_list = []
        for ip in host_list[:step]: # select supports only a max of 512
            id += 1
            sock_list.append(PingQuery(ip, id, timeout, ignore_errors))
            host_list.remove(ip)
        # Remember to use a timeout here. The risk to get an infinite loop
        # is high, because noone can guarantee that each host will reply!
        asyncore.loop(timeout)
        for sock in sock_list:
            results[sock.get_host()] = sock.get_result()
    return results
Example #19
0
 def run(self, host, port=6667):
   if self.debug:
     message = 'Connecting to %s:%s...' % (host, port)
     print >> sys.stderr, message,
   s = None
   for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
     af, socktype, proto, canonname, sa = res
     try:
       s = socket.socket(af, socktype, proto)
     except socket.error as msg:
       s = None
       continue
     try:
       self.addr = sa
       s.connect(sa)
     except socket.error as msg:
       s.close()
       s = None
       continue
     break
   if s is None:
     if self.debug:
       print >> sys.stderr, 'could not open socket'
     sys.exit(1)
   # Lets hope asyncore doesnt change anything in create_socket and connect
   # Emulating http://hg.python.org/cpython/file/2.7/Lib/asyncore.py#l295
   s.setblocking(0)
   self.set_socket(s)
   # Emulating http://hg.python.org/cpython/file/2.7/Lib/asyncore.py#l344
   self.connected = False
   self.connecting = True
   self.handle_connect_event()
   try: asyncore.loop()
   except KeyboardInterrupt:
     sys.exit()
Example #20
0
def start():
    try:
        server = Server('localhost', 0)
        write_file(str(server.port_))
        asyncore.loop()  # blocks
    except Exception as err:
        logger.exception(err)
Example #21
0
def asyncore_call():
    try:
        asyncore.loop(count=1)
    except:
        LOG_CURRENT_EXCEPTION()

    BigWorld.callback(0.1, asyncore_call)
Example #22
0
 def _listeners_and_readers_loop(self):
     while self._listeners_and_readers or not self.stopping:
         try:
             asyncore.loop(count=1, timeout=1,
                 map=self._listeners_and_readers)
         except:
             pass
Example #23
0
def run():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ht")
    except getopt.error as msg:
        print(str(msg) + "\n\n" + __doc__, file=sys.stderr)
        sys.exit()
    for opt, arg in opts:
        if opt == "-h":
            print(__doc__, file=sys.stderr)
            sys.exit()
        elif opt == "-t":
            state.isTest = True
            state.runTestServer = True
    state.createWorkers()
    if state.runTestServer:
        print("Running a test SMTP server on port 8025...")
        TestListener()
        asyncore.loop()
    else:
        state.isTest = True
        state.buildServerStrings()
        testSocketMap = {}

        def runTestServer():
            TestListener(socketMap=testSocketMap)
            asyncore.loop(map=testSocketMap)

        def runProxy():
            trainer = SMTPTrainer(Classifier(), state)
            BayesSMTPProxyListener("localhost", 8025, ("", 8026), trainer)
            Dibbler.run()

        _thread.start_new_thread(runTestServer, ())
        _thread.start_new_thread(runProxy, ())
        sb_test_support.unittest_main(argv=sys.argv + ["suite"])
Example #24
0
 def start(self):
     self.log.debug('Starting server...')
     
     try: 
         asyncore.loop()
     finally:
         self.log.debug('Server closed.')
Example #25
0
    def initiate_connect(self, host, port):
        if self.verbose:
            message = 'Connecting to %s:%s...' % (host, port)
            print >> sys.stderr, message,

        if self.use_ssl:
            self.send = self._ssl_send
            self.recv = self._ssl_recv

        for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.create_socket(af,socktype)
            except socket.error as msg:
                continue
            try:
                self.connect(sa)
            except socket.error as msg:
                self.close()
                continue
            break
        else:
            raise Exception("No connectivity")

        try: asyncore.loop()
        except KeyboardInterrupt:
            sys.exit()
        except Exception, e:
            print '[asyncore]', e
Example #26
0
def loop():
	global scheduler
	while True:
		if scheduler.empty():
			asyncore.loop(count=1)
		else:
			scheduler.run()
    def handle_noargs(self, **options):
        from django.conf import settings

        import asyncore
        import socket

        host_port = (settings.EMAIL_HOST, settings.EMAIL_PORT)

        try:
            server = MailDebuggingServer(host_port, None)
        except socket.error:
            raise CommandError('Could not set up the mail server at %s:%d.\n'
                % host_port
                + 'Make sure that you can actually host a server using the\n'
                + 'current values for settings.EMAIL_HOST and settings.EMAIL_PORT.')

        print('Mail debugging server is running at %s:%d' % host_port)
        print('Emails from this Django site will appear here instead of being sent.')
        print('Quit the server with CONTROL-C.')

        try:
            asyncore.loop()
        except KeyboardInterrupt:
            # Print a blank line after the ^C. This looks nicer.
            print('')

            pass
Example #28
0
def join_game(screen, host, port, team):
    try:
        sock = socket.socket()
        sock.connect((host, port))

        mapname = [None]
        other_team = [None]
        spawns = [None]

        def handler(type, data):
            if type == "MAP":
                mapname[0] = data
            if type == "TEAM":
                other_team[0] = deserialize_team(data)
            if type == "SPAWNS":
                spawns[0] = deserialize_spawns(data)

        conn = Messager(handler, sock)
        conn.push_message("TEAM", serialize_team(team))

        while mapname[0] is None or other_team[0] is None or spawns[0] is None:
            asyncore.loop(count=1, timeout=0.1)
    except:
        sys.excepthook(*sys.exc_info())
        return

    teams = [("Player 1", conn, other_team[0]), ("Player 2", None, team)]

    game = XadirMain(screen, mapname=mapname[0])
    game.load_resources()
    game.init_teams(teams, spawns[0])
    game.main_loop()
Example #29
0
def DevDemo(args):
    args = make_argument_parser().parse_args()
    if not args.srv_host or not args.uuidfile:
        print make_argument_parser().parse_args(['-h'])
        exit(1)


    errqueue = Queue()
    statqueue = Queue()
    errlog = ErrLog('DevDemo')
    statlog = StatLog('DevDemo')
    errworker = WorkerThread(errqueue,errlog,)
    errworker.start()
    statworker = WorkerThread(statqueue,statlog)
    statworker.start()

    host = ()
    try:
        d = args.srv_host.index(':')
        host = (args.srv_host[:d],int(args.srv_host[d:]))
    except:
        host = (args.srv_host,3478)
    uulist = read_uuid_file(args.uuidfile)
    for uid in uulist:
        apps = DevSocket(host,uid,errqueue,statqueue)
    asyncore.loop(timeout=2)
Example #30
0
 def ThreadMain(self):
   self._logger.info('Starting up')
   while not self._quit:
     asyncore.loop(timeout=0.5, count=1)
     if not asyncore.socket_map:
       time.sleep(0.5)
   self._logger.info('Quitting')
Example #31
0
def main():
    print("Server is running...")
    svr = Server()
    asyncore.loop()
Example #32
0
class Server(ServerBase):
    """A standalone HTTP server.
    """

    # ServerBase boilerplate
    channel_class = Channel
    SERVER_IDENT = 'httpy'

    def __init__(self, responder, address, threads, uid):
        """Takes an IResponder.
        """

        #validate_input(responder, address, threads, uid)

        self.responder = responder
        self.address = address
        if address[0] in ('/', '.'):
            self.sockfam = socket.AF_UNIX
        else:
            self.sockfam = socket.AF_INET
        self.threads = threads
        self.uid = uid

        # Set up signal handling so we can shut down cleanly.
        # ===================================================

        for sig in STOP_SIGNALS:
            signal.signal(sig, self.stop)

        # Satisfy ServerBase requirements.
        # ================================

        asyncore.dispatcher.__init__(self)
        self.adj = default_adj
        if (not WINDOWS) and (self.sockfam == socket.AF_UNIX):
            # The default adjustment doesn't apply here, and triggers an error.
            self.adj.socket_options = []
        self.verbose = False
        task_dispatcher = ThreadedTaskDispatcher()
        task_dispatcher.setThreadCount(self.threads)
        self.task_dispatcher = task_dispatcher
        self.logger = logging.getLogger(self.__class__.__name__)

    def start(self):
        """Start the server.
        """

        # Bind to an address.
        # ===================

        self.create_socket(self.sockfam, socket.SOCK_STREAM)
        self.set_reuse_addr()
        if (not WINDOWS) and (self.sockfam == socket.AF_UNIX):
            if os.path.exists(self.address):
                os.unlink(self.address)

        timeout = time.time() + 10
        while 1:
            if time.time() < timeout:
                try:
                    self.bind(self.address)
                    break
                except socket.error, err:
                    time.sleep(0.5)
            else:
                raise err

        # Switch user accounts.
        # =====================

        if self.uid:
            os.setreuid(self.uid)

        # Start listening.
        # ================

        self.accept_connections()

        if self.sockfam == socket.AF_INET:
            addr, port = self.socket.getsockname()
            msg = "port %s" % port
        elif (not WINDOWS) and (self.sockfam == socket.AF_UNIX):
            msg = "%s" % self.address
        print >> sys.stderr, "HTTP server started on %s" % msg

        asyncore.loop(timeout=5)
Example #33
0
    # Become nobody
    if options.setuid:
        try:
            import pwd
        except ImportError:
            print >> sys.stderr, \
                  'Cannot import module "pwd"; try running with -n option.'
            sys.exit(1)
        nobody = pwd.getpwnam('nobody')[2]
        try:
            os.setuid(nobody)
        except OSError, e:
            if e.errno != errno.EPERM: raise
            print >> sys.stderr, \
                  'Cannot setuid "nobody"; try running with -n option.'
            sys.exit(1)
    classname = options.classname
    if "." in classname:
        lastdot = classname.rfind(".")
        mod = __import__(classname[:lastdot], globals(), locals(), [""])
        classname = classname[lastdot + 1:]
    else:
        import __main__ as mod
    class_ = getattr(mod, classname)
    proxy = class_((options.localhost, options.localport),
                   (options.remotehost, options.remoteport))
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        pass
Example #34
0
 def profile_loop ():
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         pass
Example #35
0
def notifier():
  notifier = pyinotify.AsyncNotifier(wm, EventHandler())
  wdd = wm.add_watch(basepath, mask, rec=False)
  asyncore.loop()
Example #36
0
def sendmsg(msg):
    Client(msg)
    asyncore.loop(1)
Example #37
0
 def start(self):
     self.server = SMTPServerMock(("0.0.0.0", 4444),
                                  None,
                                  enable_SMTPUTF8=True)
     asyncore.loop()
Example #38
0
File: client.py Project: PMSD/KILL-
class CoreClient(asynchat.async_chat, threading.Thread):
    """This is a hybrid class of asyn_chat and Thread. It deals with
    the communications with the server. An instance of this class
    will be a component of the Client and will be runned in a different
    thread apart from the GUI.
    """
    def __init__(self, address):
        """*Note*: the connection is established in the `run` routine 
        instead of __init__.
        """

        asynchat.async_chat.__init__(self)
        threading.Thread.__init__(self)

        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_address = address

        self.set_terminator(TERM)

        # incomming data buffer
        self.data = []

        # command dictionary, dispatches the command from the server
        self.cmd_dict = {
            'OPEN': self._cmd_open,
            'CLOSE': self._cmd_close,
            'VALUE': self._cmd_value,
            'PARENT': self._cmd_parent,
            'PATH': self._cmd_path
        }

        # algorithm dictionary
        self.algo_dict = {
            ASTARM: 'ASTARM',
            ASTARE: 'ASTARE',
            ASTARC: 'ASTARC',
            DIJKSTRA: 'DIJKSTRA',
            BDBFS: 'BDBFS'
        }

    def run(self):
        """Overides the run method of threading.Thread
        """

        # try to connect the server every COUNTDOWN_COOLDOWN seconds
        # if not connected
        while not self.connected:
            try:
                self.connect(self.server_address)
            except Exception, e:
                print 'Unable to connect', self.server_address
                print 'Retry in %d seconds' % COUNTDOWN_COOLDOWN
                time.sleep(COUNTDOWN_COOLDOWN)

                self.close()
                self.create_socket(socket.AF_INET, socket.SOCK_STREAM)

        print 'connected with server at', self.server_address

        # begin the main loop
        try:
            asyncore.loop()
        except KeyboardInterrupt:
            self.close()
            raise SystemExit
Example #39
0
 def run(self):
     logger.debug("Thread Start")
     l = Listener(self._config)
     asyncore.loop()
Example #40
0
    def __str__(self):
        return "%s:%d" % (self.host, self.port)


if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG, format="%(name)s %(message)s")

    clients = []
    socket_map = {}

    HTTP_HOST = "www.baidu.com"
    HTTP_PORT = 80

    WHOIS_HOST = "36.55.244.17"
    WHOIS_PORT = 43

    for line in sys.stdin:
        line = line.strip()
        if not line:
            continue
        host, port = line.split(":")
        clients.append(
            AsyncHTTPTunnelClient(host, port, WHOIS_HOST, WHOIS_PORT, "whois",
                                  socket_map))
        #clients.append(AsyncHTTPTunnelClient(host, port, HTTP_HOST, HTTP_PORT, "http", socket_map))

    asyncore.loop(timeout=1, use_poll=True, map=socket_map)

    for c in clients:
        print c.buffer.getvalue()
def get_flowgram_distances_on_cluster(id,
                                      flowgram,
                                      flowgrams,
                                      fc,
                                      ids,
                                      num_cores,
                                      num_flows,
                                      spread,
                                      client_sockets=[]):
    """Computes distance scores of flowgram to all flowgrams in parser.

    id: The flowgram identifier, also used to name intermediate files
    
    flowgram: This flowgram is used to filter all the other flowgrams

    flowgrams: iterable filehandle of flowgram file

    fc: a sink of flowgrams, which serves as source in the next round

    ids: list of flowgram ids that should be used from flowgrams

    num_cores: number of cpus

    num_flows: Number of flows in parser

    client_sockets: A list of open sockets for client-server communication
    
    spread: historical distribution of processing runtimes

    """
    epoch = time()

    check_flowgram_ali_exe()

    qiime_config = load_qiime_config()
    min_per_core = int(qiime_config['denoiser_min_per_core'])
    CLOUD = not qiime_config['cloud_environment'] == "False"
    #if using from future import division this has to be checked,
    #as we want true integer division here

    per_core = max(min_per_core, (num_flows / num_cores) + 1)
    names = []
    scores = []

    #Need to call this here, since we iterate over the same iterator repeatedly.
    #Otherwise the call in ifilter will reset the iterator by implicitely  calling __iter__.
    #test if iter does the same
    flowgrams_iter = flowgrams.__iter__()
    #prepare input files and commands
    #synchronous client-server communication

    workload = compute_workload(num_cores, num_flows, spread)

    debug_count = 0
    for i in range(num_cores):
        socket = client_sockets[i]
        #send master flowgram to file first
        send_flowgram_to_socket(id, flowgram, socket)

        if (workload[i] < 1):
            #no data left for this poor guy
            save_send(socket, "--END--")
            continue
        else:
            # Then add all others which are still valid, i.e. in ids
            for (k, f) in (izip(
                    range(workload[i]),
                    ifilter(lambda f: ids.has_key(f.Name), flowgrams_iter))):
                fc.add(f)
                send_flowgram_to_socket(k, f, socket, trim=False)
                names.append(f.Name)
                debug_count += 1
            #send the termination signal
            save_send(socket, "--END--")

    #asynchronous client-server communication
    #ClientHandlers write data in results
    results = [None] * num_cores  #
    timing = [0.0 for x in xrange(num_cores)]
    for i in range(num_cores):
        socket = client_sockets[i]
        ClientHandler(socket, i, results, timing)
    loop()
    #end asynchronous loop

    spread = adjust_processing_time(num_cores, workload, timing, epoch)

    #flatten list
    scores = [item for list in results for item in list]

    if (debug_count != len(scores)):
        raise RuntimeError,"Something bad has happened! I received less " +\
            "alignment scores %d than there are flowgrams %d. Most likely this " \
            % (len(scores), debug_count)+\
            "means that the alignment program is not setup correctly or corrupted. "+\
            "Please run the test scripts to figure out the cause of the error."

    return (scores, names, fc)
Example #42
0
def main():
    global remote_configuration_command_id
    # FPS debug
    last_time = time.time()
    frame_counter = 0
    fps = 0
    fps_counter = 0
    # Load default settings
    load_configuration_file()
    # Controller start
    controller = main_controller()
    controller.reload_settings()
    # Servers start
    remote_configuration_server("10.42.0.1", 6000)
    tserver = telemetry_server("10.42.0.1", 7001)
    # Data logger
    datalogger = my_datalogger.datalogger("datalogger")
    # Main loop
    counter = 0
    while True:
        # Non blocking call
        asyncore.loop(timeout=0, count=1)
        #  Process command ID
        if remote_configuration_command_id == 1:
            controller.quit()
            break
        elif remote_configuration_command_id == 2:
            controller.init_ai()
        elif remote_configuration_command_id == 3:
            print("Start recording...")
            controller.start_recording()
        elif remote_configuration_command_id == 4:
            print("Stop recording!")
            controller.stop_recording()
        elif remote_configuration_command_id == 5:
            print("Start running...")
            controller.start_running()
            datalogger.start()
        elif remote_configuration_command_id == 6:
            print("Stop running!")
            datalogger.stop()
            controller.stop_running()
        elif remote_configuration_command_id == 10:
            controller.reload_settings()
        remote_configuration_command_id = 0
        # Process
        controller.process()
        # Telemetry
        counter += 1
        msg = str(counter) + ';' + str(float(
            controller.direction)) + ';' + str(float(
                controller.throttle)) + ';' + str(
                    float(controller.actual_error))
        tserver.sendTelemetry(msg)
        # Data logger
        log_list = []
        log_list.append(float(fps))
        log_list.append(float(controller.lidar_distance_gauche))
        log_list.append(float(controller.lidar_distance_droit))
        log_list.append(float(controller.actual_error))
        log_list.append(float(controller.positional_error))
        log_list.append(float(controller.pid_wall))
        log_list.append(float(controller.direction))
        log_list.append(float(controller.throttle))
        log_list.append(float(controller.mode))
        datalogger.record(log_list)
        # FPS debug
        frame_counter += 1
        if time.time() >= last_time + 1.0:
            last_time = time.time()
            fps = frame_counter
            frame_counter = 0
        # Local Debug
        if fps_counter % 60 == 0:
            print("fps:" + str(fps) + "   DIR:" + str(controller.direction) +
                  "   THR:" + str(controller.throttle) + "   MODE:" +
                  str(controller.mode) + "   LiG:" +
                  str(controller.lidar_distance_gauche) + "   LiD:" +
                  str(controller.lidar_distance_droit))
        fps_counter += 1
Example #43
0
 def asyncore_thread(self):
     asyncore.loop(map=self.channel_map)
 def loop(self):
     asyncore.loop()
Example #45
0
    def collect_incoming_data(self, data):  #读取和暂存数据
        self.data.append(data.decode())

    def found_terminator(self):  #遇到结束符时处理数据
        line = ''.join(self.data)  #内容整合为一行
        self.data = []  #清空数据列表
        print(line)  #打印


class ChatServer(dispatcher):
    def __init__(self, port):
        dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)  #指定要创建的套接字类型
        self.set_reuse_addr()  #地址重用
        self.bind(('', port))  #将服务器关联到特定地址
        self.listen(5)  #监听连接,最大连接数为5
        self.sessions = []

    def handle_accept(self):
        conn, addr, = self.accept()  #允许客户端连接,返回套接字和地址
        self.sessions.append(ChatSession(conn))
        print('Connection attempt from', addr[0])


if __name__ == '__main__':
    s = ChatServer(PORT)
    try:
        asyncore.loop()  #启动监听循环
    except KeyboardInterrupt:
        pass
Example #46
0
 def conn(self, server, port):
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
     self.connect((server, port))
     asyncore.loop()
Example #47
0
 def online_start(self):
     '''start a local server'''
     self.robots = []
     MainServer(8080)
     asyncore.loop()
     print('out')
Example #48
0
 def run(self):
     asyncore.loop()
Example #49
0
        except ValueError:
            pass
        print('after reap', pid)

    def register_parent_signal(self):
        signal.signal(signal.SIGINT, self.exit_parent)
        signal.signal(signal.SIGTERM, self.exit_parent)
        signal.signal(signal.SIGCHLD, self.reap_child)  # 监听子进程退出

    def exit_child(self, sig, frame):
        self.close()  # 关闭serversocket
        asyncore.close_all()  # 关闭所有clientsocket
        print('all closed')

    def register_child_signal(self):
        signal.signal(signal.SIGINT, self.exit_child)
        signal.signal(signal.SIGTERM, self.exit_child)

    def handle_accept(self):
        pair = self.accept()  # 接收新连接
        if pair is not None:
            sock, addr = pair
            RPCHandler(sock, addr)


if __name__ == '__main__':
    host = sys.argv[1]
    port = int(sys.argv[2])
    RPCServer(host, port)
    asyncore.loop()  # 启动事件循环
Example #50
0
                    return value

        return retrier

    def multi_interact(self, line, handler):
        for server in self.servers:
            logger.warn("Sending %s to: %s", line, server)
            try:
                server._do_interaction(line, handler.clone())
            except ServerInUse, (msg, server):
                logger.info(msg)
                # ignore
                pass

        asyncore.loop(use_poll=True,
                      timeout=ASYNCORE_TIMEOUT,
                      count=ASYNCORE_COUNT)
        results = filter(None, (s.result for s in self.servers))
        return results

    @retry_until_succeeds
    def _all_broadcast(self, cmd, *args, **kwargs):
        """Broadcast to all servers and return the results in a compacted
        dictionary, where the keys are the server objects and the values are
        the result of the command.

        """
        func = getattr(protohandler, "process_%s" % cmd)
        return self.multi_interact(*func(*args, **kwargs))

    @retry_until_succeeds
 def asyncloop():
     # check every sec if all channel are close
     asyncore.loop(1)
Example #52
0
        except ValueError:
            pass
        print 'after reap', pid

    def register_parent_signal(self):
        signal.signal(signal.SIGINT, self.exit_parent)
        signal.signal(signal.SIGTERM, self.exit_parent)
        signal.signal(signal.SIGCHLD, self.reap_child)  # 监听子进程退出...

    def exit_child(self, sig, frame):
        self.close()  # 关闭 serversocket
        asyncore.close_all()  # 关闭所有 clientsocket
        print 'all closed'

    def register_child_signal(self):
        signal.signal(signal.SIGINT, self.exit_child)
        signal.signal(signal.SIGTERM, self.exit_child)

    def handle_accept(self):
        pair = self.accept()  # 接收新连接
        if pair is not None:
            sock, addr = pair
            RPCHandler(sock, addr)


if __name__ == '__main__':
    host = sys.argv[1]
    port = int(sys.argv[2])
    RPCServer(host, port)
    asyncore.loop()  # 启动事件循环...
Example #53
0
 def test_invalid_chunk_length(self):
     self.sock.sendall('get\r\na bogus message\r\n')
     asyncore.loop(1, map=self.map, count=1)
     assert self.error and self.error[0] is asynchunk.Error
Example #54
0
 def run(self):
   print "PacClient is running!"
   asyncore.loop()
Example #55
0
 def test_chunk_length_with_extension(self):
     data = self._encoded[0:1] + '  ;  some extension  ' + self._encoded[1:]
     self.sock.sendall(data)
     asyncore.loop(1, map=self.map, count=1)
     assert self.incoming == self._chunks
     assert not self.error
Example #56
0
 def run(self):
     while not self._stopevent.isSet():
         asyncore.loop(timeout=SmtpMailsink.
                       TIME_TO_WAIT_BETWEEN_CHECKS_TO_STOP_SERVING,
                       count=1)
Example #57
0
        self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.bind(('', 0))  # bind to all interfaces and a "random" free port.
        print "Connecting..."

    # Once a "connection" is made do this stuff.
    def handle_connect(self):
        print "Connected"

    # If a "connection" is closed do this stuff.
    def handle_close(self):
        self.close()

    # If a message has arrived, process it.
    def handle_read(self):
        data, addr = self.recv(2048)
        print data

    # Actually sends the message if there was something in the buffer.
    def handle_write(self):
        if self.buffer != "":
            print self.buffer
            sent = self.sendto(self.buffer, (self.server, self.port))
            self.buffer = self.buffer[sent:]


connection = AsyncoreClientUDP("127.0.0.1", 5005)  # create the "connection"
while 1:
    asyncore.loop(count=10)  # Check for upto 10 packets this call?
    connection.buffer += raw_input(
        " Chat > ")  # raw_input (this is a blocking call)
Example #58
0
 def test_decode(self):
     self.sock.sendall(self._encoded)
     asyncore.loop(1, map=self.map, count=1)
     assert self.incoming == self._chunks
     assert not self.error
Example #59
0
def run(spin=SPIN):
    if _debug: run._debug("run spin=%r", spin)
    global running, taskManager, deferredFns, sleeptime

    # reference the task manager (a singleton)
    taskManager = TaskManager()

    # count how many times we are going through the loop
    loopCount = 0

    running = True
    while running:
        #       if _debug: run._debug("    - time: %r", time.time())
        loopCount += 1

        # get the next task
        task, delta = taskManager.get_next_task()

        try:
            # if there is a task to process, do it
            if task:
                # if _debug: run._debug("    - task: %r", task)
                taskManager.process_task(task)

            # if delta is None, there are no tasks, default to spinning
            if delta is None:
                delta = spin

            # there may be threads around, sleep for a bit
            if sleeptime and (delta > sleeptime):
                time.sleep(sleeptime)
                delta -= sleeptime

            # if there are deferred functions, use a small delta
            if deferredFns:
                delta = min(delta, 0.001)


#           if _debug: run._debug("    - delta: %r", delta)

# loop for socket activity
            asyncore.loop(timeout=delta, count=1)

            # check for deferred functions
            while deferredFns:
                # get a reference to the list
                fnlist = deferredFns
                deferredFns = []

                # call the functions
                for fn, args, kwargs in fnlist:
                    #                   if _debug: run._debug("    - call: %r %r %r", fn, args, kwargs)
                    fn(*args, **kwargs)

                # done with this list
                del fnlist

        except KeyboardInterrupt:
            if _debug: run._info("keyboard interrupt")
            running = False
        except Exception, err:
            if _debug: run._exception("an error has occurred: %s", err)
Example #60
0
def run():
    """
    Start Bulk.

    Handles all commmand line arguments, logging setup,
    and kicking off the network listener.

    """
    parser = argparse.ArgumentParser(description='A content inspecting \
                                     mail relay built on smtpd')

    parser.add_argument(
        '--bind_address',
        default='127.0.0.1',
        help='Address to bind to and listen on for incoming mail. \
              Default is 127.0.0.1')

    parser.add_argument(
        '--bind_port',
        default=1025,
        type=int,
        help='Port to bind to and to listen on for incoming mail. \
             Default is 1025')

    parser.add_argument('--remote_address',
                        default='127.0.0.1',
                        help='Remote address to forward outbound mail. \
              Default is 127.0.0.1')

    parser.add_argument(
        '--remote_port',
        default=25,
        type=int,
        help='Remote port to forward outbound mail. Default is 25')

    # Note that type can be a function
    parser.add_argument(
        '--base_log_directory',
        default='/tmp/bulk/',
        type=directory_name,
        help='Directory to write log files, messages, and attachments. \
              Default is /tmp/bulk/')

    parser.add_argument(
        '--log_all_messages',
        action='store_true',
        help='Log all messages to /base_log_directory/messages/')

    parser.add_argument(
        '--block',
        action='store_true',
        help='Block mail with quarantined attachments. Default is False')

    parser.add_argument(
        '--always_block',
        action='store_true',
        help='Turn the proxy into a server (block all). Default is false')

    parser.add_argument(
        '--save_attachments',
        action='store_true',
        help='Experimental: Save all attachments as seperate files. \
             Default is false.')

    parser.add_argument(
        '--log_config',
        default='/etc/bulk/logging.conf',
        help='Logging config file. Default is /etc/bulk/logging.conf')

    # add a group to mark certain arguments as required
    req = parser.add_argument_group('required')
    # the processor arg is the only required argument
    req.add_argument(
        '--processor',
        default=[],
        required=True,
        nargs='+',
        action=CreateProcessor,
        dest='processors',
        help='Choose a processing engine by supplying an import string as the \
             first positional argument and multiple rules files as optional \
             following arguments. For example: \
             --processor bulk.processors.basic /etc/bulk/rules/simple')

    args = parser.parse_args()
    err = validate_arguments(args)

    if err:
        raise Exception(err)

    create_sub_directories(args.base_log_directory)

    # Setup logging
    logger = setup_logging(args.log_config)
    logger.info('Starting Bulk Proxy')

    logger.info('Listening on %s:%s' % (args.bind_address, args.bind_port))

    if not args.always_block:
        logger.info('Forwarding to %s:%s' %
                    (args.remote_address, args.remote_port))

    logger.info('Bulk matches will be logged to %squarantine/' %
                args.base_log_directory)

    if args.block:
        logger.info('Emails that match a processor rule will be BLOCKED')

    if args.always_block:
        logger.info('Bulk set to BLOCK ALL mail')

    if args.log_all_messages:
        logger.info('Logging ALL messages to %smessages/' %
                    args.base_log_directory)

    if args.save_attachments:
        logger.info('Saving attachments to %sattachments/' %
                    args.base_log_directory)

    if args.processors:
        for p in args.processors:
            logger.info('Bulk using %s' % p)

    server = BulkProxy((args.bind_address, args.bind_port),
                       (args.remote_address, args.remote_port),
                       args.processors,
                       base_directory=args.base_log_directory,
                       block=args.block,
                       always_block=args.always_block,
                       log=args.log_all_messages,
                       save_attachments=args.save_attachments)

    # Kick off the main process
    asyncore.loop()