/**
   * Return a formatted message to the client. We try to re-connect with the peer on the other end
   * if possible.
   *
   * @param sipMessage Message to send.
   * @throws IOException If there is an error sending the message
   */
  public void sendMessage(SIPMessage sipMessage) throws IOException {
    byte[] msg = sipMessage.encodeAsBytes();

    long time = System.currentTimeMillis();

    this.sendMessage(msg, sipMessage instanceof SIPRequest);

    if (this.stack.serverLog.needsLogging(ServerLog.TRACE_MESSAGES))
      logMessage(sipMessage, peerAddress, peerPort, time);
  }
  /**
   * Encode this into a byte array. This is used when the body has been set as a binary array and
   * you want to encode the body as a byte array for transmission.
   *
   * @return a byte array containing the SIPRequest encoded as a byte array.
   */
  public byte[] encodeAsBytes(String transport) {
    if (this.isNullRequest()) {
      // Encoding a null message for keepalive.
      return "\r\n\r\n".getBytes();
    } else if (this.requestLine == null) {
      return new byte[0];
    }

    byte[] rlbytes = null;
    if (requestLine != null) {
      try {
        rlbytes = requestLine.encode().getBytes("UTF-8");
      } catch (UnsupportedEncodingException ex) {
        InternalErrorHandler.handleException(ex);
      }
    }
    byte[] superbytes = super.encodeAsBytes(transport);
    byte[] retval = new byte[rlbytes.length + superbytes.length];
    System.arraycopy(rlbytes, 0, retval, 0, rlbytes.length);
    System.arraycopy(superbytes, 0, retval, rlbytes.length, superbytes.length);
    return retval;
  }
  /**
   * Return a reply from a pre-constructed reply. This sends the message back to the entity who
   * caused us to create this channel in the first place.
   *
   * @param sipMessage Message string to send.
   * @throws IOException If there is a problem with sending the message.
   */
  public void sendMessage(SIPMessage sipMessage) throws IOException {
    if (sipStack.isLoggingEnabled() && this.sipStack.logStackTraceOnMessageSend) {
      if (sipMessage instanceof SIPRequest && ((SIPRequest) sipMessage).getRequestLine() != null) {
        /*
         * We dont want to log empty trace messages.
         */
        this.sipStack.logWriter.logStackTrace(LogWriter.TRACE_MESSAGES);
      } else {
        this.sipStack.logWriter.logStackTrace(LogWriter.TRACE_MESSAGES);
      }
    }

    // Test and see where we are going to send the messsage. If the message
    // is sent back to oursleves, just
    // shortcircuit processing.
    long time = System.currentTimeMillis();
    try {
      for (MessageProcessor messageProcessor : sipStack.getMessageProcessors()) {
        if (messageProcessor.getIpAddress().equals(this.peerAddress)
            && messageProcessor.getPort() == this.peerPort
            && messageProcessor.getTransport().equals(this.peerProtocol)) {
          MessageChannel messageChannel =
              messageProcessor.createMessageChannel(this.peerAddress, this.peerPort);
          if (messageChannel instanceof RawMessageChannel) {
            ((RawMessageChannel) messageChannel).processMessage(sipMessage);
            sipStack.logWriter.logDebug("Self routing message");
            return;
          }
        }
      }

      byte[] msg = sipMessage.encodeAsBytes(this.getTransport());

      sendMessage(msg, peerAddress, peerPort, peerProtocol, sipMessage instanceof SIPRequest);

    } catch (IOException ex) {
      throw ex;
    } catch (Exception ex) {
      sipStack.logWriter.logError("An exception occured while sending message", ex);
      throw new IOException("An exception occured while sending message");
    } finally {
      if (sipStack.logWriter.isLoggingEnabled(ServerLog.TRACE_MESSAGES))
        logMessage(sipMessage, peerAddress, peerPort, time);
    }
  }
  /**
   * Gets invoked by the parser as a callback on successful message parsing (i.e. no parser errors).
   *
   * @param sipMessage Mesage to process (this calls the application for processing the message).
   */
  public void processMessage(SIPMessage sipMessage) throws Exception {
    try {
      if (sipMessage.getFrom() == null
          || // sipMessage.getFrom().getTag() == null ||
          sipMessage.getTo() == null
          || sipMessage.getCallId() == null
          || sipMessage.getCSeq() == null
          || sipMessage.getViaHeaders() == null) {
        String badmsg = sipMessage.encode();
        if (LogWriter.needsLogging) {
          stack.logWriter.logMessage("bad message " + badmsg);
          stack.logWriter.logMessage(">>> Dropped Bad Msg");
        }
        stack.logBadMessage(badmsg);
        return;
      }

      ViaList viaList = sipMessage.getViaHeaders();
      // For a request
      // first via header tells where the message is coming from.
      // For response, this has already been recorded in the outgoing
      // message.
      if (sipMessage instanceof SIPRequest) {
        Via v = (Via) viaList.first();
        if (v.hasPort()) {
          this.peerPort = v.getPort();
        } else this.peerPort = 5061;
        this.peerProtocol = v.getTransport();
        try {
          this.peerAddress = mySock.getInetAddress();
          // Check to see if the received parameter matches
          // the peer address and tag it appropriately.
          // Bug fix by [email protected]
          // Should record host address not host name
          // bug fix by  Joost Yervante Damand
          if (!v.getSentBy().getInetAddress().equals(this.peerAddress)) {
            v.setParameter(Via.RECEIVED, this.peerAddress.getHostAddress());
            // @@@ hagai
            v.setParameter(Via.RPORT, new Integer(this.peerPort).toString());
          }
        } catch (java.net.UnknownHostException ex) {
          // Could not resolve the sender address.
          if (LogWriter.needsLogging) {
            stack.logWriter.logMessage("Rejecting message -- could not resolve Via Address");
          }
          return;
        } catch (java.text.ParseException ex) {
          InternalErrorHandler.handleException(ex);
        }
        // Use this for outgoing messages as well.
        if (!this.isCached) {
          ((TLSMessageProcessor) this.messageProcessor).cacheMessageChannel(this);
          this.isCached = true;
          String key = IOHandler.makeKey(mySock.getInetAddress(), this.peerPort);
          stack.ioHandler.putSocket(key, mySock);
        }
      }

      // Foreach part of the request header, fetch it and process it

      long receptionTime = System.currentTimeMillis();
      //

      if (sipMessage instanceof SIPRequest) {
        // This is a request - process the request.
        SIPRequest sipRequest = (SIPRequest) sipMessage;
        // Create a new sever side request processor for this
        // message and let it handle the rest.

        if (LogWriter.needsLogging) {
          stack.logWriter.logMessage("----Processing Message---");
        }

        // Check for reasonable size - reject message
        // if it is too long.
        if (stack.getMaxMessageSize() > 0
            && sipRequest.getSize()
                    + (sipRequest.getContentLength() == null
                        ? 0
                        : sipRequest.getContentLength().getContentLength())
                > stack.getMaxMessageSize()) {
          SIPResponse sipResponse = sipRequest.createResponse(SIPResponse.MESSAGE_TOO_LARGE);
          byte[] resp = sipResponse.encodeAsBytes();
          this.sendMessage(resp, false);
          throw new Exception("Message size exceeded");
        }

        ServerRequestInterface sipServerRequest = stack.newSIPServerRequest(sipRequest, this);
        sipServerRequest.processRequest(sipRequest, this);
        if (this.stack.serverLog.needsLogging(ServerLog.TRACE_MESSAGES)) {
          if (sipServerRequest.getProcessingInfo() == null) {
            stack.serverLog.logMessage(
                sipMessage,
                sipRequest.getViaHost() + ":" + sipRequest.getViaPort(),
                stack.getHostAddress() + ":" + stack.getPort(this.getTransport()),
                false,
                receptionTime);
          } else {
            this.stack.serverLog.logMessage(
                sipMessage,
                sipRequest.getViaHost() + ":" + sipRequest.getViaPort(),
                stack.getHostAddress() + ":" + stack.getPort(this.getTransport()),
                sipServerRequest.getProcessingInfo(),
                false,
                receptionTime);
          }
        }
      } else {
        SIPResponse sipResponse = (SIPResponse) sipMessage;
        // This is a response message - process it.
        // Check the size of the response.
        // If it is too large dump it silently.
        if (stack.getMaxMessageSize() > 0
            && sipResponse.getSize()
                    + (sipResponse.getContentLength() == null
                        ? 0
                        : sipResponse.getContentLength().getContentLength())
                > stack.getMaxMessageSize()) {
          if (LogWriter.needsLogging) this.stack.logWriter.logMessage("Message size exceeded");
          return;
        }
        ServerResponseInterface sipServerResponse = stack.newSIPServerResponse(sipResponse, this);
        sipServerResponse.processResponse(sipResponse, this);
      }
    } finally {
    }
  }
  /**
   * 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);
  }