public static FmqMsg recv (Socket input)
{
assert (input != null);
FmqMsg self = new FmqMsg (0);
ZFrame frame = null;
try {
// Read valid message frame from socket; we loop over any
// garbage data we might receive from badly-connected peers
while (true) {
// If we're reading from a ROUTER socket, get address
if (input.getType () == ZMQ.ROUTER) {
self.address = ZFrame.recvFrame (input);
if (self.address == null)
return null; // Interrupted
if (!input.hasReceiveMore ())
throw new IllegalArgumentException ();
}
// Read and parse command in frame
frame = ZFrame.recvFrame (input);
if (frame == null)
return null; // Interrupted
// Get and check protocol signature
self.needle = ByteBuffer.wrap (frame.getData ());
int signature = self.getNumber2 ();
if (signature == (0xAAA0 | 3))
break; // Valid signature
// Protocol assertion, drop message
while (input.hasReceiveMore ()) {
frame.destroy ();
frame = ZFrame.recvFrame (input);
}
frame.destroy ();
}
// Get message id, which is first byte in frame
self.id = self.getNumber1 ();
int listSize;
int hashSize;
switch (self.id) {
case OHAI:
self.protocol = self.getString ();
if (!self.protocol.equals( "FILEMQ"))
throw new IllegalArgumentException ();
self.version = self.getNumber1 ();
if (self.version != FMQ_MSG_VERSION)
throw new IllegalArgumentException ();
break;
case ORLY:
listSize = self.getNumber1 ();
self.mechanisms = new ArrayList<String> ();
while (listSize-- > 0) {
String string = self.getString ();
self.mechanisms.add (string);
}
// Get next frame, leave current untouched
if (!input.hasReceiveMore ())
throw new IllegalArgumentException ();
self.challenge = ZFrame.recvFrame (input);
break;
case YARLY:
self.mechanism = self.getString ();
// Get next frame, leave current untouched
if (!input.hasReceiveMore ())
throw new IllegalArgumentException ();
self.response = ZFrame.recvFrame (input);
break;
case OHAI_OK:
break;
case ICANHAZ:
self.path = self.getString ();
hashSize = self.getNumber1 ();
self.options = new HashMap <String, String> ();
while (hashSize-- > 0) {
String string = self.getString ();
String [] kv = string.split("=");
self.options.put(kv[0], kv[1]);
}
hashSize = self.getNumber1 ();
self.cache = new HashMap <String, String> ();
while (hashSize-- > 0) {
String string = self.getString ();
String [] kv = string.split("=");
self.cache.put(kv[0], kv[1]);
}
break;
case ICANHAZ_OK:
break;
case NOM:
self.credit = self.getNumber8 ();
self.sequence = self.getNumber8 ();
break;
case CHEEZBURGER:
self.sequence = self.getNumber8 ();
self.operation = self.getNumber1 ();
self.filename = self.getString ();
self.offset = self.getNumber8 ();
self.eof = self.getNumber1 ();
hashSize = self.getNumber1 ();
self.headers = new HashMap <String, String> ();
while (hashSize-- > 0) {
String string = self.getString ();
String [] kv = string.split("=");
self.headers.put(kv[0], kv[1]);
}
// Get next frame, leave current untouched
if (!input.hasReceiveMore ())
throw new IllegalArgumentException ();
self.chunk = ZFrame.recvFrame (input);
break;
case HUGZ:
break;
case HUGZ_OK:
break;
case KTHXBAI:
break;
case SRSLY:
self.reason = self.getString ();
break;
case RTFM:
self.reason = self.getString ();
break;
default:
throw new IllegalArgumentException ();
}
return self;
} catch (Exception e) {
// Error returns
System.out.printf ("E: malformed message '%d'\n", self.id);
self.destroy ();
return null;
} finally {
if (frame != null)
frame.destroy ();
}
}