Java Code Examples for org.zeromq.ZMQ.Socket#recv()

The following examples show how to use org.zeromq.ZMQ.Socket#recv() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZFrame.java    From aion with MIT License 6 votes vote down vote up
/**
 * Internal method to call recv on the socket. Does not trap any
 * ZMQExceptions but expects caling routine to handle them.
 * @param socket
 *            0MQ socket to read from
 * @return Byte array
 */
private byte[] recv(Socket socket, int flags)
{
    try {
        data = socket.recv(flags);
        more = socket.hasReceiveMore();
    } catch (ZMQException e) {
        ZMQ.Error error = ZMQ.Error.findByCode(e.getErrorCode());
        if (error == ZMQ.Error.ETERM || error == ZMQ.Error.ENOTSOCK) {
            data = null;
            more = false;
        } else {
            throw e;
        }
    }
    return data;
}
 
Example 2
Source File: Proxy.java    From aion with MIT License 6 votes vote down vote up
private static boolean msgProcessSend(Socket receiver, Socket sender) {
    byte[] msg = receiver.recv(0);
    if (msg == null) {
        return true;
    }

    byte[] msgMore = null;
    if (receiver.hasReceiveMore()) {
        msgMore = receiver.recv(0);

        if (msgMore == null) {
            return true;
        }
    }

    if (!sender.send(msg, msgMore == null ? ZMQ.DONTWAIT : ZMQ.SNDMORE)) {
        return true;
    }

    if (msgMore != null) {
        return !sender.send(msgMore, ZMQ.DONTWAIT);
    }

    return false;
}
 
Example 3
Source File: MsgExecutor.java    From aion_api with MIT License 6 votes vote down vote up
private void callbackRun(Context ctx) {
    Socket cbWorker = ctx.socket(ZMQ.DEALER);
    cbWorker.setReceiveTimeOut(RECVTIMEOUT);
    cbWorker.connect(CB_BIND_ADDR + addrBindNumber);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("connected!");
    }

    while (true) {
        byte[] rsp = cbWorker.recv(ZMQ.PAIR);
        if (this.running) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace(
                        "recv msg: [{}]", (rsp != null ? IUtils.bytes2Hex(rsp) : "= null"));
            }
            process(rsp);
        } else {
            break;
        }
    }

    LOGGER.info("closing!");
    cbWorker.close();
    LOGGER.info("closed!");
}
 
Example 4
Source File: MsgExecutor.java    From aion_api with MIT License 6 votes vote down vote up
private boolean invalidMsgHandle(Socket receiver, Socket sender) {

        byte[] msg = receiver.recv(ZMQ.PAIR);
        if (msg == null) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[invalidMsgHandle] {}", ErrId.getErrString(-322L));
            }
            return true;
        }

        if (!sender.send(msg, ZMQ.DONTWAIT)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[invalidMsgHandle] {}", ErrId.getErrString(-323L));
            }
            return true;
        }

        return false;
    }
 
Example 5
Source File: Proxy.java    From aion with MIT License 5 votes vote down vote up
private static boolean msgProcessRecv(Socket receiver, Socket sender, Socket hb) {
    byte[] msg = receiver.recv(0);
    if (msg == null) {
        return true;
    }

    byte[] msgMore = null;
    if (receiver.hasReceiveMore()) {
        msgMore = receiver.recv(0);

        if (msgMore == null) {
            return true;
        }
    }

    if (heartBeatMsg(msgMore)) {
        if (!hb.send(msg, ZMQ.SNDMORE)) {
            return true;
        }

        return !hb.send(msgMore, ZMQ.DONTWAIT);
    } else {
        if (!sender.send(msg, msgMore == null ? ZMQ.DONTWAIT : ZMQ.SNDMORE)) {
            return true;
        }

        if (msgMore != null) {
            return !sender.send(msgMore, ZMQ.DONTWAIT);
        }
    }

    return false;
}
 
Example 6
Source File: Session.java    From jupyter-kernel-jsr223 with Apache License 2.0 5 votes vote down vote up
public static void runKernelDebug(Session session) throws FileNotFoundException,
        InvalidKeyException,
        UnsupportedEncodingException,
        IOException {
    Session._DEBUG_ = true;
    ZContext ctx = new ZContext();
    Socket channel = ctx.createSocket(ZMQ.REP);
    channel.bind("tcp://127.0.0.1:2222");
    byte[] msg = channel.recv();
    String sArgs = new String(msg, StandardCharsets.UTF_8);
    String[] newArgs = sArgs.split(" ");
    channel.send("ok");
    runKernel(session, newArgs);
}
 
Example 7
Source File: MsgExecutor.java    From aion_api with MIT License 4 votes vote down vote up
private void heartBeatRun(Context ctx) {
    Socket hbWorker = ctx.socket(ZMQ.DEALER);
    hbWorker.connect(HB_BIND_ADDR + addrBindNumber);
    hbWorker.setReceiveTimeOut(1000);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("hbWorker connected!");
    }

    int hbTolerance = HB_TOLERANCE;
    byte[] hbMsg = ApiUtils.toReqHeader(this.ver, Message.Servs.s_hb, Message.Funcs.f_NA);

    while (this.running && hbTolerance > 0) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("send hb!");
        }
        if (!hbWorker.send(hbMsg, ZMQ.DONTWAIT)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("send heartbeat msg failed.");
            }
            continue;
        }

        byte[] rsp = hbWorker.recv(ZMQ.PAIR);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("recv msg: [{}]", (rsp != null ? IUtils.bytes2Hex(rsp) : "null"));
        }

        if (checkNotHbRspMsg(rsp)) {
            hbTolerance--;
        } else {
            hbTolerance = HB_TOLERANCE;
        }

        try {
            Thread.sleep(HB_POLL_MS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    if (this.running) {
        this.running = false;
        LOGGER.warn("timeout, disconnect the connection!");
        hbWorker.close();
        terminate();
        LOGGER.info("closed!");
    }
}
 
Example 8
Source File: MsgExecutor.java    From aion_api with MIT License 4 votes vote down vote up
private void workerRun(Context ctx) {
    Socket worker = ctx.socket(ZMQ.DEALER);
    worker.connect(WK_BIND_ADDR + addrBindNumber);
    worker.setReceiveTimeOut(RECVTIMEOUT);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("connected!");
    }

    while (true) {
        MsgReq msg = null;
        try {
            msg = queue.poll(RECVTIMEOUT, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (!this.running) {
            break;
        }

        if (msg != null && msg.req != null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("poll q: [{}]", IUtils.bytes2Hex(msg.hash));
            }

            if (!worker.send(msg.req, ZMQ.PAIR)) {
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("send msg failed. Msg: [{}]", IUtils.bytes2Hex(msg.req));
                }
                continue;
            }

            byte[] rsp = worker.recv(ZMQ.PAIR);
            if (this.running) {
                if (rsp == null) {
                    if (LOGGER.isErrorEnabled()) {
                        LOGGER.error("recv msg: [null]");
                    }
                    return;
                }

                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("recv msg: [{}]", IUtils.bytes2Hex(rsp));
                }
                process(rsp);
            } else {
                break;
            }
        }
    }

    LOGGER.info("closing!");
    worker.close();
    LOGGER.info("closed!");
}
 
Example 9
Source File: MsgExecutor.java    From aion_api with MIT License 4 votes vote down vote up
private boolean msgHandle(
        Socket receiver, Socket sender, Socket sender2, Socket sender3, Socket sender4) {

    byte[] msg = receiver.recv(ZMQ.PAIR);
    if (msg == null) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("[msgHandle] {}", ErrId.getErrString(-322L));
        }
        return false;
    }

    if (msg.length < ApiUtils.RSP_HEADER_NOHASH_LEN) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("[msgHandle] {}", ErrId.getErrString(-321L));
        }
        return false;
    }

    if (msg[1] > Message.Retcode.r_tx_Recved_VALUE) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("[msgHandle]" + " forward to txSender.");
        }

        if (!sender2.send(msg, ZMQ.PAIR)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[msgHandle] txSender{}", ErrId.getErrString(-323L));
            }
            return false;
        }

    } else if (msg[1] == Message.Retcode.r_heartbeatReturn_VALUE) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("[msgHandle] forward to hbDealer.");
        }
        if (!sender4.send(msg, ZMQ.PAIR)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("[msgHandle] hbDealer{}", ErrId.getErrString(-323L));
            }
            return false;
        }
    } else {
        if (msg[2] == 0) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("[msgHandle] forward to nonBlockSender.");
            }
            if (!sender3.send(msg, ZMQ.PAIR)) {
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("[msgHandle] nonBlockSender{}", ErrId.getErrString(-323L));
                }
                return false;
            }
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("[msgHandle] forward to normalSender.");
            }
            if (!sender.send(msg, ZMQ.PAIR)) {
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("[msgHandle] regularSender{}", ErrId.getErrString(-323L));
                }
                return false;
            }
        }
    }

    return true;
}