From 45e094d635395d5b1557bad5329f95747aafcb72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20D=C3=BCrr?= Date: Wed, 16 Dec 2020 09:49:56 +0100 Subject: [PATCH] add missing controller package --- src/netz/controller/CommandConnect.java | 81 +++++++++++++++++++++++++ src/netz/controller/CommandSend.java | 52 ++++++++++++++++ src/netz/controller/ReceiveAdapter.java | 63 +++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 src/netz/controller/CommandConnect.java create mode 100644 src/netz/controller/CommandSend.java create mode 100644 src/netz/controller/ReceiveAdapter.java diff --git a/src/netz/controller/CommandConnect.java b/src/netz/controller/CommandConnect.java new file mode 100644 index 0000000..68db13a --- /dev/null +++ b/src/netz/controller/CommandConnect.java @@ -0,0 +1,81 @@ +/* + * 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 netz.controller; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.logging.Logger; +import ohmlogger.OhmLogger; +import netz.model.ChatModel; +import netz.view.ChatView; + +/** + * + * @author hd, chris + */ +public class CommandConnect implements ActionListener +{ + + private static Logger lg = OhmLogger.getLogger(); + ChatView view; + ChatModel model; + + private static final int PORT = 35000; + private static final String IP_ADRESSE = "127.0.0.1"; + + + public CommandConnect(ChatView view, ChatModel transmitter) + { + this.view = view; + this.model = transmitter; + } + + public void registerEvents(){ + view.getBtnSetServer().addActionListener(this); + view.getBtnSetClient().addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) { + + Object src = e.getSource(); + + // connect to server + if(src == view.getBtnSetServer()){ + synchronized (this){ + try { + view.getLblType().setText("Server"); + ServerSocket sSocket = new ServerSocket(PORT); + lg.info("Server: Warte auf Verbindung ..."); + Socket s = sSocket.accept(); // Achtung: blockiert! + lg.info("Server: Verbindung akzeptiert"); + model.setSocket(s); + } catch (IOException ex) { + lg.severe(ex.toString()); + } + } + } + + // connect to vlient + if(src == view.getBtnSetClient()){ + synchronized (this){ + try { + view.getLblType().setText("Client"); + lg.info("Client: verbinde ..."); + Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert! + lg.info("Client: Verbindung hergestellt"); + model.setSocket(s); + } catch (IOException ex) { + lg.severe(ex.toString()); + } + } + } + } +} diff --git a/src/netz/controller/CommandSend.java b/src/netz/controller/CommandSend.java new file mode 100644 index 0000000..86b29d7 --- /dev/null +++ b/src/netz/controller/CommandSend.java @@ -0,0 +1,52 @@ +/* + * 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 netz.controller; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.logging.Logger; +import ohmlogger.OhmLogger; +import netz.model.ChatModel; +import netz.view.ChatView; + +/** + * + * @author hd, christ + */ +public class CommandSend implements ActionListener +{ + private static Logger lg = OhmLogger.getLogger(); + ChatView view; + ChatModel model; + + public CommandSend(ChatView view, ChatModel model) + { + this.view = view; + this.model = model; + } + + + public void registerEvents(){ + view.getBtnSend().addActionListener(this); + } + + @Override + public void actionPerformed(ActionEvent e) + { + Object src = e.getSource(); + + if(src == view.getBtnSend()){ + String msg = view.getTxtField().getText(); + if(msg != ""){ + model.sendMessage(msg); + view.getLblStatusDialog().setText("Nachricht gesendet"); + view.getjTextArea().append("Ich: " + msg +"\n"); + } + view.getTxtField().setText(""); + } + } +} diff --git a/src/netz/controller/ReceiveAdapter.java b/src/netz/controller/ReceiveAdapter.java new file mode 100644 index 0000000..339436a --- /dev/null +++ b/src/netz/controller/ReceiveAdapter.java @@ -0,0 +1,63 @@ +/* + * 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 netz.controller; + +import java.util.concurrent.Flow; +import java.util.logging.Logger; +import ohmlogger.OhmLogger; +import netz.model.ChatModel; +import netz.view.ChatView; + +/** + * + * @author hd, chris + */ + +public class ReceiveAdapter implements Flow.Subscriber +{ + private static Logger lg = OhmLogger.getLogger(); + + private ChatView view; + private ChatModel model; + private Flow.Subscription subscription; + + public ReceiveAdapter(ChatView view, ChatModel model) + { + this.view = view; + this.model = model; + } + + public void subscribe() + { + model.addSubscription(this); + } + + @Override + public void onSubscribe(Flow.Subscription subscription) + { + this.subscription = subscription; + subscription.request(1); + } + + @Override + public void onNext(String item) + { + view.getLblStatusDialog().setText("Nachricht empfangen"); + view.getjTextArea().append("Partner: " + item + "\n"); + //System.out.println(item); + } + + @Override + public void onError(Throwable throwable) + { + } + + @Override + public void onComplete() + { + } +}