Moved to package u1
This commit is contained in:
parent
6971caf831
commit
727b5c15da
161
ws2012/P2P/uebungen/2/p2p_ex2/src/u1/ClientGUI.java
Normal file
161
ws2012/P2P/uebungen/2/p2p_ex2/src/u1/ClientGUI.java
Normal file
@ -0,0 +1,161 @@
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
60
ws2012/P2P/uebungen/2/p2p_ex2/src/u1/NetworkClient.java
Normal file
60
ws2012/P2P/uebungen/2/p2p_ex2/src/u1/NetworkClient.java
Normal file
@ -0,0 +1,60 @@
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
108
ws2012/P2P/uebungen/2/p2p_ex2/src/u1/NetworkStack.java
Normal file
108
ws2012/P2P/uebungen/2/p2p_ex2/src/u1/NetworkStack.java
Normal file
@ -0,0 +1,108 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user