use of org.glassfish.grizzly.utils.BufferInputStream in project openmq by eclipse-ee4j.
the class GrizzlyMQPacketFilter method handleRead.
/**
* Method is called, when new data was read from the Connection and ready to be processed.
*
* We override this method to perform Buffer -> GIOPMessage transformation.
*
* @param ctx Context of {@link FilterChainContext} processing
* @return the next action
*/
@Override
public NextAction handleRead(final FilterChainContext ctx) throws IOException {
final GrizzlyMQPacketList packetList = GrizzlyMQPacketList.create();
final Connection c = ctx.getConnection();
final Buffer buf = ctx.getMessage();
final PacketParseState parsestate = parsestateAttr.get(c);
while (buf.hasRemaining()) {
int buflen = buf.remaining();
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "]buflen=" + buflen + ", gotpsize=" + parsestate.gotpsize + ", psize=" + parsestate.psize + ", pos=" + buf.position());
}
if (!parsestate.gotpsize) {
if (buflen < Packet.HEADER_SIZE) {
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "] not enough for header size " + Packet.HEADER_SIZE);
}
// return ctx.getStopAction(buf);
break;
}
int pos = buf.position();
parsestate.psize = GrizzlyMQPacket.parsePacketSize(buf);
buf.position(pos);
parsestate.gotpsize = true;
}
if (buflen < parsestate.psize) {
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "] not enough for packet size " + parsestate.psize);
}
// return ctx.getStopAction(buf);
break;
}
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "]reading packet at pos=" + buf.position() + ", size=" + parsestate.psize);
}
final int pos = buf.position();
Packet pkt = null;
BufferInputStream bis = null;
try {
// XXX
pkt = new GrizzlyMQPacket(false);
pkt.generateSequenceNumber(false);
pkt.generateTimestamp(false);
bis = new BufferInputStream(buf);
pkt.readPacket(bis);
// ctx.setMessage(pkt);
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "]read packet: " + pkt + ", pre-pos=" + pos);
}
packetList.getPackets().add(pkt);
} catch (OutOfMemoryError err) {
Globals.handleGlobalError(err, Globals.getBrokerResources().getKString(BrokerResources.M_LOW_MEMORY_READALLOC) + ": " + (pkt == null ? "null" : pkt.headerToString()));
buf.position(pos);
try {
Thread.sleep(1000L);
} catch (Exception e) {
}
continue;
} catch (BigPacketException e) {
GrizzlyMQIPConnection conn = connAttr.get(c);
conn.handleBigPacketException(pkt, e);
} catch (IllegalArgumentException e) {
GrizzlyMQIPConnection conn = connAttr.get(c);
conn.handleIllegalArgumentExceptionPacket(pkt, e);
} finally {
if (bis != null) {
bis.close();
}
}
buf.position(pos + parsestate.psize);
parsestate.reset();
}
// There are no packets parsed
if (packetList.getPackets().isEmpty()) {
packetList.recycle(false);
return ctx.getStopAction(buf);
}
final Buffer remainder;
if (buf.hasRemaining()) {
remainder = buf.split(buf.position());
} else {
remainder = null;
}
packetList.setPacketsBuffer(buf);
ctx.setMessage(packetList);
if (DEBUG) {
logger.log(logger.INFO, "[@" + c.hashCode() + "]handleRead.return: " + (remainder == null ? "no remainder" : "remainer=" + remainder.hasRemaining() + ", remaining=" + remainder.remaining()));
}
return ctx.getInvokeAction(remainder);
}
Aggregations