Search in sources :

Example 1 with BigPacketException

use of com.sun.messaging.jmq.io.BigPacketException in project openmq by eclipse-ee4j.

the class WebSocketConnectionHandler method onMessage.

/**
 *****************************************************
 * Implement MessageHandler.Whole interface
 **********************************************************
 */
/**
 * Called when the message has been fully received.
 *
 * @param data the message data.
 */
@Override
public void onMessage(ByteBuffer data) {
    if (logger.isLoggable(Level.FINEST)) {
        Session ss = session;
        logger.log(Level.FINEST, Thread.currentThread() + "WebSocketConnectionHandler@" + hashCode() + ": onMessage(ByteBuffer@" + data.hashCode() + "[len=" + data.remaining() + ", pos=" + data.position() + "]), ws-session=" + (ss == null ? "null" : ss.getId()) + " on JMS connection " + conn.getConnectionID());
    }
    String id = null;
    while (data.hasRemaining()) {
        synchronized (sessionLock) {
            if (session == null) {
                throw new IllegalStateException("WebSocket Session not open on JMS connection " + conn.getConnectionID());
            }
            id = session.getId();
            while (!closed && packetRead != null) {
                if (logger.isLoggable(Level.FINE)) {
                    logger.log(Level.FINE, Thread.currentThread() + "WebSocketConnectionHandler@" + hashCode() + ": onMessage() waiting for packet read to be dispatched, ws-session=" + id + " on JMS connection " + conn.getConnectionID());
                }
                try {
                    sessionLock.wait();
                } catch (InterruptedException e) {
                }
            }
            if (closed) {
                throw new IllegalStateException(AdministeredObject.cr.getKString(AdministeredObject.cr.X_WEBSOCKET_SESSION_CLOSED));
            }
            if (packetPending == null) {
                packetPending = new ReadWritePacket();
            }
        }
        try {
            if (packetPending.readPacket(data)) {
                if (!packetPending.hasBigPacketException()) {
                    synchronized (sessionLock) {
                        packetRead = packetPending;
                        packetPending = null;
                        sessionLock.notifyAll();
                    }
                    if (logger.isLoggable(Level.FINEST)) {
                        logger.log(Level.FINEST, Thread.currentThread() + "WebSocketConnectionHandler@" + hashCode() + ": onMessage(): RECEIVED PACKET=" + packetPending + ", remaining=" + data.remaining() + ",ws-session=" + id + " on JMS connection " + conn.getConnectionID());
                    }
                } else {
                    IOException ioe = packetPending.getBigPacketException();
                    packetPending = null;
                    throw new IOException("BigPacketException", ioe);
                }
            }
        } catch (BigPacketException e) {
            String[] params = { (packetPending == null ? "(r):" + packetRead : "(p):" + packetPending), this.toString(), e.getMessage() };
            String emsg = AdministeredObject.cr.getKString(AdministeredObject.cr.X_WEBSOCKET_PROCESS_PKT, params);
            logger.log(Level.SEVERE, emsg);
        } catch (IOException e) {
            onError(session, e);
            String[] params = { (packetPending == null ? "(r):" + packetRead : "(p):" + packetPending), this.toString(), e.getMessage() };
            throw new RuntimeException(AdministeredObject.cr.getKString(AdministeredObject.cr.X_WEBSOCKET_PROCESS_PKT, params), e);
        }
    }
}
Also used : BigPacketException(com.sun.messaging.jmq.io.BigPacketException) ReadWritePacket(com.sun.messaging.jmq.io.ReadWritePacket) IOException(java.io.IOException) Session(jakarta.websocket.Session)

Example 2 with BigPacketException

use of com.sun.messaging.jmq.io.BigPacketException 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)

Example 3 with BigPacketException

use of com.sun.messaging.jmq.io.BigPacketException in project openmq by eclipse-ee4j.

the class JMSWebSocket method processData.

private void processData(ByteBuffer buf) throws IOException {
    if (DEBUG) {
        logger.log(Logger.INFO, Thread.currentThread() + " processData:buf.remaining=" + buf.remaining());
    }
    List<Packet> packetList = null;
    while (buf.hasRemaining()) {
        synchronized (packetLock) {
            if (packetPending != null) {
                try {
                    if (packetPending.readPacket(buf)) {
                        if (!packetPending.hasBigPacketException()) {
                            if (packetList == null) {
                                packetList = new ArrayList<>();
                            }
                            packetList.add(packetPending);
                            if (DEBUG) {
                                logger.log(logger.INFO, Thread.currentThread() + "JMSWebSocket@" + hashCode() + " processData(): READ pending PACKET=" + packetPending + ", buf.remaining=" + buf.remaining());
                            }
                        }
                        packetPending = null;
                    }
                } catch (BigPacketException e) {
                    logger.log(logger.ERROR, Thread.currentThread() + "readPacket: " + e.getMessage(), e);
                    WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
                    // XXopt close conn if too big
                    conn.handleBigPacketException(packetPending, e);
                } catch (IllegalArgumentException e) {
                    WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
                    conn.handleIllegalArgumentExceptionPacket(packetPending, e);
                } catch (OutOfMemoryError err) {
                    // XXopt close conn
                    Globals.handleGlobalError(err, Globals.getBrokerResources().getKString(BrokerResources.M_LOW_MEMORY_READALLOC) + ": " + packetPending.headerToString());
                }
                continue;
            }
        }
        if (packetList == null) {
            packetList = new ArrayList<>();
        }
        Packet packet = new Packet(false);
        packet.generateSequenceNumber(false);
        packet.generateTimestamp(false);
        try {
            if (packet.readPacket(buf)) {
                if (!packet.hasBigPacketException()) {
                    packetList.add(packet);
                    if (DEBUG) {
                        logger.log(logger.INFO, Thread.currentThread() + "JMSWebSocket@" + hashCode() + " processData(): READ a PACKET=" + packet);
                    }
                }
            } else {
                synchronized (packetLock) {
                    packetPending = packet;
                }
            }
        } catch (BigPacketException e) {
            logger.log(logger.ERROR, "readPacket: " + e.getMessage(), e);
            WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
            // XXopt close conn if too big
            conn.handleBigPacketException(packet, e);
        } catch (IllegalArgumentException e) {
            logger.log(logger.ERROR, "readPacket: " + e.getMessage(), e);
            WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
            conn.handleIllegalArgumentExceptionPacket(packet, e);
        } catch (OutOfMemoryError err) {
            // XXopt close conn
            Globals.handleGlobalError(err, Globals.getBrokerResources().getKString(BrokerResources.M_LOW_MEMORY_READALLOC) + ": " + packet.headerToString());
        }
        continue;
    }
    if (packetList == null || packetList.isEmpty()) {
        packetList = null;
        return;
    }
    if (DEBUG) {
        logger.log(logger.INFO, "[JMSWebSocket@" + this.hashCode() + "]processData() after processed buf: remaining=" + buf.remaining());
    }
    WebSocketMQIPConnection conn = websocketApp.getMQIPConnection(this);
    for (int i = 0; i < packetList.size(); i++) {
        try {
            final Packet packet = packetList.get(i);
            conn.receivedPacket(packet);
            conn.readData();
        } catch (BrokerException e) {
            // XXclean
            Globals.getLogger().logStack(Logger.ERROR, "Failed to process packet from connection " + this, e);
        }
    }
    packetList.clear();
    packetList = null;
}
Also used : HttpRequestPacket(org.glassfish.grizzly.http.HttpRequestPacket) Packet(com.sun.messaging.jmq.io.Packet) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) BigPacketException(com.sun.messaging.jmq.io.BigPacketException)

Aggregations

BigPacketException (com.sun.messaging.jmq.io.BigPacketException)3 Packet (com.sun.messaging.jmq.io.Packet)2 IOException (java.io.IOException)2 ReadWritePacket (com.sun.messaging.jmq.io.ReadWritePacket)1 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)1 Session (jakarta.websocket.Session)1 Buffer (org.glassfish.grizzly.Buffer)1 Connection (org.glassfish.grizzly.Connection)1 HttpRequestPacket (org.glassfish.grizzly.http.HttpRequestPacket)1 CompositeBuffer (org.glassfish.grizzly.memory.CompositeBuffer)1 BufferInputStream (org.glassfish.grizzly.utils.BufferInputStream)1