package ChatProgramm; | package ChatProgramm; | ||||
import ChatProgramm.controller.CommandController; | import ChatProgramm.controller.CommandController; | ||||
import ChatProgramm.model.Client; | |||||
import ChatProgramm.model.Server; | |||||
import ChatProgramm.model.Transmitter; | |||||
import ChatProgramm.view.ChatView; | import ChatProgramm.view.ChatView; | ||||
import javax.swing.JOptionPane; | import javax.swing.JOptionPane; | ||||
import javax.swing.UIManager; | import javax.swing.UIManager; | ||||
{ | { | ||||
//Transmitter transmitter = new Transmitter(); | //Transmitter transmitter = new Transmitter(); | ||||
ChatView view = new ChatView(); | ChatView view = new ChatView(); | ||||
Server server = new Server(); | |||||
Client client = new Client(); | |||||
//CommandController controller_commands = new CommandController(view, transmitter); | //CommandController controller_commands = new CommandController(view, transmitter); | ||||
CommandController controller_commands = new CommandController(view, server, client); | |||||
CommandController controller_commands = new CommandController(view); | |||||
controller_commands.registerEvents(); | controller_commands.registerEvents(); | ||||
controller_commands.registerCommands(); | controller_commands.registerCommands(); | ||||
view.setVisible(true); | view.setVisible(true); |
private ChatView view; | private ChatView view; | ||||
//private Transmitter transmitter; | //private Transmitter transmitter; | ||||
private CommandInvoker invoker; | private CommandInvoker invoker; | ||||
private Server server; | |||||
private Client client; | |||||
// public CommandController(ChatView view, Transmitter transmitter){ | // public CommandController(ChatView view, Transmitter transmitter){ | ||||
// this.view = view; | // this.view = view; | ||||
// this.invoker = new CommandInvoker(); | // this.invoker = new CommandInvoker(); | ||||
// } | // } | ||||
public CommandController(ChatView view, Server server, Client client){ | |||||
public CommandController(ChatView view){ | |||||
this.view = view; | this.view = view; | ||||
this.invoker = new CommandInvoker(); | this.invoker = new CommandInvoker(); | ||||
this.client = client; | |||||
this.server = server; | |||||
} | } | ||||
public void registerEvents(){ | public void registerEvents(){ | ||||
} | } | ||||
public void registerCommands(){ | public void registerCommands(){ | ||||
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, server, client)); | |||||
invoker.addCommand(view.getTfNachricht(), new CommandSend(view, server, client)); | |||||
CommandSend commandSend = new CommandSend(view); | |||||
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, commandSend)); | |||||
invoker.addCommand(view.getTfNachricht(), commandSend); | |||||
} | } | ||||
/** | /** |
import ChatProgramm.util.OhmLogger; | import ChatProgramm.util.OhmLogger; | ||||
import ChatProgramm.view.ChatView; | import ChatProgramm.view.ChatView; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.logging.Level; | |||||
import java.util.logging.Logger; | import java.util.logging.Logger; | ||||
import javax.swing.JDialog; | import javax.swing.JDialog; | ||||
import javax.swing.JRadioButton; | import javax.swing.JRadioButton; | ||||
private JRadioButton rBtnClient; | private JRadioButton rBtnClient; | ||||
private JDialog dialogFenster; | private JDialog dialogFenster; | ||||
private static Logger lg = OhmLogger.getLogger(); | private static Logger lg = OhmLogger.getLogger(); | ||||
private final Client client; | |||||
private final Server server; | |||||
private CommandSend commandSend; | |||||
private ChatView view; | |||||
public CommandConnect(ChatView view, Server server, Client client) | |||||
public CommandConnect(ChatView view, CommandInterface value) | |||||
{ | { | ||||
rBtnServer = view.getBtnServer(); | rBtnServer = view.getBtnServer(); | ||||
rBtnClient = view.getBtnClient(); | rBtnClient = view.getBtnClient(); | ||||
dialogFenster = view.getjDialog1(); | dialogFenster = view.getjDialog1(); | ||||
this.client = client; | |||||
this.server = server; | |||||
commandSend = (CommandSend) value; | |||||
this.view = view; | |||||
} | } | ||||
@Override | @Override | ||||
if(rBtnServer.isSelected()){ | if(rBtnServer.isSelected()){ | ||||
lg.info("Server ausgewählt"); | lg.info("Server ausgewählt"); | ||||
try { | try { | ||||
server.connect(); | |||||
commandSend.transmitterInterface = new Server(view); | |||||
} catch (IOException ex) { | } catch (IOException ex) { | ||||
lg.info("Die Verbindung zum Server ist Fehlgeschlagen"); | lg.info("Die Verbindung zum Server ist Fehlgeschlagen"); | ||||
} | } | ||||
if(rBtnClient.isSelected()){ | if(rBtnClient.isSelected()){ | ||||
lg.info("Client ausgewählt"); | lg.info("Client ausgewählt"); | ||||
try { | try { | ||||
client.connect(); | |||||
commandSend.transmitterInterface = new Client(view); | |||||
} catch (IOException ex) { | } catch (IOException ex) { | ||||
lg.info("Die Verbindung zum Client ist Fehlgeschlagen"); | lg.info("Die Verbindung zum Client ist Fehlgeschlagen"); | ||||
public void undo() | public void undo() | ||||
{ | { | ||||
} | } | ||||
} | |||||
} |
import ChatProgramm.model.Client; | import ChatProgramm.model.Client; | ||||
import ChatProgramm.model.Server; | import ChatProgramm.model.Server; | ||||
import ChatProgramm.model.TransmitterInterface; | |||||
import ChatProgramm.util.OhmLogger; | import ChatProgramm.util.OhmLogger; | ||||
import ChatProgramm.view.ChatView; | import ChatProgramm.view.ChatView; | ||||
import java.util.logging.Logger; | import java.util.logging.Logger; | ||||
import javax.swing.JTextField; | import javax.swing.JTextField; | ||||
import ChatProgramm.model.Nachricht; | |||||
/** | /** | ||||
* | * | ||||
public class CommandSend implements CommandInterface | public class CommandSend implements CommandInterface | ||||
{ | { | ||||
private static Logger lg = OhmLogger.getLogger(); | private static Logger lg = OhmLogger.getLogger(); | ||||
private JTextField tfNachricht; | |||||
private JTextField eingabeFeld; | |||||
private String nachricht; | private String nachricht; | ||||
private final Client client; | |||||
private final Server server; | |||||
private ChatView view; | |||||
public TransmitterInterface transmitterInterface; | |||||
public Server server; | |||||
public Client client; | |||||
public CommandSend(ChatView view, Server server, Client client) | |||||
public CommandSend(ChatView view) | |||||
{ | { | ||||
tfNachricht = view.getTfNachricht(); | |||||
nachricht = tfNachricht.getText(); | |||||
this.client = client; | |||||
this.server = server; | |||||
this.view = view; | |||||
this.eingabeFeld = view.getTfNachricht(); | |||||
transmitterInterface = null; | |||||
} | } | ||||
@Override | @Override | ||||
public void execute() | public void execute() | ||||
{ | { | ||||
if(server.isConnected()) | |||||
server.send(nachricht); | |||||
if(client.isConnected()) | |||||
client.send(nachricht); | |||||
if(transmitterInterface != null && !eingabeFeld.getText().isEmpty()){ | |||||
transmitterInterface.send(new Nachricht("Du: " + eingabeFeld.getText())); | |||||
eingabeFeld.setText(""); | |||||
} | |||||
} | } | ||||
@Override | @Override | ||||
public boolean isUndoable() | public boolean isUndoable() | ||||
{ | { |
*/ | */ | ||||
package ChatProgramm.model; | package ChatProgramm.model; | ||||
import ChatProgramm.view.ChatView; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.net.Socket; | import java.net.Socket; | ||||
import java.util.logging.*; | import java.util.logging.*; | ||||
private static final String IP = "127.0.0.1"; | private static final String IP = "127.0.0.1"; | ||||
public Client(){} | |||||
public Client(ChatView view) throws IOException { | |||||
super(view); | |||||
connect(); | |||||
initIO(); | |||||
} | |||||
@Override | @Override | ||||
lg.info("Client: Verbindung wird aufgebaut"); | lg.info("Client: Verbindung wird aufgebaut"); | ||||
socket = new Socket(IP, PORT); | socket = new Socket(IP, PORT); | ||||
lg.info("Client: Verbindung aufgebaut"); | lg.info("Client: Verbindung aufgebaut"); | ||||
initIO(); | |||||
connected = true; | |||||
} catch (java.io.InterruptedIOException e) { | } catch (java.io.InterruptedIOException e) { | ||||
lg.warning("Timeout" + "(" + timeout / 1000 + "s)"); | lg.warning("Timeout" + "(" + timeout / 1000 + "s)"); | ||||
} | } |
* | * | ||||
* @author ahren | * @author ahren | ||||
*/ | */ | ||||
class Nachricht | |||||
public class Nachricht | |||||
{ | { | ||||
private String nachricht; | private String nachricht; | ||||
//private int id; | //private int id; | ||||
public String getNachricht() { | public String getNachricht() { | ||||
return nachricht; | return nachricht; | ||||
} | } | ||||
public void setNachricht(String nachricht) { | |||||
this.nachricht = nachricht; | |||||
} | |||||
} | } |
@Override | @Override | ||||
public void onNext(Nachricht item) { | public void onNext(Nachricht item) { | ||||
view.getTxtChat().setText(item.getNachricht()); | |||||
view.getTxtChat().append("\n" + item.getNachricht()); | |||||
this.subscription.request(1); | this.subscription.request(1); | ||||
} | } | ||||
package ChatProgramm.model; | package ChatProgramm.model; | ||||
import ChatProgramm.view.ChatView; | |||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.net.ServerSocket; | import java.net.ServerSocket; | ||||
import java.util.logging.*; | import java.util.logging.*; | ||||
private static Logger lg = Logger.getLogger("netz"); | private static Logger lg = Logger.getLogger("netz"); | ||||
private static final int PORT = 35000; //lt. iana port > 2¹⁵ | private static final int PORT = 35000; //lt. iana port > 2¹⁵ | ||||
public void connect() throws IOException | public void connect() throws IOException | ||||
{ | { | ||||
try | try | ||||
lg.info("Server: warte auf Verbindung"); | lg.info("Server: warte auf Verbindung"); | ||||
socket = sSocket.accept(); | socket = sSocket.accept(); | ||||
lg.info("Server: Verbindung akzeptiert"); | lg.info("Server: Verbindung akzeptiert"); | ||||
initIO(); | |||||
connected = true; | |||||
} | } | ||||
catch ( java.io.InterruptedIOException e ) | catch ( java.io.InterruptedIOException e ) | ||||
{ | { | ||||
lg.warning("Timeout"+"("+timeout/1000+"s)"); | lg.warning("Timeout"+"("+timeout/1000+"s)"); | ||||
} | } | ||||
} | } | ||||
public Server() {} | |||||
public Server(ChatView view) throws IOException { | |||||
super(view); | |||||
connect(); | |||||
initIO(); | |||||
} | |||||
} | } |
*/ | */ | ||||
package ChatProgramm.model; | package ChatProgramm.model; | ||||
import ChatProgramm.view.ChatView; | |||||
import java.io.BufferedReader; | import java.io.BufferedReader; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.io.UnsupportedEncodingException; | import java.io.UnsupportedEncodingException; | ||||
import java.net.ServerSocket; | import java.net.ServerSocket; | ||||
import java.net.Socket; | import java.net.Socket; | ||||
import java.util.concurrent.ExecutorService; | |||||
import java.util.concurrent.Executors; | |||||
import java.util.concurrent.Flow; | import java.util.concurrent.Flow; | ||||
import java.util.concurrent.Flow.Subscriber; | import java.util.concurrent.Flow.Subscriber; | ||||
import java.util.concurrent.SubmissionPublisher; | import java.util.concurrent.SubmissionPublisher; | ||||
* | * | ||||
* @author ahren | * @author ahren | ||||
*/ | */ | ||||
public abstract class Transmitter implements Runnable, Subscriber<String> { | |||||
public abstract class Transmitter implements Runnable, Subscriber<String>, TransmitterInterface { | |||||
static final int timeout = 60000; | static final int timeout = 60000; | ||||
private static final int PORT = 35000; | private static final int PORT = 35000; | ||||
protected Socket socket; | protected Socket socket; | ||||
protected BufferedReader reader; | protected BufferedReader reader; | ||||
protected PrintWriter writer; | protected PrintWriter writer; | ||||
protected boolean connected; | |||||
private Nachricht nachricht; | private Nachricht nachricht; | ||||
private boolean laufend; | private boolean laufend; | ||||
private Thread receive; | |||||
private SubmissionPublisher<Nachricht> textPublisher; | private SubmissionPublisher<Nachricht> textPublisher; | ||||
private ExecutorService eService; | |||||
private String receivedString; | private String receivedString; | ||||
private ChatView view; | |||||
private ReceiveAdapter receiveAdapter; | |||||
public Transmitter() | |||||
public Transmitter(ChatView view) | |||||
{ | { | ||||
nachricht = new Nachricht(""); | |||||
laufend = false; | |||||
receive = null; | |||||
textPublisher = new SubmissionPublisher<>(); | |||||
connected = false; | |||||
receivedString = ""; | |||||
socket = new Socket(); | |||||
eService = null; | |||||
receiveAdapter = new ReceiveAdapter(view); | |||||
textPublisher = new SubmissionPublisher<>(); | |||||
this.view = view; | |||||
addWertSubscription(receiveAdapter); | |||||
} | } | ||||
public void addWertSubscription(Subscriber<Nachricht> subscriber) | public void addWertSubscription(Subscriber<Nachricht> subscriber) | ||||
} | } | ||||
public abstract void connect() throws IOException; | public abstract void connect() throws IOException; | ||||
public boolean isConnected(){ | |||||
return connected; | |||||
} | |||||
public void initIO() { | public void initIO() { | ||||
try { | try { | ||||
} | } | ||||
} | } | ||||
public void send(String string){ | |||||
public void send(Nachricht nachricht){ | |||||
writer.println(string); | |||||
writer.println(nachricht.getNachricht()); | |||||
writer.flush(); | writer.flush(); | ||||
lg.info("Nachricht gesendet"); | lg.info("Nachricht gesendet"); | ||||
textPublisher.submit(new Nachricht(string)); | |||||
textPublisher.submit(nachricht); | |||||
// | // | ||||
// String nachricht = in.readLine(); // ACHTUNG blockiert | // String nachricht = in.readLine(); // ACHTUNG blockiert | ||||
// lg.info("Client: Serverbestätigung erhalten"); | // lg.info("Client: Serverbestätigung erhalten"); | ||||
// out.close(); | // out.close(); | ||||
// s.close(); | // s.close(); | ||||
} | } | ||||
public Nachricht receive(){ | |||||
Nachricht nachricht = new Nachricht(""); | |||||
try { | |||||
String txtNachricht = reader.readLine(); | |||||
if(!txtNachricht.isEmpty()){ | |||||
lg.info("Nachricht erhalten"); | |||||
nachricht.setNachricht("Er / Sie: " + txtNachricht); | |||||
return nachricht; | |||||
} | |||||
} catch (IOException e) { | |||||
throw new RuntimeException(e); | |||||
} | |||||
return nachricht; | |||||
} | |||||
@Override | @Override | ||||
public void run() { | public void run() { | ||||
while(laufend){ | |||||
while (true) { | |||||
lg.info("Warte auf Nachricht"); | |||||
if(laufend) { | |||||
Nachricht eingehendeNachricht = receive(); | |||||
try | |||||
{ | |||||
receivedString = reader.readLine(); // ACHTUNG blockiert | |||||
textPublisher.submit(new Nachricht(receivedString)); | |||||
} | |||||
catch (IOException ex) | |||||
{ | |||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); | |||||
if(!eingehendeNachricht.getNachricht().isEmpty()){ | |||||
textPublisher.submit(eingehendeNachricht); | |||||
} | |||||
} | |||||
else{ | |||||
break; | |||||
} | |||||
} | } | ||||
lg.info("Nachricht erhalten:" + receivedString); | |||||
} | |||||
} | } | ||||
@Override | @Override | ||||
public void onSubscribe(Flow.Subscription subscription) { | public void onSubscribe(Flow.Subscription subscription) { | ||||
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 | ||||
private void startempfangen() | private void startempfangen() | ||||
{ | { | ||||
laufend = true; | |||||
if (receive == null) | |||||
{ | |||||
receive = new Thread(this); | |||||
receive.start(); | |||||
} | |||||
synchronized (this){ | |||||
laufend = true; | |||||
} | |||||
if (eService == null){ | |||||
eService = Executors.newSingleThreadExecutor(); | |||||
eService.execute(this); | |||||
} | |||||
lg.info("Starte Chat"); | |||||
} | } | ||||
/* | |||||
* 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(Nachricht nachricht); | |||||
public Nachricht receive(); | |||||
} |