|
|
|
|
|
|
|
|
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.ExecutorService; |
|
|
import java.util.concurrent.Executors; |
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
|
|
|
public abstract void connect() throws IOException; |
|
|
public abstract void connect() throws IOException; |
|
|
|
|
|
|
|
|
public void initIO() { |
|
|
public void initIO() { |
|
|
try { |
|
|
|
|
|
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("Reader / Writer Initialisierung abgeschlossen"); |
|
|
|
|
|
startempfangen(); |
|
|
|
|
|
lg.info("Warte auf Nachricht"); |
|
|
|
|
|
|
|
|
|
|
|
} 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); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
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("Reader / Writer Initialisierung abgeschlossen"); |
|
|
|
|
|
startempfangen(); |
|
|
|
|
|
lg.info("Warte auf Nachricht"); |
|
|
|
|
|
|
|
|
|
|
|
} 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 send(Nachricht nachricht){ |
|
|
public void send(Nachricht nachricht){ |
|
|
|
|
|
|
|
|
writer.flush(); |
|
|
writer.flush(); |
|
|
lg.info("Nachricht gesendet"); |
|
|
lg.info("Nachricht gesendet"); |
|
|
textPublisher.submit(nachricht); |
|
|
textPublisher.submit(nachricht); |
|
|
// |
|
|
|
|
|
// String nachricht = in.readLine(); // ACHTUNG blockiert |
|
|
|
|
|
// lg.info("Client: Serverbestätigung erhalten"); |
|
|
|
|
|
// |
|
|
|
|
|
// lg.info("Client: fertig"); |
|
|
|
|
|
// in.close(); |
|
|
|
|
|
// out.close(); |
|
|
|
|
|
// s.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// in.close(); |
|
|
|
|
|
// out.close(); |
|
|
|
|
|
// s.close(); |
|
|
|
|
|
} |
|
|
} |
|
|
public Nachricht receive(){ |
|
|
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); |
|
|
|
|
|
|
|
|
Nachricht nachricht = new Nachricht(""); |
|
|
|
|
|
try { |
|
|
|
|
|
String txtNachricht = reader.readLine(); |
|
|
|
|
|
if(!txtNachricht.isEmpty()){ |
|
|
|
|
|
lg.info("Nachricht erhalten"); |
|
|
|
|
|
nachricht.setNachricht("Er / Sie: " + txtNachricht); |
|
|
|
|
|
return nachricht; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nachricht; |
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
return nachricht; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// public void disconnect (){ |
|
|
|
|
|
// in.close(); |
|
|
|
|
|
// out.close(); |
|
|
|
|
|
// s.close(); |
|
|
|
|
|
// } |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void run() { |
|
|
public void run() { |
|
|
while (true) { |
|
|
|
|
|
lg.info("Warte auf Nachricht"); |
|
|
|
|
|
if(laufend) { |
|
|
|
|
|
Nachricht eingehendeNachricht = receive(); |
|
|
|
|
|
|
|
|
|
|
|
if(!eingehendeNachricht.getNachricht().isEmpty()){ |
|
|
|
|
|
textPublisher.submit(eingehendeNachricht); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
else{ |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
|
|
lg.info("Warte auf Nachricht"); |
|
|
|
|
|
if(laufend) { |
|
|
|
|
|
Nachricht eingehendeNachricht = receive(); |
|
|
|
|
|
if(!eingehendeNachricht.getNachricht().isEmpty()){ |
|
|
|
|
|
textPublisher.submit(eingehendeNachricht); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
else{ |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void startempfangen() |
|
|
private void startempfangen() |
|
|
{ |
|
|
{ |
|
|
synchronized (this){ |
|
|
|
|
|
laufend = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (eService == null){ |
|
|
|
|
|
eService = Executors.newSingleThreadExecutor(); |
|
|
|
|
|
eService.execute(this); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lg.info("Starte Chat"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synchronized (this){ |
|
|
|
|
|
laufend = true; |
|
|
|
|
|
} |
|
|
|
|
|
if (eService == null){ |
|
|
|
|
|
eService = Executors.newSingleThreadExecutor(); |
|
|
|
|
|
eService.execute(this); |
|
|
|
|
|
} |
|
|
|
|
|
lg.info("Starte Chat"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |