Funktioniert (Nicht schön aber selten)
This commit is contained in:
parent
109123a241
commit
72b3888984
@ -41,8 +41,9 @@ public class CommandController implements ActionListener{
|
||||
}
|
||||
|
||||
public void registerCommands(){
|
||||
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view));
|
||||
invoker.addCommand(view.getTfNachricht(), new CommandSend(view));
|
||||
CommandSend commandSend = new CommandSend(view);
|
||||
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, commandSend));
|
||||
invoker.addCommand(view.getTfNachricht(), commandSend);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,7 +16,7 @@ public class Nachricht
|
||||
|
||||
public Nachricht(String nachricht)
|
||||
{
|
||||
this.nachricht = nachricht;
|
||||
this.setNachricht(nachricht);
|
||||
//this.id = id;
|
||||
|
||||
}
|
||||
@ -36,4 +36,8 @@ public class Nachricht
|
||||
public String getNachricht() {
|
||||
return nachricht;
|
||||
}
|
||||
|
||||
public void setNachricht(String nachricht) {
|
||||
this.nachricht = nachricht;
|
||||
}
|
||||
}
|
||||
|
@ -25,12 +25,20 @@ public class CommandConnect implements CommandInterface
|
||||
private JRadioButton rBtnClient;
|
||||
private JDialog dialogFenster;
|
||||
private static Logger lg = OhmLogger.getLogger();
|
||||
|
||||
private CommandSend commandSend;
|
||||
|
||||
private ChatView view;
|
||||
|
||||
public CommandConnect(ChatView view)
|
||||
public CommandConnect(ChatView view, CommandInterface value)
|
||||
{
|
||||
rBtnServer = view.getBtnServer();
|
||||
rBtnClient = view.getBtnClient();
|
||||
dialogFenster = view.getjDialog1();
|
||||
|
||||
commandSend = (CommandSend) value;
|
||||
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -39,7 +47,7 @@ public class CommandConnect implements CommandInterface
|
||||
if(rBtnServer.isSelected()){
|
||||
lg.info("Server ausgewählt");
|
||||
try {
|
||||
new Server();
|
||||
commandSend.transmitterInterface = new Server(view);
|
||||
} catch (IOException ex) {
|
||||
lg.info("Die Verbindung zum Server ist Fehlgeschlagen");
|
||||
}
|
||||
@ -48,7 +56,7 @@ public class CommandConnect implements CommandInterface
|
||||
if(rBtnClient.isSelected()){
|
||||
lg.info("Client ausgewählt");
|
||||
try {
|
||||
new Client();
|
||||
commandSend.transmitterInterface = new Client(view);
|
||||
} catch (IOException ex) {
|
||||
lg.info("Die Verbindung zum Client ist Fehlgeschlagen");
|
||||
|
||||
|
@ -9,8 +9,11 @@ import ChatProgramm.controller.Nachricht;
|
||||
import ChatProgramm.model.Client;
|
||||
import ChatProgramm.model.Server;
|
||||
import ChatProgramm.model.Transmitter;
|
||||
import ChatProgramm.model.TransmitterInterface;
|
||||
import ChatProgramm.util.OhmLogger;
|
||||
import ChatProgramm.view.ChatView;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Logger;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
@ -23,6 +26,7 @@ public class CommandSend implements CommandInterface
|
||||
private static Logger lg = OhmLogger.getLogger();
|
||||
|
||||
private ChatView view;
|
||||
public TransmitterInterface transmitterInterface;
|
||||
private JTextField eingabeFeld;
|
||||
|
||||
public Server server;
|
||||
@ -32,13 +36,17 @@ public class CommandSend implements CommandInterface
|
||||
{
|
||||
this.view = view;
|
||||
this.eingabeFeld = view.getTfNachricht();
|
||||
transmitterInterface = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute()
|
||||
{
|
||||
//Transmitter.send(new Nachricht(eingabeFeld.getText()));
|
||||
lg.info("Sende Nachricht");
|
||||
if(transmitterInterface != null && !eingabeFeld.getText().isEmpty()){
|
||||
lg.info("Sende Nachricht");
|
||||
transmitterInterface.send(new Nachricht(eingabeFeld.getText()));
|
||||
eingabeFeld.setText("");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -4,16 +4,9 @@
|
||||
*/
|
||||
package ChatProgramm.model;
|
||||
|
||||
import ChatProgramm.controller.Nachricht;
|
||||
import java.io.BufferedReader;
|
||||
import ChatProgramm.view.ChatView;
|
||||
|
||||
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.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.logging.*;
|
||||
|
||||
@ -27,7 +20,8 @@ public class Client extends Transmitter {
|
||||
private static Logger lg = Logger.getLogger("netz");
|
||||
|
||||
|
||||
public Client() throws IOException {
|
||||
public Client(ChatView view) throws IOException {
|
||||
super(view);
|
||||
connect();
|
||||
initIO();
|
||||
|
||||
@ -45,16 +39,6 @@ public class Client extends Transmitter {
|
||||
// s.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
new Client();
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() throws IOException {
|
||||
|
@ -5,6 +5,8 @@
|
||||
package ChatProgramm.model;
|
||||
|
||||
import ChatProgramm.model.Transmitter;
|
||||
import ChatProgramm.view.ChatView;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -47,8 +49,8 @@ public class Server extends Transmitter {
|
||||
|
||||
|
||||
|
||||
public Server() throws IOException {
|
||||
super();
|
||||
public Server(ChatView view) throws IOException {
|
||||
super(view);
|
||||
connect();
|
||||
initIO();
|
||||
|
||||
@ -66,17 +68,6 @@ public class Server extends Transmitter {
|
||||
// s.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
new Server();
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,9 @@
|
||||
package ChatProgramm.model;
|
||||
|
||||
import ChatProgramm.controller.Nachricht;
|
||||
import ChatProgramm.controller.ReceiveAdapter;
|
||||
import ChatProgramm.view.ChatView;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -13,8 +16,9 @@ import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.ServerSocket;
|
||||
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;
|
||||
@ -25,9 +29,9 @@ import java.util.logging.Logger;
|
||||
*
|
||||
* @author ahren
|
||||
*/
|
||||
public abstract class Transmitter implements Runnable, Subscriber<Nachricht> {
|
||||
public abstract class Transmitter implements Runnable, Subscriber<Nachricht>, TransmitterInterface {
|
||||
|
||||
static final int timeout = 10000;
|
||||
static final int timeout = 30000;
|
||||
protected static final int PORT = 35000;
|
||||
protected static final String IP = "127.0.0.1";
|
||||
|
||||
@ -38,11 +42,21 @@ public abstract class Transmitter implements Runnable, Subscriber<Nachricht> {
|
||||
protected PrintWriter writer;
|
||||
|
||||
private SubmissionPublisher<Nachricht> textPublisher;
|
||||
private Thread thd;
|
||||
private boolean laufend;
|
||||
private ExecutorService eService;
|
||||
private boolean laufend = false;
|
||||
|
||||
public Transmitter() {
|
||||
private ChatView view;
|
||||
|
||||
private ReceiveAdapter receiveAdapter;
|
||||
|
||||
public Transmitter(ChatView view) {
|
||||
socket = new Socket();
|
||||
eService = null;
|
||||
this.view = view;
|
||||
textPublisher = new SubmissionPublisher<>();
|
||||
|
||||
receiveAdapter = new ReceiveAdapter(view);
|
||||
addWertSubscription(receiveAdapter);
|
||||
}
|
||||
|
||||
public abstract void connect() throws IOException;
|
||||
@ -64,44 +78,70 @@ public abstract class Transmitter implements Runnable, Subscriber<Nachricht> {
|
||||
writer = new PrintWriter(osr);
|
||||
lg.info("Initialisierung abgeschlossen");
|
||||
|
||||
lg.info("Warte auf Nachricht");
|
||||
start();
|
||||
|
||||
} 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 start(){
|
||||
synchronized (this){
|
||||
laufend = true;
|
||||
}
|
||||
|
||||
if (eService == null){
|
||||
eService = Executors.newSingleThreadExecutor();
|
||||
eService.execute(this);
|
||||
}
|
||||
|
||||
lg.info("Starte Chat");
|
||||
}
|
||||
|
||||
public void send(Nachricht nachricht) {
|
||||
lg.info("Nachricht wird gesendet");
|
||||
writer.println(nachricht.getNachricht());
|
||||
writer.flush();
|
||||
|
||||
lg.info("Nachricht wird angezeigt");
|
||||
lg.info(nachricht.getNachricht());
|
||||
textPublisher.submit(nachricht);
|
||||
}
|
||||
|
||||
// public void startServer() {
|
||||
// laufend = true;
|
||||
// if (thd == null) {
|
||||
// thd = new Thread(this);
|
||||
// thd.start();
|
||||
// }
|
||||
//
|
||||
// synchronized (thd) {
|
||||
// thd.notify();
|
||||
// }
|
||||
//
|
||||
// }
|
||||
public Nachricht recieve(){
|
||||
Nachricht nachricht = new Nachricht("");
|
||||
try {
|
||||
String txtNachricht = reader.readLine();
|
||||
if(!txtNachricht.isEmpty()){
|
||||
lg.info("Nachricht erhalten");
|
||||
nachricht.setNachricht(txtNachricht);
|
||||
return nachricht;
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
return nachricht;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Nachricht nachricht = new Nachricht(reader.readLine());
|
||||
textPublisher.submit(nachricht);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||
while (true) {
|
||||
lg.info("running");
|
||||
if(laufend) {
|
||||
Nachricht eingehendeNachricht = recieve();
|
||||
|
||||
if(!eingehendeNachricht.getNachricht().isEmpty()){
|
||||
textPublisher.submit(eingehendeNachricht);
|
||||
}
|
||||
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
10
src/ChatProgramm/model/TransmitterInterface.java
Normal file
10
src/ChatProgramm/model/TransmitterInterface.java
Normal file
@ -0,0 +1,10 @@
|
||||
package ChatProgramm.model;
|
||||
|
||||
import ChatProgramm.controller.Nachricht;
|
||||
|
||||
import java.util.concurrent.Flow;
|
||||
|
||||
public interface TransmitterInterface{
|
||||
public void send(Nachricht nachricht);
|
||||
public Nachricht recieve();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user