Beispiel #1
0
def demo_msg_mysql2ftp():
    msg = MSG()
    msg.origination = URL().init("mysql").check
    msg.destination = URL().init("ftp").check
    msg.treatment = URL().init("tcp")
    case = CASE()
    case.origination = msg.origination
    case.destination = msg.destination
    msg.activity = "init"
    msg.case = case
    msg.add_datum("This is a test string", path=URL().init("file"))
    msg.add_datum("中文UTF-8编码测试", path=URL().init("file"))
    path = "..\README\Babelor-设计.png"
    with open(path, "rb") as f:
        bytes_f = f.read()
        url = URL().init("file")
        url.path = path
        msg.add_datum(bytes_f, url)

    msg_string = str(msg)

    new_msg = MSG(msg_string)
    new_bytes_f = new_msg.read_datum(3)["stream"]
    new_path = "..\README\Babelor-设计-new.png"
    with open(new_path, "wb") as f:
        f.write(new_bytes_f)
Beispiel #2
0
def demo_numpy():
    arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    msg = MSG()
    msg.add_datum(arr)
    message = str(msg)
    new_msg = MSG(message)
    print(msg)
    print(new_msg)
    print(new_msg.read_datum(0)["stream"])
Beispiel #3
0
def demo_excel():
    path = "C:/Users/geyua/PycharmProjects/Babelor/data/dir1/20190505.xlsx"
    df = pd.read_excel(path)
    msg = MSG()
    msg.add_datum(df, path)
    message = str(msg)
    new_msg = MSG(message)
    print(msg)
    print(new_msg)
Beispiel #4
0
def try_push(url: str):
    msg = MSG()
    msg.origination = URL("tcp://127.0.0.1:10001")
    msg.destination = URL("tcp://127.0.0.1:10001")
    mq = MQ(url)
    for i in range(0, 10000, 1):
        # print("push msg:", msg)
        msg.activity = str(i)
        # print("push msg:", i, msg)
        mq.push(msg)
Beispiel #5
0
def try_publish():
    msg = MSG()
    msg.origination = URL("tcp://127.0.0.1:10001")
    mq = MQ(URL("tcp://*:10001"))
    print("publish msg:", msg)
    mq.publish(msg)
Beispiel #6
0
def try_request():
    msg = MSG()
    msg.origination = URL("tcp://127.0.0.1:10001")
    mq = MQ(URL("tcp://127.0.0.1:10001"))
    msg = mq.request(msg)
Beispiel #7
0

def encrypter(url):
    myself = TEMPLE(url)
    myself.open(role="encrypter", func=func_encrypter)


def receiver(url):
    myself = TEMPLE(url)
    myself.open(role="receiver")


def receiver_init():
    # -————————————------------------------ MESSAGE -----
    case = CASE("{0}#{1}".format(origination_url, destination_url))
    receiver_msg = MSG()
    receiver_msg.case = case
    # -————————————------------------------ RECEIVER ----
    receiver_msg.origination = edge_node_url["inner"]
    receiver_msg.destination = destination_url
    # logging.warning("RECEIVER::INIT::{0} send:{1}".format(receiver_url["inner"], receiver_msg))
    recv_init = MQ(receiver_url["outer"])
    recv_init.push(receiver_msg)


def sender_init():
    # -————————————------------------------ MESSAGE -----
    case = CASE("{0}#{1}".format(origination_url, destination_url))
    sender_msg = MSG()
    sender_msg.case = case
    # -————————————------------------------ SENDER ------
Beispiel #8
0
def first_in_last_out(conn: str, me: str, queue_ctrl: Queue, pipe_in: Pipe,
                      pipe_out: Pipe):
    """
    # 先进后出 / 只进
    # 控制信号(启动)--> 输入队列(推入)--> 控制信号(需反馈)--> 输出队列(推出)
    :param conn: str            # 套接字
    :param me: str              # 传输方式 ( "REPLY", "SUBSCRIBE", "PULL")
    :param queue_ctrl: Queue    # 控制队列 ("is_active", "is_response"):(bool, bool)
    :param pipe_in: Pipe        # 输入队列 (MSG,)
    :param pipe_out: pipe       # 输出队列 (MSG,)
    :return: None
    """
    context = zmq.Context()
    # ------- REPLY ----------------------------
    if me in ["REPLY"]:
        socket = context.socket(zmq.REP)
        socket.bind(conn)
        has_response = True
    # ------- REPLY ----------------------------
    elif me in ["PUBLISH"]:
        socket = context.socket(zmq.PUB)
        socket.bind(conn)
        has_response = False
    # ------- PULL -----------------------------
    elif me in ["PULL"]:
        socket = context.socket(zmq.PULL)
        socket.bind(conn)
        has_response = False
    # ------- DEFAULT: PULL -----------------------------
    else:
        socket = context.socket(zmq.PULL)
        socket.bind(conn)
        has_response = False
    logging.debug("ZMQ::FILO::{0} bind:{1}".format(me, conn))
    # ------------------------------------- QUEUE
    is_active = queue_ctrl.get()
    while is_active:
        if queue_ctrl.empty():
            # RECV --------------------------------
            message_in = socket.recv()
            logging.debug("ZMQ::FILO::{0}::{1} recv:{2}".format(
                me, conn, message_in))
            msg_in = MSG(message_in.decode(CONFIG.Coding))
            pipe_in.send(msg_in)
            logging.debug("ZMQ::FILO::{0}::{1}::PIPE IN send:{2}".format(
                me, conn, msg_in))
            # SEND --------------------------------
            if has_response:
                try:
                    msg_out = pipe_out.recv()
                    logging.debug(
                        "ZMQ::FILO::{0}::{1}::PIPE OUT recv:{2}".format(
                            me, conn, msg_out))
                    message_out = str(msg_out).encode(CONFIG.Coding)
                    socket.send(message_out)
                    logging.debug("ZMQ::FILO::{0}::{1} send:{2}".format(
                        me, conn, message_out))
                except EOFError:
                    is_active = False
        else:
            is_active = queue_ctrl.get()
    else:
        queue_ctrl.close()
Beispiel #9
0
def first_out_last_in(conn: str, me: str, queue_ctrl: Queue, pipe_in: Pipe,
                      pipe_out: Pipe):
    """
    # 先出后进 / 只出
    # 控制信号 --> 输出队列 --> 输出队列 --> 反馈信号 --> 输入队列
    :param conn: str            # 套接字    "tcp://<hostname>:<port>"
    :param me: str              # 传输方式  ["REQUEST", "SUBSCRIBE", "PUSH"]
    :param queue_ctrl: Queue    # 控制队列  ("is_active",):(bool,)
    :param pipe_in: Pipe        # 输入队列  ("msg_in",):(MSG,)
    :param pipe_out: Pipe       # 输出队列  ("msg_out",):(MSG,)
    :return: None
    """
    context = zmq.Context()
    # ------- REQUEST ----------------------------
    if me in ["REQUEST"]:
        socket = context.socket(zmq.REQ)
        socket.connect(conn)
        handshake, has_response = 0, True
    # ------- SUBSCRIBE --------------------------
    elif me in ["SUBSCRIBE"]:
        socket = context.socket(zmq.SUB)
        socket.connect(conn)
        handshake, has_response = 1, True
    # ------- PUSH ------------------------------
    elif me in ["PUSH"]:
        socket = context.socket(zmq.PUSH)
        socket.connect(conn)
        handshake, has_response = 0, False
    # ------- DEFAULT: PUSH ---------------------
    else:
        socket = context.socket(zmq.PUSH)
        socket.connect(conn)
        handshake, has_response = 0, False
    logging.debug("ZMQ::FOLI::{0} connect:{1}".format(me, conn))
    # ------------------------------------- QUEUE
    is_active = queue_ctrl.get()
    while is_active:
        if queue_ctrl.empty():
            # SEND --------------------------------
            if handshake == 1:
                socket.setsockopt(zmq.SUBSCRIBE, '')
                logging.debug("ZMQ::FOLI::{0}::{1} send:{2}".format(
                    me, conn, "zmq.SUBSCRIBE"))
            else:
                try:
                    msg_out = pipe_out.recv()
                    logging.debug(
                        "ZMQ::FOLI::{0}::{1}::PIPE OUT recv:{2}".format(
                            me, conn, msg_out))
                    message_out = str(msg_out).encode(CONFIG.Coding)
                    socket.send(message_out)
                    logging.debug("ZMQ::FOLI::{0}::{1} send:{2}".format(
                        me, conn, message_out))
                except EOFError:
                    is_active = False
            # RECV --------------------------------
            if has_response:
                message_in = socket.recv()
                logging.debug("ZMQ::FOLI::{0}::{1} recv:{2}".format(
                    me, conn, message_in))
                msg_in = MSG(message_in.decode(CONFIG.Coding))
                pipe_in.send(msg_in)
                logging.debug("ZMQ::FOLI::{0}::{1}::PIPE IN send:{2}".format(
                    me, conn, msg_in))
        else:
            is_active = queue_ctrl.get()
    else:
        queue_ctrl.close()