} | } | ||||
public void registerCommands(){ | 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); | |||||
} | } | ||||
/** | /** |
public Nachricht(String nachricht) | public Nachricht(String nachricht) | ||||
{ | { | ||||
this.nachricht = nachricht; | |||||
this.setNachricht(nachricht); | |||||
//this.id = id; | //this.id = id; | ||||
} | } | ||||
public String getNachricht() { | public String getNachricht() { | ||||
return nachricht; | return nachricht; | ||||
} | } | ||||
public void setNachricht(String nachricht) { | |||||
this.nachricht = nachricht; | |||||
} | |||||
} | } |
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 CommandSend commandSend; | |||||
private ChatView view; | |||||
public CommandConnect(ChatView view) | |||||
public CommandConnect(ChatView view, CommandInterface value) | |||||
{ | { | ||||
rBtnServer = view.getBtnServer(); | rBtnServer = view.getBtnServer(); | ||||
rBtnClient = view.getBtnClient(); | rBtnClient = view.getBtnClient(); | ||||
dialogFenster = view.getjDialog1(); | dialogFenster = view.getjDialog1(); | ||||
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 { | ||||
new Server(); | |||||
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 { | ||||
new Client(); | |||||
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"); | ||||
import ChatProgramm.model.Client; | import ChatProgramm.model.Client; | ||||
import ChatProgramm.model.Server; | import ChatProgramm.model.Server; | ||||
import ChatProgramm.model.Transmitter; | import ChatProgramm.model.Transmitter; | ||||
import ChatProgramm.model.TransmitterInterface; | |||||
import ChatProgramm.util.OhmLogger; | import ChatProgramm.util.OhmLogger; | ||||
import ChatProgramm.view.ChatView; | import ChatProgramm.view.ChatView; | ||||
import java.util.Objects; | |||||
import java.util.logging.Logger; | import java.util.logging.Logger; | ||||
import javax.swing.JTextField; | import javax.swing.JTextField; | ||||
private static Logger lg = OhmLogger.getLogger(); | private static Logger lg = OhmLogger.getLogger(); | ||||
private ChatView view; | private ChatView view; | ||||
public TransmitterInterface transmitterInterface; | |||||
private JTextField eingabeFeld; | private JTextField eingabeFeld; | ||||
public Server server; | public Server server; | ||||
{ | { | ||||
this.view = view; | this.view = view; | ||||
this.eingabeFeld = view.getTfNachricht(); | this.eingabeFeld = view.getTfNachricht(); | ||||
transmitterInterface = null; | |||||
} | } | ||||
@Override | @Override | ||||
public void execute() | 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 | @Override |
*/ | */ | ||||
package ChatProgramm.model; | package ChatProgramm.model; | ||||
import ChatProgramm.controller.Nachricht; | |||||
import java.io.BufferedReader; | |||||
import ChatProgramm.view.ChatView; | |||||
import java.io.IOException; | 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.net.Socket; | ||||
import java.util.logging.*; | import java.util.logging.*; | ||||
private static Logger lg = Logger.getLogger("netz"); | private static Logger lg = Logger.getLogger("netz"); | ||||
public Client() throws IOException { | |||||
public Client(ChatView view) throws IOException { | |||||
super(view); | |||||
connect(); | connect(); | ||||
initIO(); | initIO(); | ||||
// s.close(); | // 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 | @Override | ||||
public void connect() throws IOException { | public void connect() throws IOException { |
package ChatProgramm.model; | package ChatProgramm.model; | ||||
import ChatProgramm.model.Transmitter; | import ChatProgramm.model.Transmitter; | ||||
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; | ||||
public Server() throws IOException { | |||||
super(); | |||||
public Server(ChatView view) throws IOException { | |||||
super(view); | |||||
connect(); | connect(); | ||||
initIO(); | initIO(); | ||||
// s.close(); | // 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); | |||||
} | |||||
} | |||||
} | } |
package ChatProgramm.model; | package ChatProgramm.model; | ||||
import ChatProgramm.controller.Nachricht; | import ChatProgramm.controller.Nachricht; | ||||
import ChatProgramm.controller.ReceiveAdapter; | |||||
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.OutputStreamWriter; | import java.io.OutputStreamWriter; | ||||
import java.io.PrintWriter; | import java.io.PrintWriter; | ||||
import java.io.UnsupportedEncodingException; | import java.io.UnsupportedEncodingException; | ||||
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<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 int PORT = 35000; | ||||
protected static final String IP = "127.0.0.1"; | protected static final String IP = "127.0.0.1"; | ||||
protected PrintWriter writer; | protected PrintWriter writer; | ||||
private SubmissionPublisher<Nachricht> textPublisher; | private SubmissionPublisher<Nachricht> textPublisher; | ||||
private Thread thd; | |||||
private boolean laufend; | |||||
private ExecutorService eService; | |||||
private boolean laufend = false; | |||||
private ChatView view; | |||||
private ReceiveAdapter receiveAdapter; | |||||
public Transmitter() { | |||||
public Transmitter(ChatView view) { | |||||
socket = new Socket(); | socket = new Socket(); | ||||
eService = null; | |||||
this.view = view; | |||||
textPublisher = new SubmissionPublisher<>(); | |||||
receiveAdapter = new ReceiveAdapter(view); | |||||
addWertSubscription(receiveAdapter); | |||||
} | } | ||||
public abstract void connect() throws IOException; | public abstract void connect() throws IOException; | ||||
writer = new PrintWriter(osr); | writer = new PrintWriter(osr); | ||||
lg.info("Initialisierung abgeschlossen"); | 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) { | } catch (IOException ex) { | ||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, 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) { | public void send(Nachricht nachricht) { | ||||
lg.info("Nachricht wird gesendet"); | lg.info("Nachricht wird gesendet"); | ||||
writer.println(nachricht.getNachricht()); | writer.println(nachricht.getNachricht()); | ||||
writer.flush(); | writer.flush(); | ||||
lg.info("Nachricht wird angezeigt"); | lg.info("Nachricht wird angezeigt"); | ||||
lg.info(nachricht.getNachricht()); | |||||
textPublisher.submit(nachricht); | 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 | @Override | ||||
public void run() { | 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; | |||||
} | |||||
} | } | ||||
} | } | ||||
package ChatProgramm.model; | |||||
import ChatProgramm.controller.Nachricht; | |||||
import java.util.concurrent.Flow; | |||||
public interface TransmitterInterface{ | |||||
public void send(Nachricht nachricht); | |||||
public Nachricht recieve(); | |||||
} |