Merged, fixed pop, fixed timeout

This commit is contained in:
senft-lap 2012-11-06 16:56:35 +01:00
commit 950d729b3b
5 changed files with 68 additions and 19 deletions

View File

@ -114,9 +114,9 @@ class BufferedNetworkStack {
case MessageType.CMD_POP:
try {
// Element popped = stack.pop();
// send(popped.toString(), client);
// System.out.println("Pop " + popped);
String popped = stack.pop();
send(popped, client);
System.out.println("Pop " + popped);
} catch (EmptyStackException e) {
System.out.println("Received pop but stack is empty");
}

View File

@ -6,6 +6,8 @@ import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class BufferedNetworkStackClient {
protected static final long TIMEOUT = 5000;
protected static final int BUF_SIZE = 128;
private DatagramChannel channel = null;
@ -15,7 +17,8 @@ public class BufferedNetworkStackClient {
buf = ByteBuffer.allocate(BUF_SIZE);
}
public void push(String s, String host, int port) throws IOException {
public void push(String s, String host, int port) throws IOException,
TimeoutException {
if (channel == null) {
channel = DatagramChannel.open();
}
@ -33,7 +36,16 @@ public class BufferedNetworkStackClient {
buf.flip();
buf = ByteBuffer.allocate(BUF_SIZE);
channel.receive(buf);
channel.configureBlocking(false);
long time = System.currentTimeMillis() + TIMEOUT;
while (buf.remaining() == BUF_SIZE && System.currentTimeMillis() < time) {
channel.receive(buf);
}
if (buf.remaining() == BUF_SIZE) {
throw new TimeoutException("ERROR: Buffer empty - no data recieved");
}
channel.configureBlocking(true);
byte[] receivedData = buf.array();
String infoPacket = new String(receivedData).trim();
@ -43,6 +55,7 @@ public class BufferedNetworkStackClient {
int remoteBufSize = Integer.valueOf(info[0]);
int sessionId = Integer.valueOf(info[1]);
System.out.println('4');
sendChunks(s, dest, remoteBufSize, sessionId);
}
@ -77,7 +90,8 @@ public class BufferedNetworkStackClient {
}
}
public String pop(String host, int port) throws IOException {
public String pop(String host, int port) throws IOException,
TimeoutException {
if (channel == null) {
channel = DatagramChannel.open();
}
@ -85,11 +99,19 @@ public class BufferedNetworkStackClient {
InetSocketAddress dest = new InetSocketAddress(host, port);
channel.send(ByteBuffer.wrap(new byte[] { MessageType.CMD_POP }), dest);
channel.receive(buf);
channel.configureBlocking(false);
long time = System.currentTimeMillis() + TIMEOUT;
while (buf.remaining() == BUF_SIZE && System.currentTimeMillis() < time) {
channel.receive(buf);
}
if (buf.remaining() == BUF_SIZE) {
throw new TimeoutException("ERROR: Buffer empty - no data recieved");
}
channel.configureBlocking(true);
byte[] receivedData = buf.array();
String strReceived = new String(receivedData).toString().trim();
System.out.println(strReceived);
return strReceived;
}

View File

@ -97,9 +97,11 @@ public class ClientGUI extends JFrame {
client.print(txtHost.getText(),
Integer.valueOf(txtPort.getText()));
} catch (NumberFormatException e1) {
e1.printStackTrace();
textPane.setText(textPane.getText()
+ "\nErroneous port number. " + e1.getMessage());
} catch (IOException e1) {
e1.printStackTrace();
textPane.setText(textPane.getText()
+ "\nCould not send to server. " + e1.getMessage());
}
}
});
@ -110,14 +112,18 @@ public class ClientGUI extends JFrame {
try {
received = client.pop(txtHost.getText(),
Integer.valueOf(txtPort.getText()));
textPane.setText(textPane.getText() + "\n" + received);
textPane.setText(textPane.getText() + "\nPopped: "
+ received);
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
textPane.setText(textPane.getText()
+ "\nErroneous port number. " + e1.getMessage());
} catch (TimeoutException e2) {
textPane.setText(textPane.getText() + "\nServer timed out."
+ e2.getMessage());
} catch (IOException e1) {
textPane.setText(textPane.getText()
+ "\nCould not send to server. " + e1.getMessage());
}
textPane.setText(textPane.getText() + "\n" + received);
}
});
btnPush.addActionListener(new ActionListener() {
@ -126,9 +132,15 @@ public class ClientGUI extends JFrame {
String host = txtHost.getText();
int port = Integer.valueOf(txtPort.getText());
client.push(userinput.getText(), host, port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e1) {
textPane.setText(textPane.getText()
+ "\nErroneous port number. " + e1.getMessage());
} catch (IOException e1) {
textPane.setText(textPane.getText()
+ "\nCould not send to server. " + e1.getMessage());
} catch (TimeoutException e) {
textPane.setText(textPane.getText() + "\nServer timed out."
+ e.getMessage());
}
}
});

View File

@ -0,0 +1,8 @@
package buffered;
public class MessageType {
protected static final byte CMD_PUSH = '0';
protected static final byte CMD_DATA = '1';
protected static final byte CMD_POP = '2';
protected static final byte CMD_PRINT = '3';
}

View File

@ -0,0 +1,7 @@
package buffered;
public class TimeoutException extends Exception {
public TimeoutException(String msg) {
super(msg);
}
}