Package org.smpp.pdu

Examples of org.smpp.pdu.PDU


   */
  final protected PDU tryReceivePDUWithTimeout(Connection connection, PDU expectedPDU, long timeout)
    throws UnknownCommandIdException, TimeoutException, PDUException, IOException {
    debug.write(DRXTX, "receivePDU: Going to receive response.");
    long startTime = Data.getCurrentTime();
    PDU pdu = null;
    if (timeout == 0) {
      // with no timeout try just once
      pdu = tryReceivePDU(connection, expectedPDU);
    } else {
      // with timeout keep trying until get some or timeout expires
      while ((pdu == null) && canContinueReceiving(startTime, timeout)) {
        pdu = tryReceivePDU(connection, expectedPDU);
      }
    }
    if (pdu != null) {
      debug.write(DRXTX, "Got pdu " + pdu.debugString());
    }
    return pdu;
  }
View Full Code Here


   * @see Unprocessed
   */
  final protected 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
View Full Code Here

   * 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();
    } 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.");
      unprocessed.setHasUnprocessed(false); // as it's incomplete - wait for new data
      unprocessed.setExpected(Data.PDU_HEADER_SIZE);
    } catch (MessageIncompleteException e) {
      if (messageIncompleteRetryCount > 1) { // paolo@bulksms.com
        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.");
      unprocessed.setHasUnprocessed(false); // as it's incomplete - wait for new data
      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();
        throw e; // caller will decide what to do
      }
      // paolo@bulksms.com: added this: see why in caller. Advantage: lets
      // us trap garbage PDUs that break things by being trapped in the
      // unprocessed buffer eternally. Disadvantage: if this was a valid
      // (well-formed) PDU with an unknown command id AND it was not fully
      // read into the buffer in one go, then:
      // 1) we will respond to the part _was_ already read (which we know
      // contains at least a header - see code in PDU) with an error, which
      // is fine.
      // 2) when we receive the second part of the incomplete PDU, it will
      // effectively seem to us to be an invalid PDU itself. It could be
      // processed as follows:
      //     - as an unknown command_id again, which is fine; or
      //     - as an incomplete message, which is a problem, because it will
      //       then have the subsequent PDU tacked to the end of it, and
      //       that PDU will then be discarded as well (and almost certainly
      //       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;
  }
View Full Code Here

      // let's remove all pdu's from the queue as since now all
      // processing should be asynchronous -- it's not wise to
      // expect that the programmer will try AFTER setting the listener
      // to call receive() which when in sync mode removes the pdus from
      // the queue
      PDU pdu;
      int queueSize;
      synchronized (pduQueue) {
        queueSize = pduQueue.size();
        for (int i = 0; i < queueSize; i++) {
          pdu = (PDU) pduQueue.dequeue();
View Full Code Here

   * @exception UnknownCommandIdException PDU with unknown id was received
   * @see ReceiverBase#tryReceivePDUWithTimeout(Connection,PDU,long)
   */
  public synchronized PDU receive(long timeout)
    throws UnknownCommandIdException, TimeoutException, NotSynchronousException, PDUException, IOException {
    PDU pdu = null;
    if (!asynchronous) {
      pdu = tryReceivePDUWithTimeout(connection, null, timeout);
    }
    return pdu;
  }
View Full Code Here

   * @return the received PDU or null if none
   * @see ReceiverBase#tryReceivePDUWithTimeout(Connection,PDU,long)
   */
  public synchronized PDU receive(PDU expectedPDU)
    throws UnknownCommandIdException, TimeoutException, NotSynchronousException, PDUException, IOException {
    PDU pdu = null;
    if (!asynchronous) {
      pdu = tryReceivePDUWithTimeout(connection, expectedPDU);
    }
    return pdu;
  }
View Full Code Here

   * @see #receiveAsync()
   * @see ReceiverBase#run()
   */
  protected PDU tryReceivePDU(Connection connection, PDU expectedPDU)
    throws UnknownCommandIdException, TimeoutException, PDUException, IOException {
    PDU pdu = null;
    if (receiver) {
      debug.write(DRXTXD2, "Is receiver/transciever => trying to get from queue.");
      synchronized (pduQueue) {
        if (expectedPDU == null) { // i.e. any pdu is acceptable
          if (!pduQueue.isEmpty()) {
            pdu = (PDU) pduQueue.dequeue();
          }
        } else {
          pdu = (PDU) pduQueue.dequeue(expectedPDU);
        }
        if (pdu == null) {
          try {
            pduQueue.wait(getQueueWaitTimeout());
          } catch (InterruptedException e) {
            // we don't care
            debug.write(DRXTX, "tryReceivePDU got interrupt waiting for queue");
          }
        }
      }
    } else {
      debug.write(DRXTX, "Is transmitter only => trying to receive from connection.");
      pdu = receivePDUFromConnection(connection, unprocessed);
      if (pdu != null) {
        if ((expectedPDU == null) || !pdu.equals(expectedPDU)) {
          debug.write(DRXTX, "This is not the pdu we expect, processing" + pdu.debugString());
          enqueue(pdu);
          pdu = null;
        }
      }
    }
View Full Code Here

   * <code>setTermException</code> is then called with the caught exception.
   *
   * @see ReceiverBase#run()
   */
  protected void receiveAsync() {
    PDU pdu = null;
    try {
      debug.write(DRXTXD2, "Receiver.receiveAsync() going to receive pdu.");
      pdu = receivePDUFromConnection(connection, unprocessed);
      // we must catch every exception as this is thread running
      // on the background and we don't want the thread to be terminated
    } catch (InvalidPDUException e) {
      // thrown when enough data were received but further parsing
      // required more than indicated by CommandLength, i.e. pdu is
      // corrupted or further parsing didn't find terminating zero
      // of a c-string i.e. pdu is corrupted
      // must send generic nack anyway
      event.write(e, "Receiver.receiveAsync(): received PDU is invalid.");
      PDU expdu = e.getPDU();
      int seqNr = expdu == null ? 0 : expdu.getSequenceNumber();
      sendGenericNack(Data.ESME_RINVMSGLEN, seqNr);
    } catch (UnknownCommandIdException e) {
      // if received unknown pdu, we must send generic nack
      event.write(e, "Receiver.receiveAsync(): Unknown command id.");
      sendGenericNack(Data.ESME_RINVCMDID, e.getSequenceNumber());
View Full Code Here

  /**
   * "Handles" the event generated for received PDUs -- just logs
   * the event and throws it away.
   */
  public void handleEvent(ServerPDUEvent event) {
    PDU pdu = event.getPDU();
    if (pdu != null) {
      if (pdu.isRequest()) {
        debug.write(DUTL, "receiver listener: handling request " + pdu.debugString());
      } else if (pdu.isResponse()) {
        debug.write(DUTL, "receiver listener: handling response " + pdu.debugString());
      } else {
        debug.write(DUTL, "receiver listener: handling strange pdu " + pdu.debugString());
      }
    }
  }
View Full Code Here

    /**
     * @see org.smpp.ServerPDUEventListener#handleEvent(org.smpp.ServerPDUEvent)
     */
    public void handleEvent(ServerPDUEvent event) {
        PDU pdu = event.getPDU();
        if(pdu.isResponse()){
            logger.info("response-received", pdu.debugString());
        } else {
            logger.warn("unknown-event", pdu.debugString());
        }
    }
View Full Code Here

TOP

Related Classes of org.smpp.pdu.PDU

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.