diff --git a/build.xml b/build.xml
deleted file mode 100644
index 644c1e2..0000000
--- a/build.xml
+++ /dev/null
@@ -1,81 +0,0 @@
-
-
- simple build file
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/src/ch/epfl/lca/sc250/PlayerProgram.java b/src/ch/epfl/lca/sc250/PlayerProgram.java
deleted file mode 100644
index 8f80499..0000000
--- a/src/ch/epfl/lca/sc250/PlayerProgram.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package ch.epfl.lca.sc250;
-
-import ch.epfl.lca.sc250.gui.finalgui.*;
-
-
-public class PlayerProgram {
- TCPClient tcpC;
- UDPServer udpS;
- ServentServer svS;
- ServentClient svC;
- CnFrameMain gui;
-
- public PlayerProgram ( ) {
- gui = new CnFrameMain("fdasdf-sdafsadfä-fsad-fsdaf--");
- tcpC = new TCPClient("in3sun23.epfl.ch", 13370, "x-way", gui);
- udpS = new UDPServer(gui);
- svC = new ServentClient(gui);
- svS = new ServentServer(gui);
- }
-
- public void launch ( ) {
- tcpC.start();
- udpS.start();
- svS.start();
- }
-
- public static void main ( String[] args ) {
- PlayerProgram p = new PlayerProgram();
- p.launch();
- }
-}
diff --git a/src/ch/epfl/lca/sc250/ServentClient.java b/src/ch/epfl/lca/sc250/ServentClient.java
deleted file mode 100644
index 97972f5..0000000
--- a/src/ch/epfl/lca/sc250/ServentClient.java
+++ /dev/null
@@ -1,79 +0,0 @@
-package ch.epfl.lca.sc250;
-
-import ch.epfl.lca.sc250.gui.finalgui.*;
-
-import java.net.*;
-import java.io.*;
-
-/**
- * @author Christophe Trefois
- */
-public class ServentClient implements ITCPSender {
-
- /**
- * Instance of the GUI
- */
- CnFrameMain myGui;
-
- private Socket sock;
- private int targetport = 13371;
- private BufferedReader inReader;
- private DataOutputStream outStream;
-
-
- /**
- * Constructor of the Client part of the Servent for
- *
- * @param windowTitle The title of the GUI
- */
- public ServentClient(CnFrameMain gui) {
- myGui = gui;
- // 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(new CnFrameMain("abc"));
- }
-
- /**
- * 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
- try {
- sock = new Socket(ipAddress, targetport);
-
- outStream = new DataOutputStream(sock.getOutputStream());
-
- inReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- } catch ( UnknownHostException uhe ) {
- System.out.println("Unknown host");
- } catch ( IOException ioe ) {
- System.out.println("IO-Error");
- }
-
- try {
- outStream.writeBytes(moneyAmount + ", " + letterPosition + "\n");
-
- // Put the answer in String message = theAnswer;
- // Call this method to unblock the GUI
- String response = inReader.readLine();
-
- myGui.receivedMessage(response);
- } catch ( IOException ioe ) {
- System.out.println("IO-Error");
- }
-
- }
-
-}
diff --git a/src/ch/epfl/lca/sc250/ServentServer.java b/src/ch/epfl/lca/sc250/ServentServer.java
deleted file mode 100644
index 45760a4..0000000
--- a/src/ch/epfl/lca/sc250/ServentServer.java
+++ /dev/null
@@ -1,74 +0,0 @@
-package ch.epfl.lca.sc250;
-
-import javax.swing.JOptionPane;
-
-import java.io.*;
-import java.net.*;
-
-import ch.epfl.lca.sc250.gui.finalgui.*;
-
-/**
- * @author Christophe Trefois
- *
- */
-public class ServentServer extends Thread {
-
- private CnFrameMain gui;
-
- public static void main(String[] args) {
- ServentServer s = new ServentServer(new CnFrameMain("abc"));
- s.loop();
- }
-
- public ServentServer ( CnFrameMain gui ) {
- this.gui = gui;
- }
-
- public void run ( ) {
- this.loop();
- }
-
- public void loop ( ) {
- // Open a Socket and listen on incoming offer requests.
- //
- try {
- boolean result;
- String message;
- ServerSocket knockSock;
- Socket sock;
- int targetport = 13371;
- BufferedReader inReader;
- DataOutputStream outStream;
-
- knockSock = new ServerSocket(targetport);
-
- while ( true ) {
-
- sock = knockSock.accept();
-
- outStream = new DataOutputStream(sock.getOutputStream());
- inReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
-
- message = inReader.readLine();
-
- String[] parsed = message.split(", ");
-
- // We suppose we got and parsed an offer, we then want the pop-up to display
- result = gui.getOfferResult(sock.getInetAddress().toString(), parsed[0], parsed[1]);
-
- if(result) {
- System.out.println("Yes");
- outStream.writeBytes("Yes\n");
- } else {
- System.out.println("No");
- outStream.writeBytes("No\n");
- }
-
- sock.close();
- }
- } catch ( IOException e ) {
- System.out.println("IO-Error");
- e.printStackTrace();
- }
- }
-}
diff --git a/src/ch/epfl/lca/sc250/TCPClient.java b/src/ch/epfl/lca/sc250/TCPClient.java
deleted file mode 100644
index 6af703b..0000000
--- a/src/ch/epfl/lca/sc250/TCPClient.java
+++ /dev/null
@@ -1,142 +0,0 @@
-package ch.epfl.lca.sc250;
-
-import java.net.*;
-import java.io.*;
-
-import ch.epfl.lca.sc250.gui.finalgui.*;
-
-public class TCPClient extends Thread {
-
- private Socket sock;
-
- private String targethost;
-
- private int targetport;
-
- private String name;
-
- private BufferedReader inr;
- private DataOutputStream outs;
-
- private CnFrameMain gui;
-
-
- public static void main ( String[] args ) {
-
- TCPClient tc = new TCPClient("in3sun23.epfl.ch", 13370, "x-way", new CnFrameMain("abc"));
- tc.init();
- tc.joinGame();
- tc.loop();
- tc.leave();
- }
-
- public void run ( ) {
- this.init();
- this.joinGame();
- this.loop();
- this.leave();
- }
-
- public void loop ( ) {
-
- boolean finished = false;
-
- BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
-
- while ( !finished ) {
-
- try {
- String cmd = consoleReader.readLine();
-
- try {
- if ( cmd.trim().equals("Bye Bye") ) {
- finished = true;
- }
-
- write(cmd);
- gui.appendText(cmd+"\n");
- gui.appendText(read()+"\n");
- } catch ( NullPointerException e ) {
- System.err.println("NullPointerException");
- gui.appendText("NullPointerException\n");
- e.printStackTrace();
- }
- } catch ( IOException e ) {
- System.err.println("IOError");
- gui.appendText("IO-Error\n");
- e.printStackTrace();
- }
- }
- }
-
- TCPClient ( String targethost, int targetport, String name, CnFrameMain gui ) {
- this.gui = gui;
- this.targethost = targethost;
- this.targetport = targetport;
- this.name = name;
- }
-
- public void init ( ) {
-
- try {
- sock = new Socket(targethost, targetport);
- } catch ( UnknownHostException uhe ) {
- System.out.println("Unknown host");
- gui.appendText("Unknown host\n");
- uhe.printStackTrace();
- } catch ( IOException e ) {
- System.err.println("IOError");
- gui.appendText("IO-Error\n");
- e.printStackTrace();
- }
-
- try {
- outs = new DataOutputStream(sock.getOutputStream());
- inr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- } catch ( IOException e ) {
- System.err.println("IOError");
- gui.appendText("IO-Error\n");
- e.printStackTrace();
- }
- }
-
- public void joinGame ( ) {
- write("Hello: " + name + "\n");
- String t = read();
- System.out.println(t);
- gui.appendText(t+"\n");
- }
-
- public void leave ( ) {
- try {
- sock.close();
- } catch ( IOException e ) {
- System.err.println("IOError");
- gui.appendText("IO-Error\n");
- e.printStackTrace();
- }
- System.out.println("Connection closed.");
- gui.appendText("Connection closed.\n");
- }
-
- public void write ( String s ) {
- try {
- outs.writeBytes(s);
- } catch ( IOException e ) {
- System.err.println("IOError");
- gui.appendText("IO-Error\n");
- e.printStackTrace();
- }
- }
-
- public String read ( ) {
- try {
- return inr.readLine();
- } catch ( IOException e ) {
- System.err.println("IOError");
- gui.appendText("IO-Error\n");
- e.printStackTrace();
- return "";
- }
- }
-}
diff --git a/src/ch/epfl/lca/sc250/UDPServer.java b/src/ch/epfl/lca/sc250/UDPServer.java
deleted file mode 100644
index f334979..0000000
--- a/src/ch/epfl/lca/sc250/UDPServer.java
+++ /dev/null
@@ -1,66 +0,0 @@
-package ch.epfl.lca.sc250;
-
-import java.net.*;
-import java.io.*;
-import java.nio.*;
-
-import ch.epfl.lca.sc250.gui.finalgui.*;
-
-public class UDPServer extends Thread {
- private DatagramSocket sock;
- private CnFrameMain gui;
-
- public static void main ( String[] args ) {
- UDPServer udpS = new UDPServer(new CnFrameMain("abc"));
- udpS.loop();
- }
-
- UDPServer ( CnFrameMain gui ) {
- this.gui = gui;
- try {
- sock = new DatagramSocket(13371);
- } catch ( SocketException se ) {
- System.out.println("Couldn't create socket.");
- }
- }
-
- public void run ( ) {
- this.loop();
- }
-
- 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
deleted file mode 100644
index 1f5b987..0000000
--- a/src/ch/epfl/lca/sc250/gui/CellRenderer.java
+++ /dev/null
@@ -1,32 +0,0 @@
-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
deleted file mode 100644
index bf86896..0000000
--- a/src/ch/epfl/lca/sc250/gui/CnFrame.java
+++ /dev/null
@@ -1,59 +0,0 @@
-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
deleted file mode 100644
index ee83b68..0000000
--- a/src/ch/epfl/lca/sc250/gui/CnFrameP2P.java
+++ /dev/null
@@ -1,206 +0,0 @@
-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
deleted file mode 100644
index 412e698..0000000
--- a/src/ch/epfl/lca/sc250/gui/CnFrameTCP.java
+++ /dev/null
@@ -1,69 +0,0 @@
-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
deleted file mode 100644
index 6a76f4d..0000000
--- a/src/ch/epfl/lca/sc250/gui/CnFrameUDP.java
+++ /dev/null
@@ -1,144 +0,0 @@
-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
deleted file mode 100644
index 3249eea..0000000
--- a/src/ch/epfl/lca/sc250/gui/ITCPSender.java
+++ /dev/null
@@ -1,17 +0,0 @@
-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);
-}
diff --git a/src/ch/epfl/lca/sc250/gui/finalgui/CellRenderer.java b/src/ch/epfl/lca/sc250/gui/finalgui/CellRenderer.java
deleted file mode 100644
index 4b38067..0000000
--- a/src/ch/epfl/lca/sc250/gui/finalgui/CellRenderer.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package ch.epfl.lca.sc250.gui.finalgui;
-
-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/finalgui/CnFrameMain.java b/src/ch/epfl/lca/sc250/gui/finalgui/CnFrameMain.java
deleted file mode 100644
index 965fc99..0000000
--- a/src/ch/epfl/lca/sc250/gui/finalgui/CnFrameMain.java
+++ /dev/null
@@ -1,390 +0,0 @@
-/**
- *
- */
-package ch.epfl.lca.sc250.gui.finalgui;
-
-import java.awt.BorderLayout;
-import java.awt.Dimension;
-import java.awt.FlowLayout;
-import java.awt.GridLayout;
-import java.awt.Toolkit;
-import java.awt.event.ActionEvent;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Vector;
-
-import javax.swing.AbstractAction;
-import javax.swing.JButton;
-import javax.swing.JFrame;
-import javax.swing.JLabel;
-import javax.swing.JOptionPane;
-import javax.swing.JPanel;
-import javax.swing.JScrollPane;
-import javax.swing.JSplitPane;
-import javax.swing.JTable;
-import javax.swing.JTextArea;
-import javax.swing.JTextField;
-import javax.swing.table.DefaultTableModel;
-
-/**
- * @author Trefex
- *
- */
-public class CnFrameMain extends JFrame {
-
- /**
- *
- */
- private static final long serialVersionUID = 8746287051727486324L;
- /**
- * 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;
- /**
- * The TextArea used to display the messages
- */
- private JTextArea textArea;
- /**
- * The TextArea used to display the messages
- */
- private JTextArea textAreaP2P;
- /**
- * 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;
- Thread loaderThread;
-
- /**
- *
- * @param windowTitle
- */
- public CnFrameMain(String windowTitle) {
- setTitle(windowTitle);
-
- // Set Behaviour when "x" is clicked.
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-
- // Get Screen Dimensions
- Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
- setBounds(50, 50, windowSize.width - 200, windowSize.height - 200);
-
- getContentPane().setLayout(new BorderLayout());
-
- JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, createUDPPanel(), createP2PPanel());
-
- JSplitPane sp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, sp, createTCPPanel());
-
- getContentPane().add(sp2, BorderLayout.CENTER);
-
-
- setVisible(true);
- }
-
- /**
- * 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);
- }
-
-
-
- /**
- * Constructs a CnFrameUDP Instance with title windowTitle
- * @param windowTitle The Title if the window
- */
- public JPanel createUDPPanel() {
-
- // 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);
-
- return centerPanel;
- }
-
- /**
- * 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(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();
- }
-
-
- /**
- * Constructs a CnFrameTCP Instance with title windowTitle
- * @param windowTitle
- */
- public JPanel createTCPPanel() {
- JPanel centerPanel = new JPanel();
-
- textArea = new JTextArea();
- textArea.setEditable(false);
-
- JScrollPane scrollPane = new JScrollPane(textArea);
-
- centerPanel.setLayout(new GridLayout(1, 1));
- centerPanel.add(scrollPane);
-
- return centerPanel;
- }
-
- /**
- * Append Text to the textArea
- * @param textToAppend
- */
- public void appendText(String textToAppend) {
- textArea.append(getCurrentTime() + " : " + textToAppend);
- textArea.setCaretPosition(textArea.getText().length());
- }
-
- /**
- * Clear the TextArea.
- *
- */
- public void clearTextArea() {
- textArea.replaceRange("", 0, textArea.getText().length());
- }
-
- /**
- * Constructs a CnFrameP2P Instance with title windowTitle
- * @param windowTitle
- */
- public JPanel createP2PPanel() {
- JPanel mainPanel = new JPanel();
- mainPanel.setLayout(new BorderLayout());
- JPanel centerPanel = new JPanel();
-
- textAreaP2P = new JTextArea();
- textAreaP2P.setEditable(false);
-
- JScrollPane scrollPane = new JScrollPane(textAreaP2P);
-
- centerPanel.setLayout(new GridLayout(1, 1));
- centerPanel.add(scrollPane);
-
- // put the centerPanel to the Center
- mainPanel.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");
- NonBlockingLoadAction nonBlocker = new NonBlockingLoadAction();
- sendMessage.addActionListener(nonBlocker);
- //sendMessage.addActionListener(this);
-
- mainPanel.add(topPanel, BorderLayout.NORTH);
- return mainPanel;
-
- }
-
- /**
- * Append Text to the textArea
- * @param textToAppend
- */
- public void appendTextP2P(String textToAppend) {
- textAreaP2P.append(getCurrentTime() + " : " + textToAppend);
- textAreaP2P.setCaretPosition(textAreaP2P.getText().length());
- }
-
- /**
- * Clear the TextArea.
- */
- public void clearTextAreaP2P() {
- textAreaP2P.replaceRange("", 0, textAreaP2P.getText().length());
- }
-
- /**
- * 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) {
- appendTextP2P(communication + "\n");
- ipAdressTextField.setEnabled(true);
- amountMoneyTextField.setEnabled(true);
- letterPosition.setEnabled(true);
- sendMessage.setText("Send Request");
- NonBlockingLoadAction nonBlocker = new NonBlockingLoadAction();
- sendMessage.addActionListener(nonBlocker);
- //sendMessage.addActionListener(this);
- sendMessage.setEnabled(true);
- ipAdressTextField.setText("");
- amountMoneyTextField.setText("");
- letterPosition.setText("");
- appendTextP2P("----- 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(null, msgToDisplay, "You got an Offer !", JOptionPane.YES_NO_OPTION);
- if(returnValue == JOptionPane.OK_OPTION) {
- chosenValue = true;
- } else {
- chosenValue = false;
- }
- return chosenValue;
- }
-
- class NonBlockingLoadAction extends AbstractAction implements Runnable {
- /**
- *
- */
- private static final long serialVersionUID = 4493869112678216390L;
- // note that this doesn't offer a means of being interrupted
- // so it refuses second launch instead
- public void actionPerformed (ActionEvent e) {
- if (loaderThread != null)
- return;
- loaderThread = new Thread ((Runnable) this);
- loaderThread.start();
-
- }
- public void run() {
- if (tcpSender == null)
- return;
- appendTextP2P("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());
- loaderThread = null;
-
- }
- }
-}
diff --git a/src/ch/epfl/lca/sc250/gui/finalgui/ITCPSender.java b/src/ch/epfl/lca/sc250/gui/finalgui/ITCPSender.java
deleted file mode 100644
index 5c45909..0000000
--- a/src/ch/epfl/lca/sc250/gui/finalgui/ITCPSender.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package ch.epfl.lca.sc250.gui.finalgui;
-
-/**
- * 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);
-}
diff --git a/src/testingPackage/GuiNewFrameTest.java b/src/testingPackage/GuiNewFrameTest.java
deleted file mode 100644
index 50a1f93..0000000
--- a/src/testingPackage/GuiNewFrameTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package testingPackage;
-
-import ch.epfl.lca.sc250.gui.finalgui.CnFrameMain;
-import ch.epfl.lca.sc250.gui.finalgui.ITCPSender;
-
-/**
- * @author Trefex
- *
- */
-public class GuiNewFrameTest implements ITCPSender {
-
- private CnFrameMain mygui;
-
- public GuiNewFrameTest(String windowTitle) {
- mygui = new CnFrameMain(windowTitle);
- mygui.setTcpSender(this);
- }
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- new GuiNewFrameTest("TP8 - Networking");
- }
-
- public void sendMessage(String ipAddress, String moneyAmount, String letterPosition) {
- try {
- Thread.sleep(10000);
- mygui.receivedMessage("We Accept Your Offer");
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
-
-}
diff --git a/tp6-8/build.xml b/tp6-8/build.xml
new file mode 100644
index 0000000..644c1e2
--- /dev/null
+++ b/tp6-8/build.xml
@@ -0,0 +1,81 @@
+
+
+ simple build file
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tp6-8/src/ch/epfl/lca/sc250/PlayerProgram.java b/tp6-8/src/ch/epfl/lca/sc250/PlayerProgram.java
new file mode 100644
index 0000000..8f80499
--- /dev/null
+++ b/tp6-8/src/ch/epfl/lca/sc250/PlayerProgram.java
@@ -0,0 +1,31 @@
+package ch.epfl.lca.sc250;
+
+import ch.epfl.lca.sc250.gui.finalgui.*;
+
+
+public class PlayerProgram {
+ TCPClient tcpC;
+ UDPServer udpS;
+ ServentServer svS;
+ ServentClient svC;
+ CnFrameMain gui;
+
+ public PlayerProgram ( ) {
+ gui = new CnFrameMain("fdasdf-sdafsadfä-fsad-fsdaf--");
+ tcpC = new TCPClient("in3sun23.epfl.ch", 13370, "x-way", gui);
+ udpS = new UDPServer(gui);
+ svC = new ServentClient(gui);
+ svS = new ServentServer(gui);
+ }
+
+ public void launch ( ) {
+ tcpC.start();
+ udpS.start();
+ svS.start();
+ }
+
+ public static void main ( String[] args ) {
+ PlayerProgram p = new PlayerProgram();
+ p.launch();
+ }
+}
diff --git a/tp6-8/src/ch/epfl/lca/sc250/ServentClient.java b/tp6-8/src/ch/epfl/lca/sc250/ServentClient.java
new file mode 100644
index 0000000..97972f5
--- /dev/null
+++ b/tp6-8/src/ch/epfl/lca/sc250/ServentClient.java
@@ -0,0 +1,79 @@
+package ch.epfl.lca.sc250;
+
+import ch.epfl.lca.sc250.gui.finalgui.*;
+
+import java.net.*;
+import java.io.*;
+
+/**
+ * @author Christophe Trefois
+ */
+public class ServentClient implements ITCPSender {
+
+ /**
+ * Instance of the GUI
+ */
+ CnFrameMain myGui;
+
+ private Socket sock;
+ private int targetport = 13371;
+ private BufferedReader inReader;
+ private DataOutputStream outStream;
+
+
+ /**
+ * Constructor of the Client part of the Servent for
+ *
+ * @param windowTitle The title of the GUI
+ */
+ public ServentClient(CnFrameMain gui) {
+ myGui = gui;
+ // 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(new CnFrameMain("abc"));
+ }
+
+ /**
+ * 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
+ try {
+ sock = new Socket(ipAddress, targetport);
+
+ outStream = new DataOutputStream(sock.getOutputStream());
+
+ inReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
+ } catch ( UnknownHostException uhe ) {
+ System.out.println("Unknown host");
+ } catch ( IOException ioe ) {
+ System.out.println("IO-Error");
+ }
+
+ try {
+ outStream.writeBytes(moneyAmount + ", " + letterPosition + "\n");
+
+ // Put the answer in String message = theAnswer;
+ // Call this method to unblock the GUI
+ String response = inReader.readLine();
+
+ myGui.receivedMessage(response);
+ } catch ( IOException ioe ) {
+ System.out.println("IO-Error");
+ }
+
+ }
+
+}
diff --git a/tp6-8/src/ch/epfl/lca/sc250/ServentServer.java b/tp6-8/src/ch/epfl/lca/sc250/ServentServer.java
new file mode 100644
index 0000000..45760a4
--- /dev/null
+++ b/tp6-8/src/ch/epfl/lca/sc250/ServentServer.java
@@ -0,0 +1,74 @@
+package ch.epfl.lca.sc250;
+
+import javax.swing.JOptionPane;
+
+import java.io.*;
+import java.net.*;
+
+import ch.epfl.lca.sc250.gui.finalgui.*;
+
+/**
+ * @author Christophe Trefois
+ *
+ */
+public class ServentServer extends Thread {
+
+ private CnFrameMain gui;
+
+ public static void main(String[] args) {
+ ServentServer s = new ServentServer(new CnFrameMain("abc"));
+ s.loop();
+ }
+
+ public ServentServer ( CnFrameMain gui ) {
+ this.gui = gui;
+ }
+
+ public void run ( ) {
+ this.loop();
+ }
+
+ public void loop ( ) {
+ // Open a Socket and listen on incoming offer requests.
+ //
+ try {
+ boolean result;
+ String message;
+ ServerSocket knockSock;
+ Socket sock;
+ int targetport = 13371;
+ BufferedReader inReader;
+ DataOutputStream outStream;
+
+ knockSock = new ServerSocket(targetport);
+
+ while ( true ) {
+
+ sock = knockSock.accept();
+
+ outStream = new DataOutputStream(sock.getOutputStream());
+ inReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
+
+ message = inReader.readLine();
+
+ String[] parsed = message.split(", ");
+
+ // We suppose we got and parsed an offer, we then want the pop-up to display
+ result = gui.getOfferResult(sock.getInetAddress().toString(), parsed[0], parsed[1]);
+
+ if(result) {
+ System.out.println("Yes");
+ outStream.writeBytes("Yes\n");
+ } else {
+ System.out.println("No");
+ outStream.writeBytes("No\n");
+ }
+
+ sock.close();
+ }
+ } catch ( IOException e ) {
+ System.out.println("IO-Error");
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/tp6-8/src/ch/epfl/lca/sc250/TCPClient.java b/tp6-8/src/ch/epfl/lca/sc250/TCPClient.java
new file mode 100644
index 0000000..6af703b
--- /dev/null
+++ b/tp6-8/src/ch/epfl/lca/sc250/TCPClient.java
@@ -0,0 +1,142 @@
+package ch.epfl.lca.sc250;
+
+import java.net.*;
+import java.io.*;
+
+import ch.epfl.lca.sc250.gui.finalgui.*;
+
+public class TCPClient extends Thread {
+
+ private Socket sock;
+
+ private String targethost;
+
+ private int targetport;
+
+ private String name;
+
+ private BufferedReader inr;
+ private DataOutputStream outs;
+
+ private CnFrameMain gui;
+
+
+ public static void main ( String[] args ) {
+
+ TCPClient tc = new TCPClient("in3sun23.epfl.ch", 13370, "x-way", new CnFrameMain("abc"));
+ tc.init();
+ tc.joinGame();
+ tc.loop();
+ tc.leave();
+ }
+
+ public void run ( ) {
+ this.init();
+ this.joinGame();
+ this.loop();
+ this.leave();
+ }
+
+ public void loop ( ) {
+
+ boolean finished = false;
+
+ BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
+
+ while ( !finished ) {
+
+ try {
+ String cmd = consoleReader.readLine();
+
+ try {
+ if ( cmd.trim().equals("Bye Bye") ) {
+ finished = true;
+ }
+
+ write(cmd);
+ gui.appendText(cmd+"\n");
+ gui.appendText(read()+"\n");
+ } catch ( NullPointerException e ) {
+ System.err.println("NullPointerException");
+ gui.appendText("NullPointerException\n");
+ e.printStackTrace();
+ }
+ } catch ( IOException e ) {
+ System.err.println("IOError");
+ gui.appendText("IO-Error\n");
+ e.printStackTrace();
+ }
+ }
+ }
+
+ TCPClient ( String targethost, int targetport, String name, CnFrameMain gui ) {
+ this.gui = gui;
+ this.targethost = targethost;
+ this.targetport = targetport;
+ this.name = name;
+ }
+
+ public void init ( ) {
+
+ try {
+ sock = new Socket(targethost, targetport);
+ } catch ( UnknownHostException uhe ) {
+ System.out.println("Unknown host");
+ gui.appendText("Unknown host\n");
+ uhe.printStackTrace();
+ } catch ( IOException e ) {
+ System.err.println("IOError");
+ gui.appendText("IO-Error\n");
+ e.printStackTrace();
+ }
+
+ try {
+ outs = new DataOutputStream(sock.getOutputStream());
+ inr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
+ } catch ( IOException e ) {
+ System.err.println("IOError");
+ gui.appendText("IO-Error\n");
+ e.printStackTrace();
+ }
+ }
+
+ public void joinGame ( ) {
+ write("Hello: " + name + "\n");
+ String t = read();
+ System.out.println(t);
+ gui.appendText(t+"\n");
+ }
+
+ public void leave ( ) {
+ try {
+ sock.close();
+ } catch ( IOException e ) {
+ System.err.println("IOError");
+ gui.appendText("IO-Error\n");
+ e.printStackTrace();
+ }
+ System.out.println("Connection closed.");
+ gui.appendText("Connection closed.\n");
+ }
+
+ public void write ( String s ) {
+ try {
+ outs.writeBytes(s);
+ } catch ( IOException e ) {
+ System.err.println("IOError");
+ gui.appendText("IO-Error\n");
+ e.printStackTrace();
+ }
+ }
+
+ public String read ( ) {
+ try {
+ return inr.readLine();
+ } catch ( IOException e ) {
+ System.err.println("IOError");
+ gui.appendText("IO-Error\n");
+ e.printStackTrace();
+ return "";
+ }
+ }
+}
diff --git a/tp6-8/src/ch/epfl/lca/sc250/UDPServer.java b/tp6-8/src/ch/epfl/lca/sc250/UDPServer.java
new file mode 100644
index 0000000..f334979
--- /dev/null
+++ b/tp6-8/src/ch/epfl/lca/sc250/UDPServer.java
@@ -0,0 +1,66 @@
+package ch.epfl.lca.sc250;
+
+import java.net.*;
+import java.io.*;
+import java.nio.*;
+
+import ch.epfl.lca.sc250.gui.finalgui.*;
+
+public class UDPServer extends Thread {
+ private DatagramSocket sock;
+ private CnFrameMain gui;
+
+ public static void main ( String[] args ) {
+ UDPServer udpS = new UDPServer(new CnFrameMain("abc"));
+ udpS.loop();
+ }
+
+ UDPServer ( CnFrameMain gui ) {
+ this.gui = gui;
+ try {
+ sock = new DatagramSocket(13371);
+ } catch ( SocketException se ) {
+ System.out.println("Couldn't create socket.");
+ }
+ }
+
+ public void run ( ) {
+ this.loop();
+ }
+
+ 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/tp6-8/src/ch/epfl/lca/sc250/gui/CellRenderer.java b/tp6-8/src/ch/epfl/lca/sc250/gui/CellRenderer.java
new file mode 100644
index 0000000..1f5b987
--- /dev/null
+++ b/tp6-8/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/tp6-8/src/ch/epfl/lca/sc250/gui/CnFrame.java b/tp6-8/src/ch/epfl/lca/sc250/gui/CnFrame.java
new file mode 100644
index 0000000..bf86896
--- /dev/null
+++ b/tp6-8/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/tp6-8/src/ch/epfl/lca/sc250/gui/CnFrameP2P.java b/tp6-8/src/ch/epfl/lca/sc250/gui/CnFrameP2P.java
new file mode 100644
index 0000000..ee83b68
--- /dev/null
+++ b/tp6-8/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/tp6-8/src/ch/epfl/lca/sc250/gui/CnFrameTCP.java b/tp6-8/src/ch/epfl/lca/sc250/gui/CnFrameTCP.java
new file mode 100644
index 0000000..412e698
--- /dev/null
+++ b/tp6-8/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/tp6-8/src/ch/epfl/lca/sc250/gui/CnFrameUDP.java b/tp6-8/src/ch/epfl/lca/sc250/gui/CnFrameUDP.java
new file mode 100644
index 0000000..6a76f4d
--- /dev/null
+++ b/tp6-8/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/tp6-8/src/ch/epfl/lca/sc250/gui/ITCPSender.java b/tp6-8/src/ch/epfl/lca/sc250/gui/ITCPSender.java
new file mode 100644
index 0000000..3249eea
--- /dev/null
+++ b/tp6-8/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);
+}
diff --git a/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/CellRenderer.java b/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/CellRenderer.java
new file mode 100644
index 0000000..4b38067
--- /dev/null
+++ b/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/CellRenderer.java
@@ -0,0 +1,32 @@
+package ch.epfl.lca.sc250.gui.finalgui;
+
+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/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/CnFrameMain.java b/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/CnFrameMain.java
new file mode 100644
index 0000000..965fc99
--- /dev/null
+++ b/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/CnFrameMain.java
@@ -0,0 +1,390 @@
+/**
+ *
+ */
+package ch.epfl.lca.sc250.gui.finalgui;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.GridLayout;
+import java.awt.Toolkit;
+import java.awt.event.ActionEvent;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Vector;
+
+import javax.swing.AbstractAction;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JSplitPane;
+import javax.swing.JTable;
+import javax.swing.JTextArea;
+import javax.swing.JTextField;
+import javax.swing.table.DefaultTableModel;
+
+/**
+ * @author Trefex
+ *
+ */
+public class CnFrameMain extends JFrame {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 8746287051727486324L;
+ /**
+ * 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;
+ /**
+ * The TextArea used to display the messages
+ */
+ private JTextArea textArea;
+ /**
+ * The TextArea used to display the messages
+ */
+ private JTextArea textAreaP2P;
+ /**
+ * 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;
+ Thread loaderThread;
+
+ /**
+ *
+ * @param windowTitle
+ */
+ public CnFrameMain(String windowTitle) {
+ setTitle(windowTitle);
+
+ // Set Behaviour when "x" is clicked.
+ setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+
+ // Get Screen Dimensions
+ Dimension windowSize = Toolkit.getDefaultToolkit().getScreenSize();
+ setBounds(50, 50, windowSize.width - 200, windowSize.height - 200);
+
+ getContentPane().setLayout(new BorderLayout());
+
+ JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, createUDPPanel(), createP2PPanel());
+
+ JSplitPane sp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, sp, createTCPPanel());
+
+ getContentPane().add(sp2, BorderLayout.CENTER);
+
+
+ setVisible(true);
+ }
+
+ /**
+ * 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);
+ }
+
+
+
+ /**
+ * Constructs a CnFrameUDP Instance with title windowTitle
+ * @param windowTitle The Title if the window
+ */
+ public JPanel createUDPPanel() {
+
+ // 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);
+
+ return centerPanel;
+ }
+
+ /**
+ * 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(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();
+ }
+
+
+ /**
+ * Constructs a CnFrameTCP Instance with title windowTitle
+ * @param windowTitle
+ */
+ public JPanel createTCPPanel() {
+ JPanel centerPanel = new JPanel();
+
+ textArea = new JTextArea();
+ textArea.setEditable(false);
+
+ JScrollPane scrollPane = new JScrollPane(textArea);
+
+ centerPanel.setLayout(new GridLayout(1, 1));
+ centerPanel.add(scrollPane);
+
+ return centerPanel;
+ }
+
+ /**
+ * Append Text to the textArea
+ * @param textToAppend
+ */
+ public void appendText(String textToAppend) {
+ textArea.append(getCurrentTime() + " : " + textToAppend);
+ textArea.setCaretPosition(textArea.getText().length());
+ }
+
+ /**
+ * Clear the TextArea.
+ *
+ */
+ public void clearTextArea() {
+ textArea.replaceRange("", 0, textArea.getText().length());
+ }
+
+ /**
+ * Constructs a CnFrameP2P Instance with title windowTitle
+ * @param windowTitle
+ */
+ public JPanel createP2PPanel() {
+ JPanel mainPanel = new JPanel();
+ mainPanel.setLayout(new BorderLayout());
+ JPanel centerPanel = new JPanel();
+
+ textAreaP2P = new JTextArea();
+ textAreaP2P.setEditable(false);
+
+ JScrollPane scrollPane = new JScrollPane(textAreaP2P);
+
+ centerPanel.setLayout(new GridLayout(1, 1));
+ centerPanel.add(scrollPane);
+
+ // put the centerPanel to the Center
+ mainPanel.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");
+ NonBlockingLoadAction nonBlocker = new NonBlockingLoadAction();
+ sendMessage.addActionListener(nonBlocker);
+ //sendMessage.addActionListener(this);
+
+ mainPanel.add(topPanel, BorderLayout.NORTH);
+ return mainPanel;
+
+ }
+
+ /**
+ * Append Text to the textArea
+ * @param textToAppend
+ */
+ public void appendTextP2P(String textToAppend) {
+ textAreaP2P.append(getCurrentTime() + " : " + textToAppend);
+ textAreaP2P.setCaretPosition(textAreaP2P.getText().length());
+ }
+
+ /**
+ * Clear the TextArea.
+ */
+ public void clearTextAreaP2P() {
+ textAreaP2P.replaceRange("", 0, textAreaP2P.getText().length());
+ }
+
+ /**
+ * 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) {
+ appendTextP2P(communication + "\n");
+ ipAdressTextField.setEnabled(true);
+ amountMoneyTextField.setEnabled(true);
+ letterPosition.setEnabled(true);
+ sendMessage.setText("Send Request");
+ NonBlockingLoadAction nonBlocker = new NonBlockingLoadAction();
+ sendMessage.addActionListener(nonBlocker);
+ //sendMessage.addActionListener(this);
+ sendMessage.setEnabled(true);
+ ipAdressTextField.setText("");
+ amountMoneyTextField.setText("");
+ letterPosition.setText("");
+ appendTextP2P("----- 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(null, msgToDisplay, "You got an Offer !", JOptionPane.YES_NO_OPTION);
+ if(returnValue == JOptionPane.OK_OPTION) {
+ chosenValue = true;
+ } else {
+ chosenValue = false;
+ }
+ return chosenValue;
+ }
+
+ class NonBlockingLoadAction extends AbstractAction implements Runnable {
+ /**
+ *
+ */
+ private static final long serialVersionUID = 4493869112678216390L;
+ // note that this doesn't offer a means of being interrupted
+ // so it refuses second launch instead
+ public void actionPerformed (ActionEvent e) {
+ if (loaderThread != null)
+ return;
+ loaderThread = new Thread ((Runnable) this);
+ loaderThread.start();
+
+ }
+ public void run() {
+ if (tcpSender == null)
+ return;
+ appendTextP2P("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());
+ loaderThread = null;
+
+ }
+ }
+}
diff --git a/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/ITCPSender.java b/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/ITCPSender.java
new file mode 100644
index 0000000..5c45909
--- /dev/null
+++ b/tp6-8/src/ch/epfl/lca/sc250/gui/finalgui/ITCPSender.java
@@ -0,0 +1,17 @@
+package ch.epfl.lca.sc250.gui.finalgui;
+
+/**
+ * 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);
+}
diff --git a/tp6-8/src/testingPackage/GuiNewFrameTest.java b/tp6-8/src/testingPackage/GuiNewFrameTest.java
new file mode 100644
index 0000000..50a1f93
--- /dev/null
+++ b/tp6-8/src/testingPackage/GuiNewFrameTest.java
@@ -0,0 +1,35 @@
+package testingPackage;
+
+import ch.epfl.lca.sc250.gui.finalgui.CnFrameMain;
+import ch.epfl.lca.sc250.gui.finalgui.ITCPSender;
+
+/**
+ * @author Trefex
+ *
+ */
+public class GuiNewFrameTest implements ITCPSender {
+
+ private CnFrameMain mygui;
+
+ public GuiNewFrameTest(String windowTitle) {
+ mygui = new CnFrameMain(windowTitle);
+ mygui.setTcpSender(this);
+ }
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ new GuiNewFrameTest("TP8 - Networking");
+ }
+
+ public void sendMessage(String ipAddress, String moneyAmount, String letterPosition) {
+ try {
+ Thread.sleep(10000);
+ mygui.receivedMessage("We Accept Your Offer");
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+}