Merge branch 'master' of mojotrollz.eu:college

Conflicts:
	ws2012/P2P/uebungen/2/p2p_ex2/src/client/BufferedNetworkStackClient.java
This commit is contained in:
senft-lap 2012-11-09 22:47:28 +01:00
commit 8fe059d674
3 changed files with 49 additions and 24 deletions

View File

@ -9,7 +9,7 @@ import common.MessageType;
import common.Util;
public class BufferedNetworkStackClient {
protected static final long TIMEOUT = 5000;
protected static final long TIMEOUT = 3000;
protected static final int BUF_SIZE = 2048;
@ -28,7 +28,7 @@ public class BufferedNetworkStackClient {
InetSocketAddress dest = new InetSocketAddress(host, port);
buf.clear();
buf = ByteBuffer.allocate(BUF_SIZE);
buf.put(MessageType.CMD_PUSH);
buf.putInt(s.length());
@ -66,7 +66,7 @@ public class BufferedNetworkStackClient {
for (int chunkId = 0; chunkId < numChunks; chunkId++) {
buf.flip();
buf.clear();
buf = ByteBuffer.allocate(BUF_SIZE);
buf.put(MessageType.CMD_DATA);
buf.putInt(sessionId);
@ -85,7 +85,7 @@ public class BufferedNetworkStackClient {
try {
channel.send(buf, dest);
buf.clear();
buf = ByteBuffer.allocate(BUF_SIZE);
} catch (IOException e) {
e.printStackTrace();
}
@ -97,6 +97,7 @@ public class BufferedNetworkStackClient {
if (channel == null) {
channel = DatagramChannel.open();
}
buf = ByteBuffer.allocate(BUF_SIZE);
InetSocketAddress dest = new InetSocketAddress(host, port);
channel.send(ByteBuffer.wrap(new byte[] { MessageType.CMD_POP }), dest);

View File

@ -19,7 +19,7 @@ import javax.swing.border.EmptyBorder;
public class ClientGUI extends JFrame {
private JPanel contentPane;
private JTextField userinput;
private JTextField txtMsg;
private JTextField txtPort;
private JScrollPane scrollPane;
private JTextPane textPane;
@ -46,6 +46,7 @@ public class ClientGUI extends JFrame {
* Create the frame.
*/
public ClientGUI() {
setTitle("BufferedNetworkStack Client");
setResizable(false);
client = new BufferedNetworkStackClient();
@ -79,10 +80,10 @@ public class ClientGUI extends JFrame {
JLabel lblMessage = new JLabel("Message:");
panelPref.add(lblMessage);
userinput = new JTextField();
userinput.setText("asd");
panelPref.add(userinput);
userinput.setColumns(10);
txtMsg = new JTextField();
txtMsg.setText("Enter a message");
panelPref.add(txtMsg);
txtMsg.setColumns(10);
JButton btnPush = new JButton("Push");
panelPref.add(btnPush);
@ -98,10 +99,12 @@ public class ClientGUI extends JFrame {
Integer.valueOf(txtPort.getText()));
} catch (NumberFormatException e1) {
textPane.setText(textPane.getText()
+ "\nErroneous port number. " + e1.getMessage());
+ "Erroneous port number. " + e1.getMessage()
+ "\n");
} catch (IOException e1) {
textPane.setText(textPane.getText()
+ "\nCould not send to server. " + e1.getMessage());
+ "Could not send to server. " + e1.getMessage()
+ "\n");
}
}
});
@ -112,35 +115,51 @@ public class ClientGUI extends JFrame {
try {
received = client.pop(txtHost.getText(),
Integer.valueOf(txtPort.getText()));
textPane.setText(textPane.getText() + "\nPopped: "
+ received);
textPane.setText(textPane.getText() + "Popped: " + received
+ "\n");
} catch (NumberFormatException e1) {
textPane.setText(textPane.getText()
+ "\nErroneous port number. " + e1.getMessage());
+ "Erroneous port number. " + e1.getMessage()
+ "\n");
} catch (TimeoutException e2) {
textPane.setText(textPane.getText() + "\nServer timed out."
+ e2.getMessage());
textPane.setText(textPane.getText() + "Server timed out."
+ e2.getMessage() + "\n");
} catch (IOException e1) {
textPane.setText(textPane.getText()
+ "\nCould not send to server. " + e1.getMessage());
+ "Could not send to server. " + e1.getMessage()
+ "\n");
} catch (IllegalArgumentException e1) {
textPane.setText(textPane.getText() + e1.getMessage()
+ "\n");
}
}
});
btnPush.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String host = txtHost.getText();
int port = Integer.valueOf(txtPort.getText());
client.push(userinput.getText(), host, port);
if (!txtMsg.getText().isEmpty()) {
String host = txtHost.getText();
int port = Integer.valueOf(txtPort.getText());
client.push(txtMsg.getText(), host, port);
textPane.setText(textPane.getText() + "Pushed: "
+ txtMsg.getText() + "\n");
} else {
textPane.setText(textPane.getText()
+ "Please enter a message.\n");
}
} 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());
+ "Could not send to server. " + e1.getMessage()
+ "\n");
} catch (TimeoutException e) {
textPane.setText(textPane.getText() + "\nServer timed out."
+ e.getMessage());
textPane.setText(textPane.getText() + "Server timed out."
+ e.getMessage() + "\n");
} catch (IllegalArgumentException e) {
textPane.setText(textPane.getText() + e.getMessage() + "\n");
}
}
});

View File

@ -144,7 +144,12 @@ public class BufferedNetworkStack {
break;
case MessageType.CMD_PRINT:
System.out.println(BufferedNetworkStack.this.toString());
if (stack.isEmpty()) {
System.out.println("Stack is empty");
} else {
System.out
.println(BufferedNetworkStack.this.toString());
}
break;
default: