From 22efa7c18f5b7551b5143c68dcd1e5e454cb0e2a Mon Sep 17 00:00:00 2001 From: schuhmannje82308 Date: Mon, 18 Dec 2023 10:24:38 +0100 Subject: [PATCH] Init mit Aufgabe10 --- build.xml | 73 + manifest.mf | 3 + nbproject/build-impl.xml | 1771 +++++++++++++++++ nbproject/genfiles.properties | 8 + nbproject/project.properties | 97 + nbproject/project.xml | 15 + src/ChatProgramm/Start.java | 43 + .../controller/CommandController.java | 56 + .../controller/commands/CommandConnect.java | 76 + .../controller/commands/CommandInterface.java | 17 + .../controller/commands/CommandInvoker.java | 69 + .../controller/commands/CommandSend.java | 61 + src/ChatProgramm/model/Client.java | 41 + src/ChatProgramm/model/Nachricht.java | 31 + src/ChatProgramm/model/ReceiveAdapter.java | 44 + src/ChatProgramm/model/Server.java | 45 + src/ChatProgramm/model/Transmitter.java | 166 ++ .../model/TransmitterInterface.java | 17 + src/ChatProgramm/util/OhmLogger.java | 111 ++ src/ChatProgramm/view/ChatView.form | 151 ++ src/ChatProgramm/view/ChatView.java | 285 +++ src/config.properties | 8 + 22 files changed, 3188 insertions(+) create mode 100644 build.xml create mode 100644 manifest.mf create mode 100644 nbproject/build-impl.xml create mode 100644 nbproject/genfiles.properties create mode 100644 nbproject/project.properties create mode 100644 nbproject/project.xml create mode 100644 src/ChatProgramm/Start.java create mode 100644 src/ChatProgramm/controller/CommandController.java create mode 100644 src/ChatProgramm/controller/commands/CommandConnect.java create mode 100644 src/ChatProgramm/controller/commands/CommandInterface.java create mode 100644 src/ChatProgramm/controller/commands/CommandInvoker.java create mode 100644 src/ChatProgramm/controller/commands/CommandSend.java create mode 100644 src/ChatProgramm/model/Client.java create mode 100644 src/ChatProgramm/model/Nachricht.java create mode 100644 src/ChatProgramm/model/ReceiveAdapter.java create mode 100644 src/ChatProgramm/model/Server.java create mode 100644 src/ChatProgramm/model/Transmitter.java create mode 100644 src/ChatProgramm/model/TransmitterInterface.java create mode 100644 src/ChatProgramm/util/OhmLogger.java create mode 100644 src/ChatProgramm/view/ChatView.form create mode 100644 src/ChatProgramm/view/ChatView.java create mode 100644 src/config.properties diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..8748e0b --- /dev/null +++ b/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project Aufgabe11_GrafChat. + + + diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..b36368b --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,1771 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..fe7e424 --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=05f3ad23 +build.xml.script.CRC32=1a88b66a +build.xml.stylesheet.CRC32=f85dc8f2@1.108.0.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=05f3ad23 +nbproject/build-impl.xml.script.CRC32=fdc076e6 +nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.108.0.48 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..0f8ee00 --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,97 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=Aufgabe11_GrafChat +application.vendor=le +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.modulepath=\ + ${run.modulepath} +debug.test.classpath=\ + ${run.test.classpath} +debug.test.modulepath=\ + ${run.test.modulepath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/Aufgabe11_GrafChat.jar +dist.javadoc.dir=${dist.dir}/javadoc +dist.jlink.dir=${dist.dir}/jlink +dist.jlink.output=${dist.jlink.dir}/Aufgabe11_GrafChat +endorsed.classpath= +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.modulepath= +javac.processormodulepath= +javac.processorpath=\ + ${javac.classpath} +javac.source=17 +javac.target=17 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.modulepath=\ + ${javac.modulepath} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.html5=false +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +# The jlink additional root modules to resolve +jlink.additionalmodules= +# The jlink additional command line parameters +jlink.additionalparam= +jlink.launcher=true +jlink.launcher.name=Aufgabe11_GrafChat +main.class=ChatProgramm.Start +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.modulepath=\ + ${javac.modulepath} +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +run.test.modulepath=\ + ${javac.test.modulepath} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..486974f --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + Aufgabe11_GrafChat + + + + + + + + + diff --git a/src/ChatProgramm/Start.java b/src/ChatProgramm/Start.java new file mode 100644 index 0000000..440bd8d --- /dev/null +++ b/src/ChatProgramm/Start.java @@ -0,0 +1,43 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template + */ + +package ChatProgramm; + +import ChatProgramm.controller.CommandController; +import ChatProgramm.view.ChatView; +import javax.swing.JOptionPane; +import javax.swing.UIManager; + +/** + * Builder Class + * @author le + */ +public class Start +{ + public Start() + { + ChatView view = new ChatView(); + CommandController controller_commands = new CommandController(view); + controller_commands.registerEvents(); + controller_commands.registerCommands(); + view.setVisible(true); + } + + /** + * @param args the command line arguments + */ + public static void main(String[] args) + { + try + { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch (Exception ex) + { + JOptionPane.showMessageDialog(null, ex.toString()); + } + new Start(); + } +} \ No newline at end of file diff --git a/src/ChatProgramm/controller/CommandController.java b/src/ChatProgramm/controller/CommandController.java new file mode 100644 index 0000000..94a4819 --- /dev/null +++ b/src/ChatProgramm/controller/CommandController.java @@ -0,0 +1,56 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package ChatProgramm.controller; + +import ChatProgramm.controller.commands.CommandConnect; +import ChatProgramm.controller.commands.CommandInvoker; +import ChatProgramm.controller.commands.CommandSend; +import ChatProgramm.view.ChatView; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * + * @author ahren + */ +public class CommandController implements ActionListener{ + + private ChatView view; + private CommandInvoker invoker; + + public CommandController(ChatView view){ + this.view = view; + this.invoker = new CommandInvoker(); + } + + public void registerEvents(){ + view.getBtnConnect().addActionListener(this); + view.getTfNachricht().addActionListener(this); + } + + public void registerCommands(){ + CommandSend commandSend = new CommandSend(view); + invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, commandSend)); + invoker.addCommand(view.getTfNachricht(), commandSend); + } + + /** + * Ausführen des jeweiligen Kommandos + * @param e Referenz auf das Event + */ + @Override + public void actionPerformed(ActionEvent e) { + Component key = (Component)e.getSource(); + invoker.executeCommand(key); +// if(key == view.getBtnOpen()|| key==view.getMiOpen()) +// invoker.deleteStack(); +// } + } + +} + + diff --git a/src/ChatProgramm/controller/commands/CommandConnect.java b/src/ChatProgramm/controller/commands/CommandConnect.java new file mode 100644 index 0000000..7603372 --- /dev/null +++ b/src/ChatProgramm/controller/commands/CommandConnect.java @@ -0,0 +1,76 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package ChatProgramm.controller.commands; + +import ChatProgramm.model.Client; +import ChatProgramm.model.Server; +import ChatProgramm.util.OhmLogger; +import ChatProgramm.view.ChatView; +import java.io.IOException; +import java.util.logging.Logger; +import javax.swing.JDialog; +import javax.swing.JRadioButton; + +/** + * + * @author ahren + */ +public class CommandConnect implements CommandInterface +{ + private JRadioButton rBtnServer; + private JRadioButton rBtnClient; + private JDialog dialogFenster; + private static Logger lg = OhmLogger.getLogger(); + private CommandSend commandSend; + private ChatView view; + + public CommandConnect(ChatView view, CommandInterface value) + { + rBtnServer = view.getBtnServer(); + rBtnClient = view.getBtnClient(); + dialogFenster = view.getjDialog1(); + + commandSend = (CommandSend) value; + + this.view = view; + } + + @Override + public void execute() + { + if(rBtnServer.isSelected()){ + lg.info("Server ausgewählt"); + try { + commandSend.transmitterInterface = new Server(view); + } catch (IOException ex) { + lg.info("Die Verbindung zum Server ist Fehlgeschlagen"); + } + } + + if(rBtnClient.isSelected()){ + lg.info("Client ausgewählt"); + try { + commandSend.transmitterInterface = new Client(view); + } catch (IOException ex) { + lg.info("Die Verbindung zum Client ist Fehlgeschlagen"); + + } + } + + dialogFenster.setVisible(false); + } + + @Override + public boolean isUndoable() + { + return false; + } + + @Override + public void undo() + { + } +} \ No newline at end of file diff --git a/src/ChatProgramm/controller/commands/CommandInterface.java b/src/ChatProgramm/controller/commands/CommandInterface.java new file mode 100644 index 0000000..77bd988 --- /dev/null +++ b/src/ChatProgramm/controller/commands/CommandInterface.java @@ -0,0 +1,17 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package ChatProgramm.controller.commands; + +/** + * + * @author ahren + */ +public interface CommandInterface +{ + public void execute(); + public void undo(); + public boolean isUndoable(); +} diff --git a/src/ChatProgramm/controller/commands/CommandInvoker.java b/src/ChatProgramm/controller/commands/CommandInvoker.java new file mode 100644 index 0000000..5162f05 --- /dev/null +++ b/src/ChatProgramm/controller/commands/CommandInvoker.java @@ -0,0 +1,69 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package ChatProgramm.controller.commands; + +import java.awt.Component; +import java.util.HashMap; +import java.util.Stack; + +/** + * + * @author ahren + */ +public class CommandInvoker { + + private HashMap commands; + private Stack undoStack; + + public CommandInvoker(){ + commands = new HashMap<>(); + undoStack = new Stack(); + } + + /** + * Fügt ein Kommando zur Kommando-"Datenbank" = HashMap hinzu + * @param key Quelle des Events + * @param value Referenz auf das zugehörige Kommando-Objekt + */ + public void addCommand(Component key, CommandInterface value){ + commands.put(key, value); + } + + /** + * Führt Kommando der Eventquelle "key" aus und legt die Referenz + * des Kommando in den Undo-Stack + * @param key Referenz auf die Eventquelle + */ + public void executeCommand(Component key){ + CommandInterface command = commands.get(key); + command.execute(); + if (command.isUndoable()) + { + undoStack.push(command); + } + } + /** + * Falls der Stack Elemente enthält, wird das oberste Element geholt + * und die Methode "undo" des Commands aufgerufen + */ + public void undoCommand() + { + if (!undoStack.isEmpty()) + { + undoStack.pop().undo(); + } + } + + /** + * Löscht bei Öffnen einer neuen Datei den Stack + */ + public void deleteStack() + { + while(!undoStack.isEmpty()) + undoStack.pop(); + } + +} diff --git a/src/ChatProgramm/controller/commands/CommandSend.java b/src/ChatProgramm/controller/commands/CommandSend.java new file mode 100644 index 0000000..38c4627 --- /dev/null +++ b/src/ChatProgramm/controller/commands/CommandSend.java @@ -0,0 +1,61 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package ChatProgramm.controller.commands; + +import ChatProgramm.model.Client; +import ChatProgramm.model.Server; +import ChatProgramm.model.TransmitterInterface; +import ChatProgramm.util.OhmLogger; +import ChatProgramm.view.ChatView; +import java.util.logging.Logger; +import javax.swing.JTextField; +import ChatProgramm.model.Nachricht; + +/** + * + * @author ahren + */ +public class CommandSend implements CommandInterface +{ + private static Logger lg = OhmLogger.getLogger(); + private JTextField eingabeFeld; + private String nachricht; + private ChatView view; + public TransmitterInterface transmitterInterface; + public Server server; + public Client client; + + + + + public CommandSend(ChatView view) + { + this.view = view; + this.eingabeFeld = view.getTfNachricht(); + transmitterInterface = null; + } + + @Override + public void execute() + { + if(transmitterInterface != null && !eingabeFeld.getText().isEmpty()){ + transmitterInterface.send(eingabeFeld.getText()); + eingabeFeld.setText(""); + } + } + + + @Override + public boolean isUndoable() + { + return false; + } + + @Override + public void undo() + { + } +} diff --git a/src/ChatProgramm/model/Client.java b/src/ChatProgramm/model/Client.java new file mode 100644 index 0000000..4921a13 --- /dev/null +++ b/src/ChatProgramm/model/Client.java @@ -0,0 +1,41 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template + */ +package ChatProgramm.model; + +import ChatProgramm.view.ChatView; +import java.io.IOException; +import java.net.Socket; +import java.util.logging.*; + +/** + * Builder Class + * + * @author le + */ +public class Client extends Transmitter { + + private static Logger lg = Logger.getLogger("netz"); + private static final int PORT = 35000; //lt. iana port > 2¹⁵ + private static final String IP = "127.0.0.1"; + + + public Client(ChatView view) throws IOException { + super(view); + connect(); + initIO(); + } + + + @Override + public void connect() throws IOException { + try { + lg.info("Client: Verbindung wird aufgebaut"); + socket = new Socket(IP, PORT); + lg.info("Client: Verbindung aufgebaut"); + } catch (java.io.InterruptedIOException e) { + lg.warning("Timeout" + "(" + timeout / 1000 + "s)"); + } + } +} diff --git a/src/ChatProgramm/model/Nachricht.java b/src/ChatProgramm/model/Nachricht.java new file mode 100644 index 0000000..12a7270 --- /dev/null +++ b/src/ChatProgramm/model/Nachricht.java @@ -0,0 +1,31 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + +package ChatProgramm.model; + +/** + * + * @author ahren + */ +public class Nachricht +{ + private String nachricht; + + public Nachricht(String nachricht) + { + this.nachricht = nachricht; + } + + /** + * @return the nachricht + */ + public String getNachricht() { + return nachricht; + } + + public void setNachricht(String nachricht) { + this.nachricht = nachricht; + } +} \ No newline at end of file diff --git a/src/ChatProgramm/model/ReceiveAdapter.java b/src/ChatProgramm/model/ReceiveAdapter.java new file mode 100644 index 0000000..20dd7de --- /dev/null +++ b/src/ChatProgramm/model/ReceiveAdapter.java @@ -0,0 +1,44 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package ChatProgramm.model; + +import ChatProgramm.view.ChatView; +import java.util.concurrent.Flow; +import java.util.concurrent.Flow.Subscriber; + +/** + * + * @author ahren + */ +public class ReceiveAdapter implements Subscriber { + + private ChatView view; + private Flow.Subscription subscription; + + public ReceiveAdapter(ChatView view) { + this.view = view; + } + + @Override + public void onSubscribe(Flow.Subscription subscription) { + this.subscription = subscription; + this.subscription.request(1); + } + + @Override + public void onNext(Nachricht item) { + + view.getTxtChat().append(item.getNachricht()); + this.subscription.request(1); + } + + @Override + public void onError(Throwable throwable) { + } + + @Override + public void onComplete(){ + } +} diff --git a/src/ChatProgramm/model/Server.java b/src/ChatProgramm/model/Server.java new file mode 100644 index 0000000..06a88ac --- /dev/null +++ b/src/ChatProgramm/model/Server.java @@ -0,0 +1,45 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template + */ + +package ChatProgramm.model; + +import ChatProgramm.view.ChatView; +import java.io.IOException; +import java.net.ServerSocket; +import java.util.logging.*; + +/** + * Builder Class + * @author le + */ +public class Server extends Transmitter +{ + private static Logger lg = Logger.getLogger("netz"); + private static final int PORT = 35000; //lt. iana port > 2¹⁵ + + + public void connect() throws IOException + { + try + { + ServerSocket sSocket = new ServerSocket(PORT); + sSocket.setSoTimeout(timeout); + lg.info("Server: warte auf Verbindung"); + socket = sSocket.accept(); + lg.info("Server: Verbindung akzeptiert"); + } + catch ( java.io.InterruptedIOException e ) + { + lg.warning("Timeout"+"("+timeout/1000+"s)"); + } + } + public Server(ChatView view) throws IOException { + super(view); + connect(); + initIO(); + + + } +} diff --git a/src/ChatProgramm/model/Transmitter.java b/src/ChatProgramm/model/Transmitter.java new file mode 100644 index 0000000..62d5191 --- /dev/null +++ b/src/ChatProgramm/model/Transmitter.java @@ -0,0 +1,166 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package ChatProgramm.model; + +import ChatProgramm.view.ChatView; +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.io.UnsupportedEncodingException; +import java.net.Socket; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Flow; +import java.util.concurrent.Flow.Subscriber; +import java.util.concurrent.SubmissionPublisher; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author ahren + */ +public abstract class Transmitter implements Runnable, Subscriber, TransmitterInterface { + + static final int timeout = 60000; + private static final int PORT = 35000; + + private static Logger lg = Logger.getLogger("netz"); + + protected Socket socket; + protected BufferedReader reader; + protected PrintWriter writer; + + private Nachricht nachricht; + private String txtNachricht; + private boolean laufend; + private SubmissionPublisher textPublisher; + private ExecutorService eService; + private String receivedString; + private ChatView view; + private ReceiveAdapter receiveAdapter; + + public Transmitter(ChatView view) + { + socket = new Socket(); + eService = null; + receiveAdapter = new ReceiveAdapter(view); + textPublisher = new SubmissionPublisher<>(); + this.view = view; + addWertSubscription(receiveAdapter); + nachricht = new Nachricht(""); + } + + public void addWertSubscription(Subscriber subscriber) + { + textPublisher.subscribe(subscriber); + } + + public abstract void connect() throws IOException; + + public void initIO() { + try { + lg.info("Initialisiere reader und writer"); + InputStream is = socket.getInputStream(); + OutputStream os = socket.getOutputStream(); + + InputStreamReader isr = new InputStreamReader(is, "UTF-8"); + OutputStreamWriter osr = new OutputStreamWriter(os, "UTF-8"); + + reader = new BufferedReader(isr); + writer = new PrintWriter(osr); + lg.info("Reader / Writer Initialisierung abgeschlossen"); + startempfangen(); + lg.info("Warte auf Nachricht"); + + } catch (UnsupportedEncodingException ex) { + Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); + } catch (IOException ex) { + Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public void send(String txtNachricht){ + + writer.println(txtNachricht); + writer.flush(); + lg.info("Nachricht gesendet"); + nachricht.setNachricht("\nDu: " + txtNachricht); + textPublisher.submit(nachricht); + } + public Nachricht receive(){ + try { + txtNachricht = reader.readLine(); + if(!txtNachricht.isEmpty()){ + lg.info("Nachricht erhalten"); + nachricht.setNachricht("\nEr / Sie: " + txtNachricht); + return nachricht; + } + + } catch (IOException e) { + throw new RuntimeException(e); + } + return nachricht; + } + +// public void disconnect (){ +// in.close(); +// out.close(); +// s.close(); +// } + + @Override + public void run() { + while (true) { + lg.info("Warte auf Nachricht"); + if(laufend) { + nachricht = receive(); + if(!nachricht.getNachricht().isEmpty()){ + textPublisher.submit(nachricht); + } + } + else{ + break; + } + } + } + + + @Override + public void onSubscribe(Flow.Subscription subscription) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public void onNext(String item) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public void onError(Throwable throwable) { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public void onComplete() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + private void startempfangen() + { + synchronized (this){ + laufend = true; + } + if (eService == null){ + eService = Executors.newSingleThreadExecutor(); + eService.execute(this); + } + lg.info("Starte Chat"); + } +} diff --git a/src/ChatProgramm/model/TransmitterInterface.java b/src/ChatProgramm/model/TransmitterInterface.java new file mode 100644 index 0000000..2cacc1b --- /dev/null +++ b/src/ChatProgramm/model/TransmitterInterface.java @@ -0,0 +1,17 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template + */ +package ChatProgramm.model; + +import ChatProgramm.model.Nachricht; +/** + * + * @author ahren + */ +public interface TransmitterInterface +{ + public void send(String txtNachricht); + public Nachricht receive(); + +} diff --git a/src/ChatProgramm/util/OhmLogger.java b/src/ChatProgramm/util/OhmLogger.java new file mode 100644 index 0000000..f0e32da --- /dev/null +++ b/src/ChatProgramm/util/OhmLogger.java @@ -0,0 +1,111 @@ +package ChatProgramm.util; + +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + + + +import java.io.IOException; +import java.io.InputStream; +import java.time.LocalDateTime; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +//import java.util.Properties; +import java.util.logging.*; + +/** + * Klasse zur eigens konfigurierten Log-Ausgabe in der Console + * @author ahrens + */ +public class OhmLogger +{ + private static final String LOGGER_NAME = "OhmLogger"; + private static Logger lg = null; + + /** + * privater Konstrukter -> Singleton + */ + private OhmLogger() + { + } + + /** + * Public Method zum Erstellen des Loggers und zum Aufruf der Methode + * zum Initialisieren + * @return Referenz auf Logger + */ + public static Logger getLogger() + { + if (lg == null) + { + lg = Logger.getLogger(LOGGER_NAME); + initLogger(); + } + return lg; + } + + /** + * Methode zum Initialisieren, Suchen der Konfigurationsdatei, Anlegen eines + * neuen Console Handlers, Löschen der bisherigen Standard Handler und + * Einfügen des eigenen Handler, Setzen des Levels, je nach Properties + */ + private static void initLogger() + { + try (InputStream configFile = OhmLogger.class.getClassLoader().getResourceAsStream("config.properties")) { + if (configFile != null) { + Properties properties = new Properties(); + properties.load(configFile); + java.util.logging.Level classLogLevel = java.util.logging.Level.parse(properties.getProperty("log.level")); + + + ConsoleHandler ch = new ConsoleHandler(); + ch.setFormatter(new OhmFormatter()); + lg.setUseParentHandlers(false); + lg.getHandlers(); + lg.addHandler(ch); + + + lg.setLevel(classLogLevel); + + String logDirectory = properties.getProperty("log.directory"); + String logFileName = properties.getProperty("log.filename"); + + FileHandler fileHandler = new FileHandler(logDirectory + "/" + logFileName, true); + fileHandler.setFormatter(new OhmFormatter()); + lg.addHandler(fileHandler); + + + } else { + System.err.println("Unable to find config.properties file. OhmLogger will use default settings."); + } + } catch (IOException | SecurityException e) { + System.err.println("Error configuring OhmLogger: " + e.getMessage()); + } + + } +} + +/** + * Klasse zum Setzen des Formates des Auszugebenden Log-Strings + * @author ahren + */ +class OhmFormatter extends Formatter +{ + @Override + public String format(LogRecord record) + { + String logline = "| "; + + LocalDateTime ldt = LocalDateTime.now(); + logline += ldt.toString(); + logline += " | " + record.getLevel(); + logline += " | " + record.getSourceClassName(); + logline += " | " + record.getMessage(); + logline += " |\n"; + + return logline; + } +} \ No newline at end of file diff --git a/src/ChatProgramm/view/ChatView.form b/src/ChatProgramm/view/ChatView.form new file mode 100644 index 0000000..6ad137b --- /dev/null +++ b/src/ChatProgramm/view/ChatView.form @@ -0,0 +1,151 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ChatProgramm/view/ChatView.java b/src/ChatProgramm/view/ChatView.java new file mode 100644 index 0000000..08ad619 --- /dev/null +++ b/src/ChatProgramm/view/ChatView.java @@ -0,0 +1,285 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package ChatProgramm.view; + +/** + * + * @author ahren + */ +public class ChatView extends javax.swing.JFrame +{ + + /** + * 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") + // //GEN-BEGIN:initComponents + private void initComponents() + { + + dialogChooseMode = new javax.swing.JDialog(); + lblMode = new javax.swing.JLabel(); + BtnServer = new javax.swing.JRadioButton(); + BtnClient = new javax.swing.JRadioButton(); + jPanel1 = new javax.swing.JPanel(); + BtnConnect = new javax.swing.JButton(); + BtnGrpMode = new javax.swing.ButtonGroup(); + tfNachricht = new javax.swing.JTextField(); + jScrollPane2 = new javax.swing.JScrollPane(); + txtChat = new javax.swing.JTextArea(); + + dialogChooseMode.setMinimumSize(new java.awt.Dimension(278, 208)); + dialogChooseMode.getContentPane().setLayout(new java.awt.GridLayout(0, 1)); + + lblMode.setText(" Choose your mode:"); + dialogChooseMode.getContentPane().add(lblMode); + + BtnGrpMode.add(BtnServer); + BtnServer.setText("Server"); + BtnServer.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { + BtnServerActionPerformed(evt); + } + }); + dialogChooseMode.getContentPane().add(BtnServer); + + BtnGrpMode.add(BtnClient); + BtnClient.setText("Client"); + BtnClient.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { + BtnClientActionPerformed(evt); + } + }); + dialogChooseMode.getContentPane().add(BtnClient); + + BtnConnect.setText("Connect"); + BtnConnect.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { + BtnConnectActionPerformed(evt); + } + }); + + javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); + jPanel1.setLayout(jPanel1Layout); + jPanel1Layout.setHorizontalGroup( + jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel1Layout.createSequentialGroup() + .addContainerGap(65, Short.MAX_VALUE) + .addComponent(BtnConnect) + .addContainerGap(76, Short.MAX_VALUE)) + ); + jPanel1Layout.setVerticalGroup( + jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel1Layout.createSequentialGroup() + .addContainerGap() + .addComponent(BtnConnect) + .addContainerGap(57, Short.MAX_VALUE)) + ); + + dialogChooseMode.getContentPane().add(jPanel1); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setMinimumSize(new java.awt.Dimension(568, 548)); + addWindowListener(new java.awt.event.WindowAdapter() + { + public void windowOpened(java.awt.event.WindowEvent evt) + { + formWindowOpened(evt); + } + }); + + tfNachricht.setToolTipText(""); + tfNachricht.addMouseListener(new java.awt.event.MouseAdapter() + { + public void mouseClicked(java.awt.event.MouseEvent evt) + { + tfNachrichtMouseClicked(evt); + } + }); + tfNachricht.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(java.awt.event.ActionEvent evt) + { + tfNachrichtActionPerformed(evt); + } + }); + getContentPane().add(tfNachricht, java.awt.BorderLayout.PAGE_END); + + jScrollPane2.setPreferredSize(new java.awt.Dimension(238, 150)); + + txtChat.setColumns(20); + txtChat.setRows(5); + txtChat.setText("Chat:"); + jScrollPane2.setViewportView(txtChat); + + getContentPane().add(jScrollPane2, java.awt.BorderLayout.CENTER); + + pack(); + }// //GEN-END:initComponents + + private void BtnClientActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_BtnClientActionPerformed + {//GEN-HEADEREND:event_BtnClientActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_BtnClientActionPerformed + + private void BtnServerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_BtnServerActionPerformed + {//GEN-HEADEREND:event_BtnServerActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_BtnServerActionPerformed + + private void tfNachrichtActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_tfNachrichtActionPerformed + {//GEN-HEADEREND:event_tfNachrichtActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_tfNachrichtActionPerformed + + private void formWindowOpened(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowOpened + {//GEN-HEADEREND:event_formWindowOpened + dialogChooseMode.show(); + }//GEN-LAST:event_formWindowOpened + + private void tfNachrichtMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tfNachrichtMouseClicked + {//GEN-HEADEREND:event_tfNachrichtMouseClicked + + }//GEN-LAST:event_tfNachrichtMouseClicked + + private void BtnConnectActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_BtnConnectActionPerformed + {//GEN-HEADEREND:event_BtnConnectActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_BtnConnectActionPerformed + + /** + * @param args the command line arguments + */ + public static void main(String args[]) + { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try + { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) + { + if ("Nimbus".equals(info.getName())) + { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } + catch (ClassNotFoundException ex) + { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (InstantiationException ex) + { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (IllegalAccessException ex) + { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + catch (javax.swing.UnsupportedLookAndFeelException ex) + { + java.util.logging.Logger.getLogger(ChatView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() + { + public void run() + { + new ChatView().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JRadioButton BtnClient; + private javax.swing.JButton BtnConnect; + private javax.swing.ButtonGroup BtnGrpMode; + private javax.swing.JRadioButton BtnServer; + private javax.swing.JDialog dialogChooseMode; + private javax.swing.JPanel jPanel1; + private javax.swing.JScrollPane jScrollPane2; + private javax.swing.JLabel lblMode; + private javax.swing.JTextField tfNachricht; + private javax.swing.JTextArea txtChat; + // End of variables declaration//GEN-END:variables + + /** + * @return the BtnClient + */ + public javax.swing.JRadioButton getBtnClient() + { + return BtnClient; + } + + /** + * @return the BtnConnect + */ + public javax.swing.JButton getBtnConnect() + { + return BtnConnect; + } + + /** + * @return the BtnGrpMode + */ + public javax.swing.ButtonGroup getBtnGrpMode() + { + return BtnGrpMode; + } + + /** + * @return the BtnServer + */ + public javax.swing.JRadioButton getBtnServer() + { + return BtnServer; + } + + /** + * @return the jDialog1 + */ + public javax.swing.JDialog getjDialog1() + { + return dialogChooseMode; + } + + /** + * @return the tfNachricht + */ + public javax.swing.JTextField getTfNachricht() + { + return tfNachricht; + } + + /** + * @return the txtChat + */ + public javax.swing.JTextArea getTxtChat() + { + return txtChat; + } +} diff --git a/src/config.properties b/src/config.properties new file mode 100644 index 0000000..b96e11a --- /dev/null +++ b/src/config.properties @@ -0,0 +1,8 @@ +log.level=INFO + +log.directory=../Aufgabe7_bandit +log.filename=ohmLog + +# Beispiel f\u00fcr verschiedene Level f\u00fcr unterschiedliche Pakete +# log.level.bandit.Zahlengenerator=FINE +# log.level.bandit.Model.Wuerfel=WARNING