|
|
|
|
|
|
|
|
package ChatProgramm.model; |
|
|
package ChatProgramm.model; |
|
|
|
|
|
|
|
|
import ChatProgramm.view.ChatView; |
|
|
import ChatProgramm.view.ChatView; |
|
|
|
|
|
import java.io.BufferedInputStream; |
|
|
|
|
|
import java.io.BufferedOutputStream; |
|
|
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.InputStreamReader; |
|
|
import java.io.InputStreamReader; |
|
|
|
|
|
import java.io.ObjectInputStream; |
|
|
|
|
|
import java.io.ObjectOutputStream; |
|
|
import java.io.OutputStream; |
|
|
import java.io.OutputStream; |
|
|
import java.io.OutputStreamWriter; |
|
|
import java.io.OutputStreamWriter; |
|
|
import java.io.PrintWriter; |
|
|
import java.io.PrintWriter; |
|
|
|
|
|
|
|
|
private static Logger lg = Logger.getLogger("netz"); |
|
|
private static Logger lg = Logger.getLogger("netz"); |
|
|
|
|
|
|
|
|
protected Socket socket; |
|
|
protected Socket socket; |
|
|
protected BufferedReader reader; |
|
|
|
|
|
protected PrintWriter writer; |
|
|
|
|
|
|
|
|
protected ObjectInputStream reader; |
|
|
|
|
|
protected ObjectOutputStream writer; |
|
|
|
|
|
|
|
|
private Nachricht nachricht; |
|
|
|
|
|
|
|
|
private Figur figur; |
|
|
private String txtNachricht; |
|
|
private String txtNachricht; |
|
|
private boolean laufend; |
|
|
private boolean laufend; |
|
|
private SubmissionPublisher<Nachricht> textPublisher; |
|
|
|
|
|
|
|
|
private SubmissionPublisher<Figur> figurPublisher; |
|
|
private ExecutorService eService; |
|
|
private ExecutorService eService; |
|
|
private String receivedString; |
|
|
private String receivedString; |
|
|
private ChatView view; |
|
|
private ChatView view; |
|
|
|
|
|
|
|
|
socket = new Socket(); |
|
|
socket = new Socket(); |
|
|
eService = null; |
|
|
eService = null; |
|
|
receiveAdapter = new ReceiveAdapter(view); |
|
|
receiveAdapter = new ReceiveAdapter(view); |
|
|
textPublisher = new SubmissionPublisher<>(); |
|
|
|
|
|
|
|
|
figurPublisher = new SubmissionPublisher<>(); |
|
|
this.view = view; |
|
|
this.view = view; |
|
|
addWertSubscription(receiveAdapter); |
|
|
addWertSubscription(receiveAdapter); |
|
|
nachricht = new Nachricht(""); |
|
|
|
|
|
|
|
|
figur = new Figur(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void addWertSubscription(Subscriber<Nachricht> subscriber) |
|
|
|
|
|
|
|
|
public void addWertSubscription(Subscriber<Figur> subscriber) |
|
|
{ |
|
|
{ |
|
|
textPublisher.subscribe(subscriber); |
|
|
|
|
|
|
|
|
figurPublisher.subscribe(subscriber); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public abstract void connect() throws IOException; |
|
|
public abstract void connect() throws IOException; |
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(is, "UTF-8"); |
|
|
|
|
|
OutputStreamWriter osr = new OutputStreamWriter(os, "UTF-8"); |
|
|
|
|
|
|
|
|
|
|
|
reader = new BufferedReader(isr); |
|
|
|
|
|
writer = new PrintWriter(osr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
writer = new ObjectOutputStream(bos); |
|
|
lg.info("Reader / Writer Initialisierung abgeschlossen"); |
|
|
lg.info("Reader / Writer Initialisierung abgeschlossen"); |
|
|
startempfangen(); |
|
|
startempfangen(); |
|
|
lg.info("Warte auf Nachricht"); |
|
|
lg.info("Warte auf Nachricht"); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void send(String txtNachricht){ |
|
|
|
|
|
|
|
|
|
|
|
writer.println(txtNachricht); |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* |
|
|
|
|
|
* @param figur |
|
|
|
|
|
* @throws IOException |
|
|
|
|
|
*/ |
|
|
|
|
|
@Override |
|
|
|
|
|
public void send(Figur figur){ |
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
writer.writeObject(figur); |
|
|
writer.flush(); |
|
|
writer.flush(); |
|
|
|
|
|
} |
|
|
|
|
|
catch (IOException ex) |
|
|
|
|
|
{ |
|
|
|
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
|
|
} |
|
|
lg.info("Nachricht gesendet"); |
|
|
lg.info("Nachricht gesendet"); |
|
|
nachricht.setNachricht("\nDu: " + txtNachricht); |
|
|
|
|
|
textPublisher.submit(nachricht); |
|
|
|
|
|
|
|
|
figurPublisher.submit(figur); |
|
|
} |
|
|
} |
|
|
public Nachricht receive(){ |
|
|
|
|
|
|
|
|
public Figur receive(){ |
|
|
try { |
|
|
try { |
|
|
txtNachricht = reader.readLine(); |
|
|
|
|
|
|
|
|
figur = (Figur) reader.readObject(); |
|
|
if(!txtNachricht.isEmpty()){ |
|
|
if(!txtNachricht.isEmpty()){ |
|
|
lg.info("Nachricht erhalten"); |
|
|
lg.info("Nachricht erhalten"); |
|
|
nachricht.setNachricht("\nEr / Sie: " + txtNachricht); |
|
|
|
|
|
return nachricht; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return figur; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
throw new RuntimeException(e); |
|
|
throw new RuntimeException(e); |
|
|
} |
|
|
} |
|
|
return nachricht; |
|
|
|
|
|
|
|
|
catch (ClassNotFoundException ex) |
|
|
|
|
|
{ |
|
|
|
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
|
|
} |
|
|
|
|
|
return figur; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// public void disconnect (){ |
|
|
// public void disconnect (){ |
|
|
|
|
|
|
|
|
while (true) { |
|
|
while (true) { |
|
|
lg.info("Warte auf Nachricht"); |
|
|
lg.info("Warte auf Nachricht"); |
|
|
if(laufend) { |
|
|
if(laufend) { |
|
|
nachricht = receive(); |
|
|
|
|
|
if(!nachricht.getNachricht().isEmpty()){ |
|
|
|
|
|
textPublisher.submit(nachricht); |
|
|
|
|
|
|
|
|
figur = receive(); |
|
|
|
|
|
if(!figur.getPunkte().isEmpty()){ |
|
|
|
|
|
figurPublisher.submit(figur); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
else{ |
|
|
else{ |