}
}
public void sendResponse(SipResponse sipResponse) throws IOException {
//18.2.2
SipHeaderFieldValue topVia = Utils.getTopVia(sipResponse);
String topViaValue = topVia.getValue();
StringBuffer buf = new StringBuffer(topViaValue);
String hostport = null;
int i = topViaValue.length() - 1;
while (i > 0) {
char c = buf.charAt(i);
if (c == ' ' || c == '\t') {
hostport = buf.substring(i + 1);
break;
}
--i;
}
if (hostport == null) {
throw new RuntimeException("host or ip address not found in top via");
}
String host;
int port;
int colonPos = hostport.indexOf(RFC3261.TRANSPORT_PORT_SEP);
if (colonPos > -1) {
host = hostport.substring(0, colonPos);
port = Integer.parseInt(
hostport.substring(colonPos + 1, hostport.length()));
} else {
host = hostport;
port = RFC3261.TRANSPORT_DEFAULT_PORT;
}
String transport;
if (buf.indexOf(RFC3261.TRANSPORT_TCP) > -1) {
transport = RFC3261.TRANSPORT_TCP;
} else if (buf.indexOf(RFC3261.TRANSPORT_UDP) > -1) {
transport = RFC3261.TRANSPORT_UDP;
} else {
logger.error("no transport found in top via header," +
" discarding response");
return;
}
String received =
topVia.getParam(new SipHeaderParamName(RFC3261.PARAM_RECEIVED));
if (received != null) {
host = received;
}
//RFC3581
//TODO check config
String rport = topVia.getParam(new SipHeaderParamName(
RFC3261.PARAM_RPORT));
if (rport != null) {
port = Integer.parseInt(rport);
}
SipTransportConnection connection;
try {
connection = new SipTransportConnection(config.getLocalInetAddress(),
config.getSipPort(), InetAddress.getByName(host),
port, transport);
} catch (UnknownHostException e) {
logger.error("unknwon host", e);
return;
}
//actual sending
//TODO manage maddr parameter in top via for multicast
if (buf.indexOf(RFC3261.TRANSPORT_TCP) > -1) {
// Socket socket = (Socket)factory.connections.get(connection);
// if (!socket.isClosed()) {
// try {
// socket.getOutputStream().write(data);
// } catch (IOException e) {
// e.printStackTrace();
// return;
// //TODO
// }
// } else {
// try {
// socket = new Socket(host, port);
// factory.connections.put(connection, socket);
// socket.getOutputStream().write(data);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// /*
// * TODO
// * If connection attempt fails, use the procedures in RFC3263
// * for servers in order to determine the IP address and
// * port to open the connection and send the response to.
// */
// return;
// }
// }
} else {
MessageSender messageSender = messageSenders.get(connection);
if (messageSender == null) {
messageSender = createMessageSender(connection);
}
//add contact header
SipHeaderFieldName contactName = new SipHeaderFieldName(RFC3261.HDR_CONTACT);
SipHeaders respHeaders = sipResponse.getSipHeaders();
StringBuffer contactBuf = new StringBuffer();
contactBuf.append(RFC3261.LEFT_ANGLE_BRACKET);
contactBuf.append(RFC3261.SIP_SCHEME);
contactBuf.append(RFC3261.SCHEME_SEPARATOR);
contactBuf.append(messageSender.getContact());
contactBuf.append(RFC3261.RIGHT_ANGLE_BRACKET);
respHeaders.add(contactName, new SipHeaderFieldValue(contactBuf.toString()));
messageSender.sendMessage(sipResponse);
}