diff --git a/src/chatprogramm/Start.java b/src/chatprogramm/Start.java index e100d74..49581c2 100644 --- a/src/chatprogramm/Start.java +++ b/src/chatprogramm/Start.java @@ -1,9 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - package chatprogramm; import chatprogramm.controller.ConnectController; @@ -13,27 +7,35 @@ import chatprogramm.model.Transmitter; import chatprogramm.view.ChatView; /** - * Builder Class - * @author le + * + * @author Marian */ -public class Start -{ - public Start() - { - ChatView view = new ChatView(); - Transmitter model = new Transmitter(); - ConnectController conCon = new ConnectController(view, model); - ReceiveAdapterController reiAdapCon = new ReceiveAdapterController(view, model); - SendController sendCon = new SendController(view, model); - view.setVisible(true); - - } +public class Start { - /** - * @param args the command line arguments - */ - public static void main(String[] args) - { - new Start(); - } + public Start() + { + ChatView view = new ChatView(); + Transmitter model = new Transmitter(); + + ConnectController controllerConnect = new ConnectController(model, view); + controllerConnect.registerEvents(); + + SendController controllerSend = new SendController(model, view); + controllerSend.registerEvents(); + + ReceiveAdapterController rxAdapter = new ReceiveAdapterController(view); + model.addObserver(rxAdapter); + + view.setTitle("ICQ 0.1 xD"); + view.setVisible(true); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) + { + Start start = new Start(); + } + } diff --git a/src/chatprogramm/controller/ConnectController.java b/src/chatprogramm/controller/ConnectController.java index 9d8c6dc..ca8e249 100644 --- a/src/chatprogramm/controller/ConnectController.java +++ b/src/chatprogramm/controller/ConnectController.java @@ -1,87 +1,95 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - package chatprogramm.controller; -import chatprogramm.logger.OhmLogger; import chatprogramm.model.Transmitter; +import chatprogramm.logger.OhmLogger; import chatprogramm.view.ChatView; -import java.io.IOException; -import java.util.logging.Level; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.logging.Logger; import javax.swing.JOptionPane; /** * - * @author Gerhard + * @author Marian */ -public class ConnectController +public class ConnectController implements ActionListener { - private ChatView view; - private Transmitter model; - private static final Logger logger = OhmLogger.getLogger(); - private String ip = null; - private int port = 0; - - public ConnectController(ChatView view, Transmitter model) - { - this.view = view; - this.model = model; - chooseConnection(); - } - - void chooseConnection() - { - Object[] options = {"Client", "Server"}; - int choice = JOptionPane.showOptionDialog(view, "Wähle deine Verbindungsart:", "Client oder Server", 0, 1, null, options, null); - - if(choice == 1) + private Transmitter model; + private ChatView view; + private static Logger logger = OhmLogger.getLogger(); + + public ConnectController(Transmitter m, ChatView v) { - logger.info("Server"); - String port = JOptionPane.showInputDialog(view, "PORT eingeben"); - - logger.info("Port für Server ist: localhost:" + port); - startServer(); - - - } - else - { - logger.info("Client"); - port = Integer.parseInt(JOptionPane.showInputDialog(view, "PORT eingeben")); - ip = JOptionPane.showInputDialog(view, "IP vom Server bitte"); - - logger.info("Client IP Adresse und Port ist: " + ip + ":" + port); - startClient(); - + this.model = m; + this.view = v; } - } - - public void startServer() - { - try + public void registerEvents() { - model.createServer(port); + this.view.getBtConnect().addActionListener(this); + this.view.getRbClient().addActionListener(this); + this.view.getRbServer().addActionListener(this); } - catch (IOException ex) + + @Override + public void actionPerformed(ActionEvent ae) { - Logger.getLogger(ConnectController.class.getName()).log(Level.SEVERE, null, ex); + Object o = ae.getSource(); + + if (o == view.getBtConnect()) { + int port = -1; + + try { + port = Integer.parseInt(view.getTfPort().getText()); + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(view, "Port ungültig! Muss eine Zahl sein"); + logger.severe(e.toString()); + return; + } + + if (view.getRbClient().isSelected() && (validIP(view.getTfIP().getText()) == false)) { + JOptionPane.showMessageDialog(view, "Ungültige IP-Adresse!"); + logger.severe("IP-Adresse ungültig"); + return; + } + + model.connectToPeer(view.getRbServer().isSelected(), view.getTfIP().getText(), port); + + view.getRbClient().setEnabled(false); + view.getRbServer().setEnabled(false); + view.getTfIP().setEnabled(false); + view.getTfPort().setEnabled(false); + } else { + view.getTfIP().setEnabled(view.getRbClient().isSelected()); + } } - } - - public void startClient() - { - try - { - model.createClient(port, ip); + + private boolean validIP (String ip) { + try { + if ( ip == null || ip.isEmpty() ) { + return false; + } + + String[] parts = ip.split( "\\." ); + if ( parts.length != 4 ) { + return false; + } + + for ( String s : parts ) { + int i = Integer.parseInt( s ); + if ( (i < 0) || (i > 255) ) { + return false; + } + } + if ( ip.endsWith(".") ) { + return false; + } + + return true; + } catch (NumberFormatException e) { + logger.severe(e.toString()); + return false; + } } - catch (IOException ex) - { - Logger.getLogger(ConnectController.class.getName()).log(Level.SEVERE, null, ex); - } - } + } diff --git a/src/chatprogramm/controller/ReceiveAdapterController.java b/src/chatprogramm/controller/ReceiveAdapterController.java index 316b1f1..f6aae0a 100644 --- a/src/chatprogramm/controller/ReceiveAdapterController.java +++ b/src/chatprogramm/controller/ReceiveAdapterController.java @@ -1,33 +1,28 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - package chatprogramm.controller; -import chatprogramm.model.Transmitter; import chatprogramm.view.ChatView; import java.util.Observable; import java.util.Observer; /** * - * @author Gerhard + * @author Marian */ public class ReceiveAdapterController implements Observer { - private ChatView view; - private Transmitter model; - - public ReceiveAdapterController(ChatView view, Transmitter model) - { - this.view = view; - this.model = model; - } - - @Override - public void update(Observable o, Object arg) - { - } + private ChatView view; + + public ReceiveAdapterController(ChatView view) + { + this.view = view; + } + + @Override + public void update(Observable observable, Object object) + { + String msg = (String)object; + + view.getTaCommunication().append(msg + "\n"); + } + } diff --git a/src/chatprogramm/controller/SendController.java b/src/chatprogramm/controller/SendController.java index 35cb005..9d662c7 100644 --- a/src/chatprogramm/controller/SendController.java +++ b/src/chatprogramm/controller/SendController.java @@ -1,26 +1,35 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - package chatprogramm.controller; import chatprogramm.model.Transmitter; import chatprogramm.view.ChatView; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; /** * - * @author Gerhard + * @author Marian */ -public class SendController +public class SendController implements ActionListener { - private ChatView view; - private Transmitter model; - - public SendController(ChatView view, Transmitter model) - { - this.view = view; - this.model = model; - } + private Transmitter model; + private ChatView view; + + public SendController(Transmitter m, ChatView v) + { + this.model = m; + this.view = v; + } + + public void registerEvents() + { + this.view.getBtSend().addActionListener(this); + this.view.getTfMessage().addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent ae) + { + model.sendMessage(view.getTfMessage().getText()); + view.getTfMessage().setText(""); + } } diff --git a/src/chatprogramm/model/Client.java b/src/chatprogramm/model/Client.java index b8b3af3..1fc31bd 100644 --- a/src/chatprogramm/model/Client.java +++ b/src/chatprogramm/model/Client.java @@ -1,9 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - package chatprogramm.model; import chatprogramm.logger.OhmLogger; @@ -14,54 +8,128 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; import java.net.Socket; +import java.util.Observable; +import java.util.logging.Level; import java.util.logging.Logger; /** - * Builder Class - * @author le + * + * @author Marian */ -public class Client +public class Client extends Observable implements Runnable { - private static final Logger lg = OhmLogger.getLogger(); - private static int PORT = 35000; - private static String IP_ADRESSE = "127.0.0.1"; - - public Client(int port, String ip) throws IOException - { - if(port != 0) {this.PORT = port;}; - if(ip != null | ip != "") {this.IP_ADRESSE = ip;}; + private Socket socket; + private BufferedReader reader; + private PrintWriter writer; + private boolean ready; + private Thread thd; + private String ip; + private int port; + private static Logger logger = OhmLogger.getLogger(); - lg.info("Client: verbinde ..."); - Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert! - lg.info("Client: Verbindung hergestellt"); - InputStream iStream = s.getInputStream(); - OutputStream oStream = s.getOutputStream(); - InputStreamReader isr = new InputStreamReader(iStream, "UTF-8"); - OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8"); + public Client(String ip, int port) + { + this.ip = ip; + this.port = port; + this.socket = null; + this.reader = null; + this.writer = null; + this.ready = false; + this.thd = null; + } - BufferedReader in = new BufferedReader(isr); - //BufferedWriter out = new BufferedWriter(osr); - PrintWriter out = new PrintWriter(osr); + public void init() + { + if (thd == null) { + thd = new Thread(this); + thd.start(); + } + } - lg.info("Client: Stream initialisiert"); - - out.println("Hallo Du Server Du - ich bin der client"); - out.flush(); // wirklich absenden!! - - lg.info("Client: Nachricht versendet"); - - String quittung = in.readLine(); // Achtung blockiert - lg.info("Client: Quittung empfangen"); + public void sendMessage(String msg) + { + if (ready) { + writer.println(msg); + writer.flush(); + } else { + logger.warning("Server not ready to send message. Connect first"); + } + } - System.out.println("Client: Quittung EMPFANGEN - " + quittung); + @Override + public void run() + { + logger.info("Running client..."); + + while (true) + { + if (socket == null) { + try { + socket = new Socket(ip, port); + logger.info("Connected to server"); + } catch (IOException e) { + logger.severe(e.getMessage()); + socket = null; + try { + Thread.sleep(1000); + } catch (InterruptedException ex) { + Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); + } + continue; + } + + InputStream iStream; + try { + iStream = socket.getInputStream(); + } catch (IOException e) { + logger.severe(e.getMessage()); + return; + } + OutputStream oStream; + try { + oStream = socket.getOutputStream(); + } catch (IOException e) { + logger.severe(e.getMessage()); + return; + } - - - out.close(); - in.close(); - } + InputStreamReader isr; + try { + isr = new InputStreamReader(iStream, "UTF-8"); + } catch (UnsupportedEncodingException e) { + logger.severe(e.getMessage()); + return; + } + OutputStreamWriter osr; + try { + osr = new OutputStreamWriter(oStream, "UTF-8"); + } catch (UnsupportedEncodingException e) { + logger.severe(e.getMessage()); + return; + } - + reader = new BufferedReader(isr); + writer = new PrintWriter(osr); + + ready = true; + } + + if (ready) { + String msg; + try { + logger.info("Waiting for message"); + msg = reader.readLine(); + } catch (IOException e) { + logger.severe(e.toString()); + return; + } + + this.setChanged(); + this.notifyObservers(msg); + } + } + } } diff --git a/src/chatprogramm/model/Server.java b/src/chatprogramm/model/Server.java index dfb3578..d028bc9 100644 --- a/src/chatprogramm/model/Server.java +++ b/src/chatprogramm/model/Server.java @@ -1,9 +1,3 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - package chatprogramm.model; import chatprogramm.logger.OhmLogger; @@ -14,54 +8,140 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; import java.net.ServerSocket; import java.net.Socket; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.Observable; +import java.util.logging.Level; import java.util.logging.Logger; /** - * Builder Class - * @author le + * + * @author Marian */ -public class Server +public class Server extends Observable implements Runnable { - private static final Logger lg = OhmLogger.getLogger(); - private static int PORT = 35000; - - public Server(int port) throws IOException - { - if(port != 0) + private int port; + private ServerSocket server; + private Socket client; + private static Logger logger = OhmLogger.getLogger(); + private BufferedReader reader; + private PrintWriter writer; + private volatile boolean ready; + private Thread thd; + private static final SimpleDateFormat sdf = new SimpleDateFormat("HH.mm.ss"); + + + public Server(int port) { - this.PORT = port; + this.port = port; + this.server = null; + this.client = null; + this.reader = null; + this.writer = null; + this.ready = false; + this.thd = null; } - ServerSocket sSocket = new ServerSocket(PORT); - lg.info("Server: Warte auf Verbindung ..."); - Socket s = sSocket.accept(); // Achtung: blockiert! - lg.info("Server: Verbindung akzeptiert"); - InputStream iStream = s.getInputStream(); - OutputStream oStream = s.getOutputStream(); - InputStreamReader isr = new InputStreamReader(iStream, "UTF-8"); - OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8"); - - BufferedReader in = new BufferedReader(isr); - //BufferedWriter out = new BufferedWriter(osr); - PrintWriter out = new PrintWriter(osr); - - lg.info("Server: Stream initialisiert"); - lg.info("Server: warte auf Nachricht ..."); - - String nachricht = in.readLine(); // Achtung blockiert - lg.info("Server: Nachricht empfangen"); + public void init() + { + if (thd == null) { + thd = new Thread(this); + thd.start(); + } + } - System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht); + @Override + public void run() + { + logger.info("Running server..."); + + while (true) + { + if (server == null) { + try { + server = new ServerSocket(port); + } catch (IOException e) { + logger.severe(e.toString()); + return; + } + + logger.info("Waiting for client to connect"); + try { + client = server.accept(); + } catch (IOException e) { + logger.severe(e.toString()); + return; + } + logger.info("Client connected"); + + InputStream iStream; + try { + iStream = client.getInputStream(); + } catch (IOException e) { + logger.severe(e.toString()); + return; + } + OutputStream oStream; + try { + oStream = client.getOutputStream(); + } catch (IOException e) { + logger.severe(e.toString()); + return; + } - out.println("Server -> ich habe die Nachricht erhalten"); - lg.info("Server: Quittung versendet"); - - out.flush(); // wirklich absenden!! - - out.close(); - in.close(); - } + InputStreamReader isr; + try { + isr = new InputStreamReader(iStream, "UTF-8"); + } catch (UnsupportedEncodingException e) { + logger.severe(e.toString()); + return; + } + OutputStreamWriter osr; + try { + osr = new OutputStreamWriter(oStream, "UTF-8"); + } catch (UnsupportedEncodingException e) { + logger.severe(e.toString()); + return; + } + reader = new BufferedReader(isr); + writer = new PrintWriter(osr); + + ready = true; + + } + + if (ready) { + try { + logger.info("Waiting for message"); + String msg = reader.readLine(); + if (msg == null) { + ready = false; + continue; + } + + this.setChanged(); + this.notifyObservers(msg); + } catch (IOException e) { + logger.severe(e.toString()); + ready = false; + continue; + } + } + } + } + + public void sendMessage(String msg) + { + if (ready) { + writer.println(msg); + writer.flush(); + } else { + logger.warning("Server not ready to send message. Connect first"); + } + } + } diff --git a/src/chatprogramm/model/Transmitter.java b/src/chatprogramm/model/Transmitter.java index 720fe49..9c20854 100644 --- a/src/chatprogramm/model/Transmitter.java +++ b/src/chatprogramm/model/Transmitter.java @@ -1,34 +1,78 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ - package chatprogramm.model; -import java.io.IOException; + +import chatprogramm.controller.ReceiveAdapterController; +import chatprogramm.logger.OhmLogger; +import java.text.SimpleDateFormat; +import java.util.Observable; +import java.util.Observer; +import java.util.logging.Logger; /** * - * @author Gerhard + * @author Marian */ -public class Transmitter +public class Transmitter extends Observable implements Observer { - Server server; - Client client; - - public Transmitter() - { + boolean connected; + Server srv; + Client cli; + boolean mode; + boolean initialized; + ReceiveAdapterController Adapter; + private static Logger logger = OhmLogger.getLogger(); + + public Transmitter() + { + connected = false; + mode = false; + initialized = false; + srv = null; + cli = null; + } + + public void connectToPeer(boolean mode, String ip, int port) + { + if (initialized) { + logger.info("Chat already running"); + return; + } + + this.mode = mode; + + if (mode) { + logger.info("Running as server"); + srv = new Server(port); + srv.addObserver(this); + srv.init(); + } else { + logger.info("Running as client"); + cli = new Client(ip, port); + cli.addObserver(this); + cli.init(); + } + + initialized = true; + } + + public void sendMessage(String msg) + { + if (!initialized) { + logger.warning("Chat not initialized"); + return; + } + + if (mode) { + srv.sendMessage(msg); + } else { + cli.sendMessage(msg); + } + } - } - - public void createServer(int port) throws IOException - { - server = new Server(port); - } - - public void createClient(int port, String ip) throws IOException - { - client = new Client(port, ip); - } + @Override + public void update(Observable o, Object o1) + { + this.setChanged(); + this.notifyObservers(o1); + } } diff --git a/src/chatprogramm/view/ChatView.form b/src/chatprogramm/view/ChatView.form index 3d3cda3..74fb4fe 100644 --- a/src/chatprogramm/view/ChatView.form +++ b/src/chatprogramm/view/ChatView.form @@ -1,29 +1,16 @@ -
diff --git a/src/chatprogramm/view/ChatView.java b/src/chatprogramm/view/ChatView.java index e488b88..422ac33 100644 --- a/src/chatprogramm/view/ChatView.java +++ b/src/chatprogramm/view/ChatView.java @@ -7,182 +7,262 @@ package chatprogramm.view; /** * - * @author Gerhard + * @author max */ -public class ChatView extends javax.swing.JFrame -{ - /** - * @return the btnClear - */ - public javax.swing.JButton getBtnClear() - { - return btnClear; - } +public class ChatView extends javax.swing.JFrame { - /** - * @return the btnSend - */ - public javax.swing.JButton getBtnSend() - { - return btnSend; - } - - /** - * @return the taEmpfangen - */ - public javax.swing.JTextArea getTaEmpfangen() - { - return taEmpfangen; - } - - /** - * @return the taSenden - */ - public javax.swing.JTextArea getTaSenden() - { - return taSenden; - } - - /** - * Creates new form ChatView - */ - public ChatView() - { - initComponents(); - } - - /** - * This method is called from within the constructor to initialize the form. - * WARNING: Do NOT modify this code. The content of this method is always - * regenerated by the Form Editor. - */ - @SuppressWarnings("unchecked") - //