use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.
the class TooManyOpenFilesTester method poll.
/**
* Polls while keeping the selector opened.
* @param socket the socket to poll
*/
private void poll(ZContext ctx, Socket socket) {
// Poll socket for a reply, with timeout
Poller poller = ctx.createPoller(1);
poller.register(socket, Poller.POLLIN);
int rc = poller.poll(REQUEST_TIMEOUT);
assertThat(rc, is(1));
boolean readable = poller.pollin(0);
assertThat(readable, is(true));
}
use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.
the class bstarsrv method main.
// .split main task
// This is our main task. First we bind/connect our sockets with our
// peer and make sure we will get state messages correctly. We use
// three sockets; one to publish state, one to subscribe to state, and
// one for client requests/replies:
public static void main(String[] argv) {
// Arguments can be either of:
// -p primary server, at tcp://localhost:5001
// -b backup server, at tcp://localhost:5002
ZContext ctx = new ZContext();
Socket statepub = ctx.createSocket(ZMQ.PUB);
Socket statesub = ctx.createSocket(ZMQ.SUB);
statesub.subscribe(ZMQ.SUBSCRIPTION_ALL);
Socket frontend = ctx.createSocket(ZMQ.ROUTER);
bstarsrv fsm = new bstarsrv();
if (argv.length == 1 && argv[0].equals("-p")) {
System.out.printf("I: Primary active, waiting for backup (passive)\n");
frontend.bind("tcp://*:5001");
statepub.bind("tcp://*:5003");
statesub.connect("tcp://localhost:5004");
fsm.state = State.STATE_PRIMARY;
} else if (argv.length == 1 && argv[0].equals("-b")) {
System.out.printf("I: Backup passive, waiting for primary (active)\n");
frontend.bind("tcp://*:5002");
statepub.bind("tcp://*:5004");
statesub.connect("tcp://localhost:5003");
fsm.state = State.STATE_BACKUP;
} else {
System.out.printf("Usage: bstarsrv { -p | -b }\n");
ctx.destroy();
System.exit(0);
}
// .split handling socket input
// We now process events on our two input sockets, and process these
// events one at a time via our finite-state machine. Our "work" for
// a client request is simply to echo it back.
Poller poller = ctx.createPoller(2);
poller.register(frontend, ZMQ.Poller.POLLIN);
poller.register(statesub, ZMQ.Poller.POLLIN);
// Set timer for next outgoing state message
long sendStateAt = System.currentTimeMillis() + HEARTBEAT;
while (!Thread.currentThread().isInterrupted()) {
int timeLeft = (int) ((sendStateAt - System.currentTimeMillis()));
if (timeLeft < 0)
timeLeft = 0;
int rc = poller.poll(timeLeft);
if (rc == -1)
// Context has been shut down
break;
if (poller.pollin(0)) {
// Have a client request
ZMsg msg = ZMsg.recvMsg(frontend);
fsm.event = Event.CLIENT_REQUEST;
if (fsm.stateMachine() == false)
// Answer client by echoing request back
msg.send(frontend);
else
msg.destroy();
}
if (poller.pollin(1)) {
// Have state from our peer, execute as event
String message = statesub.recvStr();
fsm.event = Event.values()[Integer.parseInt(message)];
if (fsm.stateMachine())
// Error, so exit
break;
fsm.peerExpiry = System.currentTimeMillis() + 2 * HEARTBEAT;
}
// If we timed out, send state to peer
if (System.currentTimeMillis() >= sendStateAt) {
statepub.send(String.valueOf(fsm.state.ordinal()));
sendStateAt = System.currentTimeMillis() + HEARTBEAT;
}
}
if (Thread.currentThread().isInterrupted())
System.out.printf("W: interrupted\n");
// Shutdown sockets and context
ctx.destroy();
}
use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.
the class clonecli5 method run.
public void run() {
ZContext ctx = new ZContext();
Socket snapshot = ctx.createSocket(ZMQ.DEALER);
snapshot.connect("tcp://localhost:5556");
Socket subscriber = ctx.createSocket(ZMQ.SUB);
subscriber.connect("tcp://localhost:5557");
subscriber.subscribe(SUBTREE.getBytes(ZMQ.CHARSET));
Socket publisher = ctx.createSocket(ZMQ.PUSH);
publisher.connect("tcp://localhost:5558");
Map<String, kvmsg> kvMap = new HashMap<String, kvmsg>();
// get state snapshot
snapshot.sendMore("ICANHAZ?");
snapshot.send(SUBTREE);
long sequence = 0;
while (true) {
kvmsg kvMsg = kvmsg.recv(snapshot);
if (kvMsg == null)
// Interrupted
break;
sequence = kvMsg.getSequence();
if ("KTHXBAI".equalsIgnoreCase(kvMsg.getKey())) {
System.out.println("Received snapshot = " + kvMsg.getSequence());
kvMsg.destroy();
// done
break;
}
System.out.println("receiving " + kvMsg.getSequence());
kvMsg.store(kvMap);
}
Poller poller = ctx.createPoller(1);
poller.register(subscriber);
Random random = new Random();
// now apply pending updates, discard out-of-getSequence messages
long alarm = System.currentTimeMillis() + 5000;
while (true) {
int rc = poller.poll(Math.max(0, alarm - System.currentTimeMillis()));
if (rc == -1)
// Context has been shut down
break;
if (poller.pollin(0)) {
kvmsg kvMsg = kvmsg.recv(subscriber);
if (kvMsg == null)
// Interrupted
break;
if (kvMsg.getSequence() > sequence) {
sequence = kvMsg.getSequence();
System.out.println("receiving " + sequence);
kvMsg.store(kvMap);
} else
kvMsg.destroy();
}
if (System.currentTimeMillis() >= alarm) {
kvmsg kvMsg = new kvmsg(0);
kvMsg.fmtKey("%s%d", SUBTREE, random.nextInt(10000));
kvMsg.fmtBody("%d", random.nextInt(1000000));
kvMsg.setProp("ttl", "%d", random.nextInt(30));
kvMsg.send(publisher);
kvMsg.destroy();
alarm = System.currentTimeMillis() + 1000;
}
}
ctx.destroy();
}
use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.
the class clonesrv3 method run.
public void run() {
ZContext ctx = new ZContext();
Socket snapshot = ctx.createSocket(ZMQ.ROUTER);
snapshot.bind("tcp://*:5556");
Socket publisher = ctx.createSocket(ZMQ.PUB);
publisher.bind("tcp://*:5557");
Socket collector = ctx.createSocket(ZMQ.PULL);
collector.bind("tcp://*:5558");
Poller poller = ctx.createPoller(2);
poller.register(collector, Poller.POLLIN);
poller.register(snapshot, Poller.POLLIN);
long sequence = 0;
while (!Thread.currentThread().isInterrupted()) {
if (poller.poll(1000) < 0)
// Context has been shut down
break;
// apply state updates from main thread
if (poller.pollin(0)) {
kvsimple kvMsg = kvsimple.recv(collector);
if (// Interrupted
kvMsg == null)
break;
kvMsg.setSequence(++sequence);
kvMsg.send(publisher);
clonesrv3.kvMap.put(kvMsg.getKey(), kvMsg);
System.out.printf("I: publishing update %5d\n", sequence);
}
// execute state snapshot request
if (poller.pollin(1)) {
byte[] identity = snapshot.recv(0);
if (identity == null)
// Interrupted
break;
String request = snapshot.recvStr();
if (!request.equals("ICANHAZ?")) {
System.out.println("E: bad request, aborting");
break;
}
Iterator<Entry<String, kvsimple>> iter = kvMap.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, kvsimple> entry = iter.next();
kvsimple msg = entry.getValue();
System.out.println("Sending message " + entry.getValue().getSequence());
this.sendMessage(msg, identity, snapshot);
}
// now send end message with getSequence number
System.out.println("Sending state snapshot = " + sequence);
snapshot.send(identity, ZMQ.SNDMORE);
kvsimple message = new kvsimple("KTHXBAI", sequence, ZMQ.SUBSCRIPTION_ALL);
message.send(snapshot);
}
}
System.out.printf(" Interrupted\n%d messages handled\n", sequence);
ctx.destroy();
}
use of org.zeromq.ZMQ.Poller in project jeromq by zeromq.
the class titanic method main.
// .split worker task
// This is the main thread for the Titanic worker. It starts three child
// threads; for the request, reply, and close services. It then dispatches
// requests to workers using a simple brute force disk queue. It receives
// request UUIDs from the {{titanic.request}} service, saves these to a disk
// file, and then throws each request at MDP workers until it gets a
// response.
public static void main(String[] args) {
boolean verbose = (args.length > 0 && "-v".equals(args[0]));
ZContext ctx = new ZContext();
Socket requestPipe = ZThread.fork(ctx, new TitanicRequest());
ZThread.start(new TitanicReply());
ZThread.start(new TitanicClose());
Poller poller = ctx.createPoller(1);
poller.register(requestPipe, ZMQ.Poller.POLLIN);
// Main dispatcher loop
while (true) {
// We'll dispatch once per second, if there's no activity
int rc = poller.poll(1000);
if (rc == -1)
// Interrupted
break;
if (poller.pollin(0)) {
// Ensure message directory exists
new File(TITANIC_DIR).mkdirs();
// Append UUID to queue, prefixed with '-' for pending
ZMsg msg = ZMsg.recvMsg(requestPipe);
if (msg == null)
// Interrupted
break;
String uuid = msg.popString();
BufferedWriter wfile = null;
try {
wfile = new BufferedWriter(new FileWriter(TITANIC_DIR + "/queue", true));
wfile.write("-" + uuid + "\n");
} catch (IOException e) {
e.printStackTrace();
break;
} finally {
try {
if (wfile != null)
wfile.close();
} catch (IOException e) {
}
}
msg.destroy();
}
// Brute force dispatcher
//"?........:....:....:....:............:";
byte[] entry = new byte[37];
RandomAccessFile file = null;
try {
file = new RandomAccessFile(TITANIC_DIR + "/queue", "rw");
while (file.read(entry) > 0) {
// UUID is prefixed with '-' if still waiting
if (entry[0] == '-') {
if (verbose)
System.out.printf("I: processing request %s\n", new String(entry, 1, entry.length - 1, ZMQ.CHARSET));
if (serviceSuccess(new String(entry, 1, entry.length - 1, ZMQ.CHARSET))) {
// Mark queue entry as processed
file.seek(file.getFilePointer() - 37);
file.writeBytes("+");
file.seek(file.getFilePointer() + 36);
}
}
// Skip end of line, LF or CRLF
if (file.readByte() == '\r')
file.readByte();
if (Thread.currentThread().isInterrupted())
break;
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
}
}
}
}
}
Aggregations