int flag = 0;
int count = 0;
String topic = null;
boolean more = false;
boolean stop = false;
ZLog zlog = null;
Msg msg;
ZMsg response = null;
while (!Thread.currentThread().isInterrupted()
&& !stop) {
msg = worker.base().recv(0);
if (msg == null)
break;
more = msg.hasMore();
switch (state) {
case START:
byte[] id = msg.data();
if (id == null)
break;
flag = id[1];
topic = ZPUtils.getTopic(id);
if (topic == null) {
break;
}
state = TOPIC;
zlog = logMgr.get(topic);
if (flag > 0) {
response = new ZMsg();
response.add(id);
}
break;
case TOPIC:
if (msg.size() == 0 && more) { // bottom
state = COUNT;
if (flag > 0)
response.add(msg.data());
break;
}
case COUNT:
if (decoder) {
count = ByteBuffer.wrap(msg.data()).getInt();
state = MESSAGE;
break;
}
else {
state = SINGLE;
}
case SINGLE:
if (store(zlog, msg)) {
if (flag > 0 && zlog.flushed()) {
response.add(new ZFrame(msg.buf().array()));
response.send(worker);
}
} else
stop = true;
if (!more)
state = START;
break;
case MESSAGE:
if (store(zlog, count, msg)) {
if (flag > 0 && zlog.flushed()) {
response.add(getLastFrame(msg.buf().duplicate()));
response.send(worker);
}
} else
stop = true;