def __init__(self, uuid, proc, path, port):
        """Initialize an engine based on existing system process. """
        self.uuid = uuid
        self.proc = proc
        self.port = port
        self.path = path

        self.util = psutil.Process(proc.pid)
        self.evaluating = False
        self.queue = collections.deque()
        self.url = "http://localhost:%s" % port
        self.files = []

        self.out = StringIO()
        self.err = StringIO()

        stdout = proc.stdout.fileno()
        stderr = proc.stderr.fileno()

        self._set_nonblocking(stdout)
        self._set_nonblocking(stderr)

        ioloop = tornado.ioloop.IOLoop.instance()

        ioloop.add_handler(stdout, self._on_stdout, ioloop.READ | ioloop.ERROR)
        ioloop.add_handler(stderr, self._on_stderr, ioloop.READ | ioloop.ERROR)
Esempio n. 2
0
    def __init__(self, session, user, ws_handler):
        global args
        wdir = SESSIONS_DIR + session

        mkdir_p(wdir)
        
        
        # Delete and create a dummy file so that the directory mtime changes....
        # 
        
        tmp_fname = wdir + '/' + str(time.time())

        ftmp = open(tmp_fname, 'w+')
        ftmp.close()
        os.remove(tmp_fname)
        
        self.mtime = os.stat(wdir).st_mtime;
        
        self.h = subprocess.Popen(args.julia, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=wdir)
        self.session = session

        jl_stdout = self.h.stdout.fileno()
        flags = fcntl.fcntl(jl_stdout, fcntl.F_GETFL)| os.O_NDELAY
        fcntl.fcntl(jl_stdout, fcntl.F_SETFL, flags)
        ioloop.add_handler(jl_stdout, functools.partial(push_to_clients, self.h.stdout, self), ioloop.READ|ioloop.ERROR)
    
        global g_sessions
        g_sessions[session] = self
        
        self.resp_buffer = ''
        self.clients = []
        
        self.history = deque([])
        self.add_user(user, ws_handler)
        self.cmd_q = deque()
Esempio n. 3
0
    def add_task(self, taskname, *args, **kwargs):
        """Run a function in a Process. All args and kwargs except
        `callback` are passed to task.

        :param taskname: a `callable`
        :keyword callback: callable with a single argument (task result)

        This method creates a random UnixSocket under /tmp/ for
        communication, registers a handler on `tornado.ioloop.IOLoop`
        with its fd, creates a `Pipe` for communicating back the
        result and runs `taskname` using `run_as_process`. Which runs
        the given task, sends result back through and connects to the
        UnixSocket to let IOLoop know
        """
        user_cb = kwargs.pop('callback')
        assert callable(user_cb)

        ioloop = tornado.ioloop.IOLoop().instance()
        fname = '/tmp/proc_socket_%s' % uuid4()
        # create & bind socket
        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        sock.bind(fname)
        sock.listen(1)
        # pass down input callback
        callback = functools.partial(self._on_complete, user_cb)
        ioloop.add_handler(sock.fileno(), callback, ioloop.READ)
        # subprocess
        self.tor_conn, proc_conn = Pipe()
        run_as_process(fname, proc_conn, target=taskname,
                       args=args, kwargs=kwargs)
        self.process_socket = sock
Esempio n. 4
0
def main():
    global sources, collector, collector_stream
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        (r"/", MainHandler),
        (r"/echo",EchoWebSocket),
        (r"/p7",P7WebSocket)
    ], static_path=os.path.join(os.path.dirname(__file__), "client"))
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    
    
    # Start the time signal
    time_signal()
    # Start the fake source providers
    fake_sources()
    
    ioloop = tornado.ioloop.IOLoop.instance()
    
    # Set up collector
    collector = collectd.Reader(host="0.0.0.0")
    collector._sock.setblocking(0)
    ioloop.add_handler(collector._sock.fileno(), on_collect, ioloop.READ)

    ioloop.start()
Esempio n. 5
0
    def add_task(self, taskname, *args, **kwargs):
        """Run a Celery task. All args and kwargs except `callback` are
        passed to task.

        :param taskname: celery task
        :keyword callback: callable with a single argument (task result)

        This method creates a random UnixSocket under /tmp/ for
        communication, registers a handler on `tornado.ioloop.IOLoop`
        with its fd, calls `taskname.apply_async(args, kwargs)` and
        links to notifier subtask to be run upon successful completion.

        :attr:`celery_result` contains return value of apply_async
        """
        user_cb = kwargs.pop('callback')
        assert callable(user_cb)

        ioloop = tornado.ioloop.IOLoop().instance()
        fname = '/tmp/task_socket_%s' % uuid4()
        # create & bind socket
        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        sock.bind(fname)
        sock.listen(1)
        # pass down input callback
        callback = functools.partial(self._on_complete, user_cb)
        ioloop.add_handler(sock.fileno(), callback, ioloop.READ)
        # subtask
        link = celery_notifier.subtask(args=(fname,), immutable=True)
        self.celery_result = taskname.apply_async(args, kwargs, link=link)
        self.celery_socket = sock
Esempio n. 6
0
def install_package(filename, callback):
    filename = os.path.join(DOWNLOAD_TMP_DIR, filename)

    if not os.path.exists(filename):
        callback({
            'ok'     : False,
            'error'  : "Failed to find archive",
            'removed': [],
        })
        return

    proc = subprocess.Popen(['tar','zxf', filename],
                            cwd=DOWNLOAD_TMP_DIR,
                            stdout=subprocess.PIPE)

    def install_all_bundles():
        removed   = []
        installed = []

        for bundle in os.listdir(DOWNLOAD_TMP_DIR):
            tmppath    = os.path.join(DOWNLOAD_TMP_DIR, bundle)
            bundlepath = os.path.join(LV2_PLUGIN_DIR, bundle)

            if os.path.exists(bundlepath):
                removed += remove_bundle_to_lilv_world(bundlepath, True)
                shutil.rmtree(bundlepath)

            shutil.move(tmppath, bundlepath)
            installed += add_bundle_to_lilv_world(bundlepath, True)

        # TODO - make ingen refresh lv2 world

        if len(installed) == 0:
            resp = {
                'ok'     : False,
                'error'  : "No plugins found in bundle",
                'removed': removed,
            }
        else:
            resp = {
                'ok'       : True,
                'removed'  : removed,
                'installed': installed,
            }

        return resp

    def end_untar_pkgs(fileno, event):
        if proc.poll() is None:
            return
        ioloop.remove_handler(fileno)
        os.remove(filename)
        callback(install_all_bundles())

    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.add_handler(proc.stdout.fileno(), end_untar_pkgs, 16)
Esempio n. 7
0
 def _start_handler(self, q):
     self.process_count += 1
     if hasattr(q, '_reader'):
         # use the reader fd to hook into ioloop
         self.fd = q._reader.fileno()
         ioloop = tornado.ioloop.IOLoop.instance()
         ioloop.add_handler(self.fd, lambda fd, events: self._process_q(), tornado.ioloop.IOLoop.READ | tornado.ioloop.IOLoop.ERROR)
     else:
         self.timer = tornado.ioloop.PeriodicCallback(lambda: self._process_q(), 1000)
         self.timer.start()
Esempio n. 8
0
    def communicate(self):
        self.log.info("Adding handler")
        fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NONBLOCK)

        def utf8_error(e):
            self.log.error(e)

        self.reader = io.open(self.fd, "rb", buffering=0, closefd=False)
        self.writer = io.open(self.fd, "wt", encoding="utf-8", closefd=False)
        ioloop.add_handler(self.fd, self.shell_handler, ioloop.READ | ioloop.ERROR)
def handle_server(fd, event):
    s = fd_map[fd]
    if event & IOLoop.READ:
        get_connection, cli_addr = s.accept()
        print "connection %s" % cli_addr[0]
        get_connection.setblocking(0)
        get_connection_fd = get_connection.fileno()
        fd_map[get_connection_fd] = get_connection
        handle = functools.partial(handle_client, cli_addr[0])
        ioloop.add_handler(get_connection_fd, handle, IOLoop.READ)
        message_queue_map[get_connection] = Queue.Queue()
Esempio n. 10
0
    def communicate(self):
        self.log.info('Adding handler')
        fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NONBLOCK)

        def utf8_error(e):
            self.log.error(e)

        self.reader = io.open(self.fd, 'rb', buffering=0, closefd=False)
        self.writer = io.open(self.fd, 'wt', encoding='utf-8', closefd=False)
        ioloop.add_handler(self.fd, self.shell_handler,
                           ioloop.READ | ioloop.ERROR)
Esempio n. 11
0
def query_via_udp(name, callback, type=TYPE.A, server='127.0.0.1', port=53, *, sock=None, ioloop=None):
  q = mkquery((name, type)).pack()
  if sock is None:
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  sock.sendto(q, (server, port))

  if ioloop is None:
    ioloop = tornado.ioloop.IOLoop.instance()
  ioloop.add_handler(sock.fileno(),
                     partial(_recv_dns_msg, callback, ioloop, sock),
                     ioloop.READ)
Esempio n. 12
0
def accept(sock):
    @return_future
    def wrap_add_handler(callback):
       def wait_for_accept(fd,events): # TODO more intelligently check for events?
            try:
                result = sock.accept()
                callback(result)
            except socket.error, e:
                if e.args[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
                    raise

       ioloop.add_handler(sock.fileno(), wait_for_accept, ioloop.READ)
Esempio n. 13
0
    def communicate(self):
        self.log.info('PTY forked : %s (%s)' %
                      (os.ttyname(self.fd), os.ctermid()))
        fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NONBLOCK)

        def utf8_error(e):
            self.log.error(e)

        self.reader = io.open(self.fd, 'rb', buffering=0, closefd=False)
        self.writer = io.open(self.fd, 'wt', encoding='utf-8', closefd=False)
        ioloop.add_handler(self.fd, self.shell_handler,
                           ioloop.READ | ioloop.ERROR)
Esempio n. 14
0
def main():
    commander = getInstance(Commander)
    """ tell the commander to clean up the node """
    nodeinst = getInstance(node.Node)
    commander.add(nodeinst.stop)
    """ add the stdin handler, and tell the commander to clean up the ioloop """
    ioloop = tornado.ioloop.IOLoop.instance()
    getInstance.singletons[tornado.ioloop.IOLoop] = ioloop
    ioloop.add_handler(sys.stdin.fileno(), stdinHandler, ioloop.READ)
    commander.add(ioloop.stop)
    """ now serve the web-page, start listening """
    web()
    listen()
    """ start it up! """
    ioloop.start()
Esempio n. 15
0
    def subprocess(self, cmd, callback):
        ioloop = tornado.ioloop.IOLoop.instance()
        args = shlex.split(cmd)
        LOG.debug(args)
        pipe = subprocess.Popen(args, stdout = subprocess.PIPE, close_fds = True)
        fd = pipe.stdout.fileno()
        result = []

        def recv(*args):
            data = pipe.stdout.readline()
            if data:
                result.append(data)
            elif pipe.poll() is not None:
                ioloop.remove_handler(fd)
                callback(''.join(result))

        ioloop.add_handler(fd, recv, ioloop.READ)
Esempio n. 16
0
    def subprocess(self, cmd, callback):
        ioloop = tornado.ioloop.IOLoop.instance()
        args = shlex.split(cmd)
        LOG.debug(args)
        pipe = subprocess.Popen(args, stdout=subprocess.PIPE, close_fds=True)
        fd = pipe.stdout.fileno()
        result = []

        def recv(*args):
            data = pipe.stdout.readline()
            if data:
                result.append(data)
            elif pipe.poll() is not None:
                ioloop.remove_handler(fd)
                callback(''.join(result))

        ioloop.add_handler(fd, recv, ioloop.READ)
Esempio n. 17
0
    def communicate(self):
        fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NONBLOCK)

        self.reader = io.open(
            self.fd,
            'rb',
            buffering=0,
            closefd=False
        )
        self.writer = io.open(
            self.fd,
            'wt',
            encoding='utf-8',
            closefd=False
        )
        ioloop.add_handler(
            self.fd, self.shellHandle, ioloop.READ | ioloop.ERROR)
Esempio n. 18
0
    def subprocess(self, cmd, callback):
        ioloop = tornado.ioloop.IOLoop.instance()
        PIPE = subprocess.PIPE
        cmd = " ".join(cmd)
        pipe = subprocess.Popen(cmd, shell=True, stdin=PIPE,
            stdout=PIPE, stderr=subprocess.STDOUT, close_fds=True)
        fd = pipe.stdout.fileno()

        def read(*args):
            data = pipe.stdout.readline()
            if data:
                callback(data)
            elif pipe.poll() is not None:
                ioloop.remove_handler(fd)
                callback(None)

        # read handler
        ioloop.add_handler(fd, self.async_callback(read), ioloop.READ)
Esempio n. 19
0
  def start_task_as_admin(self, *args, **kwargs):
    task_cb = kwargs.pop('callback')
    assert callable(task_cb)
    taskname = self.taskname

    ioloop = tornado.ioloop.IOLoop.instance()
    socket_file = '/tmp/forge.d/forge_user_task_notify_%s' % uuid.uuid4()

    sox = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sox.bind(socket_file)
    sox.listen(1)

    cb = functools.partial(self.__on_complete, task_cb, socket_file)
    ioloop.add_handler(sox.fileno(), cb, ioloop.READ)
    self.tornado_conn, proc_conn = multiprocessing.Pipe()
    self.__run_process_as_admin(socket_file, proc_conn, taskname=taskname, args=args, kwargs=kwargs)
    
    self.process_sox = sox
Esempio n. 20
0
def query_via_udp(name,
                  callback,
                  type=TYPE.A,
                  server='127.0.0.1',
                  port=53,
                  *,
                  sock=None,
                  ioloop=None):
    q = mkquery((name, type)).pack()
    if sock is None:
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(q, (server, port))

    if ioloop is None:
        ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.add_handler(sock.fileno(),
                       partial(_recv_dns_msg, callback, ioloop, sock),
                       ioloop.READ)
Esempio n. 21
0
def main():
	commander = getInstance(Commander)	

	""" tell the commander to clean up the node """
	nodeinst  = getInstance(node.Node)
	commander.add(nodeinst.stop)

	""" add the stdin handler, and tell the commander to clean up the ioloop """
	ioloop    = tornado.ioloop.IOLoop.instance()
	getInstance.singletons[tornado.ioloop.IOLoop] = ioloop
	ioloop.add_handler(sys.stdin.fileno(), stdinHandler, ioloop.READ)
	commander.add(ioloop.stop)

	""" now serve the web-page, start listening """
	web()
	listen()

	""" start it up! """
	ioloop.start()
Esempio n. 22
0
  def __init__(self, family, address, onaccept_func):
    """Initialize a ListenSocket.

    Args:
      family: eg. socket.AF_INET, socket.AF_INET6, socket.AF_UNIX
      address: eg. ('0.0.0.0', 1234) or '/tmp/unix/socket/path'
      onaccept_func: called with newly-accepted socket, with parameters
        (address, sock).
    """
    self.onaccept_func = onaccept_func
    self.family = family
    self.address = address
    self.sock = None
    self.sock = _ListenSocket(family, address)
    if family != socket.AF_UNIX:
      self.address = self.sock.getsockname()[:2]
    print 'Listening on %r' % (self.address,)
    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.add_handler(self.sock.fileno(), self._Accept, ioloop.READ)
Esempio n. 23
0
def main():
    parse_command_line()

    if options.split:
        split_file = tempfile.mktemp(suffix="-mpu6050.log")
        print("Writing data to '%s'" % split_file)
        outf = open(split_file, 'wb')
        then = time.time()
        def split_write(message):
            outf.write(b"#" * 10)
            outf.write(b"\n")
            outf.write(message)
    else:
        def split_write(_message):
            pass

    settings = {
        "static_path": os.path.join(os.path.dirname(__file__), "static"),
    }

    app = tornado.web.Application(
        [
            (r'/', IndexHandler),
            (r'/ws', WebSocketHandler),
        ],
        **settings
    )

    socket = nanomsg.Socket(nanomsg.PAIR)
    print("Connecting to nanomsg on '%s'" % options.uri)
    socket.connect(options.uri)
    print("Server listening on 'http://localhost:%i/'" % options.port)
    app.listen(options.port)
    ioloop = tornado.ioloop.IOLoop.instance()



    ioloop.add_handler(
        socket.fd,
        partial(imu_message_arrived, socket, split_write),
        tornado.ioloop.IOLoop.READ | tornado.ioloop.IOLoop.WRITE | tornado.ioloop.IOLoop.ERROR
        )
    ioloop.start()
Esempio n. 24
0
  def __init__(self, family, address, onaccept_func):
    """Initialize a ListenSocket.

    Args:
      family: eg. socket.AF_INET, socket.AF_INET6, socket.AF_UNIX
      address: eg. ('0.0.0.0', 1234) or '/tmp/unix/socket/path'
      onaccept_func: called with newly-accepted socket, with parameters
        (address, sock).
    """
    self.onaccept_func = onaccept_func
    self.family = family
    self.address = address
    self.sock = None
    self.sock = _ListenSocket(family, address)
    if family != socket.AF_UNIX:
      self.address = self.sock.getsockname()[:2]
    print 'Listening on %r' % (self.address,)
    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.add_handler(self.sock.fileno(), self._Accept, ioloop.READ)
Esempio n. 25
0
    def communicate(self):
        self.log.debug('Adding handler')
        fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NONBLOCK)

        def utf8_error(e):
            self.log.error(e)

        self.reader = io.open(
            self.fd,
            'rb',
            buffering=0,
            closefd=False
        )
        self.writer = io.open(
            self.fd,
            'wt',
            encoding='utf-8',
            closefd=False
        )
        ioloop.add_handler(
            self.fd, self.shell_handler, ioloop.READ | ioloop.ERROR)
Esempio n. 26
0
def main():
    # add web handler
    application = tornado.web.Application([
        (r"/", MainHandler),
        (r"/ws", EchoWebSocket),
    ])
    application.listen(8888)
    pcap = Pcap()
    dev  = pcap.create(pcap.device)
    print 'using', pcap.device
    #can_mon = pcap.can_set_rfmon(dev)
    #print 'can mon:', can_mon
    #if can_mon == 0:
    #    print pcap.get_error(dev)
    #else:
    #    mon  = pcap.set_rfmon(dev, 1)
    #    print 'rf mon:', mon
    #to   = pcap.set_timeout(dev, 0)
    #print 'timeout', to
    #buff = pcap.set_buff_size(dev, 65535)
    #print 'buff size:', buff
    #snap = pcap.set_snaplen(dev, 65535)
    #print 'snap:', snap
    #prom = pcap.set_promisc(dev, 1)
    #print 'prom:', prom
    act  = pcap.activate(dev)
    print 'activated:', act
    nb   = pcap.set_nonblock(dev, 1)
    print "non block", nb
    fd   = pcap.get_fd(dev)
    print 'fd:', fd
    gnb  = pcap.get_nonblock(dev)
    print 'get nonblock', gnb
    cbh = CallbackHandler()
    ioloop = tornado.ioloop.IOLoop.instance()
    if fd > 0 and gnb > 0:
        read_partial = partial(read_fd, **{'dev':dev, 'pcap':pcap, 'cbh':cbh})
        ioloop.add_handler(fd, read_partial, ioloop.READ)
    #ioloop.add_timeout(ioloop.time()+5, shutdown, *(pcap, dev))
    ioloop.start()
Esempio n. 27
0
    def start_ioloop(self):
        """
        Method to start ioloop for paho-mqtt mosquitto clients so that it can 
        process read/write events for the sockets.

        Using tornado's ioloop, since if we use any of the loop*() function provided by
        phao-mqtt library, it will either block the entire tornado thread, or it will 
        keep on creating separate thread for each client if we use loop_start() fucntion. 

        We don't want to block thread or to create so many threads unnecessarily given 
        python GIL.

        Since the separate threads calls the loop() function indefinitely, and since its doing 
        network io, its possible it may release GIL, but I haven't checked that yet, if that 
        is the case, we can very well use loop_start().Pattern

        But for now we will add handlers to tornado's ioloop().

        """

        # pi('start_ioloop')

        # the socket conection of the present mqtt mosquitto client object
        self._sock = self._client.socket()

        # adding tornado iooloop handler
        events = READ | WRITE | ERROR

        # print '[MosquittoClient] adding tornado handler now'

        if self._sock:

            # print 'self._sock is present, hence adding handler'

            ioloop.add_handler(self._sock.fileno(), self._events_handler,
                               events)

        else:
            LOGGER.warning('[MosquittoClient] client socket is closed already')
Esempio n. 28
0
    def communicate(self):
        self.log.info('PTY forked : %s (%s)' % (
            os.ttyname(self.fd), os.ctermid()))
        fcntl.fcntl(self.fd, fcntl.F_SETFL, os.O_NONBLOCK)

        def utf8_error(e):
            self.log.error(e)

        self.reader = io.open(
            self.fd,
            'rb',
            buffering=0,
            closefd=False
        )
        self.writer = io.open(
            self.fd,
            'wt',
            encoding='utf-8',
            closefd=False
        )
        ioloop.add_handler(
            self.fd, self.shell_handler, ioloop.READ | ioloop.ERROR)
Esempio n. 29
0
    def start_ioloop(self):
        """
        Method to start ioloop for paho-mqtt mosquitto clients so that it can 
        process read/write events for the sockets.

        Using tornado's ioloop, since if we use any of the loop*() function provided by
        phao-mqtt library, it will either block the entire tornado thread, or it will 
        keep on creating separate thread for each client if we use loop_start() fucntion. 

        We don't want to block thread or to create so many threads unnecessarily given 
        python GIL.

        Since the separate threads calls the loop() function indefinitely, and since its doing 
        network io, its possible it may release GIL, but I haven't checked that yet, if that 
        is the case, we can very well use loop_start().Pattern

        But for now we will add handlers to tornado's ioloop().

        """ 

        # pi('start_ioloop')

        # the socket conection of the present mqtt mosquitto client object 
        self._sock = self._client.socket()

        # adding tornado iooloop handler 
        events = READ | WRITE | ERROR

        # print '[MosquittoClient] adding tornado handler now'

        if self._sock:

            # print 'self._sock is present, hence adding handler'

            ioloop.add_handler(self._sock.fileno(), self._events_handler, events)

        else: 
            LOGGER.warning('[MosquittoClient] client socket is closed already')
Esempio n. 30
0
    logging.info(" [*] Listening on 0.0.0.0:" + str(options.options.port))
    print "*SYSOUT* User API Websocket is created at localhost:" + str(options.options.port) + "/api"

    # Create a userControl object
    userControl = UserControl()

    # Install signal handlers
    signalCallback = functools.partial(signalHandler, userControl)
    signal.signal(signal.SIGTERM, signalCallback)
    signal.signal(signal.SIGINT, signalCallback)

    # Start listener thread
    t = threading.Thread(target=listenerThread)
    t.daemon = True
    # Start it
    t.start()

    # Create ioloop
    ioloop = ioloop.IOLoop.instance()

    # Add user control via stdin pipe
    ioloop.add_handler(sys.stdin.fileno(), userControl.userPlayer, ioloop.READ)

    # Before starting ioloop, stop led blinking,
    # which will light up correct LED and give information to the user
    # that all is ready
    if platform.machine() == "mips":
        subprocess.call(["/etc/init.d/led_blink", "stop"])

    ioloop.start()
Esempio n. 31
0
def input_add(fd, cond):
  ioloop.add_handler(fd, pypurple.io_invoke, cond)
Esempio n. 32
0
def inotify_handler(event):
    if (event.mask & pyinotify.IN_CLOSE_WRITE) and not (event.mask & pyinotify.IN_ISDIR):
        file_name = os.path.join(event.path, event.name)
        if os.path.isfile(file_name):
            if file_name[0] == ".":
                file_name = file_name[1:]
            for socket in active_sockets:
                socket.send_update(file_name)

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/(livereload.js)", tornado.web.StaticFileHandler, {'path': "js"}),
        (r"/livereload", LiveReloadSocket),
    ])
    application.listen(35729)
    ioloop = tornado.ioloop.IOLoop.instance()

    wm = pyinotify.WatchManager()
    inotifier = pyinotify.Notifier(wm, inotify_handler)
    wm.add_watch(".", pyinotify.ALL_EVENTS, rec=True)
    def inotify_fd_handler(*args):
        inotifier.process_events()
        if inotifier.check_events():
            inotifier.read_events()
            inotifier.process_events()

    ioloop.add_handler(wm.get_fd(), inotify_fd_handler, ioloop.READ)

    ioloop.start()
Esempio n. 33
0
    #print "start user script"
    #userControl.start()

    # Install signal handlers
    signalCallback = functools.partial(signalHandler, userControl)
    signal.signal(signal.SIGTERM, signalCallback)
    signal.signal(signal.SIGINT, signalCallback)

    # Prepare lock for the thread
    weioRunnerGlobals.lockConn = multiprocessing.Lock()

    # Start listener thread
    t = threading.Thread(target=listenerThread)
    t.daemon = True
    # Start it
    t.start()

    # Create ioloop
    ioloop = ioloop.IOLoop.instance()

    # Add user control via stdin pipe
    ioloop.add_handler(sys.stdin.fileno(), userControl.userPlayer, ioloop.READ)

    # Before starting ioloop, stop led blinking,
    # which will light up correct LED and give information to the user
    # that all is ready
    if (platform.machine() == 'mips'):
        subprocess.call(["/etc/init.d/led_blink", "stop"])

    ioloop.start()
Esempio n. 34
0
 def start(self, ioloop):
     ioloop.add_handler(self._channel._reader.fileno(), self._on_channel,
                        ioloop.READ)
Esempio n. 35
0
	def on_close(self):
		print 'websocket closed'	

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# int main(int argc, char** argv)
#

if __name__=='__main__':
	# need to use these global variables
	global tunfd
	global running
	global application
	try:
		# the event loop
		ioloop = tornado.ioloop.IOLoop.instance()
		ioloop.add_handler(sys.stdin.fileno(), stdinHandler, ioloop.READ)
		# initialize application and listen
		application = tornado.web.Application([
			(r'/websocket', BasicWebSocket),
		])
		application.ws = None
		subprocess.check_call('sudo /sbin/ifconfig en1 inet 169.254.134.89 netmask 255.255.0.0 alias', shell=True)
		application.listen(port)
		print 'running...'
	except Exception, err:
		raise Exception('Error: %s\n' % str(err))

	try:
		ioloop.start()
	except Exception, err:
		raise Exception('Error: %s\n' % str(err))
Esempio n. 36
0
    
def make_xen_pipe():
    """
    用户发送从xenserver接收到的报警到主线程
    """
    xen_read_fd, xen_write_fd = os.pipe()
    
    flags = fcntl.fcntl(xen_read_fd, fcntl.F_GETFL)
    fcntl.fcntl(xen_read_fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
    
    flags = fcntl.fcntl(xen_write_fd, fcntl.F_GETFL)
    fcntl.fcntl(xen_write_fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
    
    return xen_read_fd, xen_write_fd


if __name__ == '__main__':
    # make simple test
    def nagios_read_handler(fd, events):
        print os.read(fd, 1024)
    
    nagios_read, nagios_write = make_nagios_pipe()
    xen_read, xen_write = make_xen_pipe()
        
    ioloop = ioloop.IOLoop().instance()
    ioloop.add_handler(nagios_read, nagios_read_handler, ioloop.READ)
    ioloop.add_handler(xen_read, nagios_read_handler, ioloop.READ)
    os.write(nagios_write, "hello nagios\n")
    os.write(xen_write, "hello xenserver\n")
    ioloop.start()
    
Esempio n. 37
0
    (r'/', IndexHandler),
    (r'/ws', WebSocketHandler),
    (r'/ajax', AjaxHandler),
    (r"/login", LoginHandler),
    (r"/download", DownloadHandler),
    (r"/logout", LogoutHandler),
]

settings = dict(
    static_path = os.path.join(os.getcwd(), 'static'),
    template_path = os.path.join(os.getcwd(), 'templates'),
    debug = True,
    cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
)

app = tornado.web.Application(handlers, **settings)
ioloop.add_handler(cnx.fileno(), watch_db, ioloop.READ)

def main():
    try:
        app.listen(8000)
        listen('agent_state_changes')
        print('server is running in port 8000')
        ioloop.start()
    except KeyboardInterrupt as e:
        print('stopping server')
        ioloop.stop()

if __name__ == '__main__':
	main()
Esempio n. 38
0

#>>> get_ip_address('lo')
#'127.0.0.1'

#>>> get_ip_address('eth0')
#'38.113.228.130'

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# int main(int argc, char** argv)
#

if __name__ == '__main__':
    try:
        application = tornado.web.Application([
            (r'/websocket', BasicWebSocket),
        ])
        application.listen(port)
    except Exception, err:
        print('Error: could not initialize application and listen' + str(err))
    try:
        ioloop.add_handler(sys.stdin.fileno(), stdinHandler, ioloop.READ)
        print 'running ...'
        # our ip address that we need to assign to outgoing packet that we forward:
        our_ip = get_ip_address('eth0')
        print('our ip address of eth0 to assign to packets: ' + our_ip)
        ioloop.start()
    finally:
        if tunfd:
            os.close(tunfd)