예제 #1
0
  /**
   * Send a message to a specified receiver address.
   *
   * @param msg message string to send.
   * @param peerAddress Address of the place to send it to.
   * @param peerPort the port to send it to.
   * @param peerProtocol protocol to use to send.
   * @throws IOException If there is trouble sending this message.
   */
  protected void sendMessage(
      byte[] msg, InetAddress peerAddress, int peerPort, String peerProtocol, boolean retry)
      throws IOException {
    // Via is not included in the request so silently drop the reply.
    if (peerPort == -1) {
      if (sipStack.isLoggingEnabled()) {
        this.sipStack.logWriter.logDebug(getClass().getName() + ":sendMessage: Dropping reply!");
      }
      throw new IOException("Receiver port not set ");
    } else {
      if (sipStack.isLoggingEnabled()) {
        this.sipStack.logWriter.logDebug(
            getClass().getName()
                + ":sendMessage "
                + peerAddress.getHostAddress()
                + "/"
                + peerPort
                + "\n"
                + new String(msg));
        this.sipStack.logWriter.logDebug("*******************\n");
      }
    }
    if (peerProtocol.compareToIgnoreCase("UDP") == 0) {
      DatagramPacket reply = new DatagramPacket(msg, msg.length, peerAddress, peerPort);

      try {
        DatagramSocket sock;
        if (sipStack.udpFlag) {
          sock = ((UDPMessageProcessor) messageProcessor).sock;

        } else {
          // bind to any interface and port.
          sock = sipStack.getNetworkLayer().createDatagramSocket();
        }
        if (sipStack.isLoggingEnabled()) {
          this.sipStack.logWriter.logDebug(
              "sendMessage "
                  + peerAddress.getHostAddress()
                  + "/"
                  + peerPort
                  + "\n"
                  + new String(msg));
        }
        sock.send(reply);
        if (!sipStack.udpFlag) sock.close();
      } catch (IOException ex) {
        throw ex;
      } catch (Exception ex) {
        InternalErrorHandler.handleException(ex);
      }

    } else {
      // Use TCP to talk back to the sender.
      Socket outputSocket =
          sipStack.ioHandler.sendBytes(
              this.messageProcessor.getIpAddress(), peerAddress, peerPort, "tcp", msg, retry);
      OutputStream myOutputStream = outputSocket.getOutputStream();
      myOutputStream.write(msg, 0, msg.length);
      myOutputStream.flush();
      // The socket is cached (dont close it!);
    }
  }