diff --git a/CellRenderer.java b/CellRenderer.java
deleted file mode 100644
index 0f31fb1..0000000
--- a/CellRenderer.java
+++ /dev/null
@@ -1,30 +0,0 @@
-import java.awt.Component;
-
-import javax.swing.JTable;
-import javax.swing.SwingConstants;
-import javax.swing.table.DefaultTableCellRenderer;
-
-/**
- * This Class is used to overwrite the Default Cell Renderer in the JTable used in CnFrameUDP
- *
- * @author trefois
- */
-public class CellRenderer extends DefaultTableCellRenderer {
-
- /**
- * Generated serialID by Eclipse
- */
- private static final long serialVersionUID = 6305609864454156868L;
-
- /**
- * Overwrites getTableCellRendererComponent
- *
Puts Horizontal Alignement to CENTER
- */
- public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
- super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
- row, column);
- this.setHorizontalAlignment(SwingConstants.CENTER);
- return this;
- }
-
-}
diff --git a/CnFrame.java b/CnFrame.java
deleted file mode 100644
index 88a26f8..0000000
--- a/CnFrame.java
+++ /dev/null
@@ -1,53 +0,0 @@
-import java.awt.BorderLayout;
-import java.awt.Dimension;
-import java.awt.Toolkit;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-
-import javax.swing.JFrame;
-
-/**
- * This class is the Super Class of CnFrameUDP and CnFrameTCP.
- * @author trefois
- *
- */
-public class CnFrame extends JFrame {
- /**
- * Generated serialID by Eclipse
- */
- private static final long serialVersionUID = -5235044694247669643L;
-
- /**
- * Custom Constructor.
- * Sets the Window title to windowTitle
- * @param windowTitle The Title provided as String
- */
- public CnFrame (String windowTitle) {
- this.setTitle(windowTitle);
- frameConstructor();
- }
-
- /**
- * Construct the main Frame.
- */
- private void frameConstructor() {
- // Set Behaviour when "x" is clicked.
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-
- // Get Screen Dimensions
- Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
- setBounds(50, 50, windowSize.width / 2, windowSize.height / 2);
-
- getContentPane().setLayout(new BorderLayout());
- }
-
- /**
- * Retrieves the current time in the following format: dd/MMM/yyyy HH:mm:ss
- * @return The String representation of the current Date
- */
- public String getCurrentTime() {
- DateFormat dateFormat = new SimpleDateFormat("dd / MMMM / yyyy -- HH:mm:ss");
- java.util.Date date = new java.util.Date();
- return dateFormat.format(date);
- }
-}
diff --git a/CnFrameTCP.java b/CnFrameTCP.java
deleted file mode 100644
index 7f032d7..0000000
--- a/CnFrameTCP.java
+++ /dev/null
@@ -1,67 +0,0 @@
-import java.awt.BorderLayout;
-import java.awt.GridLayout;
-
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.JTextArea;
-
-/**
- * This Class dispalys messages received from the Server. E.g. Server Hello etc...
- * Call appendText(String) to update the GUI
- *
- * @author trefois
- */
-public class CnFrameTCP extends CnFrame {
-
- /**
- * Generated serialID by Eclipse
- */
- private static final long serialVersionUID = 4899514760671051872L;
-
- /**
- * The TextArea used to display the messages
- */
- private JTextArea textArea;
-
- /**
- * Constructs a CnFrameTCP Instance with title windowTitle
- * @param windowTitle
- */
- public CnFrameTCP(String windowTitle) {
- super(windowTitle);
- setSize(500, 250);
-
- JPanel centerPanel = new JPanel();
-
- textArea = new JTextArea();
- textArea.setEditable(false);
-
- JScrollPane scrollPane = new JScrollPane(textArea);
-
- centerPanel.setLayout(new GridLayout(1, 1));
- centerPanel.add(scrollPane);
-
- // put the centerPanel to the Center
- getContentPane().add(centerPanel, BorderLayout.CENTER);
-
- // Make this frame visible
- setVisible(true);
- }
-
- /**
- * Append Text to the textArea
- * @param textToAppend
- */
- public void appendText(String textToAppend) {
- textArea.append(super.getCurrentTime() + " : " + textToAppend);
- textArea.setCaretPosition(textArea.getText().length());
- }
-
- /**
- * Clear the TextArea.
- *
- */
- public void clearTextArea() {
- textArea.replaceRange("", 0, textArea.getText().length());
- }
-}
diff --git a/CnFrameUDP.java b/CnFrameUDP.java
deleted file mode 100644
index 2ee6ae5..0000000
--- a/CnFrameUDP.java
+++ /dev/null
@@ -1,140 +0,0 @@
-import java.awt.BorderLayout;
-import java.util.Vector;
-
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.JTable;
-import javax.swing.table.DefaultTableModel;
-
-
-/**
- * This Class is used to display UDP Broadcast Messages sent from the Server to the Client.
- * The Client should create a CnFrameUDP Instance and call the method "appendBroadcastMessage"
- * each time an UDP Broadcast is received.
- *
Example:
- *
- * CnFrameUDP cnFrameUDP = new CnFrameUDP("Networking Course");
- * while(true) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- cnFrameUDP.appendBroadcastMessage("192.168.1.5", "****X***X****X");
- }
- *
- * @author Christophe Trefois
- *
- */
-public class CnFrameUDP extends CnFrame {
- /**
- * Generated serialID by Eclipse
- */
- private static final long serialVersionUID = 2774784580042117224L;
-
- /**
- * Vector containing the table rows data
- */
- private Vector tableRows;
-
- /**
- * Vector containing the column names. Filled with addColumns.
- */
- private Vector columns;
-
- /**
- * Array containing the column names
- */
- private static final String[] columnNames = {"Received At", "IP Address", "Letters"};
-
- /**
- * tableModel used by the Table
- */
- private DefaultTableModel tableModel;
-
- /**
- * The table used to display the broadcast messages
- */
- private JTable broadcastTable;
-
- /**
- * Constructs a CnFrameUDP Instance with title windowTitle
- * @param windowTitle The Title if the window
- */
- public CnFrameUDP(String windowTitle) {
- super(windowTitle);
-
- // Create new JPanel
- JPanel centerPanel = new JPanel();
-
- // Fill the columns Vector
- addColumns(columnNames);
-
- tableModel = new DefaultTableModel();
- tableModel.setDataVector(tableRows, columns);
-
- // create new JPanel
- broadcastTable = new JTable(tableModel);
- broadcastTable.getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
- broadcastTable.getColumnModel().getColumn(1).setCellRenderer(new CellRenderer());
- broadcastTable.getColumnModel().getColumn(2).setCellRenderer(new CellRenderer());
-
- // Add ScrollBar to the broadcastTable
- JScrollPane scrollPane = new JScrollPane(broadcastTable);
- centerPanel.add(scrollPane);
-
- // put the centerPanel to the Center
- getContentPane().add(centerPanel, BorderLayout.CENTER);
-
- // Pack
- pack();
-
- // Make this frame visible
- setVisible(true);
- }
-
- /**
- * Fill the column name vector
- * @param columnNames the array of Strings to fill the vector with
- */
- private void addColumns(String[] columnNames) {
- // Init tableRows and columns Vectors
- tableRows = new Vector();
- columns = new Vector();
-
- for (int i = 0; i < columnNames.length; i++)
- columns.addElement((String) columnNames[i]);
- }
-
- /**
- * Adds a row to the JTable
- * @param broadcastIP The IP from the broadcast message
- * @param letters The Letters
- */
- private void addRow(String broadcastIP, String letters) {
- Vector tempVector = new Vector();
- tempVector.addElement(super.getCurrentTime());
- tempVector.addElement(broadcastIP);
- tempVector.addElement(letters);
- tableRows.addElement(tempVector);
- broadcastTable.addNotify();
- }
-
- /**
- * Modifies the GUI by appending the broadcast Message
- * @param broadcastIP the IP extracted from the broadcast UDP Message
- * @param broadcastLetters the letters from the UDP Broadcast
- */
- public void appendBroadcastMessage(String broadcastIP, String broadcastLetters) {
- addRow(broadcastIP, broadcastLetters);
- }
-
- /**
- * Clears the Table
- *
- */
- public void clearTable() {
- tableRows.clear();
- broadcastTable.addNotify();
- }
-}
diff --git a/TCPClient.java b/TCPClient.java
deleted file mode 100644
index fdf96c2..0000000
--- a/TCPClient.java
+++ /dev/null
@@ -1,65 +0,0 @@
-import java.net.*;
-import java.io.*;
-
-public class TCPClient {
-
- private Socket sock;
-
- private String targethost;
-
- private int targetport;
-
- private String name;
-
- private BufferedReader inr;
- private DataOutputStream outs;
-
- private CnFrameTCP gui;
-
-
- public static void main ( String[] args ) {
- CnFrameTCP gui = new CnFrameTCP("TP 4: TCP Part");
-
- try {
- TCPClient tc = new TCPClient(gui, "in3sun23.epfl.ch", 13370, "x-way");
-
- tc.join();
-
- tc.leave();
-
- } catch ( IOException ioe ) {
- System.out.println("IO-Error");
- gui.appendText("IO-Error");
- }
- }
-
- TCPClient ( CnFrameTCP gui, String targethost, int targetport, String name ) throws IOException {
- this.gui = gui;
- this.targethost = targethost;
- this.targetport = targetport;
- this.name = name;
-
- try {
- sock = new Socket(targethost, targetport);
- } catch ( UnknownHostException uhe ) {
- System.out.println("Unknown host");
- gui.appendText("Unknown host");
- }
-
- outs = new DataOutputStream(sock.getOutputStream());
-
- inr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- }
-
- public void join ( ) throws IOException {
- outs.writeBytes("Hello: " + name + "\n");
- System.out.println(inr.readLine());
- gui.appendText(inr.readLine());
- }
-
- public void leave ( ) throws IOException {
- sock.close();
- System.out.println("Connection closed.");
- gui.appendText("Connection closed.");
- }
-}
diff --git a/UDPServer.java b/UDPServer.java
deleted file mode 100644
index c50670e..0000000
--- a/UDPServer.java
+++ /dev/null
@@ -1,55 +0,0 @@
-import java.net.*;
-import java.io.*;
-import java.nio.*;
-
-public class UDPServer {
- private DatagramSocket sock;
- private CnFrameUDP gui;
-
- public static void main ( String[] args ) {
- CnFrameUDP gui = new CnFrameUDP("Title of the window");
- UDPServer udpS = new UDPServer(gui);
- udpS.loop();
- }
-
- UDPServer ( CnFrameUDP gui ) {
- this.gui = gui;
- try {
- sock = new DatagramSocket(13371);
- } catch ( SocketException se ) {
- System.out.println("Couldn't create socket.");
- }
- }
-
- public void loop ( ) {
- int buffsize = 4096;
- byte[] buff = new byte[buffsize];
- byte[] head = new byte[3];
- byte[] addr = new byte[4];
- byte[] load = new byte[buffsize-7];
- DatagramPacket rp;
- String data;
-
- while ( true ) {
- rp = new DatagramPacket(buff, buff.length);
- try {
- sock.receive(rp);
- } catch ( IOException ioe ) {
- System.out.println("IO-Error");
- }
-
- ByteBuffer bb = ByteBuffer.wrap(rp.getData());
- bb.get(head);
- bb.get(addr);
- bb.get(load);
-
- //System.out.println(rp.getAddress().toString() + ":" + Integer.toString(rp.getPort()) + " > " + new String(load));
- try {
- System.out.println(InetAddress.getByAddress(addr).toString() + " > " + new String(load).trim());
- gui.appendBroadcastMessage(InetAddress.getByAddress(addr).toString(), new String(load).trim());
- } catch ( UnknownHostException uhe ) {
- System.out.println("Errornous packet address");
- }
- }
- }
-}
diff --git a/build.xml b/build.xml
new file mode 100644
index 0000000..92f7504
--- /dev/null
+++ b/build.xml
@@ -0,0 +1,57 @@
+
+
+ simple build file
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/ch/epfl/lca/sc250/ServentClient.java b/src/ch/epfl/lca/sc250/ServentClient.java
new file mode 100644
index 0000000..f208b2e
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/ServentClient.java
@@ -0,0 +1,51 @@
+package ch.epfl.lca.sc250;
+
+import ch.epfl.lca.sc250.gui.CnFrameP2P;
+import ch.epfl.lca.sc250.gui.ITCPSender;
+
+/**
+ * @author Christophe Trefois
+ */
+public class ServentClient implements ITCPSender {
+
+ /**
+ * Instance of the GUI
+ */
+ CnFrameP2P myGui;
+
+ /**
+ * Constructor of the Client part of the Servent for
+ *
+ * @param windowTitle The title of the GUI
+ */
+ public ServentClient(String windowTitle) {
+ myGui = new CnFrameP2P(windowTitle);
+ // Give the GUI an instance of the ServentClient
+ myGui.setTcpSender(this);
+ }
+
+ /**
+ * Creates a new instance of ServentClient
+ * @param args
+ */
+ public static void main(String[] args) {
+ new ServentClient(" - Client of Servent - Computer Networking");
+ }
+
+ /**
+ * This method should open a TCP Socket and send the offer to the other player
+ */
+ public void sendMessage(String ipAddress, String moneyAmount, String letterPosition) {
+ // Open Communication to other Player and wait for a result
+ // When result is recevied, display it in GUI
+
+ System.out.println("Message sent to " + ipAddress + " : " + moneyAmount + ", " + letterPosition);
+
+ // Opening Socket, Sending Request, Fetching answer
+ // Put the answer in String message = theAnswer;
+ // Call this method to unblock the GUI
+
+ // myGui.receivedMessage(message);
+ }
+
+}
diff --git a/src/ch/epfl/lca/sc250/ServentServer.java b/src/ch/epfl/lca/sc250/ServentServer.java
new file mode 100644
index 0000000..44be898
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/ServentServer.java
@@ -0,0 +1,42 @@
+package ch.epfl.lca.sc250;
+
+import javax.swing.JOptionPane;
+
+/**
+ * @author Christophe Trefois
+ *
+ */
+public class ServentServer {
+ public static void main(String[] args) {
+ // Open a Socket and listen on incoming offer requests.
+
+ // We suppose we got and parsed an offer, we then want the pop-up to display
+ boolean result = getOfferResult("127.0.0.1", "15", "5");
+ if(result) {
+ System.out.println("Yes");
+ } else {
+ System.out.println("No");
+ }
+ }
+
+ /**
+ * When an offer is received, display a Confirm Dialog where the user can choose Yes or No
+ * @param IP The IP we got the offer from
+ * @param amount The amount the other player gives us for a letter
+ * @param position The position of the letter he wants to buy
+ * @return True or False
+ */
+
+ public static boolean getOfferResult(String IP, String amount, String position) {
+ boolean chosenValue = false;
+ String msgToDisplay = "Buyer from IP: " + IP + "\nWants to buy Letter at Position " + position + " for " + amount + "$";
+ int returnValue = JOptionPane.showConfirmDialog(null, msgToDisplay, "You got an Offer !", JOptionPane.YES_NO_OPTION);
+ if(returnValue == JOptionPane.OK_OPTION) {
+ chosenValue = true;
+ } else {
+ chosenValue = false;
+ }
+ return chosenValue;
+ }
+
+}
diff --git a/src/ch/epfl/lca/sc250/TCPClient.java b/src/ch/epfl/lca/sc250/TCPClient.java
new file mode 100644
index 0000000..055a8ad
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/TCPClient.java
@@ -0,0 +1,69 @@
+package ch.epfl.lca.sc250;
+
+import java.net.*;
+import java.io.*;
+
+import ch.epfl.lca.sc250.gui.*;
+
+public class TCPClient {
+
+ private Socket sock;
+
+ private String targethost;
+
+ private int targetport;
+
+ private String name;
+
+ private BufferedReader inr;
+ private DataOutputStream outs;
+
+ private CnFrameTCP gui;
+
+
+ public static void main ( String[] args ) {
+ CnFrameTCP gui = new CnFrameTCP("TP 4: TCP Part");
+
+ try {
+ TCPClient tc = new TCPClient(gui, "in3sun23.epfl.ch", 13370, "x-way");
+
+ tc.join();
+
+ tc.leave();
+
+ } catch ( IOException ioe ) {
+ System.out.println("IO-Error");
+ gui.appendText("IO-Error");
+ }
+ }
+
+ TCPClient ( CnFrameTCP gui, String targethost, int targetport, String name ) throws IOException {
+ this.gui = gui;
+ this.targethost = targethost;
+ this.targetport = targetport;
+ this.name = name;
+
+ try {
+ sock = new Socket(targethost, targetport);
+ } catch ( UnknownHostException uhe ) {
+ System.out.println("Unknown host");
+ gui.appendText("Unknown host");
+ }
+
+ outs = new DataOutputStream(sock.getOutputStream());
+
+ inr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
+ }
+
+ public void join ( ) throws IOException {
+ outs.writeBytes("Hello: " + name + "\n");
+ System.out.println(inr.readLine());
+ gui.appendText(inr.readLine());
+ }
+
+ public void leave ( ) throws IOException {
+ sock.close();
+ System.out.println("Connection closed.");
+ gui.appendText("Connection closed.");
+ }
+}
diff --git a/src/ch/epfl/lca/sc250/UDPServer.java b/src/ch/epfl/lca/sc250/UDPServer.java
new file mode 100644
index 0000000..db0ebac
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/UDPServer.java
@@ -0,0 +1,63 @@
+package ch.epfl.lca.sc250;
+
+import java.net.*;
+import java.io.*;
+import java.nio.*;
+
+import ch.epfl.lca.sc250.gui.*;
+
+public class UDPServer {
+ private DatagramSocket sock;
+ private CnFrameUDP gui;
+
+ public static void main ( String[] args ) {
+ CnFrameUDP gui = new CnFrameUDP("Title of the window");
+ UDPServer udpS = new UDPServer(gui);
+ udpS.loop();
+ }
+
+ UDPServer ( CnFrameUDP gui ) {
+ this.gui = gui;
+ try {
+ sock = new DatagramSocket(13371);
+ } catch ( SocketException se ) {
+ System.out.println("Couldn't create socket.");
+ }
+ }
+
+ public void loop ( ) {
+ int buffsize = 4096;
+ byte[] buff = new byte[buffsize];
+ byte[] head = new byte[3];
+ byte[] nick = new byte[4];
+ byte[] addr = new byte[4];
+ byte[] load = new byte[buffsize-11];
+ DatagramPacket rp;
+ String data;
+
+ while ( true ) {
+ rp = new DatagramPacket(buff, buff.length);
+ try {
+ sock.receive(rp);
+ } catch ( IOException ioe ) {
+ System.out.println("IO-Error");
+ }
+
+ System.out.println("Recieve packet...");
+
+ ByteBuffer bb = ByteBuffer.wrap(rp.getData());
+ bb.get(head);
+ bb.get(nick);
+ bb.get(addr);
+ bb.get(load);
+
+ //System.out.println(rp.getAddress().toString() + ":" + Integer.toString(rp.getPort()) + " > " + new String(load));
+ try {
+ System.out.println(InetAddress.getByAddress(addr).toString() + " > " + new String(load).trim());
+ gui.appendBroadcastMessage(InetAddress.getByAddress(addr).toString(), InetAddress.getByAddress(nick).toString(), new String(load).trim());
+ } catch ( UnknownHostException uhe ) {
+ System.out.println("Errornous packet address");
+ }
+ }
+ }
+}
diff --git a/src/ch/epfl/lca/sc250/gui/CellRenderer.java b/src/ch/epfl/lca/sc250/gui/CellRenderer.java
new file mode 100644
index 0000000..1f5b987
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/gui/CellRenderer.java
@@ -0,0 +1,32 @@
+package ch.epfl.lca.sc250.gui;
+
+import java.awt.Component;
+
+import javax.swing.JTable;
+import javax.swing.SwingConstants;
+import javax.swing.table.DefaultTableCellRenderer;
+
+/**
+ * This Class is used to overwrite the Default Cell Renderer in the JTable used in CnFrameUDP
+ *
+ * @author trefois
+ */
+public class CellRenderer extends DefaultTableCellRenderer {
+
+ /**
+ * Generated serialID by Eclipse
+ */
+ private static final long serialVersionUID = 6305609864454156868L;
+
+ /**
+ * Overwrites getTableCellRendererComponent
+ *
Puts Horizontal Alignement to CENTER
+ */
+ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
+ super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
+ row, column);
+ this.setHorizontalAlignment(SwingConstants.CENTER);
+ return this;
+ }
+
+}
diff --git a/src/ch/epfl/lca/sc250/gui/CnFrame.java b/src/ch/epfl/lca/sc250/gui/CnFrame.java
new file mode 100644
index 0000000..bf86896
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/gui/CnFrame.java
@@ -0,0 +1,59 @@
+package ch.epfl.lca.sc250.gui;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.Toolkit;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+
+import javax.swing.JFrame;
+
+/**
+ * This class is the Super Class of CnFrameUDP and CnFrameTCP.
+ * @author Christophe Trefois
+ *
+ */
+public class CnFrame extends JFrame {
+ /**
+ * Generated serialID by Eclipse
+ */
+ private static final long serialVersionUID = -5235044694247669643L;
+
+ public CnFrame() {
+
+ }
+
+ /**
+ * Custom Constructor.
+ * Sets the Window title to windowTitle
+ * @param windowTitle The Title provided as String
+ */
+ public CnFrame (String windowTitle) {
+ this.setTitle(windowTitle);
+ frameConstructor();
+ }
+
+ /**
+ * Construct the main Frame.
+ */
+ private void frameConstructor() {
+ // Set Behaviour when "x" is clicked.
+ setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+
+ // Get Screen Dimensions
+ Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
+ setBounds(50, 50, windowSize.width / 2, windowSize.height / 2);
+
+ getContentPane().setLayout(new BorderLayout());
+ }
+
+ /**
+ * Retrieves the current time in the following format: dd/MMM/yyyy HH:mm:ss
+ * @return The String representation of the current Date
+ */
+ public String getCurrentTime() {
+ DateFormat dateFormat = new SimpleDateFormat("dd / MMMM / yyyy -- HH:mm:ss");
+ java.util.Date date = new java.util.Date();
+ return dateFormat.format(date);
+ }
+}
diff --git a/src/ch/epfl/lca/sc250/gui/CnFrameP2P.java b/src/ch/epfl/lca/sc250/gui/CnFrameP2P.java
new file mode 100644
index 0000000..ee83b68
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/gui/CnFrameP2P.java
@@ -0,0 +1,206 @@
+package ch.epfl.lca.sc250.gui;
+
+import java.awt.BorderLayout;
+import java.awt.FlowLayout;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JButton;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+import javax.swing.JTextField;
+
+/**
+ * @author Christophe Trefois
+ *
+ */
+public class CnFrameP2P extends CnFrame implements ActionListener {
+
+ /**
+ * Generated serialID by Eclipse for Serializble Objects
+ */
+ private static final long serialVersionUID = -3251522255074162116L;
+
+ /**
+ * The TextArea used to display the messages
+ */
+ private JTextArea textArea;
+
+ /**
+ * TextField of the IP
+ */
+ private JTextField ipAdressTextField;
+
+ /**
+ * TextField of the Amount of Money
+ */
+ private JTextField amountMoneyTextField;
+
+ /**
+ * TextField of the position of the letter
+ */
+ private JTextField letterPosition;
+
+ /**
+ * Button used to send the request
+ */
+ private JButton sendMessage;
+
+ /**
+ * Instance of the main Program which is of type ITCPSender
+ */
+ private ITCPSender tcpSender = null;
+
+ /**
+ * Constructs a CnFrameP2P Instance with title windowTitle
+ *
+ * @param windowTitle
+ */
+ public CnFrameP2P(String windowTitle) {
+ super(windowTitle);
+
+ setSize(600, 300);
+
+ JPanel centerPanel = new JPanel();
+
+ textArea = new JTextArea();
+ textArea.setEditable(false);
+
+ JScrollPane scrollPane = new JScrollPane(textArea);
+
+ centerPanel.setLayout(new GridLayout(1, 1));
+ centerPanel.add(scrollPane);
+
+ // put the centerPanel to the Center
+ getContentPane().add(centerPanel, BorderLayout.CENTER);
+
+ JPanel topPanel = new JPanel();
+ topPanel.setLayout(new FlowLayout());
+
+ topPanel.add(new JLabel("IP Address: "));
+ ipAdressTextField = new JTextField();
+ topPanel.add(ipAdressTextField);
+ ipAdressTextField.setPreferredSize(new java.awt.Dimension(78, 20));
+
+ topPanel.add(new JLabel("Money: "));
+ amountMoneyTextField = new JTextField();
+ topPanel.add(amountMoneyTextField);
+ amountMoneyTextField.setPreferredSize(new java.awt.Dimension(49, 20));
+
+ topPanel.add(new JLabel("Position: "));
+ letterPosition = new JTextField();
+ topPanel.add(letterPosition);
+ letterPosition.setPreferredSize(new java.awt.Dimension(56, 20));
+
+ sendMessage = new JButton();
+ topPanel.add(sendMessage);
+ sendMessage.setText("Send Request");
+ sendMessage.setActionCommand("Send");
+ sendMessage.addActionListener(this);
+
+ getContentPane().add(topPanel, BorderLayout.NORTH);
+
+ // Make this frame visible
+ setVisible(true);
+ }
+
+ /**
+ * Append Text to the textArea
+ *
+ * @param textToAppend
+ */
+ public void appendText(String textToAppend) {
+ textArea.append(super.getCurrentTime() + " : " + textToAppend);
+ textArea.setCaretPosition(textArea.getText().length());
+ }
+
+ /**
+ * Clear the TextArea.
+ */
+ public void clearTextArea() {
+ textArea.replaceRange("", 0, textArea.getText().length());
+ }
+
+ /**
+ *
+ */
+ public void actionPerformed(ActionEvent arg0) {
+ String command = arg0.getActionCommand();
+ if (command.equalsIgnoreCase("Send")) {
+ if (tcpSender == null)
+ return;
+ appendText("IP: " + ipAdressTextField.getText() + " - Buy For: "
+ + amountMoneyTextField.getText() + " - Position: "
+ + letterPosition.getText() + "\n");
+ ipAdressTextField.setEnabled(false);
+ amountMoneyTextField.setEnabled(false);
+ letterPosition.setEnabled(false);
+ sendMessage.setText("Waiting ...");
+ sendMessage.removeActionListener(this);
+ sendMessage.setEnabled(false);
+ tcpSender.sendMessage(ipAdressTextField.getText(),
+ amountMoneyTextField.getText(), letterPosition.getText());
+ }
+ }
+
+ /**
+ * This method is called when an Answer is received from the other Player.
+ *
+ * It then is displayed in the GUI, and the GUI is reset.
+ *
+ * @param communication
+ */
+ public void receivedMessage(String communication) {
+ appendText(communication + "\n");
+ ipAdressTextField.setEnabled(true);
+ amountMoneyTextField.setEnabled(true);
+ letterPosition.setEnabled(true);
+ sendMessage.setText("Send Request");
+ sendMessage.addActionListener(this);
+ sendMessage.setEnabled(true);
+ ipAdressTextField.setText("");
+ amountMoneyTextField.setText("");
+ letterPosition.setText("");
+ appendText("----- Communication Completed --------\n");
+ }
+
+ /**
+ * Used to work with the Interface
+ *
+ * @param tcpSender
+ */
+ public void setTcpSender(ITCPSender tcpSender) {
+ this.tcpSender = tcpSender;
+ }
+
+ /**
+ * When an offer is received, display a Confirm Dialog where the user can
+ * choose Yes or No
+ *
+ * @param IP
+ * The IP we got the offer from
+ * @param amount
+ * The amount the other player gives us for a letter
+ * @param position
+ * The position of the letter he wants to buy
+ * @return True or False
+ */
+ public boolean getOfferResult(String IP, String amount, String position) {
+ boolean chosenValue = false;
+ String msgToDisplay = "Buyer from IP: " + IP
+ + "\nWants to buy Letter at Position " + position + " for "
+ + amount + "$";
+ int returnValue = JOptionPane.showConfirmDialog(this, msgToDisplay,
+ "You got an Offer !", JOptionPane.YES_NO_OPTION);
+ if (returnValue == JOptionPane.OK_OPTION) {
+ chosenValue = true;
+ } else {
+ chosenValue = false;
+ }
+ return chosenValue;
+ }
+}
diff --git a/src/ch/epfl/lca/sc250/gui/CnFrameTCP.java b/src/ch/epfl/lca/sc250/gui/CnFrameTCP.java
new file mode 100644
index 0000000..644b0fc
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/gui/CnFrameTCP.java
@@ -0,0 +1,69 @@
+package ch.epfl.lca.sc250.gui;
+
+import java.awt.BorderLayout;
+import java.awt.GridLayout;
+
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+
+/**
+ * This Class dispalys messages received from the Server. E.g. Server Hello etc...
+ * Call appendText(String) to update the GUI
+ *
+ * @author Christophe Trefois
+ */
+public class CnFrameTCP extends CnFrame {
+
+ /**
+ * Generated serialID by Eclipse
+ */
+ private static final long serialVersionUID = 4899514760671051872L;
+
+ /**
+ * The TextArea used to display the messages
+ */
+ private JTextArea textArea;
+
+ /**
+ * Constructs a CnFrameTCP Instance with title windowTitle
+ * @param windowTitle
+ */
+ public CnFrameTCP(String windowTitle) {
+ super(windowTitle);
+ setSize(500, 250);
+
+ JPanel centerPanel = new JPanel();
+
+ textArea = new JTextArea();
+ textArea.setEditable(false);
+
+ JScrollPane scrollPane = new JScrollPane(textArea);
+
+ centerPanel.setLayout(new GridLayout(1, 1));
+ centerPanel.add(scrollPane);
+
+ // put the centerPanel to the Center
+ getContentPane().add(centerPanel, BorderLayout.CENTER);
+
+ // Make this frame visible
+ setVisible(true);
+ }
+
+ /**
+ * Append Text to the textArea
+ * @param textToAppend
+ */
+ public void appendText(String textToAppend) {
+ textArea.append(super.getCurrentTime() + " : " + textToAppend);
+ textArea.setCaretPosition(textArea.getText().length());
+ }
+
+ /**
+ * Clear the TextArea.
+ *
+ */
+ public void clearTextArea() {
+ textArea.replaceRange("", 0, textArea.getText().length());
+ }
+}
diff --git a/src/ch/epfl/lca/sc250/gui/CnFrameUDP.java b/src/ch/epfl/lca/sc250/gui/CnFrameUDP.java
new file mode 100644
index 0000000..6a76f4d
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/gui/CnFrameUDP.java
@@ -0,0 +1,144 @@
+package ch.epfl.lca.sc250.gui;
+
+import java.awt.BorderLayout;
+import java.util.Vector;
+
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.table.DefaultTableModel;
+
+
+/**
+ * This Class is used to display UDP Broadcast Messages sent from the Server to the Client.
+ * The Client should create a CnFrameUDP Instance and call the method "appendBroadcastMessage"
+ * each time an UDP Broadcast is received.
+ * Example:
+ *
+ * CnFrameUDP cnFrameUDP = new CnFrameUDP("Networking Course");
+ * while(true) {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ cnFrameUDP.appendBroadcastMessage("192.168.1.5", "****X***X****X");
+ }
+ *
+ * @author Christophe Trefois
+ *
+ */
+public class CnFrameUDP extends CnFrame {
+ /**
+ * Generated serialID by Eclipse
+ */
+ private static final long serialVersionUID = 2774784580042117224L;
+
+ /**
+ * Vector containing the table rows data
+ */
+ private Vector tableRows;
+
+ /**
+ * Vector containing the column names. Filled with addColumns.
+ */
+ private Vector columns;
+
+ /**
+ * Array containing the column names
+ */
+ private static final String[] columnNames = {"Received At", "User ID", "IP Address", "Letters"};
+
+ /**
+ * tableModel used by the Table
+ */
+ private DefaultTableModel tableModel;
+
+ /**
+ * The table used to display the broadcast messages
+ */
+ private JTable broadcastTable;
+
+ /**
+ * Constructs a CnFrameUDP Instance with title windowTitle
+ * @param windowTitle The Title if the window
+ */
+ public CnFrameUDP(String windowTitle) {
+ super(windowTitle);
+
+ // Create new JPanel
+ JPanel centerPanel = new JPanel();
+
+ // Fill the columns Vector
+ addColumns(columnNames);
+
+ tableModel = new DefaultTableModel();
+ tableModel.setDataVector(tableRows, columns);
+
+ // create new JPanel
+ broadcastTable = new JTable(tableModel);
+ broadcastTable.getColumnModel().getColumn(0).setCellRenderer(new CellRenderer());
+ broadcastTable.getColumnModel().getColumn(1).setCellRenderer(new CellRenderer());
+ broadcastTable.getColumnModel().getColumn(2).setCellRenderer(new CellRenderer());
+ broadcastTable.getColumnModel().getColumn(3).setCellRenderer(new CellRenderer());
+
+ // Add ScrollBar to the broadcastTable
+ JScrollPane scrollPane = new JScrollPane(broadcastTable);
+ centerPanel.add(scrollPane);
+
+ // put the centerPanel to the Center
+ getContentPane().add(centerPanel, BorderLayout.CENTER);
+
+ // Pack
+ pack();
+
+ // Make this frame visible
+ setVisible(true);
+ }
+
+ /**
+ * Fill the column name vector
+ * @param columnNames the array of Strings to fill the vector with
+ */
+ private void addColumns(String[] columnNames) {
+ // Init tableRows and columns Vectors
+ tableRows = new Vector();
+ columns = new Vector();
+
+ for (int i = 0; i < columnNames.length; i++)
+ columns.addElement((String) columnNames[i]);
+ }
+
+ /**
+ * Adds a row to the JTable
+ * @param broadcastIP The IP from the broadcast message
+ * @param letters The Letters
+ */
+ private void addRow(String broadcastIP, String userID, String letters) {
+ Vector tempVector = new Vector();
+ tempVector.addElement(super.getCurrentTime());
+ tempVector.addElement(userID);
+ tempVector.addElement(broadcastIP);
+ tempVector.addElement(letters);
+ tableRows.addElement(tempVector);
+ broadcastTable.addNotify();
+ }
+
+ /**
+ * Modifies the GUI by appending the broadcast Message
+ * @param broadcastIP the IP extracted from the broadcast UDP Message
+ * @param broadcastLetters the letters from the UDP Broadcast
+ */
+ public void appendBroadcastMessage(String broadcastIP, String userID, String broadcastLetters) {
+ addRow(broadcastIP, userID, broadcastLetters);
+ }
+
+ /**
+ * Clears the Table
+ *
+ */
+ public void clearTable() {
+ tableRows.clear();
+ broadcastTable.addNotify();
+ }
+}
diff --git a/src/ch/epfl/lca/sc250/gui/ITCPSender.java b/src/ch/epfl/lca/sc250/gui/ITCPSender.java
new file mode 100644
index 0000000..3249eea
--- /dev/null
+++ b/src/ch/epfl/lca/sc250/gui/ITCPSender.java
@@ -0,0 +1,17 @@
+package ch.epfl.lca.sc250.gui;
+
+/**
+ * This interface needs to be implemented by TP7 in order to work.
+ * @author Christophe Trefois
+ *
+ */
+public interface ITCPSender {
+ /**
+ * This method is supposed to send a msg over a TCP Socket to the other Player.
+ *
+ * @param ipAddress IP Address we want to make a request to
+ * @param moneyAmount The amount of money we offer on the letter
+ * @param letterPosition The position of the letter that we want to buy
+ */
+ public void sendMessage(String ipAddress, String moneyAmount, String letterPosition);
+}