Search in sources :

Example 16 with ByteBuffer

use of org.smpp.util.ByteBuffer in project opensmpp by OpenSmpp.

the class TLVInt method getValueData.

protected ByteBuffer getValueData() throws ValueNotSetException {
    ByteBuffer valueBuf = new ByteBuffer();
    valueBuf.appendInt(getValue());
    return valueBuf;
}
Also used : ByteBuffer(org.smpp.util.ByteBuffer)

Example 17 with ByteBuffer

use of org.smpp.util.ByteBuffer in project opensmpp by OpenSmpp.

the class TLVOctets method getValueData.

protected ByteBuffer getValueData() throws ValueNotSetException {
    ByteBuffer valueBuf = new ByteBuffer();
    valueBuf.appendBuffer(getValue());
    return valueBuf;
}
Also used : ByteBuffer(org.smpp.util.ByteBuffer)

Example 18 with ByteBuffer

use of org.smpp.util.ByteBuffer in project opensmpp by OpenSmpp.

the class PDUHeader method getData.

public ByteBuffer getData() {
    ByteBuffer buffer = new ByteBuffer();
    buffer.appendInt(getCommandLength());
    buffer.appendInt(getCommandId());
    buffer.appendInt(getCommandStatus());
    buffer.appendInt(getSequenceNumber());
    return buffer;
}
Also used : ByteBuffer(org.smpp.util.ByteBuffer)

Example 19 with ByteBuffer

use of org.smpp.util.ByteBuffer in project opensmpp by OpenSmpp.

the class ReceiverBase method tryGetUnprocessedPDU.

/**
 * Tries to create a PDU from the buffer provided.
 * Returns the PDU if successfull or null if not or an exception
 * if the PDU is incorrect.
 */
private final PDU tryGetUnprocessedPDU(Unprocessed unprocessed) throws UnknownCommandIdException, PDUException {
    debug.write(DRXTX, "trying to create pdu from unprocessed buffer");
    PDU pdu = null;
    ByteBuffer unprocBuffer = unprocessed.getUnprocessed();
    try {
        pdu = PDU.createPDU(unprocBuffer);
        unprocessed.check();
        // Reset counter after successful createPDU (as per bug #2138444):
        messageIncompleteRetryCount = 0;
    } catch (HeaderIncompleteException e) {
        // the header wasn't received completly, we will try to
        // receive the rest next time
        debug.write(DRXTXD, "incomplete message header, will wait for the rest.");
        // as it's incomplete - wait for new data
        unprocessed.setHasUnprocessed(false);
        unprocessed.setExpected(Data.PDU_HEADER_SIZE);
    } catch (MessageIncompleteException e) {
        // busy servers and PDUs split across TCP packets.
        if (messageIncompleteRetryCount > 5) {
            messageIncompleteRetryCount = 0;
            event.write("Giving up on incomplete messages - probably garbage in unprocessed buffer. Flushing unprocessed buffer.");
            unprocessed.reset();
        }
        // the message wasn't received completly, less bytes than command
        // length has been received, will try to receive the rest next time
        debug.write(DRXTXD, "incomplete message, will wait for the rest.");
        // as it's incomplete - wait for new data
        unprocessed.setHasUnprocessed(false);
        unprocessed.setExpected(Data.PDU_HEADER_SIZE);
        messageIncompleteRetryCount++;
    } catch (UnknownCommandIdException e) {
        // message with invalid id was received, should send generic_nack
        debug.write(DRXTX, "unknown pdu, might remove from unprocessed buffer. CommandId=" + e.getCommandId());
        if (e.getCommandLength() <= unprocBuffer.length()) {
            // have already enough to remove
            try {
                unprocBuffer.removeBytes(e.getCommandLength());
            } catch (NotEnoughDataInByteBufferException e1) {
                // can't happen, we've checked it above
                throw new Error("Not enough data in buffer even if previously checked that there was enough.");
            }
            unprocessed.check();
            // caller will decide what to do
            throw e;
        }
        // discarded via an UnknownCommandIdException again).
        throw e;
    } catch (PDUException e) {
        // paolo@bulksms.com: safer to catch all other PDU exceptions and force
        // force a check() here - some exception in parsing should not be allowed
        // to leave ghost data in the Unprocessed buffer (even though this is now
        // less likely after improvements to PDU.createPDU()):
        unprocessed.check();
        throw e;
    }
    /* paolo@bulksms.com: concerned that this is too broad, and will
		   stop useful exceptions from being passed back up the call stack,
			so disabling for now:
		} catch (Exception e) {
			debug.write(DRXTX, "Exception catched: " + e.toString());
			StringWriter stringWriter = new StringWriter();
			PrintWriter printWriter = new PrintWriter(stringWriter);
			e.printStackTrace(printWriter);
			debug.write(DRXTX, stringWriter.toString());
		}
		*/
    if (pdu != null) {
        debug.write(DRXTX, "received complete pdu" + pdu.debugString());
        debug.write(DRXTX, "there is " + unprocBuffer.length() + " bytes left in unprocessed buffer");
    }
    // unprocessed.check();
    return pdu;
}
Also used : PDU(org.smpp.pdu.PDU) MessageIncompleteException(org.smpp.pdu.MessageIncompleteException) NotEnoughDataInByteBufferException(org.smpp.util.NotEnoughDataInByteBufferException) UnknownCommandIdException(org.smpp.pdu.UnknownCommandIdException) PDUException(org.smpp.pdu.PDUException) HeaderIncompleteException(org.smpp.pdu.HeaderIncompleteException) ByteBuffer(org.smpp.util.ByteBuffer)

Example 20 with ByteBuffer

use of org.smpp.util.ByteBuffer 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

ByteBuffer (org.smpp.util.ByteBuffer)21 Test (org.junit.Test)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 PDU (org.smpp.pdu.PDU)2 UnknownCommandIdException (org.smpp.pdu.UnknownCommandIdException)2 HeaderIncompleteException (org.smpp.pdu.HeaderIncompleteException)1 MessageIncompleteException (org.smpp.pdu.MessageIncompleteException)1 PDUException (org.smpp.pdu.PDUException)1 NotEnoughDataInByteBufferException (org.smpp.util.NotEnoughDataInByteBufferException)1