/** * 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.isLogStackTraceOnMessageSend()) { if (sipMessage instanceof SIPRequest && ((SIPRequest) sipMessage).getRequestLine() != null) { /* * We dont want to log empty trace messages. */ this.sipStack.getStackLogger().logStackTrace(StackLogger.TRACE_INFO); } else { this.sipStack.getStackLogger().logStackTrace(StackLogger.TRACE_INFO); } } // 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); if (sipStack.isLoggingEnabled(LogWriter.TRACE_DEBUG)) sipStack.getStackLogger().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.getStackLogger().logError("An exception occured while sending message", ex); throw new IOException("An exception occured while sending message"); } finally { if (sipStack.getStackLogger().isLoggingEnabled(ServerLogger.TRACE_MESSAGES) && !sipMessage.isNullRequest()) logMessage(sipMessage, peerAddress, peerPort, time); else if (sipStack.getStackLogger().isLoggingEnabled(ServerLogger.TRACE_DEBUG)) sipStack.getStackLogger().logDebug("Sent EMPTY Message"); } }
/** * Process the message through the transaction and sends it to the SIP peer. * * @param messageToSend Message to send to the SIP peer. */ public void sendMessage(final SIPMessage messageToSend) throws IOException { // Use the peer address, port and transport // that was specified when the transaction was // created. Bug was noted by Bruce Evangelder // soleo communications. try { final RawMessageChannel channel = (RawMessageChannel) encapsulatedChannel; for (MessageProcessor messageProcessor : sipStack.getMessageProcessors()) { boolean addrmatch = messageProcessor .getIpAddress() .getHostAddress() .toString() .equals(this.getPeerAddress()); if (addrmatch && messageProcessor.getPort() == this.getPeerPort() && messageProcessor.getTransport().equalsIgnoreCase(this.getPeerProtocol())) { if (channel instanceof TCPMessageChannel) { try { Runnable processMessageTask = new Runnable() { public void run() { try { ((TCPMessageChannel) channel) .processMessage( (SIPMessage) messageToSend.clone(), getPeerInetAddress()); } catch (Exception ex) { if (getSIPStack() .getStackLogger() .isLoggingEnabled(ServerLogger.TRACE_ERROR)) { getSIPStack() .getStackLogger() .logError("Error self routing message cause by: ", ex); } } } }; getSIPStack().getSelfRoutingThreadpoolExecutor().execute(processMessageTask); } catch (Exception e) { sipStack.getStackLogger().logError("Error passing message in self routing", e); } if (sipStack.isLoggingEnabled(LogWriter.TRACE_DEBUG)) sipStack.getStackLogger().logDebug("Self routing message"); return; } if (channel instanceof RawMessageChannel) { try { Runnable processMessageTask = new Runnable() { public void run() { try { ((RawMessageChannel) channel) .processMessage((SIPMessage) messageToSend.clone()); } catch (Exception ex) { if (getSIPStack() .getStackLogger() .isLoggingEnabled(ServerLogger.TRACE_ERROR)) { getSIPStack() .getStackLogger() .logError("Error self routing message cause by: ", ex); } } } }; getSIPStack().getSelfRoutingThreadpoolExecutor().execute(processMessageTask); } catch (Exception e) { sipStack.getStackLogger().logError("Error passing message in self routing", e); } if (sipStack.isLoggingEnabled(LogWriter.TRACE_DEBUG)) sipStack.getStackLogger().logDebug("Self routing message"); return; } } } encapsulatedChannel.sendMessage(messageToSend, this.peerInetAddress, this.peerPort); } finally { this.startTransactionTimer(); } }