Пример #1
0
def nexus_transfer():
    obj = Transfer("NEXUS", use_log=True)
    obj.start([socket_m.getfqdn(), "50100"])

    callback_params = {"run_loop": True}

    # number to receive + open signal + close signal
    try:
        while callback_params["run_loop"]:
            try:
                obj.read(callback_params, open_callback, read_callback,
                         close_callback)
            except KeyboardInterrupt:
                break
            except Exception:
                logging.error("nexus_transfer break", exc_info=True)
                break
    finally:
        obj.stop()
Пример #2
0
def main():
    sender_thread = SenderAsThread()
    sender_thread.start()

    obj = Transfer("NEXUS", use_log=True)
    obj.start([socket_m.get_fqdn(), "50100"])

    callback_params = {"run_loop": True}

    try:
        while callback_params["run_loop"]:
            try:
                obj.read(callback_params, open_callback, read_callback,
                         close_callback)
            except KeyboardInterrupt:
                break
            except Exception:
                logging.error("break", exc_info=True)
                break
    finally:
        sender_thread.stop()
        obj.stop()

    print("\n==== TEST END: nexus transfer ====\n")
Пример #3
0
class NexusReceiver:
    def __init__(self):
        self.transfer = None

        try:
            params = argument_parsing()
        except Exception:
            self.log = self.get_logger()
            raise

        self.log = self.get_logger()

        self.whitelist = params["whitelist"]

        self.log.info("Configured whitelist: {0}".format(self.whitelist))

        self.target_dir = os.path.normpath(params["target_dir"])
        self.data_ip = params["data_stream_ip"]
        self.data_port = params["data_stream_port"]

        self.log.info("Writing to directory '{0}'".format(self.target_dir))

        self.transfer = Transfer("NEXUS", use_log=True)

        try:
            self.run()
        except KeyboardInterrupt:
            pass
        except Exception:
            self.log.error("Stopping due to unknown error condition",
                           exc_info=True)
        finally:
            self.stop()

    def get_logger(self):
        logger = logging.getLogger("NexusReceiver")
        return logger

    def open_callback(self, params, filename):
        target_file = os.path.join(BASE_DIR, "data", "target", "local",
                                   filename)

        try:
            params["target_fp"] = open(target_file, "wb")
        except IOError as e:
            # errno.ENOENT == "No such file or directory"
            if e.errno == errno.ENOENT:
                try:
                    target_path = os.path.split(target_file)[0]
                    print("target_path", target_path)
                    os.makedirs(target_path)

                    params["target_fp"] = open(target_file, "wb")
                    print("New target directory created:", target_path)
                except Exception:
                    raise
            else:
                raise
        print(params, filename)

    def read_callback(self, params, received_data):
        metadata = received_data[0]
        data = received_data[1]
        print(params, metadata)

        params["target_fp"].write(data)

    def close_callback(self, params, data):
        print(params, data)
        params["target_fp"].close()

    def run(self):
        callback_params = {"target_fp": None}

        try:
            self.transfer.start([self.data_ip, self.data_port], self.whitelist)
#            self.transfer.start(self.data_port)
        except Exception:
            self.log.error("Could not initiate stream", exc_info=True)
            raise

        # run loop, and wait for incoming messages
        while True:
            try:
                data = self.transfer.read(callback_params, self.open_callback,
                                          self.read_callback,
                                          self.close_callback)
                logging.debug("Retrieved: " + str(data)[:100])


#                if data == "CLOSE_FILE":
#                    break
            except KeyboardInterrupt:
                break
            except Exception:
                self.log.error("Could not read")
                raise

    def stop(self):
        if self.transfer:
            self.log.info("Shutting down receiver...")
            self.transfer.stop()
            self.transfer = None

    def __exit__(self):
        self.stop()

    def __del__(self):
        self.stop()