private void connectToStateProvider(StateHeader hdr) {
IpAddress address = hdr.bind_addr;
String tmp_state_id = hdr.getStateId();
InputStream bis = null;
StateTransferInfo sti = null;
Socket socket = new Socket();
try {
socket.bind(new InetSocketAddress(bind_addr, 0));
int bufferSize = socket.getReceiveBufferSize();
socket.setReceiveBufferSize(socket_buffer_size);
if (log.isDebugEnabled())
log.debug("Connecting to state provider " + address.getIpAddress() + ":"
+ address.getPort() + ", original buffer size was " + bufferSize
+ " and was reset to " + socket.getReceiveBufferSize());
Util.connect(socket, new InetSocketAddress(address.getIpAddress(), address.getPort()), 0);
if (log.isDebugEnabled())
log.debug("Connected to state provider, my end of the socket is "
+ socket.getLocalAddress() + ":" + socket.getLocalPort()
+ " passing inputstream up...");
// write out our state_id and address
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(tmp_state_id);
out.writeObject(local_addr);
bis = new BufferedInputStream(new StreamingInputStreamWrapper(socket),socket_buffer_size);
sti = new StateTransferInfo(hdr.sender, bis, tmp_state_id);
up_prot.up(new Event(Event.STATE_TRANSFER_INPUTSTREAM, sti));
} catch (IOException e) {
if (log.isWarnEnabled()) {
log.warn("State reader socket thread spawned abnormaly", e);
}
// pass null stream up so that JChannel.getState() returns false
InputStream is = null;
sti = new StateTransferInfo(hdr.sender, is, tmp_state_id);
up_prot.up(new Event(Event.STATE_TRANSFER_INPUTSTREAM, sti));
} finally {
if (!socket.isConnected()) {
if (log.isWarnEnabled())
log.warn("Could not connect to state provider. Closing socket...");
}
Util.close(bis);
Util.close(socket);