Search in sources :

Example 6 with PDU

use of org.smpp.pdu.PDU in project opensmpp by OpenSmpp.

the class ReceiverBase method receivePDUFromConnection.

/**
 * Elementary method receiving data from connection, trying to create
 * PDU from them and buffering data in case the PDU
 * isn't still complete. It has timeout checking for incomplete
 * messages: if the message isn't received completly for certain time
 * and no new data are received for this time, then exception is thrown
 * as this could indicate communication problem.
 *
 * @param connection the connection to receive the data from
 * @return either PDU, if complete received or null
 *
 * @exception IOException exception during communication
 * @exception PDUException incorrect format of PDU
 * @throws TimeoutException rest of data not received for too long time
 * @throws UnknownCommandIdException PDU with unknown id was received
 * @see Connection
 * @see Unprocessed
 */
protected final PDU receivePDUFromConnection(Connection connection, Unprocessed unprocessed) throws UnknownCommandIdException, TimeoutException, PDUException, IOException {
    debug.write(DRXTXD2, "ReceiverBase.receivePDUFromConnection start");
    PDU pdu = null;
    ByteBuffer buffer;
    ByteBuffer unprocBuffer;
    try {
        // first check if there is something left from the last time
        if (unprocessed.getHasUnprocessed()) {
            unprocBuffer = unprocessed.getUnprocessed();
            debug.write(DRXTX, "have unprocessed " + unprocBuffer.length() + " bytes from previous try");
            pdu = tryGetUnprocessedPDU(unprocessed);
        }
        if (pdu == null) {
            // only if we didn't manage to get pdu from unproc
            buffer = connection.receive();
            unprocBuffer = unprocessed.getUnprocessed();
            // if received something now or have something from the last receive
            if (buffer.length() != 0) {
                unprocBuffer.appendBuffer(buffer);
                unprocessed.setLastTimeReceived();
                pdu = tryGetUnprocessedPDU(unprocessed);
            } else {
                debug.write(DRXTXD2, "no data received this time.");
                // check if it's not too long since we received any data
                long timeout = getReceiveTimeout();
                if ((unprocBuffer.length() > 0) && ((unprocessed.getLastTimeReceived() + timeout) < Data.getCurrentTime())) {
                    debug.write(DRXTX, "and it's been very long time.");
                    unprocessed.reset();
                    throw new TimeoutException(timeout, unprocessed.getExpected(), unprocBuffer.length());
                }
            }
        }
    } catch (UnknownCommandIdException e) {
        // paolo@bulksms.com: if we got an UnknownCommandIdException here, the
        // chances are excellent that some trailing garbage is hanging around in
        // the unprocessed buffer. Given that it's unlikely that it contained a
        // valid PDU, we don't rethrow the error - nothing to respond to with a
        // gnack. Was originally just checking this if the unprocessed buffer had
        // content, but that's not enough.
        event.write(e, "There is _probably_ garbage in the unprocessed buffer - flushing unprocessed buffer now.");
        unprocessed.reset();
    }
    debug.write(DRXTXD2, "ReceiverBase.receivePDUFromConnection finished");
    return pdu;
}
Also used : PDU(org.smpp.pdu.PDU) UnknownCommandIdException(org.smpp.pdu.UnknownCommandIdException) ByteBuffer(org.smpp.util.ByteBuffer)

Aggregations

PDU (org.smpp.pdu.PDU)6 UnknownCommandIdException (org.smpp.pdu.UnknownCommandIdException)3 PDUException (org.smpp.pdu.PDUException)2 ByteBuffer (org.smpp.util.ByteBuffer)2 IOException (java.io.IOException)1 GenericNack (org.smpp.pdu.GenericNack)1 HeaderIncompleteException (org.smpp.pdu.HeaderIncompleteException)1 InvalidPDUException (org.smpp.pdu.InvalidPDUException)1 MessageIncompleteException (org.smpp.pdu.MessageIncompleteException)1 NotEnoughDataInByteBufferException (org.smpp.util.NotEnoughDataInByteBufferException)1