|
|
@@ -5,10 +5,14 @@ |
|
|
|
package ChatProgramm.model; |
|
|
|
|
|
|
|
import ChatProgramm.view.ChatView; |
|
|
|
import java.io.BufferedInputStream; |
|
|
|
import java.io.BufferedOutputStream; |
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.ObjectInputStream; |
|
|
|
import java.io.ObjectOutputStream; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.io.OutputStreamWriter; |
|
|
|
import java.io.PrintWriter; |
|
|
@@ -34,13 +38,13 @@ public abstract class Transmitter implements Runnable, Subscriber<String>, Trans |
|
|
|
private static Logger lg = Logger.getLogger("netz"); |
|
|
|
|
|
|
|
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 boolean laufend; |
|
|
|
private SubmissionPublisher<Nachricht> textPublisher; |
|
|
|
private SubmissionPublisher<Figur> figurPublisher; |
|
|
|
private ExecutorService eService; |
|
|
|
private String receivedString; |
|
|
|
private ChatView view; |
|
|
@@ -51,15 +55,15 @@ public abstract class Transmitter implements Runnable, Subscriber<String>, Trans |
|
|
|
socket = new Socket(); |
|
|
|
eService = null; |
|
|
|
receiveAdapter = new ReceiveAdapter(view); |
|
|
|
textPublisher = new SubmissionPublisher<>(); |
|
|
|
figurPublisher = new SubmissionPublisher<>(); |
|
|
|
this.view = view; |
|
|
|
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; |
|
|
@@ -69,12 +73,17 @@ public abstract class Transmitter implements Runnable, Subscriber<String>, Trans |
|
|
|
lg.info("Initialisiere reader und writer"); |
|
|
|
InputStream is = socket.getInputStream(); |
|
|
|
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"); |
|
|
|
startempfangen(); |
|
|
|
lg.info("Warte auf Nachricht"); |
|
|
@@ -86,27 +95,42 @@ public abstract class Transmitter implements Runnable, Subscriber<String>, Trans |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void send(String txtNachricht){ |
|
|
|
|
|
|
|
writer.println(txtNachricht); |
|
|
|
/** |
|
|
|
* |
|
|
|
* @param figur |
|
|
|
* @throws IOException |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void send(Figur figur){ |
|
|
|
try |
|
|
|
{ |
|
|
|
writer.writeObject(figur); |
|
|
|
writer.flush(); |
|
|
|
} |
|
|
|
catch (IOException ex) |
|
|
|
{ |
|
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); |
|
|
|
} |
|
|
|
lg.info("Nachricht gesendet"); |
|
|
|
nachricht.setNachricht("\nDu: " + txtNachricht); |
|
|
|
textPublisher.submit(nachricht); |
|
|
|
figurPublisher.submit(figur); |
|
|
|
} |
|
|
|
public Nachricht receive(){ |
|
|
|
public Figur receive(){ |
|
|
|
try { |
|
|
|
txtNachricht = reader.readLine(); |
|
|
|
figur = (Figur) reader.readObject(); |
|
|
|
if(!txtNachricht.isEmpty()){ |
|
|
|
lg.info("Nachricht erhalten"); |
|
|
|
nachricht.setNachricht("\nEr / Sie: " + txtNachricht); |
|
|
|
return nachricht; |
|
|
|
|
|
|
|
return figur; |
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException 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 (){ |
|
|
@@ -120,9 +144,9 @@ public abstract class Transmitter implements Runnable, Subscriber<String>, Trans |
|
|
|
while (true) { |
|
|
|
lg.info("Warte auf Nachricht"); |
|
|
|
if(laufend) { |
|
|
|
nachricht = receive(); |
|
|
|
if(!nachricht.getNachricht().isEmpty()){ |
|
|
|
textPublisher.submit(nachricht); |
|
|
|
figur = receive(); |
|
|
|
if(!figur.getPunkte().isEmpty()){ |
|
|
|
figurPublisher.submit(figur); |
|
|
|
} |
|
|
|
} |
|
|
|
else{ |