timeout implemented

This commit is contained in:
Ulf Gebhardt 2012-11-06 15:40:32 +01:00
parent c4aac841fc
commit 1c680009c5

View File

@ -11,6 +11,8 @@ public class BufferedNetworkStackClient {
protected static final byte CMD_POP = '2';
protected static final byte CMD_PRINT = '3';
protected static final long TIMEOUT = 5000;
protected static final int BUF_SIZE = 128;
private DatagramChannel channel = null;
@ -38,11 +40,15 @@ public class BufferedNetworkStackClient {
buf.flip();
buf = ByteBuffer.allocate(BUF_SIZE);
channel.configureBlocking(false);
if(channel.receive(buf) == null){
channel.configureBlocking(false);
long time = System.currentTimeMillis() + TIMEOUT;
boolean rec = false;
while(rec = channel.receive(buf) == null && System.currentTimeMillis() < time){}
if(!rec){
throw new Exception("ERROR: Buffer empty - not data recieved");
}
channel.configureBlocking(true);
byte[] receivedData = buf.array();
String infoPacket = new String(receivedData).trim();
@ -97,10 +103,13 @@ public class BufferedNetworkStackClient {
channel.send(ByteBuffer.wrap(new byte[] { CMD_POP }), dest);
channel.configureBlocking(false);
if(channel.receive(buf) == null){
long time = System.currentTimeMillis() + TIMEOUT;
boolean rec = false;
while(rec = channel.receive(buf) == null && System.currentTimeMillis() < time){}
if(!rec){
throw new Exception("ERROR: Buffer empty - not data recieved");
}
channel.configureBlocking(true);
byte[] receivedData = buf.array();
String strReceived = new String(receivedData).toString().trim();