コード例 #1
0
ファイル: wiki_daemon.py プロジェクト: AviPeltz/PolyVoice
def run_daemon(qa_pipe: Connection):
    wiki_daemon = WikiDaemon(WIKI_PAGE)
    wiki_daemon.update_wiki_cache()
    wiki_daemon.reload_spacy_docs()
    print("wiki_daemon: Child process started")
    next_wiki_update = time.time() + UPDATE_PERIOD_SECS
    while True:
        now = time.time()
        if next_wiki_update < now:
            print("wiki_daemon: Checking Wikipedia for updates")
            updated = wiki_daemon.update_wiki_cache()

            if updated:
                print("wiki_daemon: Got new revision, updating")

            next_wiki_update = now + UPDATE_PERIOD_SECS

        if qa_pipe.poll(timeout=next_wiki_update - now):
            try:
                question = qa_pipe.recv()
                qa_pipe.send(wiki_daemon.inquiry(question))
            except EOFError:
                # Pipe was closed on other end, we're done here
                qa_pipe.close()
                return
            except ValueError:
                print("Answer was too large to send!")

                # Make sure the caller isn't still waiting for an object
                try:
                    qa_pipe.send("")
                except EOFError:
                    qa_pipe.close()
                    return
コード例 #2
0
 def update(self, send_pipe: Connection, connection: serial.Serial) -> None:
     values = [''] * 8
     formats = [''] * 8
     channel = 0
     while True:
         bite = connection.read()
         if bite:
             hexx = bite.hex()
             dec = int(hexx, 16)
             # Channels are encoded as > 127 decimals
             if dec > 127:
                 channel_potential = self.get_channel(hexx)
                 if channel_potential < 0 and abs(channel_potential) != abs(channel):
                     self.process(channel, values, formats)
                     send_pipe.send(self.data)
                     # Reset Channel and Digit Values
                     channel = channel_potential
                     values = [''] * 8
                     formats = [''] * 8
                 else:
                     channel = channel_potential
             else:
                 # Bytes not representing a channel are values for the preceeding channel
                 digit, value = self.get_value(hexx)
                 if channel > 0:
                     formats[digit] = value
                 else:
                     values[digit] = value
コード例 #3
0
ファイル: imaging.py プロジェクト: Axelancerr/Life
def do_edit_image(edit_function: Callable[..., Any], image_bytes: bytes,
                  pipe: Connection, **kwargs) -> None:

    try:
        with Image(blob=image_bytes) as image, Color("transparent") as colour:

            if image.format != "GIF":
                image.background_color = colour
                edit_function(image, **kwargs)

            else:
                image.coalesce()
                image.iterator_reset()

                image.background_color = colour
                edit_function(image, **kwargs)
                while image.iterator_next():
                    image.background_color = colour
                    edit_function(image, **kwargs)

                image.optimize_transparency()

            edited_image_format = image.format
            edited_image_bytes = image.make_blob()

            pipe.send((edited_image_bytes, edited_image_format))

    except Exception as e:
        print(e, file=sys.stderr)
        pipe.send(ValueError)
コード例 #4
0
ファイル: _jobs.py プロジェクト: depixusgenome/trackanalysis
    def _runjob(pipe: Connection, procs: TaskCacheList, keys: List[int]):
        "runs a job"
        if pipe.poll():
            return

        frame = next(iter(procs.run()), None)
        if frame is None:
            pipe.send((None, None))
            return

        if callable(getattr(frame, 'bead', None)):
            raise NotImplementedError()

        for i in keys:
            if pipe.poll():
                return

            try:
                out = (i, frame[i])
            except ProcessorException as exc:
                out = (i, exc)

            if pipe.poll():
                return

            pipe.send(out)

        if not pipe.poll():
            pipe.send((None, None))
コード例 #5
0
def run_main(control_fd, data_fd, func):
    if iswindows:
        from multiprocessing.connection import PipeConnection as Connection
    else:
        from multiprocessing.connection import Connection
    with Connection(control_fd) as control_conn, Connection(data_fd) as data_conn:
        func(control_conn, data_conn)
コード例 #6
0
def _grant_handles(conn: Connection, handles: Iterable[IO]) -> None:
    log = logging.getLogger('_grant_handles')
    cpid = conn.recv()
    log.info('Attempting to grant file handles to pid {}.'.format(cpid))
    for handle in handles:
        _grant_handle(conn, handle, cpid)
    conn.send(None)
コード例 #7
0
def report_progress(conn_out: connection.Connection, prefix: str = '') -> None:
    """Tell the user how much work has been done.

    :param prefix: A message to print before the progress prompt, e.g.
        'Progress: '.
    :param conn_out: A multiprocessing ``Connection`` object from which values
        may be read. Each value emitted by the object should be a float between
        0 and 1, inclusive, where 0 indicates 0% complete, and 1 represents
        100% complete. When a value of 1 is received, this function will
        return.
    :return: Nothing.
    """
    while True:
        progress = conn_out.recv()
        # \r is carriage return. The other is an ANSI escape code. See:
        # https://en.wikipedia.org/wiki/ANSI_escape_code
        print(
            '\r\033[K' + prefix + f'{progress * 100:.0f}%',
            end='',
        )
        sys.stdout.flush()
        if progress == 1:
            conn_out.close()
            print()
            break
コード例 #8
0
 def evaluate(self, weight_path: str, sendPipe: connection.Connection, *args, **kwargs):
     '''
     功能: 单个模型测试
     '''
     # 限制内存增长
     gpus = tf.config.experimental.list_physical_devices('GPU')
     if gpus:
         try:
             for gpu in gpus:
               tf.config.experimental.set_memory_growth(gpu, True)
         except RuntimeError as e:
             print(e)
     # 模型配置
     model = self.model_preparation()
     model = model_cnn_common.model_compile(self.type, model)
     # 导入权重
     model.load_weights(weight_path, by_name=False)
     # 生成器准备
     self.generator_preparation()
     # 数据准备
     self.data_preparation()
     # 数据预取
     self.testsets = self.testsets.prefetch(
         buffer_size=tf.data.AUTOTUNE)
     # 模型评估
     loss = model.evaluate(
         self.testsets,
         verbose=1,
         return_dict=True)
     # 返回
     sendPipe.send(loss)
     # 对应主进程, 确保管道关闭
     sendPipe.close()
コード例 #9
0
 def relay(
     sub_id: str,
     shutdown: threading.Event,
     channel: connection.Connection,
     callback: Callable[[Dict[str, Any]], None],
 ) -> None:
     while not shutdown.is_set():
         try:
             if channel.poll(timeout=1):
                 ev = channel.recv()
                 if ev['event_name'] == eventNames.SUBSCRIBED:
                     logger.info(
                         'Subscriber#{0} subscribe ack received'.format(
                             sub_id, ), )
                 elif ev['event_name'] == eventNames.UNSUBSCRIBED:
                     logger.info(
                         'Subscriber#{0} unsubscribe ack received'.format(
                             sub_id, ), )
                     break
                 elif ev['event_name'] == eventNames.DISPATCHER_SHUTDOWN:
                     logger.info(
                         'Subscriber#{0} received dispatcher shutdown event'
                         .format(sub_id, ), )
                     break
                 else:
                     callback(ev)
         except queue.Empty:
             pass
         except EOFError:
             break
         except KeyboardInterrupt:
             break
     logger.debug('bbye!!!')
コード例 #10
0
ファイル: data_collector.py プロジェクト: imoneoi/MBRL
def worker_(
        pipe: connection.Connection,
        env_fn_wrapper: CloudpickleWrapper,
        policy_fn_wrapper: CloudpickleWrapper,
        num_data_points: int
):
    # Unwrap
    env = env_fn_wrapper.data()
    policy = policy_fn_wrapper.data()

    # collect
    obs = env.reset()
    for step in range(num_data_points):
        act = policy.infer(obs)
        next_obs, rew, done, info = env.step(act)

        # send data
        pipe.send({
            "obs": obs,
            "act": act,
            "rew": rew,
            "done": done,
            "next_obs": next_obs
        })

        # update obs
        obs = next_obs
        # reset if done
        if done:
            obs = env.reset()
コード例 #11
0
ファイル: insbin.py プロジェクト: wuriyanto48/insbinpy
        def download(sender: Connection):
            url = self.__url
            req = requests.get(url, stream=True)

            if req.status_code != 200:
                raise Exception('url returned code {}'.format(req.status_code))

            # file_name = req.headers['Content-Disposition']
            # print(file_name)

            while True:

                # download piece of file from given url
                s = req.raw.read(1024)

                # tarfile returns b'', if the download process finished
                if not s:
                    break

                tmp_file.write(s)
            req.raw.close()

            # send notification to log() process
            # indicating the download process done
            sender.send(True)
コード例 #12
0
 def update(self, send_pipe: Connection, connection: serial.Serial) -> None:
     while True:
         send_pipe.send(self.data)
         # Filter out 'SYNC IDLE' transmissions
         control_character = 0
         while control_character != b'\x16':
             try:
                 control_character = connection.read()
             except SerialException:
                 continue
         message = ''
         last_character = b''
         # Real Time Data Messages end with a 'END TRANSMISSION BLOCK'
         while last_character != b'\x17':
             hexx = last_character.hex()  # hexadecimal value of byte
             if hexx != '':
                 dec = int(hexx,
                           16)  # decimal representation of hexadecimal byte
                 message += chr(dec)
             else:
                 pass
             try:
                 last_character = connection.read()
             except SerialException:
                 continue
         self.parse(message)
コード例 #13
0
 def _process(
         self,
         conn_lru_dict: LRUCacheType[multiprocessing_connection.Connection,
                                     bool],
         handle_pool: ThreadPool,
         c_recv: multiprocessing_connection.Connection,
         c_send: multiprocessing_connection.Connection,
         wait=multiprocessing_connection.wait):
     while True:
         try:
             for conn in wait(list(conn_lru_dict.keys()) + [c_recv]):
                 if conn == c_recv:
                     c_recv.recv_bytes()
                     continue
                 del conn_lru_dict[conn]
                 if not conn.closed:
                     handle_pool.spawn(self._handle, conn_lru_dict, conn,
                                       c_send)
                 else:
                     self.logger.debug("conn %s was closed" % conn)
         except OSError as e:
             if getattr(e, "winerror", 0) == 6:
                 continue
             logging.exception("OSError")
         except:
             logging.exception("error")
コード例 #14
0
def _player_process(conn: Connection, player_class: Type[AIPlayer],
                    player_args: Tuple[Any]) -> None:  # pragma: no cover
    """
  Used to run an AIPlayer in a separate process.
  See OutOfProcessComputerPlayer.__init__() for more details.
  """
    logging.info("AIProcess: Started")
    player = player_class(*player_args)
    logging.info("AIProcess: Player created successfully: %s", player)
    conn.send(player.cheater)
    while True:
        logging.info(
            "AIProcess: Waiting for a GameView from the UI process...")
        try:
            game_view, game_points = conn.recv()
        except EOFError:
            logging.info("AIProcess: The pipe was closed. Exiting.")
            break
        logging.info("AIProcess: GameView received. Processing...")
        action = player.request_next_action(game_view, game_points)
        logging.info("AIProcess: Sending action (%s) to the UI process",
                     action)
        try:
            conn.send(action)
        except BrokenPipeError:
            logging.info("AIProcess: The pipe was closed. Exiting.")
            break
コード例 #15
0
ファイル: example_analyzer.py プロジェクト: woodnsec/grapl
def _analyzer(client: DgraphClient, graph: Subgraph, sender: Connection):
    hits = analyze_by_node_key(client, graph, signature_graph)

    for hit in hits:
        sender.send(ExecutionHit.from_parent_child('suspicious-svchost', hit))

    sender.send(ExecutionComplete())
コード例 #16
0
ファイル: lulapy.py プロジェクト: JoseitoOliveira/lulapy
def lulapy_broker(conn: Connection) -> None:
    connections.append(conn)

    funcs: Dict[Text, Callable] = {
        'message': message,
        'subscribe': subscribe,
        'unsubscribe': unsubscribe,
        'close': close,
        'add_connection': add_connection
    }

    while True:

        if len(connections) == 0:
            break

        for conn in connections:
            if conn.closed:
                connections.remove(conn)
                for topic in topics.keys():
                    topics[topic].remove(conn)

            elif conn.poll():
                data = conn.recv()
                _type = data['type']

                funcs[_type](data, conn)
コード例 #17
0
 def _run_user_proc(user: pwd.struct_passwd, pipe: Connection) -> None:
     os.setgid(user.pw_gid)
     os.setuid(user.pw_uid)
     os.environ[
         "DBUS_SESSION_BUS_ADDRESS"
     ] = f"unix:path=/run/user/{user.pw_uid}/bus"
     log.debug(f"Subprocess created with uid={os.getuid()} and gid={os.getgid()}")
     try:
         temp_noti: Notify.Notification = Notify.Notification.new("No message.")
         while not pipe.closed:
             log.debug(
                 f"Blocking for new notifications from {pipe} on subprocess for user {user}"
             )
             try:
                 input = pipe.recv()
             except KeyboardInterrupt:
                 input = "QUIT"
             if isinstance(input, Notification):
                 temp_noti.update(input.summary, input.body, input.app_icon)
                 temp_noti.set_hint_byte("urgency", input.urgency.value)
                 try:
                     temp_noti.show()
                 except GDBusError as e:
                     log.exception("Failed to show notification.")
             elif isinstance(input, str) and input == "QUIT":
                 log.debug(f"Quit message received. Shutting down.")
                 pipe.close()
                 break
     except EOFError:
         log.debug(f"Pipe closed {pipe} on subprocess for user {user}")
コード例 #18
0
ファイル: Device.py プロジェクト: wuyasec/urh
    def device_send(cls, ctrl_connection: Connection, send_config: SendConfig, dev_parameters: OrderedDict):
        if not cls.init_device(ctrl_connection, is_tx=True, parameters=dev_parameters):
            return False

        if cls.ASYNCHRONOUS:
            cls.enter_async_send_mode(send_config.get_data_to_send)
        else:
            cls.prepare_sync_send(ctrl_connection)

        exit_requested = False
        buffer_size = cls.CONTINUOUS_SEND_BUFFER_SIZE if send_config.continuous else cls.SEND_BUFFER_SIZE
        if not cls.ASYNCHRONOUS and buffer_size == 0:
            logger.warning("Send buffer size is zero!")

        while not exit_requested and not send_config.sending_is_finished():
            if cls.ASYNCHRONOUS:
                time.sleep(0.5)
            else:
                cls.send_sync(send_config.get_data_to_send(buffer_size))

            while ctrl_connection.poll():
                result = cls.process_command(ctrl_connection.recv(), ctrl_connection, is_tx=True)
                if result == cls.Command.STOP.name:
                    exit_requested = True
                    break

        if exit_requested:
            logger.debug("{}: exit requested. Stopping sending".format(cls.__class__.__name__))
        if send_config.sending_is_finished():
            logger.debug("{}: sending is finished.".format(cls.__class__.__name__))

        cls.shutdown_device(ctrl_connection)
        ctrl_connection.close()
def get_last_message(connection: Connection) -> dict:
    message = {}
    try:
        while connection.poll():
            message = connection.recv()
    except EOFError:
        pass
    return message
コード例 #20
0
ファイル: example_analyzer.py プロジェクト: woodnsec/grapl
def analyzer(graph: Subgraph, sender: Connection):
    try:
        print('analyzing')
        client = DgraphClient(DgraphClientStub('db.mastergraph:9080'))
        _analyzer(client, graph, sender)
    except Exception as e:
        print('analyzer failed: {}'.format(e))
        sender.send(None)
コード例 #21
0
ファイル: lulapy.py プロジェクト: JoseitoOliveira/lulapy
def close(data, conn: Connection):
    connections.remove(conn)
    for topic in topics.keys():
        if conn in topics[topic]:
            topics[topic].remove(conn)

    if not conn.closed:
        conn.close()
コード例 #22
0
ファイル: multiprocessing_misc.py プロジェクト: MC-8/misc
def Pconsumer_fcn(Pconn: Connection, loopCondition: Value):
    counter = 0
    while loopCondition.value == 1:
        if Pconn.poll(0.1):
            _ = Pconn.recv()
            counter += 1
    Pconn.close()
    print(f"Consumer msg: Received: {counter} messages\n")
コード例 #23
0
ファイル: USRP.py プロジェクト: Cyber-Forensic/urh
 def setup_device(cls, ctrl_connection: Connection, device_identifier):
     ret = usrp.open(device_identifier)
     ctrl_connection.send("OPEN:" + str(ret))
     success = ret == 0
     if success:
         device_repr = usrp.get_device_representation()
         ctrl_connection.send(device_repr)
     return success
コード例 #24
0
 def setup_device(cls, ctrl_connection: Connection, device_identifier):
     ret = usrp.open(device_identifier)
     ctrl_connection.send("OPEN:" + str(ret))
     success = ret == 0
     if success:
         device_repr = usrp.get_device_representation()
         ctrl_connection.send(device_repr)
     return success
コード例 #25
0
 def _log(self, logstring, conn: Connection):
     """
     Use remote logging on Colony.
     """
     conn.send(
         Msg("%s '%s': %s" %
             (self.__class__.__name__, self._kwargs['name']
              if 'name' in self._kwargs else 'noname', logstring)))
コード例 #26
0
ファイル: parallel.py プロジェクト: joshis1/Python
def _find_prime(nbits: int, pipe: Connection) -> None:
    while True:
        integer = rsa.randnum.read_random_odd_int(nbits)

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return
コード例 #27
0
def _fast_run_command_target(command: List[str],
                             w: Connection,
                             stdin: Optional[int] = None,
                             stdout: Optional[int] = None,
                             stderr: Optional[int] = None) -> None:
    """Low-level command invoker with low overhead.

        Writes the CommandResult instance to the provided connection w.
        Uses pure os wrappers around libc commands, so it's pretty fast.
    """

    # this function is to be run in a dedicated process anyway, so we can overwrite
    # current process' fds for more accurate child execution time measurement
    if stdin is not None:
        os.dup2(stdin, 0, inheritable=True)

    if stdout is not None:
        os.dup2(stdout, 1, inheritable=True)

    if stderr is not None:
        os.dup2(stderr, 2, inheritable=True)

    _close_all_fds(STANDARD_FDS | {w.fileno()})

    pid = os.fork()
    if pid == 0:  # in child
        os.close(w.fileno())
        os.execvp(command[0], command)
        os._exit(1)  # this should never happen (unless execvp fails)

    opts = os.WUNTRACED
    start_time = time.monotonic()
    _, status = os.waitpid(pid, opts)
    end_time = time.monotonic()

    rusage = resource.getrusage(resource.RUSAGE_CHILDREN)

    real_time = end_time - start_time
    cpu_time = rusage.ru_utime + rusage.ru_stime
    max_memory = rusage.ru_maxrss

    # mac os shows result in bytes, normal OSes -- in kilobytes
    if platform.platform() != 'darwin':
        max_memory *= 1024

    if os.WIFSIGNALED(status):
        code = 128 + os.WTERMSIG(status)
    else:
        code = os.WEXITSTATUS(status)

    result = CommandStats(
        return_code=code,
        real_time=math.ceil(real_time * 1000),
        cpu_time=math.ceil(cpu_time * 1000),
        max_memory=math.ceil(max_memory),
    )

    w.send(result)
コード例 #28
0
def show_feed(index: int, pipe: Connection):
    name = "Cam " + str(index)
    size = big
    print("running size:", size)
    cap = cv2.VideoCapture(index, backend)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, size[0])
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, size[1])
    save_dir = "C:/users/phill/companion app save folder/"
    vid_ext = ".avi"
    codec = "DIVX"
    frame_size = big
    fps = 30

    writer = cv2.VideoWriter()
    self.writer = cv2.VideoWriter(
        save_dir + timestamp + self.name + '_output' + vid_ext,
        cv2.VideoWriter_fourcc(*codec), fps, frame_size)
    start = time.time()
    num_frames = 0
    resize = 0
    while True:
        if pipe.poll():
            msg = pipe.recv()
            if msg == "small":
                resize = 1
            elif msg == "big":
                resize = 2
            elif msg == "reset":
                resize = 0
            elif msg == "close":
                cv2.destroyWindow(name)
                cap.release()
                break
        ret, frame = cap.read()
        if ret and frame is not None:
            if resize == 1:
                frame = imutils.resize(frame, width=small[0])
            elif resize == 2:
                frame = imutils.resize(frame, width=big[0])
            cv2.imshow(name, frame)
            num_frames += 1
        else:
            print("Cam failed")
            cv2.destroyWindow(name)
            cap.release()
            break
        keypress = cv2.waitKey(1) % 0xFF
        if keypress == ord('q'):
            break
    end = time.time()
    seconds = end - start
    if seconds > 0:
        fps = num_frames / seconds
    else:
        fps = 0
    print("time taken:", seconds, "fps:", fps, "camera:", index,
          "frames handled:", num_frames)
    cap.release()
コード例 #29
0
ファイル: USRP.py プロジェクト: starling021/uh
 def init_device(cls, ctrl_connection: Connection, is_tx: bool,
                 parameters: OrderedDict):
     usrp.set_tx(is_tx)
     success = super().init_device(ctrl_connection, is_tx, parameters)
     if success:
         ctrl_connection.send(
             "Current antenna is {} (possible antennas: {})".format(
                 usrp.get_antenna(), ", ".join(usrp.get_antennas())))
     return success
コード例 #30
0
ファイル: HackRF.py プロジェクト: Cyber-Forensic/urh
    def shutdown_device(cls, ctrl_conn: Connection):
        logger.debug("HackRF: closing device")
        ret = hackrf.close()
        ctrl_conn.send("CLOSE:" + str(ret))

        ret = hackrf.exit()
        ctrl_conn.send("EXIT:" + str(ret))

        return True
コード例 #31
0
def log_data(conn: Connection, cs_logger):
    print('Starting log...')
    while True:
        data = conn.recv()
        if not data:
            conn.close()
            print('Closing...')
            return
        cs_logger.info(data)
コード例 #32
0
ファイル: HackRF.py プロジェクト: wuyasec/urh
    def shutdown_device(cls, ctrl_conn: Connection):
        logger.debug("HackRF: closing device")
        ret = hackrf.close()
        ctrl_conn.send("CLOSE:" + str(ret))

        ret = hackrf.exit()
        ctrl_conn.send("EXIT:" + str(ret))

        return True
コード例 #33
0
ファイル: HackRF.py プロジェクト: NickMinnellaCS96/urh
    def setup_device(cls, ctrl_connection: Connection, device_identifier):
        ret = hackrf.setup(device_identifier)
        msg = "SETUP"
        if device_identifier:
            msg += " ({})".format(device_identifier)
        msg += ": "+str(ret)
        ctrl_connection.send(msg)

        return ret == 0
コード例 #34
0
ファイル: rawsocket.py プロジェクト: gumblex/orizonhub
    class RawSocketHandler(socketserver.BaseRequestHandler):
        def setup(self):
            registry.append(self)

        def handle(self):
            self.conn = Connection(self.request.detach())
            while self.conn._handle:
                try:
                    obj = json.loads(self.conn.recv_bytes().decode('utf-8'))
                except EOFError:
                    break
                if obj['type'] == 'message':
                    bus.post(nt_from_dict(Message, obj['message'], None))
                    self.conn.send_bytes(json.dumps({'ret': True}).encode('utf-8'))
                elif obj['type'] == 'request':
                    m = bus.post_sync(nt_from_dict(Message, obj['message'], None))
                    if m:
                        ret = {"ret": True, "response": m._asdict()}
                    else:
                        ret = {"ret": False, "response": None}
                    self.conn.send_bytes(json.dumps(ret).encode('utf-8'))

        def send(self, msg):
            if isinstance(msg, Message):
                ret = {"type": "message", "message": msg._asdict()}
            else:
                ret = {"type": "response", "response": msg._asdict()}
            self.conn.send_bytes(json.dumps(ret).encode('utf-8'))

        def finish(self):
            registry.remove(self)

        def close(self):
            self.conn.close()
コード例 #35
0
ファイル: LimeSDR.py プロジェクト: Cyber-Forensic/urh
    def setup_device(cls, ctrl_connection: Connection, device_identifier):
        ret = limesdr.open()
        ctrl_connection.send("OPEN:" + str(ret))
        limesdr.disable_all_channels()
        if ret != 0:
            return False

        ret = limesdr.init()
        ctrl_connection.send("INIT:" + str(ret))

        return ret == 0
コード例 #36
0
ファイル: SoundCard.py プロジェクト: NickMinnellaCS96/urh
 def prepare_sync_send(cls, ctrl_connection: Connection):
     try:
         cls.pyaudio_stream = cls.pyaudio_handle.open(format=pyaudio.paFloat32,
                                                      channels=2,
                                                      rate=cls.SAMPLE_RATE,
                                                      output=True)
         ctrl_connection.send("Successfully started pyaudio stream")
         return 0
     except Exception as e:
         logger.exception(e)
         ctrl_connection.send("Failed to start pyaudio stream")
コード例 #37
0
ファイル: SoundCard.py プロジェクト: NickMinnellaCS96/urh
 def prepare_sync_receive(cls, ctrl_connection: Connection):
     try:
         cls.pyaudio_stream = cls.pyaudio_handle.open(format=pyaudio.paFloat32,
                                                      channels=2,
                                                      rate=cls.SAMPLE_RATE,
                                                      input=True,
                                                      frames_per_buffer=cls.CHUNK_SIZE)
         ctrl_connection.send("Successfully started pyaudio stream")
         return 0
     except Exception as e:
         logger.exception(e)
         ctrl_connection.send("Failed to start pyaudio stream")
コード例 #38
0
ファイル: live_spectrogram.py プロジェクト: jopohl/urh
def generate_data(connection: Connection, num_samples=32768):
    frequency = 0.1
    divisor = 200
    pos = 0
    while True:
        result = np.zeros(num_samples, dtype=np.complex64)
        result.real = np.cos(2 * np.pi * frequency * np.arange(pos, pos + num_samples))
        result.imag = np.sin(2 * np.pi * frequency * np.arange(pos, pos + num_samples))
        pos += num_samples
        if pos / num_samples >= divisor:
            frequency *= 2
            if frequency >= 1:
                frequency = 0.1
            pos = 0
        connection.send(result)
コード例 #39
0
ファイル: Device.py プロジェクト: jopohl/urh
    def device_send(cls, ctrl_connection: Connection, send_config: SendConfig, dev_parameters: OrderedDict):
        if not cls.init_device(ctrl_connection, is_tx=True, parameters=dev_parameters):
            ctrl_connection.send("failed to start tx mode")
            return False

        if cls.ASYNCHRONOUS:
            ret = cls.enter_async_send_mode(send_config.get_data_to_send)
        else:
            ret = cls.prepare_sync_send(ctrl_connection)

        if ret != 0:
            ctrl_connection.send("failed to start tx mode")
            return False

        exit_requested = False
        buffer_size = cls.CONTINUOUS_TX_CHUNK_SIZE if send_config.continuous else cls.SYNC_TX_CHUNK_SIZE
        if not cls.ASYNCHRONOUS and buffer_size == 0:
            logger.warning("Send buffer size is zero!")

        ctrl_connection.send("successfully started tx mode")

        while not exit_requested and not send_config.sending_is_finished():
            if cls.ASYNCHRONOUS:
                try:
                    time.sleep(0.5)
                except KeyboardInterrupt:
                    pass
            else:
                cls.send_sync(send_config.get_data_to_send(buffer_size))

            while ctrl_connection.poll():
                result = cls.process_command(ctrl_connection.recv(), ctrl_connection, is_tx=True)
                if result == cls.Command.STOP.name:
                    exit_requested = True
                    break

        if not cls.ASYNCHRONOUS:
            # Some Sync send calls (e.g. USRP) are not blocking, so we wait a bit here to ensure
            # that the send buffer on the SDR is cleared
            time.sleep(0.75)

        if exit_requested:
            logger.debug("{}: exit requested. Stopping sending".format(cls.__class__.__name__))
        if send_config.sending_is_finished():
            logger.debug("{}: sending is finished.".format(cls.__class__.__name__))

        cls.shutdown_device(ctrl_connection, is_tx=True)
        ctrl_connection.close()
コード例 #40
0
ファイル: PlutoSDR.py プロジェクト: jopohl/urh
    def setup_device(cls, ctrl_connection: Connection, device_identifier):
        device_identifier = device_identifier if isinstance(device_identifier, str) else ""
        try:
            device_identifier = re.search("\[.*\]", device_identifier).groups(0)
        except (IndexError, AttributeError):
            pass

        if not device_identifier:
            _, uris = plutosdr.scan_devices()
            try:
                device_identifier = uris[0]
            except IndexError:
                ctrl_connection.send("Could find a connected PlutoSDR")
                return False

        ret = plutosdr.open(device_identifier)
        ctrl_connection.send("OPEN ({}):{}".format(device_identifier, ret))
        return ret == 0
コード例 #41
0
 def _process(self, conn_lru_dict: LRUCacheType[multiprocessing_connection.Connection, bool],
              handle_pool: ThreadPool,
              c_recv: multiprocessing_connection.Connection,
              c_send: multiprocessing_connection.Connection,
              wait=multiprocessing_connection.wait):
     while True:
         try:
             for conn in wait(list(conn_lru_dict.keys()) + [c_recv]):
                 if conn == c_recv:
                     c_recv.recv_bytes()
                     continue
                 del conn_lru_dict[conn]
                 if not conn.closed:
                     handle_pool.spawn(self._handle, conn_lru_dict, conn, c_send)
                 else:
                     self.logger.debug("conn %s was closed" % conn)
         except OSError as e:
             if getattr(e, "winerror", 0) == 6:
                 continue
             logging.exception("OSError")
         except:
             logging.exception("error")
コード例 #42
0
ファイル: HackRF.py プロジェクト: NickMinnellaCS96/urh
    def shutdown_device(cls, ctrl_conn: Connection, is_tx: bool):
        if is_tx:
            result = hackrf.stop_tx_mode()
            ctrl_conn.send("STOP TX MODE:" + str(result))
        else:
            result = hackrf.stop_rx_mode()
            ctrl_conn.send("STOP RX MODE:" + str(result))

        result = hackrf.close()
        ctrl_conn.send("CLOSE:" + str(result))

        result = hackrf.exit()
        ctrl_conn.send("EXIT:" + str(result))

        return True
コード例 #43
0
ファイル: SoundCard.py プロジェクト: NickMinnellaCS96/urh
 def setup_device(cls, ctrl_connection: Connection, device_identifier):
     ctrl_connection.send("Initializing pyaudio...")
     try:
         cls.pyaudio_handle = pyaudio.PyAudio()
         ctrl_connection.send("Initialized pyaudio")
         return True
     except Exception as e:
         logger.exception(e)
         ctrl_connection.send("Failed to initialize pyaudio")
コード例 #44
0
ファイル: rawsocket.py プロジェクト: gumblex/orizonhub
 def handle(self):
     self.conn = Connection(self.request.detach())
     while self.conn._handle:
         try:
             obj = json.loads(self.conn.recv_bytes().decode('utf-8'))
         except EOFError:
             break
         if obj['type'] == 'message':
             bus.post(nt_from_dict(Message, obj['message'], None))
             self.conn.send_bytes(json.dumps({'ret': True}).encode('utf-8'))
         elif obj['type'] == 'request':
             m = bus.post_sync(nt_from_dict(Message, obj['message'], None))
             if m:
                 ret = {"ret": True, "response": m._asdict()}
             else:
                 ret = {"ret": False, "response": None}
             self.conn.send_bytes(json.dumps(ret).encode('utf-8'))
コード例 #45
0
ファイル: LimeSDR.py プロジェクト: NickMinnellaCS96/urh
    def init_device(cls, ctrl_connection: Connection, is_tx: bool, parameters: OrderedDict):
        if not cls.setup_device(ctrl_connection, device_identifier=parameters["identifier"]):
            return False

        limesdr.enable_channel(True, is_tx, parameters[cls.Command.SET_CHANNEL_INDEX.name])
        limesdr.set_tx(is_tx)

        for parameter, value in parameters.items():
            cls.process_command((parameter, value), ctrl_connection, is_tx)

        antennas = limesdr.get_antenna_list()
        ctrl_connection.send("Current normalized gain is {0:.2f}".format(limesdr.get_normalized_gain()))
        ctrl_connection.send("Current antenna is {0}".format(antennas[limesdr.get_antenna()]))
        ctrl_connection.send("Current chip temperature is {0:.2f}°C".format(limesdr.get_chip_temperature()))

        return True
コード例 #46
0
ファイル: HackRF.py プロジェクト: NickMinnellaCS96/urh
 def enter_async_receive_mode(cls, data_connection: Connection, ctrl_connection: Connection):
     ret = hackrf.start_rx_mode(data_connection.send_bytes)
     ctrl_connection.send("Start RX MODE:" + str(ret))
     return ret
コード例 #47
0
ファイル: PlutoSDR.py プロジェクト: jopohl/urh
 def prepare_sync_receive(cls, ctrl_connection: Connection):
     ctrl_connection.send("Initializing PlutoSDR..")
     ret = plutosdr.setup_rx(cls.SYNC_RX_CHUNK_SIZE)
     return ret
コード例 #48
0
ファイル: RTLSDR.py プロジェクト: NickMinnellaCS96/urh
 def receive_sync(cls, data_conn: Connection):
     data_conn.send_bytes(rtlsdr.read_sync())
コード例 #49
0
ファイル: RTLSDR.py プロジェクト: NickMinnellaCS96/urh
 def prepare_sync_receive(cls, ctrl_connection: Connection):
     ret = rtlsdr.reset_buffer()
     ctrl_connection.send("RESET_BUFFER:" + str(ret))
     return ret
コード例 #50
0
ファイル: RTLSDR.py プロジェクト: NickMinnellaCS96/urh
 def setup_device(cls, ctrl_connection: Connection, device_identifier):
     # identifier gets set in self.receive_process_arguments
     device_number = int(device_identifier)
     ret = rtlsdr.open(device_number)
     ctrl_connection.send("OPEN (#{}):{}".format(device_number, ret))
     return ret == 0
コード例 #51
0
ファイル: HackRF.py プロジェクト: Cyber-Forensic/urh
 def setup_device(cls, ctrl_connection: Connection, device_identifier):
     ret = hackrf.setup()
     ctrl_connection.send("SETUP:" + str(ret))
     return ret == 0
コード例 #52
0
ファイル: Device.py プロジェクト: NickMinnellaCS96/urh
    def device_receive(cls, data_connection: Connection, ctrl_connection: Connection, dev_parameters: OrderedDict):
        if not cls.init_device(ctrl_connection, is_tx=False, parameters=dev_parameters):
            ctrl_connection.send("failed to start rx mode")
            return False

        try:
            cls.adapt_num_read_samples_to_sample_rate(dev_parameters[cls.Command.SET_SAMPLE_RATE.name])
        except NotImplementedError:
            # Many SDRs like HackRF or AirSpy do not need to calculate SYNC_RX_CHUNK_SIZE
            # as default values are either fine or given by the hardware
            pass

        if cls.ASYNCHRONOUS:
            ret = cls.enter_async_receive_mode(data_connection, ctrl_connection)
        else:
            ret = cls.prepare_sync_receive(ctrl_connection)

        if ret != 0:
            ctrl_connection.send("failed to start rx mode")
            return False

        exit_requested = False
        ctrl_connection.send("successfully started rx mode")

        while not exit_requested:
            if cls.ASYNCHRONOUS:
                time.sleep(0.25)
            else:
                cls.receive_sync(data_connection)
            while ctrl_connection.poll():
                result = cls.process_command(ctrl_connection.recv(), ctrl_connection, is_tx=False)
                if result == cls.Command.STOP.name:
                    exit_requested = True
                    break

        cls.shutdown_device(ctrl_connection, is_tx=False)
        data_connection.close()
        ctrl_connection.close()
コード例 #53
0
ファイル: PlutoSDR.py プロジェクト: jopohl/urh
 def prepare_sync_send(cls, ctrl_connection: Connection):
     ctrl_connection.send("Initializing PlutoSDR...")
     ret = plutosdr.setup_tx(cls.SYNC_TX_CHUNK_SIZE // 2)
     return ret
コード例 #54
0
ファイル: AirSpy.py プロジェクト: Cyber-Forensic/urh
 def setup_device(cls, ctrl_connection: Connection, device_identifier):
     ret = airspy.open()
     ctrl_connection.send("OPEN:" + str(ret))
     return ret == 0
コード例 #55
0
 def _handle(self, conn_lru_dict: LRUCacheType[multiprocessing_connection.Connection, bool],
             conn: multiprocessing_connection.Connection, c_send: multiprocessing_connection.Connection):
     try:
         data = conn.recv_bytes()
         if not data:
             raise EOFError
         self.logger.debug("parse conn %s" % conn)
         # self.logger.debug(data)
         try:
             result = self.handle(data)
             if result is not None:
                 conn.send_bytes(result)
         except Exception:
             self.logger.exception("handle error")
         conn_lru_dict[conn] = True
         c_send.send_bytes(b'ok')
     except OSError:
         self.logger.debug("conn %s was closed" % conn)
         conn.close()
     except EOFError:
         self.logger.debug("conn %s was eof" % conn)
         conn.close()
     except BrokenPipeError:
         self.logger.debug("conn %s was broken" % conn)
         conn.close()
コード例 #56
0
ファイル: LimeSDR.py プロジェクト: NickMinnellaCS96/urh
 def prepare_sync_send(cls, ctrl_connection: Connection):
     ctrl_connection.send("Initializing stream...")
     limesdr.setup_stream(cls.SEND_FIFO_SIZE)
     ret = limesdr.start_stream()
     ctrl_connection.send("Initialize stream:{0}".format(ret))
     return ret
コード例 #57
0
ファイル: SoundCard.py プロジェクト: jopohl/urh
 def receive_sync(cls, data_conn: Connection):
     if cls.pyaudio_stream:
         data_conn.send_bytes(cls.pyaudio_stream.read(cls.CHUNK_SIZE, exception_on_overflow=False))
コード例 #58
0
ファイル: USRP.py プロジェクト: jopohl/urh
 def prepare_sync_receive(cls, ctrl_connection: Connection):
     ctrl_connection.send("Initializing stream...")
     usrp.setup_stream()
     return usrp.start_stream(cls.SYNC_RX_CHUNK_SIZE)
コード例 #59
0
ファイル: USRP.py プロジェクト: jopohl/urh
 def prepare_sync_send(cls, ctrl_connection: Connection):
     ctrl_connection.send("Initializing stream...")
     usrp.setup_stream()
     ret = usrp.start_stream(0)
     ctrl_connection.send("Initialize stream:{0}".format(ret))
     return ret
コード例 #60
0
ファイル: SoundCard.py プロジェクト: NickMinnellaCS96/urh
 def receive_sync(cls, data_conn: Connection):
     if cls.pyaudio_stream:
         data_conn.send_bytes(cls.pyaudio_stream.read(cls.CHUNK_SIZE))