GUI for BufferedNetworkStack

This commit is contained in:
senft-desktop 2012-11-06 10:18:49 +01:00
parent 31d453b149
commit a7e090dc32
3 changed files with 235 additions and 3 deletions

View File

@ -1,4 +1,4 @@
package u1;
package buffered;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -73,7 +73,8 @@ class BufferedNetworkStack {
byte messageType = receivedData[0];
String strReceived = new String(receivedData).toString().trim();
// System.out.println(strReceived);
System.out.println(messageType);
System.out.println(strReceived);
switch (messageType) {
case CMD_PUSH:
@ -146,7 +147,7 @@ class BufferedNetworkStack {
public static void main(String args[]) {
try {
new BufferedNetworkStack(1234);
new BufferedNetworkStack(9999);
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -0,0 +1,99 @@
package buffered;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class BufferedNetworkStackClient {
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';
private DatagramChannel channel = null;
private ByteBuffer buf;
public BufferedNetworkStackClient() {
buf = ByteBuffer.allocate(1024);
}
public void push(String s, String host, int port) throws IOException {
if (channel == null) {
channel = DatagramChannel.open();
}
InetSocketAddress dest = new InetSocketAddress(host, port);
buf.clear();
buf.put(CMD_PUSH);
byte[] length = String.valueOf(s.length()).getBytes();
buf.put(length);
buf.flip();
channel.send(buf, dest);
buf.flip();
buf.clear();
channel.receive(buf);
byte[] receivedData = buf.array();
String infoPacket = new String(receivedData).trim();
// 0 = buffer_size, 1 = id
String[] info = infoPacket.split(",");
int remoteBufSize = Integer.valueOf(info[0]);
int sessionId = Integer.valueOf(info[1]);
sendChunks(s, dest, remoteBufSize, sessionId);
}
private void sendChunks(String s, InetSocketAddress dest, int bufSize,
int sessionId) {
// The amount of actual data we can store in a packet
int lenData = bufSize - 3;
// Number of chunks needed to send 's'. We use bufSize-2 because the
// first 2 bytes of each packet are metadata (session_id, chunk_id).
int numChunks = (int) Math.ceil((s.length() * 1.0 / lenData));
// System.out.println(numChunks);
for (int chunkId = 0; chunkId < numChunks; chunkId++) {
buf.flip();
buf.clear();
buf.put(CMD_DATA);
buf.put(String.valueOf(sessionId).getBytes());
buf.put(String.valueOf(chunkId).getBytes());
int chunkStart = chunkId * lenData;
buf.put(s.substring(chunkStart, chunkStart + lenData).getBytes());
buf.flip();
try {
// System.out.println(Arrays.toString(buf.array()));
channel.send(buf, dest);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String pop(String host, int port) throws IOException {
if (channel == null) {
channel = DatagramChannel.open();
}
InetSocketAddress dest = new InetSocketAddress(host, port);
// TODO
channel.send(ByteBuffer.wrap("pop".getBytes()), dest);
return "pop";
}
public void print() {
}
}

View File

@ -0,0 +1,132 @@
package buffered;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
public class ClientGUI extends JFrame {
private JPanel contentPane;
private JTextField userinput;
private JTextField txtPort;
private JScrollPane scrollPane;
// private int port;
private JTextPane textPane;
private BufferedNetworkStackClient client = null;
private JTextField txtHost;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClientGUI frame = new ClientGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ClientGUI() {
client = new BufferedNetworkStackClient();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 369);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panelPref = new JPanel();
contentPane.add(panelPref, BorderLayout.NORTH);
panelPref.setLayout(new GridLayout(2, 4, 0, 0));
JLabel lblHost = new JLabel("Hostname:");
panelPref.add(lblHost);
txtHost = new JTextField();
txtHost.setText("localhost");
panelPref.add(txtHost);
txtHost.setColumns(10);
JLabel lblPort = new JLabel("Port:");
panelPref.add(lblPort);
txtPort = new JTextField();
panelPref.add(txtPort);
txtPort.setText("9999");
txtPort.setColumns(10);
JLabel lblMessage = new JLabel("Message:");
panelPref.add(lblMessage);
userinput = new JTextField();
userinput.setText("asd");
panelPref.add(userinput);
userinput.setColumns(10);
JButton btnPush = new JButton("Push");
panelPref.add(btnPush);
JButton btnPop = new JButton("Pop");
panelPref.add(btnPop);
btnPop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String received = "";
try {
received = client.pop(txtHost.getText(),
Integer.valueOf(txtPort.getText()));
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
textPane.setText(textPane.getText() + "\n" + received);
}
});
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);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
JPanel panelResp = new JPanel();
contentPane.add(panelResp, BorderLayout.CENTER);
panelResp.setLayout(new BorderLayout(5, 5));
JLabel lblServerResponse = new JLabel("Server response:");
panelResp.add(lblServerResponse, BorderLayout.NORTH);
textPane = new JTextPane();
textPane.setEditable(false);
scrollPane = new JScrollPane(textPane);
panelResp.add(scrollPane, BorderLayout.CENTER);
}
}