Proper package/class structure

This commit is contained in:
senft-desktop 2012-11-09 13:35:17 +01:00
parent 4bd9764d32
commit 5b4f5a8b00
10 changed files with 48 additions and 372 deletions

View File

@ -1,8 +0,0 @@
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

@ -1,10 +1,12 @@
package buffered;
package client;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import common.MessageType;
public class BufferedNetworkStackClient {
protected static final long TIMEOUT = 5000;

View File

@ -1,4 +1,4 @@
package buffered;
package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;

View File

@ -1,4 +1,4 @@
package buffered;
package client;
public class TimeoutException extends Exception {
public TimeoutException(String msg) {

View File

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

View File

@ -1,4 +1,4 @@
package buffered;
package server;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -9,39 +9,10 @@ import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.Stack;
import common.MessageType;
class BufferedNetworkStack {
/**
* One single element on the stack.
*/
public class Element {
public int id;
public String[] chunks;
public Element(int size, int id) {
chunks = new String[size];
this.id = id;
// System.out.println("Created new Element, size: " + size);
}
public boolean isComplete() {
for (String s : chunks) {
if (s == null || s.isEmpty()) {
return false;
}
}
return true;
}
public String toString() {
StringBuilder result = new StringBuilder(16);
for (String s : chunks) {
result.append(s);
}
return result.toString();
}
}
private DatagramChannel channel;
protected static final int BUF_SIZE = 10;

View File

@ -0,0 +1,32 @@
package server;
/**
* One single element on the stack.
*/
public class Element {
public int id;
public String[] chunks;
public Element(int size, int id) {
chunks = new String[size];
this.id = id;
// System.out.println("Created new Element, size: " + size);
}
public boolean isComplete() {
for (String s : chunks) {
if (s == null || s.isEmpty()) {
return false;
}
}
return true;
}
public String toString() {
StringBuilder result = new StringBuilder(16);
for (String s : chunks) {
result.append(s);
}
return result.toString();
}
}

View File

@ -1,161 +0,0 @@
package u1;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ClientGUI extends JFrame {
private JPanel contentPane;
private JTextField userinput;
private JTextField portnumber;
private JScrollPane scrollPane;
private int port;
private JTextPane textPane;
private NetworkClient client;
// private Client client = new Client();
/**
* 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() {
setTitle("Client v0.1");
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(null);
userinput = new JTextField();
userinput.setBounds(18, 88, 413, 28);
contentPane.add(userinput);
userinput.setColumns(10);
JLabel lblMessage = new JLabel("Message:");
lblMessage.setBounds(18, 60, 123, 16);
contentPane.add(lblMessage);
JButton btnPush = new JButton("Push");
btnPush.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Trying to push \""+ userinput.getText() + "\".");
try {
client.push(userinput.getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnPush.setBounds(18, 128, 70, 44);
contentPane.add(btnPush);
JButton btnPop = new JButton("Pop");
btnPop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String received = client.pop();
String time = sdf.format(new Date()) + ": ";
textPane.setText(textPane.getText() + time + received + "\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnPop.setBounds(100, 128, 70, 44);
contentPane.add(btnPop);
textPane = new JTextPane();
scrollPane = new JScrollPane(textPane);
scrollPane.setBounds(18, 228, 413, 113);
contentPane.add(scrollPane);
JLabel lblServerResponse = new JLabel("Server response:");
lblServerResponse.setBounds(18, 200, 123, 16);
contentPane.add(lblServerResponse);
portnumber = new JTextField();
portnumber.setBounds(54, 20, 61, 28);
portnumber.setText("1234");
contentPane.add(portnumber);
portnumber.setColumns(10);
JLabel lblPort = new JLabel("Port:");
lblPort.setBounds(18, 26, 61, 16);
contentPane.add(lblPort);
final JLabel serverInformation = new JLabel("");
serverInformation.setBounds(260, 20, 184, 28);
contentPane.add(serverInformation);
JButton listenToPort = new JButton("Listen to port");
listenToPort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
port = -1;
boolean portOkay = false;
try {
port = Integer.parseInt(portnumber.getText());
if (1 > port || port > 65535
|| portnumber.getText().isEmpty()) {
throw new Exception();
}
serverInformation.setText("");
portOkay = true;
} catch (Exception ex) {
serverInformation.setForeground(Color.RED);
serverInformation.setText("invalid port!");
}
if (portOkay) {
try {
client = new NetworkClient("localhost", port);
serverInformation.setForeground(Color.BLUE);
serverInformation.setText("listening to port " + port +"!");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
listenToPort.setBounds(114, 21, 146, 29);
contentPane.add(listenToPort);
}
}

View File

@ -1,60 +0,0 @@
package u1;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class NetworkClient {
private DatagramChannel channel;
private ByteBuffer buf;
private InetAddress inetaddr;
private int port;
public NetworkClient(final String address, final int port)
throws IOException {
this.port = port;
inetaddr = InetAddress.getByName(address);
buf = ByteBuffer.allocate(1024);
channel = DatagramChannel.open();
channel.socket().connect(inetaddr, port);
buf.clear();
}
public void push(String text) throws IOException {
if (channel.socket().isConnected()) {
text = "0" + text;
channel.send(ByteBuffer.wrap(text.getBytes()),
new InetSocketAddress(inetaddr, port));
channel.configureBlocking(false);
channel.receive(buf);
String received = new String(buf.array());
System.out.println("Received (push): " + received);
buf.clear();
} else {
System.out.println("not connected anymore.");
}
}
public String pop () throws IOException {
if (channel.socket().isConnected()) {
String text = "1";
channel.send(ByteBuffer.wrap(text.getBytes()),
new InetSocketAddress(inetaddr, port));
channel.configureBlocking(false);
channel.receive(buf);
String received = new String(buf.array());
System.out.println("Received (pop): " + received);
buf.clear();
return received;
} else {
System.out.println("not connected anymore.");
return "fail";
}
}
}

View File

@ -1,108 +0,0 @@
package u1;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.util.EmptyStackException;
import java.util.Stack;
class NetworkStack {
private DatagramChannel channel;
protected static final byte CMD_PUSH = '0';
protected static final byte CMD_SPLIT = '-';
protected static final byte CMD_POP = '1';
protected static final byte CMD_PRINT = '2';
private Stack<String> stack;
public NetworkStack(final int port) throws IOException {
stack = new Stack<String>();
channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(port));
ByteBuffer buf = ByteBuffer.allocate(2);
buf.clear();
while (true) {
// write stuff from channel to buffer
SocketAddress client = channel.receive(buf);
// make buffer readable
buf.flip();
byte[] receivedData = buf.array();
byte messageType = receivedData[0];
String received = new String(receivedData.toString().trim());
switch (messageType) {
case CMD_PUSH:
String value = new String(receivedData).substring(1).trim();
stack.push(value);
System.out.println("Push " + value);
break;
case CMD_SPLIT:
String old = stack.pop();
String valueNew = old
+ new String(receivedData).substring(1).trim();
stack.push(valueNew);
System.out.println("Pop previous message: " + old
+ " and Push " + valueNew);
break;
case CMD_POP:
try {
String popped = stack.pop() + '\n';
send(popped, client);
System.out.println("Pop " + popped);
} catch (EmptyStackException e) {
System.out.println("Received pop but stack is empty");
}
break;
case CMD_PRINT:
StringBuilder result = new StringBuilder(128);
for (String s : stack) {
result.append("[").append(s).append("],");
}
result.append('\n');
send(result.toString(), client);
break;
default:
System.out.println("Received unknown command: " + "["
+ received + "]");
break;
}
// clear buffer and make it ready to write
buf.clear();
}
}
private void send(String text, SocketAddress client) {
try {
channel.send(ByteBuffer.wrap(text.getBytes()), client);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
try {
new NetworkStack(1234);
} catch (IOException e) {
e.printStackTrace();
}
}
}