2 new revisions:
Revision: 1f1bf55b1475
Author: vralev <vralev@8e71dc83-d81e-6649-80f2-80b843a9b2be>
Date: Thu Jul 19 14:20:11 2012
Log: JSIP-416...
http://code.google.com/p/jain-sip/source/detail?r=1f1bf55b1475
Revision: a2feb0a93484
Author: mranga <mranga@8e71dc83-d81e-6649-80f2-80b843a9b2be>
Date: Tue Jul 31 12:09:28 2012
Log: JSIP-428: Fix thread auditor (apply patch supplied by
ywang4074)...
http://code.google.com/p/jain-sip/source/detail?r=a2feb0a93484
==============================================================================
Revision: 1f1bf55b1475
Author: vralev <vralev@8e71dc83-d81e-6649-80f2-80b843a9b2be>
Date: Thu Jul 19 14:20:11 2012
Log: JSIP-416
Savepoint for TCP/TLS reconnect cleanup.
git-svn-id:
https://svn.java.net/svn/jsip~svn/branches/JAIN-SIP-NIO-year-2012@2134
8e71dc83-d81e-6649-80f2-80b843a9b2be
http://code.google.com/p/jain-sip/source/detail?r=1f1bf55b1475
Modified:
/src/gov/nist/javax/sip/stack/ConnectionOrientedMessageChannel.java
/src/gov/nist/javax/sip/stack/NIOHandler.java
/src/gov/nist/javax/sip/stack/NioTcpMessageChannel.java
/src/gov/nist/javax/sip/stack/NioTcpMessageProcessor.java
/src/gov/nist/javax/sip/stack/NioTlsMessageChannel.java
=======================================
--- /src/gov/nist/javax/sip/stack/ConnectionOrientedMessageChannel.java Thu
May 31 13:40:31 2012
+++ /src/gov/nist/javax/sip/stack/ConnectionOrientedMessageChannel.java Thu
Jul 19 14:20:11 2012
@@ -302,7 +302,7 @@
// the peer address and tag it appropriately.
Hop hop =
sipStack.addressResolver.resolveAddress(v.getHop());
this.peerProtocol = v.getTransport();
- if(peerPortAdvertisedInHeaders <= 0) {
+ //if(peerPortAdvertisedInHeaders <= 0) {
int hopPort = v.getPort();
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
logger.logDebug("hop port = " + hopPort + " for
request " + sipMessage + " for this channel " + this + " key " + key);
@@ -316,7 +316,7 @@
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
logger.logDebug("3.Storing
peerPortAdvertisedInHeaders = " + peerPortAdvertisedInHeaders + " for this
channel " + this + " key " + key);
}
- }
+ //}
// may be needed to reconnect, when diff than peer address
if(peerAddressAdvertisedInHeaders == null) {
peerAddressAdvertisedInHeaders = hop.getHost();
=======================================
--- /src/gov/nist/javax/sip/stack/NIOHandler.java Fri Jul 6 15:40:57 2012
+++ /src/gov/nist/javax/sip/stack/NIOHandler.java Thu Jul 19 14:20:11 2012
@@ -129,8 +129,7 @@
* writes can occur from multiple threads. We write in chunks to allow
the
* other side to synchronize for large sized writes.
*/
- private void writeChunks(SocketChannel channel, byte[] bytes, int
length)
- throws IOException {
+ private void writeChunks(SocketChannel channel, byte[] bytes, int
length) {
// Chunk size is 16K - this hack is for large
// writes over slow connections.
synchronized (channel) {
@@ -184,9 +183,15 @@
SocketChannel clientSock = null;
keyedSemaphore.enterIOCriticalSection(key);
+ boolean newSocket = false;
try {
clientSock = getSocket(key);
while (retry_count < max_retry) {
+ if(clientSock != null && (!clientSock.isConnected() |
| !clientSock.isOpen()) ) {
+ removeSocket(key);
+ clientSock = null;
+ newSocket = true;
+ }
if (clientSock == null) {
if
(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
logger.logDebug(
@@ -202,52 +207,25 @@
// the IP address is on a per listening point
basis.
try {
clientSock = messageProcessor.blockingConnect(new
InetSocketAddress(receiverAddress, contactPort), 10000);
+ newSocket = true;
//sipStack.getNetworkLayer().createSocket(
// receiverAddress, contactPort, senderAddress);
TODO: sender address needed
} catch (SocketException e) { // We must catch the
socket timeout exceptions here, any SocketException not just
ConnectException
logger.logError("Problem connecting " +
- receiverAddress + " " + contactPort + " " +
senderAddress + " for message " + new String(bytes, "UTF-8"));
+ receiverAddress + " " + contactPort + " " +
senderAddress + " for message " + (messageChannel.isSecure()?"<<<ENCRYPTED
MESSAGE>>>":new String(bytes, "UTF-8")));
// new connection is bad.
// remove from our table the socket and its
semaphore
removeSocket(key);
throw new SocketException(e.getClass() + " " +
e.getMessage() + " " + e.getCause() + " Problem connecting " +
receiverAddress + " " + contactPort + " " +
senderAddress + " for message " + new String(bytes, "UTF-8"));
}
- writeChunks(clientSock, bytes, length);
putSocket(key, clientSock);
break;
} else {
- try {
- writeChunks(clientSock, bytes, length);
- break;
- } catch (IOException ex) {
- if (logger
- .isLoggingEnabled(LogWriter.TRACE_WARN))
- logger.logWarning(
- "IOException occured retryCount "
- + retry_count);
- try {
- clientSock.close();
- } catch (Exception e) {
- }
- clientSock = null;
- retry_count++;
- // This is a server tx trying to send a
response.
- if ( !isClient ) {
-
removeSocket(key);
- throw ex;
- }
- if(retry_count >= max_retry) {
- // old
connection is bad.
- // remove from
our table the socket and its semaphore
-
removeSocket(key);
- } else {
- // don't remove
the semaphore on retry
-
socketTable.remove(key);
- }
- }
+ break;
}
}
+
} catch (IOException ex) {
if (logger.isLoggingEnabled(LogWriter.TRACE_ERROR)) {
logger.logError(
@@ -285,12 +263,12 @@
if (!isClient) {
receiverAddress = InetAddress.getByName(messageChannel
.getViaHost());
- contactPort = messageChannel.getViaPort();
- if (contactPort == -1)
+ contactPort =
messageChannel.peerPortAdvertisedInHeaders;
+ if (contactPort <= 0)
contactPort = 5060;
key = makeKey(receiverAddress, messageChannel
- .getViaPort());
+ .peerPortAdvertisedInHeaders);
clientSock = this.getSocket(key);
if (clientSock == null) {
if
(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
@@ -299,45 +277,36 @@
" port = " + contactPort);
}
clientSock = messageProcessor.blockingConnect(new
InetSocketAddress(receiverAddress, contactPort), 10000);
-
-
- writeChunks(clientSock, bytes, length);
+ newSocket = true;
+ messageChannel.peerPort =
contactPort;
putSocket(key, clientSock);
- return clientSock;
- } else {
- if
(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
- logger.logDebug(
- "sending to " + key );
- }
- try {
- writeChunks(clientSock, bytes, length);
- return clientSock;
- } catch (IOException ioe) {
- if (logger
- .isLoggingEnabled(LogWriter.TRACE_ERROR))
- logger.logError(
- "IOException occured ", ioe);
- if (logger
- .isLoggingEnabled(LogWriter.TRACE_DEBUG))
- logger.logDebug(
- "Removing and Closing socket");
- // old connection is bad.
- // remove from our table.
- removeSocket(key);
- try {
- clientSock.close();
- } catch (Exception e) {
- }
- clientSock = null;
- throw ioe;
- }
- }
+ }
+
+ if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
+ logger.logDebug(
+ "sending to " + key );
+ }
+
+
} else {
logger.logError("IOException occured at " , ex);
throw ex;
}
+
+ return clientSock;
} finally {
- keyedSemaphore.leaveIOCriticalSection(key);
+ try {
+ if(clientSock != null) {
+ if(newSocket && messageChannel instanceof
NioTlsMessageChannel) {
+ //messageChannel.onNewSocket();
+ } else {
+ writeChunks(clientSock, bytes, length);
+ }
+ }
+
+ } finally {
+ keyedSemaphore.leaveIOCriticalSection(key);
+ }
}
if (clientSock == null) {
=======================================
--- /src/gov/nist/javax/sip/stack/NioTcpMessageChannel.java Mon Jul 16
02:49:59 2012
+++ /src/gov/nist/javax/sip/stack/NioTcpMessageChannel.java Thu Jul 19
14:20:11 2012
@@ -228,102 +228,14 @@
logger.logDebug("sendMessage isClient = " + isClient + " this = " +
this);
}
lastActivityTimeStamp = System.currentTimeMillis();
- /*
- * /* Patch from [email protected] (Issue 119 ) This patch
avoids the
- * case where two TCPMessageChannels are now pointing to the
same
- * socket.getInputStream().
- *
- * JvB 22/5 removed
- */
- SocketChannel sock = null;
+
NIOHandler nioHandler = ((NioTcpMessageProcessor)
messageProcessor).nioHandler;
if(this.socketChannel != null && this.socketChannel.isConnected() &&
this.socketChannel.isOpen()) {
nioHandler.putSocket(NIOHandler.makeKey(this.peerAddress,
this.peerPort), this.socketChannel);
}
- IOException problem = null;
- try {
- sock = ((NioTcpMessageProcessor)
messageProcessor).nioHandler.sendBytes(this.messageProcessor
- .getIpAddress(), this.peerAddress,
this.peerPort,
- this.peerProtocol, msg, isClient, this);
- } catch (IOException any) {
- problem = any;
- logger.logWarning("Failed to connect " + this.peerAddress +
":"
- + this.peerPort + " but trying the
advertised port="
- + this.peerPortAdvertisedInHeaders
- + " if it's different than the port we just
failed on");
- }
- if (sock == null) { // If we couldn't connect to the host, try
the
- // advertised port as
failsafe
- if (this.peerPort != this.peerPortAdvertisedInHeaders
- && peerPortAdvertisedInHeaders > 0) {
// no point in trying
-
// same port
- logger.logWarning("Couldn't connect to peerAddress =
"
- + peerAddress + " peerPort = " + peerPort
+ " key = "
- + key + " retrying on
peerPortAdvertisedInHeaders "
- + peerPortAdvertisedInHeaders);
-
- sock = ((NioTcpMessageProcessor)
messageProcessor).nioHandler.sendBytes(this.messageProcessor
- .getIpAddress(),
this.peerAddress,
-
this.peerPortAdvertisedInHeaders, this.peerProtocol,
- msg, isClient, this);
- this.peerPort =
this.peerPortAdvertisedInHeaders;
- this.key = MessageChannel.getKey(peerAddress, peerPort,
"TCP");
- logger.logWarning("retry suceeded to peerAddress =
"
- + peerAddress + "
peerPortAdvertisedInHeaders = "
- + peerPortAdvertisedInHeaders + "
key = " + key);
- } else {
- throw problem; // throw the original excpetion
we had from the
- // first attempt
- }
- }
-
- // Created a new socket so close the old one and stick the new
- // one in its place but dont do this if it is a datagram socket.
- // (could have replied via udp but received via tcp!).
- // if (mySock == null && s != null) {
- // this.uncache();
- // } else
- if (sock != socketChannel && sock != null) {
- if (socketChannel != null) {
- if
(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
- logger
- .logWarning("Old socket
different than new socket on channel "
- + sock + "
" + socketChannel);
- logger.logStackTrace();
- logger.logWarning("Old socket local ip
address "
- +
socketChannel.socket().getLocalSocketAddress());
- logger.logWarning("Old socket remote ip
address "
- +
socketChannel.socket().getRemoteSocketAddress());
- logger.logWarning("New socket local ip
address "
- +
sock.socket().getLocalSocketAddress());
- logger.logWarning("New socket remote ip
address "
- +
sock.socket().getRemoteSocketAddress());
- }
- close(false);
- }
- if (problem == null) {
- if (socketChannel != null) {
- if
(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
- logger
- .logWarning("There was no exception for the retry mechanism so
creating a new thread based on the new socket for incoming "
-
+ key);
- }
- }
- socketChannel = sock;
-
- } else {
- if
(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
- logger
- .logWarning("There was an exception for the retry mechanism so not
creating a new thread based on the new socket for incoming "
- + key);
- }
- socketChannel = sock;
- }
- }
-
+ sendTCPMessage(msg, this.peerAddress, this.peerPort, isClient);
}
-
/**
* Send a message to a specified address.
*
@@ -338,6 +250,22 @@
*/
public void sendMessage(byte message[], InetAddress receiverAddress,
int receiverPort, boolean retry) throws IOException {
+ sendTCPMessage(message, receiverAddress, receiverPort, retry);
+ }
+ /**
+ * Send a message to a specified address.
+ *
+ * @param message
+ * Pre-formatted message to send.
+ * @param receiverAddress
+ * Address to send it to.
+ * @param receiverPort
+ * Receiver port.
+ * @throws IOException
+ * If there is a problem connecting or sending.
+ */
+ public void sendTCPMessage(byte message[], InetAddress receiverAddress,
+ int receiverPort, boolean retry) throws IOException {
if (message == null || receiverAddress == null) {
logger.logError("receiverAddress = " + receiverAddress);
throw new IllegalArgumentException("Null argument");
@@ -361,48 +289,11 @@
+ this + " key " + key);
}
}
-
- SocketChannel sock = null;
- IOException problem = null;
- try {
- sock = ((NioTcpMessageProcessor)
messageProcessor).nioHandler.sendBytes(this.messageProcessor
+ NIOHandler nioHandler = ((NioTcpMessageProcessor)
messageProcessor).nioHandler;
+
+ SocketChannel sock = nioHandler.sendBytes(this.messageProcessor
.getIpAddress(), receiverAddress, receiverPort,
"TCP",
message, retry, this);
- } catch (IOException any) {
- problem = any;
- logger.logWarning("Failed to connect " + this.peerAddress +
":"
- + receiverPort + " but trying the advertised
port="
- + this.peerPortAdvertisedInHeaders
- + " if it's different than the port we just
failed on");
- logger.logError("Error is ", any);
-
- }
- if (sock == null) { // If we couldn't connect to the host, try
the
- // advertised port as
failsafe
- if (receiverPort != this.peerPortAdvertisedInHeaders
- && peerPortAdvertisedInHeaders > 0) {
// no point in trying
-
// same port
- logger.logWarning("Couldn't connect to
receiverAddress = "
- + receiverAddress + " receiverPort =
" + receiverPort
- + " key = " + key
- + " retrying on
peerPortAdvertisedInHeaders "
- + peerPortAdvertisedInHeaders);
- sock = ((NioTcpMessageProcessor)
messageProcessor).nioHandler.sendBytes(this.messageProcessor
- .getIpAddress(),
receiverAddress,
- this.peerPortAdvertisedInHeaders,
"TCP", message,
- retry, this);
- this.peerPort =
this.peerPortAdvertisedInHeaders;
- this.key = MessageChannel.getKey(peerAddress,
- peerPortAdvertisedInHeaders,
"TCP");
-
- logger.logWarning("retry suceeded to receiverAddress
= "
- + receiverAddress + "
peerPortAdvertisedInHeaders = "
- + peerPortAdvertisedInHeaders + "
key = " + key);
- } else {
- throw problem; // throw the original excpetion
we had from the
- // first attempt
- }
- }
if (sock != socketChannel && sock != null) {
if (socketChannel != null) {
@@ -420,29 +311,31 @@
logger.logWarning("New socket remote ip
address "
+
sock.socket().getRemoteSocketAddress());
}
- close(false);
- }
- if (problem == null) {
- if (socketChannel != null) {
- if
(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
- logger
- .logWarning("There was no exception for the retry mechanism so we
keep going "
-
+ key);
- }
- }
+ close(false); // we can call socketChannel.close() directly but we
better use the inherited method
+
socketChannel = sock;
- } else {
+ putMessageChannel(socketChannel, this);
+
+ onNewSocket();
+ }
+
+ if (socketChannel != null) {
if
(logger.isLoggingEnabled(LogWriter.TRACE_WARN)) {
logger
- .logWarning("There was an exception for the retry mechanism so
stop "
- + key);
- }
- socketChannel = sock;
- }
+ .logWarning("There was no exception for the retry mechanism so we
keep going "
+ + key);
+ }
+ }
+ socketChannel = sock;
}
}
+ public void onNewSocket() {
+
+ }
+
+
/**
* Exception processor for exceptions detected from the parser. (This is
* invoked by the parser when an error is detected).
=======================================
--- /src/gov/nist/javax/sip/stack/NioTcpMessageProcessor.java Wed Apr 11
12:02:54 2012
+++ /src/gov/nist/javax/sip/stack/NioTcpMessageProcessor.java Thu Jul 19
14:20:11 2012
@@ -78,9 +78,10 @@
return initiateConnection(address, timeout);
}
- public void send(SocketChannel socket, byte[] data) {
+ public void send(SocketChannel socket, byte[] data) {
if(logger.isLoggingEnabled(LogWriter.TRACE_DEBUG))
logger.logDebug("Sending data " + data.length + " bytes on socket "
+ socket);
+
synchronized (this.changeRequests) {
this.changeRequests.add(new ChangeRequest(socket,
ChangeRequest.CHANGEOPS, SelectionKey.OP_WRITE));
=======================================
--- /src/gov/nist/javax/sip/stack/NioTlsMessageChannel.java Thu Jul 5
22:32:44 2012
+++ /src/gov/nist/javax/sip/stack/NioTlsMessageChannel.java Thu Jul 19
14:20:11 2012
@@ -90,16 +90,25 @@
appFrameBuffer.clear();
return appFrameBuffer;
}
+
+ public static class SSLReconnectedException extends IOException {
+ private static final long serialVersionUID = 1L;}
@Override
- protected void sendMessage(byte[] msg, final boolean isClient) throws
IOException {
+ protected void sendMessage(final byte[] msg, final boolean isClient)
throws IOException {
+ lastMessage = new byte[msg.length];
+ for(int q=0;q<msg.length;q++) lastMessage[q] = msg[q];
+ String s = new String(lastMessage);
+ //logger.logError(">>>>>>>>>>>>>>FFF>>>>>" + s);
ByteBuffer b = ByteBuffer.wrap(msg);
try {
sslStateMachine.wrap(b, encryptedFrameBuffer, new MessageSendCallback()
{
@Override
public void doSend(byte[] bytes) throws
IOException {
-
NioTlsMessageChannel.super.sendMessage(bytes, isClient);
+
+
NioTlsMessageChannel.super.sendMessage(bytes, isClient);
+
}
});
} catch (Exception e) {
@@ -109,12 +118,34 @@
public void sendEncryptedData(byte[] msg) throws IOException {
// bypass the encryption for already encrypted data or TLS
metadata
- super.sendMessage(msg, false);
- }
-
+ if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
+ logger.logDebug("sendEncryptedData " + " this = " + this + " peerPort
= " + peerPort + " addr = " + peerAddress);
+ }
+ lastActivityTimeStamp = System.currentTimeMillis();
+
+ NIOHandler nioHandler = ((NioTcpMessageProcessor)
messageProcessor).nioHandler;
+ if(this.socketChannel != null && this.socketChannel.isConnected() &&
this.socketChannel.isOpen()) {
+ nioHandler.putSocket(NIOHandler.makeKey(this.peerAddress,
this.peerPort), this.socketChannel);
+ }
+ super.sendMessage(msg, this.peerAddress, this.peerPort, true);
+ }
+ private byte[] lastMessage = null;
@Override
- public void sendMessage(byte message[], final InetAddress
receiverAddress,
+ public void sendMessage(final byte message[], final InetAddress
receiverAddress,
final int receiverPort, final boolean retry) throws
IOException {
+ lastMessage = new byte[message.length];
+ for(int q=0;q<message.length;q++) {
+
+ lastMessage[q] = message[q];
+ if(message[q]<0) {
+ int r = 0 ;
+ r++;r=r;
+ }
+ }
+ String s = new String(lastMessage);
+
+ //logger.logError(">>>>>>>>>>>>>>FFF>>>>>" + s);
+
ByteBuffer b = ByteBuffer.wrap(message);
try {
sslStateMachine.wrap(b, encryptedFrameBuffer, new MessageSendCallback()
{
@@ -123,6 +154,7 @@
public void doSend(byte[] bytes) throws
IOException {
NioTlsMessageChannel.super.sendMessage(bytes,
receiverAddress,
receiverPort, retry);
+
}
});
} catch (IOException e) {
@@ -169,5 +201,24 @@
public String getTransport() {
return "TLS";
}
+
+ @Override
+ public void onNewSocket() {
+ super.onNewSocket();
+ logger.logError("NEW SOCKET", new RuntimeException());
+ try {
+ init(true);
+ createBuffers();
+ String s = new String(lastMessage, "UTF-8");
+ sendMessage(lastMessage, false);
+ } catch (Exception e) {
+ logger.logError("Cant reinit", e);
+ }
+ }
+
+ @Override
+ public boolean isSecure() {
+ return true;
+ }
}
==============================================================================
Revision: a2feb0a93484
Author: mranga <mranga@8e71dc83-d81e-6649-80f2-80b843a9b2be>
Date: Tue Jul 31 12:09:28 2012
Log: JSIP-428: Fix thread auditor (apply patch supplied by ywang4074)
Backport to NIO Branch
git-svn-id: https://svn.java.net/svn/jsip~svn/trunk@2135
8e71dc83-d81e-6649-80f2-80b843a9b2be
(cherry picked from commit 6fe1ce544d6002734187937d9cff8012e189336c)
http://code.google.com/p/jain-sip/source/detail?r=a2feb0a93484
Modified:
/src/gov/nist/javax/sip/stack/UDPMessageChannel.java
=======================================
--- /src/gov/nist/javax/sip/stack/UDPMessageChannel.java Thu Dec 2
14:44:53 2010
+++ /src/gov/nist/javax/sip/stack/UDPMessageChannel.java Tue Jul 31
12:09:28 2012
@@ -59,6 +59,7 @@
import java.net.Socket;
import java.text.ParseException;
import java.util.Hashtable;
+import java.util.concurrent.TimeUnit;
import javax.sip.address.Hop;
import javax.sip.message.Response;
@@ -281,13 +282,25 @@
// Send a heartbeat to the thread auditor
threadHandle.ping();
+
try {
- packet =
udpMessageProcessor.messageQueue.take().packet;
+ DatagramQueuedMessageDispatch work =
udpMessageProcessor.messageQueue.poll(threadHandle
+ .getPingIntervalInMillisecs(),
TimeUnit.MILLISECONDS);
+
+ if (!udpMessageProcessor.isRunning) {
+ return;
+ }
+ if (work == null) {
+ continue;
+ } else {
+ packet = (DatagramPacket)
(work.packet);
+ this.incomingPacket = packet;
+ }
} catch (InterruptedException ex) {
- if (!udpMessageProcessor.isRunning)
- return;
- }
- this.incomingPacket = packet;
+ if (!udpMessageProcessor.isRunning) {
+ return;
+ }
+ }
} else {
packet = this.incomingPacket;
}