#11 Recognizing a dropped connection

open
nobody
None
5
2014-08-23
2005-11-16
Bob Krause
No

Applications need to be able to reconize when a
connection has been dropped so that they can reconnect
if necessary, but not abandon a good connection every
time an i/o operation throws an IOException. Based on
the JSR82 spec, RemoteDevice.getRemoteDevice() should
throw an IOException when it is passed a Connection
that is closed. But this is not the current behavior.
If a ClientSession object that has lost it's connection
is passed to this method, the method returns a
RemoteDevice with its 'closed' field having a value of
'true', but no exception is thrown.

The following modification corrects this inconsistency
with the spec.

public static RemoteDevice
getRemoteDevice(Connection conn) throws IOException {
if (conn == null) throw new
NullPointerException("Connection is null.");
RemoteDevice dev=null;
if (conn instanceof BTConnection) {
if (((BTConnection)conn).isClosed())
throw new IOException("Connection Closed");
dev = ((BTConnection)conn).getRemoteDevice();
}
else if (conn instanceof OBEXConnection) dev =
((OBEXConnection)conn).getRemoteDevice();
else if (conn instanceof SessionNotifierImpl) {
if
(((SessionNotifierImpl)conn).getConnectionNotifier() ==
null)
throw new IOException("Connection Closed");
dev =
((RFCommConnectionNotifierImpl)((SessionNotifierImpl)conn).getConnectionNotifier()).getRemoteDevice();
}
else if (conn instanceof ConnectionNotifier) {
if (((ConnectionNotifier)conn).isNotifierClosed())
throw new IOException("Connection Closed");
dev = ((ConnectionNotifier)conn).getRemoteDevice();
}
else throw new ClassCastException ("Connection type
not supported");
if(dev==null) throw new IOException("The remote
device could not be determined!");
return dev;
}

Discussion