Search in sources :

Example 1 with BufferInputStream

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);
}
Also used : Buffer(org.glassfish.grizzly.Buffer) CompositeBuffer(org.glassfish.grizzly.memory.CompositeBuffer) Packet(com.sun.messaging.jmq.io.Packet) BigPacketException(com.sun.messaging.jmq.io.BigPacketException) Connection(org.glassfish.grizzly.Connection) IOException(java.io.IOException) BigPacketException(com.sun.messaging.jmq.io.BigPacketException) BufferInputStream(org.glassfish.grizzly.utils.BufferInputStream)

Aggregations

BigPacketException (com.sun.messaging.jmq.io.BigPacketException)1 Packet (com.sun.messaging.jmq.io.Packet)1 IOException (java.io.IOException)1 Buffer (org.glassfish.grizzly.Buffer)1 Connection (org.glassfish.grizzly.Connection)1 CompositeBuffer (org.glassfish.grizzly.memory.CompositeBuffer)1 BufferInputStream (org.glassfish.grizzly.utils.BufferInputStream)1