InitIO klappt und wir landen in der senden Funktion

This commit is contained in:
Jens Schuhmann 2023-12-18 16:55:17 +01:00
parent f05b0f0014
commit 6642dc8179
5 changed files with 38 additions and 17 deletions

View File

@ -26,7 +26,7 @@ public class Start
GrafikView zeichenflaeche = view.getGvZeichenflaeche(); GrafikView zeichenflaeche = view.getGvZeichenflaeche();
zeichenflaeche.setModel(model); zeichenflaeche.setModel(model);
CommandController controller_commands = new CommandController(view); CommandController controller_commands = new CommandController(view, model);
controller_commands.registerEvents(); controller_commands.registerEvents();
controller_commands.registerCommands(); controller_commands.registerCommands();

View File

@ -8,6 +8,7 @@ package ChatProgramm.controller;
import ChatProgramm.controller.commands.CommandConnect; import ChatProgramm.controller.commands.CommandConnect;
import ChatProgramm.controller.commands.CommandInvoker; import ChatProgramm.controller.commands.CommandInvoker;
import ChatProgramm.controller.commands.CommandSend; import ChatProgramm.controller.commands.CommandSend;
import ChatProgramm.model.GrafikModel;
import ChatProgramm.view.ChatView; import ChatProgramm.view.ChatView;
import java.awt.Component; import java.awt.Component;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@ -20,10 +21,12 @@ import java.awt.event.ActionListener;
public class CommandController implements ActionListener{ public class CommandController implements ActionListener{
private ChatView view; private ChatView view;
private GrafikModel model;
private CommandInvoker invoker; private CommandInvoker invoker;
public CommandController(ChatView view){ public CommandController(ChatView view, GrafikModel model){
this.view = view; this.view = view;
this.model = model;
this.invoker = new CommandInvoker(); this.invoker = new CommandInvoker();
} }
@ -34,7 +37,7 @@ public class CommandController implements ActionListener{
} }
public void registerCommands(){ public void registerCommands(){
CommandSend commandSend = new CommandSend(view.getGvZeichenflaeche()); CommandSend commandSend = new CommandSend(view.getGvZeichenflaeche(), model);
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, commandSend)); invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, commandSend));
//invoker.addCommand(view.getTfNachricht(), commandSend); //invoker.addCommand(view.getTfNachricht(), commandSend);
} }

View File

@ -27,7 +27,7 @@ public class GrafikController extends MouseAdapter implements MouseMotionListene
{ {
this.view = view; this.view = view;
this.model = model; this.model = model;
commandSend = new CommandSend(view); commandSend = new CommandSend(view, model);
} }
public void registerEvents() public void registerEvents()

View File

@ -6,6 +6,8 @@
package ChatProgramm.controller.commands; package ChatProgramm.controller.commands;
import ChatProgramm.model.Client; import ChatProgramm.model.Client;
import ChatProgramm.model.Figur;
import ChatProgramm.model.GrafikModel;
import ChatProgramm.model.Server; import ChatProgramm.model.Server;
import ChatProgramm.model.TransmitterInterface; import ChatProgramm.model.TransmitterInterface;
import ChatProgramm.util.OhmLogger; import ChatProgramm.util.OhmLogger;
@ -25,6 +27,7 @@ public class CommandSend implements CommandInterface
private JTextField eingabeFeld; private JTextField eingabeFeld;
private String nachricht; private String nachricht;
private GrafikView view; private GrafikView view;
private GrafikModel model;
public TransmitterInterface transmitterInterface; public TransmitterInterface transmitterInterface;
public Server server; public Server server;
public Client client; public Client client;
@ -32,9 +35,10 @@ public class CommandSend implements CommandInterface
public CommandSend(GrafikView view) public CommandSend(GrafikView view, GrafikModel model)
{ {
this.view = view; this.view = view;
this.model = model;
//ToDo: Hier muss auch der gFrame referenziert werden //ToDo: Hier muss auch der gFrame referenziert werden
//this.eingabeFeld = view.getTfNachricht(); //this.eingabeFeld = view.getTfNachricht();
transmitterInterface = null; transmitterInterface = null;
@ -44,6 +48,8 @@ public class CommandSend implements CommandInterface
public void execute() public void execute()
{ {
lg.info("wir sind drin"); lg.info("wir sind drin");
Figur aktuelleFigur = model.getFiguren().getLast();
transmitterInterface.send(aktuelleFigur);
//ToDo in dieser methode muss die Figur serialisiert werden und zum //ToDo in dieser methode muss die Figur serialisiert werden und zum
//übermitteln bereitgestellt werden //übermitteln bereitgestellt werden

View File

@ -30,7 +30,7 @@ import java.util.logging.Logger;
* *
* @author ahren * @author ahren
*/ */
public abstract class Transmitter implements Runnable, Subscriber<String>, TransmitterInterface { public abstract class Transmitter implements Runnable, Subscriber<Figur>, TransmitterInterface {
static final int timeout = 60000; static final int timeout = 60000;
private static final int PORT = 35000; private static final int PORT = 35000;
@ -73,17 +73,14 @@ public abstract class Transmitter implements Runnable, Subscriber<String>, Trans
lg.info("Initialisiere reader und writer"); lg.info("Initialisiere reader und writer");
InputStream is = socket.getInputStream(); InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream(); OutputStream os = socket.getOutputStream();
lg.info("1");
BufferedOutputStream bos = new BufferedOutputStream(os);
BufferedInputStream bis = new BufferedInputStream(is);
lg.info("2");
// InputStreamReader isr = new InputStreamReader(is, "UTF-8");
// OutputStreamWriter osr = new OutputStreamWriter(os, "UTF-8");
reader = new ObjectInputStream(bis); // Bruh im ernst mann muss zuerst den writer und dann den reader initialisieren
writer = new ObjectOutputStream(bos); // andersrum ist das blockierend weil die Streams von hinten nach vorne
// gelesen werden
writer = new ObjectOutputStream(os);
writer.flush();
reader = new ObjectInputStream(is);
lg.info("Reader / Writer Initialisierung abgeschlossen"); lg.info("Reader / Writer Initialisierung abgeschlossen");
startempfangen(); startempfangen();
lg.info("Warte auf Nachricht"); lg.info("Warte auf Nachricht");
@ -115,6 +112,21 @@ public abstract class Transmitter implements Runnable, Subscriber<String>, Trans
figurPublisher.submit(figur); figurPublisher.submit(figur);
} }
public Figur receive(){ public Figur receive(){
Object receivedObject;
try {
receivedObject = reader.readObject();
if (receivedObject instanceof Figur) {
Figur receivedFigur = (Figur) receivedObject;
// Verarbeiten Sie die empfangene Figur
}
} catch (IOException ex) {
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
}
try { try {
figur = (Figur) reader.readObject(); figur = (Figur) reader.readObject();
if(!txtNachricht.isEmpty()){ if(!txtNachricht.isEmpty()){
@ -162,7 +174,7 @@ public abstract class Transmitter implements Runnable, Subscriber<String>, Trans
} }
@Override @Override
public void onNext(String item) { public void onNext(Figur item) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
} }