if (FALSE) {
library(rzmq)
# Create a set of REP-REQ sockets that
# have a Send, Receive, Send, Receive, ...
# pattern.
context = init.context()
in.socket = init.socket(context,"ZMQ_REP")
bind.socket(in.socket,"tcp://*:5557")
out.socket = init.socket(context,"ZMQ_REQ")
connect.socket(out.socket,"tcp://*:5557")
# Poll the REP and REQ sockets for all events.
events <- poll.socket(list(in.socket, out.socket),
list(c("read", "write", "error"),
c("read", "write", "error")),
timeout=0L)
# The REQ socket is writable without blocking.
paste("Is upstream REP socket readable without blocking?", events[[1]]$read)
paste("Is upstream REP socket writable without blocking?", events[[1]]$write)
paste("Is downstream REQ socket readable without blocking?", events[[2]]$read)
paste("Is downstream REQ socket writable without blocking?", events[[2]]$write)
# Send a message to the REP socket from the REQ socket. The
# REQ socket must respond before the REP socket can send
# another message.
send.socket(out.socket, "Hello World")
events <- poll.socket(list(in.socket, out.socket),
list(c("read", "write", "error"),
c("read", "write", "error")),
timeout=0L)
# The incoming message is readable on the REP socket.
paste("Is upstream REP socket readable without blocking?", events[[1]]$read)
paste("Is upstream REP socket writable without blocking?", events[[1]]$write)
paste("Is downstream REQ socket readable without blocking?", events[[2]]$read)
paste("Is downstream REQ socket writable without blocking?", events[[2]]$write)
receive.socket(in.socket)
events <- poll.socket(list(in.socket, out.socket),
list(c("read", "write", "error"),
c("read", "write", "error")),
timeout=0L)
# The REQ socket is waiting for a response from the REP socket.
paste("Is upstream REP socket readable without blocking?", events[[1]]$read)
paste("Is upstream REP socket writable without blocking?", events[[1]]$write)
paste("Is downstream REQ socket readable without blocking?", events[[2]]$read)
paste("Is downstream REQ socket writable without blocking?", events[[2]]$write)
send.socket(in.socket, "Greetings")
events <- poll.socket(list(in.socket, out.socket),
list(c("read", "write", "error"),
c("read", "write", "error")),
timeout=0L)
# The REP response is waiting to be read on the REQ socket.
paste("Is upstream REP socket readable without blocking?", events[[1]]$read)
paste("Is upstream REP socket writable without blocking?", events[[1]]$write)
paste("Is downstream REQ socket readable without blocking?", events[[2]]$read)
paste("Is downstream REQ socket writable without blocking?", events[[2]]$write)
# Complete the REP-REQ transaction cycle by reading
# the REP response.
receive.socket(out.socket)
}
Run the code above in your browser using DataLab