/** Run method specified by runnnable. */
  public void run() {
    // Assume no thread pooling (bug fix by spierhj)
    ThreadAuditor.ThreadHandle threadHandle = null;

    while (true) {
      // Create a new string message parser to parse the list of messages.
      if (myParser == null) {
        myParser = new StringMsgParser();
        myParser.setParseExceptionListener(this);
      }
      // messages that we write out to him.
      DatagramPacket packet;

      if (sipStack.threadPoolSize != -1) {
        synchronized (((UDPMessageProcessor) messageProcessor).messageQueue) {
          while (((UDPMessageProcessor) messageProcessor).messageQueue.isEmpty()) {
            // Check to see if we need to exit.
            if (!((UDPMessageProcessor) messageProcessor).isRunning) return;
            try {
              // We're part of a thread pool. Ask the auditor to
              // monitor this thread.
              if (threadHandle == null) {
                threadHandle = sipStack.getThreadAuditor().addCurrentThread();
              }

              // Send a heartbeat to the thread auditor
              threadHandle.ping();

              // Wait for packets
              // Note: getPingInterval returns 0 (infinite) if the
              // thread auditor is disabled.
              ((UDPMessageProcessor) messageProcessor)
                  .messageQueue.wait(threadHandle.getPingIntervalInMillisecs());
            } catch (InterruptedException ex) {
              if (!((UDPMessageProcessor) messageProcessor).isRunning) return;
            }
          }
          packet =
              (DatagramPacket) ((UDPMessageProcessor) messageProcessor).messageQueue.removeFirst();
        }
        this.incomingPacket = packet;
      } else {
        packet = this.incomingPacket;
      }

      // Process the packet. Catch and log any exception we may throw.
      try {
        processIncomingDataPacket(packet);
      } catch (Exception e) {

        sipStack.logWriter.logError("Error while processing incoming UDP packet", e);
      }

      if (sipStack.threadPoolSize == -1) {
        return;
      }
    }
  }
  /**
   * This is input reading thread for the pipelined parser. You feed it input through the input
   * stream (see the constructor) and it calls back an event listener interface for message
   * processing or error. It cleans up the input - dealing with things like line continuation
   */
  public void run() {

    Pipeline inputStream = this.rawInputStream;
    // inputStream = new MyFilterInputStream(this.rawInputStream);
    // I cannot use buffered reader here because we may need to switch
    // encodings to read the message body.
    try {
      while (true) {
        this.sizeCounter = this.maxMessageSize;
        // this.messageSize = 0;
        StringBuffer inputBuffer = new StringBuffer();

        if (Debug.parserDebug) Debug.println("Starting parse!");

        String line1;
        String line2 = null;

        while (true) {
          try {
            line1 = readLine(inputStream);
            // ignore blank lines.
            if (line1.equals("\n")) {
              if (Debug.parserDebug) Debug.println("Discarding " + line1);
              continue;
            } else break;
          } catch (IOException ex) {
            Debug.printStackTrace(ex);
            this.rawInputStream.stopTimer();
            return;
          }
        }

        inputBuffer.append(line1);
        // Guard against bad guys.
        this.rawInputStream.startTimer();

        while (true) {
          try {
            line2 = readLine(inputStream);
            inputBuffer.append(line2);
            if (line2.trim().equals("")) break;
          } catch (IOException ex) {
            this.rawInputStream.stopTimer();
            Debug.printStackTrace(ex);
            return;
          }
        }

        // Stop the timer that will kill the read.
        this.rawInputStream.stopTimer();
        inputBuffer.append(line2);
        StringMsgParser smp = new StringMsgParser(sipMessageListener);
        smp.readBody = false;
        SIPMessage sipMessage = null;

        try {
          sipMessage = smp.parseSIPMessage(inputBuffer.toString());
          if (sipMessage == null) {
            this.rawInputStream.stopTimer();
            continue;
          }
        } catch (ParseException ex) {
          // Just ignore the parse exception.
          continue;
        }

        if (Debug.parserDebug) Debug.println("Completed parsing message");
        ContentLength cl = (ContentLength) sipMessage.getContentLength();
        int contentLength = 0;
        if (cl != null) {
          contentLength = cl.getContentLength();
        } else {
          contentLength = 0;
        }

        if (Debug.parserDebug) {
          Debug.println("contentLength " + contentLength);
          Debug.println("sizeCounter " + this.sizeCounter);
          Debug.println("maxMessageSize " + this.maxMessageSize);
        }

        if (contentLength == 0) {
          sipMessage.removeContent();
        } else if (maxMessageSize == 0 || contentLength < this.sizeCounter) {
          byte[] message_body = new byte[contentLength];
          int nread = 0;
          while (nread < contentLength) {
            // Start my starvation timer.
            // This ensures that the other end
            // writes at least some data in
            // or we will close the pipe from
            // him. This prevents DOS attack
            // that takes up all our connections.
            this.rawInputStream.startTimer();
            try {

              int readlength = inputStream.read(message_body, nread, contentLength - nread);
              if (readlength > 0) {
                nread += readlength;
              } else {
                break;
              }
            } catch (IOException ex) {
              ex.printStackTrace();
              break;
            } finally {
              // Stop my starvation timer.
              this.rawInputStream.stopTimer();
            }
          }
          sipMessage.setMessageContent(message_body);
        }
        // Content length too large - process the message and
        // return error from there.
        if (sipMessageListener != null) {
          try {
            sipMessageListener.processMessage(sipMessage);
          } catch (Exception ex) {
            // fatal error in processing - close the
            // connection.
            break;
          }
        }
      }
    } finally {
      try {
        inputStream.close();
      } catch (IOException e) {
        InternalErrorHandler.handleException(e);
      }
    }
  }
  /**
   * Process an incoming datagram
   *
   * @param packet is the incoming datagram packet.
   */
  private void processIncomingDataPacket(DatagramPacket packet) throws Exception {
    this.peerAddress = packet.getAddress();
    int packetLength = packet.getLength();
    // Read bytes and put it in a eueue.
    byte[] bytes = packet.getData();
    byte[] msgBytes = new byte[packetLength];
    System.arraycopy(bytes, 0, msgBytes, 0, packetLength);

    // Do debug logging.
    if (sipStack.isLoggingEnabled()) {
      this.sipStack.logWriter.logDebug(
          "UDPMessageChannel: processIncomingDataPacket : peerAddress = "
              + peerAddress.getHostAddress()
              + "/"
              + packet.getPort()
              + " Length = "
              + packetLength);
    }

    SIPMessage sipMessage = null;
    try {
      this.receptionTime = System.currentTimeMillis();
      sipMessage = myParser.parseSIPMessage(msgBytes);
      myParser = null;
    } catch (ParseException ex) {
      myParser = null; // let go of the parser reference.
      if (sipStack.isLoggingEnabled()) {
        this.sipStack.logWriter.logDebug("Rejecting message !  " + new String(msgBytes));
        this.sipStack.logWriter.logDebug("error message " + ex.getMessage());
        this.sipStack.logWriter.logException(ex);
      }

      // JvB: send a 400 response for requests (except ACK)
      // Currently only UDP, @todo also other transports
      String msgString = new String(msgBytes, 0, packetLength);
      if (!msgString.startsWith("SIP/") && !msgString.startsWith("ACK ")) {

        String badReqRes = createBadReqRes(msgString, ex);
        if (badReqRes != null) {
          if (sipStack.isLoggingEnabled()) {
            sipStack.getLogWriter().logDebug("Sending automatic 400 Bad Request:");
            sipStack.getLogWriter().logDebug(badReqRes);
          }
          try {
            this.sendMessage(badReqRes.getBytes(), peerAddress, packet.getPort(), "UDP", false);
          } catch (IOException e) {
            this.sipStack.logWriter.logException(e);
          }
        } else {
          if (sipStack.isLoggingEnabled()) {
            sipStack.getLogWriter().logDebug("Could not formulate automatic 400 Bad Request");
          }
        }
      }

      return;
    }
    // No parse exception but null message - reject it and
    // march on (or return).
    // exit this message processor if the message did not parse.

    if (sipMessage == null) {
      if (sipStack.isLoggingEnabled()) {
        this.sipStack.logWriter.logDebug("Rejecting message !  + Null message parsed.");
      }
      return;
    }
    ViaList viaList = sipMessage.getViaHeaders();
    // Check for the required headers.
    if (sipMessage.getFrom() == null
        || sipMessage.getTo() == null
        || sipMessage.getCallId() == null
        || sipMessage.getCSeq() == null
        || sipMessage.getViaHeaders() == null) {
      String badmsg = new String(msgBytes);
      if (sipStack.isLoggingEnabled()) {
        this.sipStack.logWriter.logError("bad message " + badmsg);
        this.sipStack.logWriter.logError(
            ">>> Dropped Bad Msg "
                + "From = "
                + sipMessage.getFrom()
                + "To = "
                + sipMessage.getTo()
                + "CallId = "
                + sipMessage.getCallId()
                + "CSeq = "
                + sipMessage.getCSeq()
                + "Via = "
                + sipMessage.getViaHeaders());
      }

      sipStack.logWriter.logError("BAD MESSAGE!");

      return;
    }
    // For a request first via header tells where the message
    // is coming from.
    // For response, just get the port from the packet.
    if (sipMessage instanceof SIPRequest) {
      Via v = (Via) viaList.getFirst();
      Hop hop = sipStack.addressResolver.resolveAddress(v.getHop());
      this.peerPort = hop.getPort();
      this.peerProtocol = v.getTransport();

      this.peerPacketSourceAddress = packet.getAddress();
      this.peerPacketSourcePort = packet.getPort();
      try {
        this.peerAddress = packet.getAddress();
        // Check to see if the received parameter matches
        // the peer address and tag it appropriately.

        boolean hasRPort = v.hasParameter(Via.RPORT);
        if (hasRPort || !hop.getHost().equals(this.peerAddress.getHostAddress())) {
          v.setParameter(Via.RECEIVED, this.peerAddress.getHostAddress());
        }

        if (hasRPort) {
          v.setParameter(Via.RPORT, Integer.toString(this.peerPacketSourcePort));
        }
      } catch (java.text.ParseException ex1) {
        InternalErrorHandler.handleException(ex1);
      }

    } else {

      this.peerPacketSourceAddress = packet.getAddress();
      this.peerPacketSourcePort = packet.getPort();
      this.peerAddress = packet.getAddress();
      this.peerPort = packet.getPort();
      this.peerProtocol = ((Via) viaList.getFirst()).getTransport();
    }

    this.processMessage(sipMessage);
  }