diff --git a/src/chatprogramm/Client.java b/src/chatprogramm/Client.java deleted file mode 100644 index 20d91ce..0000000 --- a/src/chatprogramm/Client.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.net.Socket; -import java.util.logging.Logger; - -/** - * Builder Class - * @author le - */ -public class Client -{ - private static final Logger lg = Logger.getLogger("netz"); - private static final int PORT = 35000; - private static final String IP_ADRESSE = "127.0.0.1"; - - public Client() throws IOException - { - 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"); - - BufferedReader in = new BufferedReader(isr); - //BufferedWriter out = new BufferedWriter(osr); - PrintWriter out = new PrintWriter(osr); - - 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"); - - System.out.println("Client: Quittung EMPFANGEN - " + quittung); - - - - out.close(); - in.close(); - } - - /** - * @param args the command line arguments - */ - public static void main(String[] args) - { - try - { - new Client(); - } - catch (IOException ex) - { - lg.severe(ex.toString()); - } - } -} diff --git a/src/chatprogramm/Server.java b/src/chatprogramm/Server.java deleted file mode 100644 index 4cd3017..0000000 --- a/src/chatprogramm/Server.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.logging.Logger; - -/** - * Builder Class - * @author le - */ -public class Server -{ - private static final Logger lg = Logger.getLogger("netz"); - private static final int PORT = 35000; - - public Server() throws IOException - { - 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"); - - System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht); - - out.println("Server -> ich habe die Nachricht erhalten"); - lg.info("Server: Quittung versendet"); - - out.flush(); // wirklich absenden!! - - out.close(); - in.close(); - } - - /** - * @param args the command line arguments - */ - public static void main(String[] args) - { - try - { - new Server(); - } - catch (IOException ex) - { - lg.severe(ex.toString()); - } - } -} diff --git a/src/chatprogramm/Start.java b/src/chatprogramm/Start.java index a38a647..e100d74 100644 --- a/src/chatprogramm/Start.java +++ b/src/chatprogramm/Start.java @@ -6,6 +6,12 @@ package chatprogramm; +import chatprogramm.controller.ConnectController; +import chatprogramm.controller.ReceiveAdapterController; +import chatprogramm.controller.SendController; +import chatprogramm.model.Transmitter; +import chatprogramm.view.ChatView; + /** * Builder Class * @author le @@ -14,6 +20,12 @@ 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); } diff --git a/src/chatprogramm/controller/ConnectController.java b/src/chatprogramm/controller/ConnectController.java index 9339fca..9d8c6dc 100644 --- a/src/chatprogramm/controller/ConnectController.java +++ b/src/chatprogramm/controller/ConnectController.java @@ -6,8 +6,13 @@ package chatprogramm.controller; +import chatprogramm.logger.OhmLogger; import chatprogramm.model.Transmitter; import chatprogramm.view.ChatView; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JOptionPane; /** * @@ -17,10 +22,66 @@ public class ConnectController { 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) + { + 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(); + + } + + } + + public void startServer() + { + try + { + model.createServer(port); + } + catch (IOException ex) + { + Logger.getLogger(ConnectController.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void startClient() + { + try + { + model.createClient(port, ip); + } + catch (IOException ex) + { + Logger.getLogger(ConnectController.class.getName()).log(Level.SEVERE, null, ex); + } } } diff --git a/src/chatprogramm/model/Client.java b/src/chatprogramm/model/Client.java index b1de18c..b8b3af3 100644 --- a/src/chatprogramm/model/Client.java +++ b/src/chatprogramm/model/Client.java @@ -24,11 +24,14 @@ import java.util.logging.Logger; public class Client { private static final Logger lg = OhmLogger.getLogger(); - private static final int PORT = 35000; - private static final String IP_ADRESSE = "127.0.0.1"; + private static int PORT = 35000; + private static String IP_ADRESSE = "127.0.0.1"; - public Client() throws IOException + public Client(int port, String ip) throws IOException { + if(port != 0) {this.PORT = port;}; + if(ip != null | ip != "") {this.IP_ADRESSE = ip;}; + lg.info("Client: verbinde ..."); Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert! lg.info("Client: Verbindung hergestellt"); @@ -60,18 +63,5 @@ public class Client in.close(); } - /** - * @param args the command line arguments - */ - public static void main(String[] args) - { - try - { - new Client(); - } - catch (IOException ex) - { - lg.severe(ex.toString()); - } - } + } diff --git a/src/chatprogramm/model/Server.java b/src/chatprogramm/model/Server.java index f762176..dfb3578 100644 --- a/src/chatprogramm/model/Server.java +++ b/src/chatprogramm/model/Server.java @@ -25,10 +25,14 @@ import java.util.logging.Logger; public class Server { private static final Logger lg = OhmLogger.getLogger(); - private static final int PORT = 35000; + private static int PORT = 35000; - public Server() throws IOException + public Server(int port) throws IOException { + if(port != 0) + { + this.PORT = port; + } ServerSocket sSocket = new ServerSocket(PORT); lg.info("Server: Warte auf Verbindung ..."); Socket s = sSocket.accept(); // Achtung: blockiert! @@ -60,18 +64,4 @@ public class Server in.close(); } - /** - * @param args the command line arguments - */ - public static void main(String[] args) - { - try - { - new Server(); - } - catch (IOException ex) - { - lg.severe(ex.toString()); - } - } } diff --git a/src/chatprogramm/model/Transmitter.java b/src/chatprogramm/model/Transmitter.java index 06efa61..720fe49 100644 --- a/src/chatprogramm/model/Transmitter.java +++ b/src/chatprogramm/model/Transmitter.java @@ -6,14 +6,29 @@ package chatprogramm.model; +import java.io.IOException; + /** * * @author Gerhard */ public class Transmitter { + Server server; + Client client; + public Transmitter() { } + + 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); + } } diff --git a/src/chatprogramm/view/ChatView.form b/src/chatprogramm/view/ChatView.form index b64cbe0..3d3cda3 100644 --- a/src/chatprogramm/view/ChatView.form +++ b/src/chatprogramm/view/ChatView.form @@ -2,21 +2,6 @@
- - - - - - - - - - - - - - - @@ -38,7 +23,7 @@ - + @@ -50,7 +35,7 @@ - + @@ -74,6 +59,16 @@ + + + + + + + + + + diff --git a/src/chatprogramm/view/ChatView.java b/src/chatprogramm/view/ChatView.java index 122651b..e488b88 100644 --- a/src/chatprogramm/view/ChatView.java +++ b/src/chatprogramm/view/ChatView.java @@ -27,14 +27,6 @@ public class ChatView extends javax.swing.JFrame return btnSend; } - /** - * @return the jDialogServerClient - */ - public javax.swing.JDialog getjDialogServerClient() - { - return jDialogServerClient; - } - /** * @return the taEmpfangen */ @@ -69,10 +61,11 @@ public class ChatView extends javax.swing.JFrame private void initComponents() { - jDialogServerClient = new javax.swing.JDialog(); chatControll = new javax.swing.JLayeredPane(); btnSend = new javax.swing.JButton(); btnClear = new javax.swing.JButton(); + btnServer = new javax.swing.JButton(); + jButton1 = new javax.swing.JButton(); chatContent = new javax.swing.JLayeredPane(); jScrollPane1 = new javax.swing.JScrollPane(); taEmpfangen = new javax.swing.JTextArea(); @@ -82,17 +75,6 @@ public class ChatView extends javax.swing.JFrame jMenu2 = new javax.swing.JMenu(); jMenu3 = new javax.swing.JMenu(); - javax.swing.GroupLayout jDialogServerClientLayout = new javax.swing.GroupLayout(jDialogServerClient.getContentPane()); - jDialogServerClient.getContentPane().setLayout(jDialogServerClientLayout); - jDialogServerClientLayout.setHorizontalGroup( - jDialogServerClientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 400, Short.MAX_VALUE) - ); - jDialogServerClientLayout.setVerticalGroup( - jDialogServerClientLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGap(0, 300, Short.MAX_VALUE) - ); - setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); chatControll.setLayout(new java.awt.FlowLayout()); @@ -103,6 +85,12 @@ public class ChatView extends javax.swing.JFrame btnClear.setText("clear"); chatControll.add(btnClear); + btnServer.setText("jButton1"); + chatControll.add(btnServer); + + jButton1.setText("jButton1"); + chatControll.add(jButton1); + getContentPane().add(chatControll, java.awt.BorderLayout.PAGE_END); chatContent.setLayout(new javax.swing.BoxLayout(chatContent, javax.swing.BoxLayout.PAGE_AXIS)); @@ -130,6 +118,7 @@ public class ChatView extends javax.swing.JFrame setJMenuBar(jMenuBar1); pack(); + setLocationRelativeTo(null); }// //GEN-END:initComponents /** @@ -184,9 +173,10 @@ public class ChatView extends javax.swing.JFrame // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnClear; private javax.swing.JButton btnSend; + private javax.swing.JButton btnServer; private javax.swing.JLayeredPane chatContent; private javax.swing.JLayeredPane chatControll; - private javax.swing.JDialog jDialogServerClient; + private javax.swing.JButton jButton1; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenuBar jMenuBar1;