149 lines
4.4 KiB
Java
149 lines
4.4 KiB
Java
/*
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
|
*/
|
|
package ChatProgramm.model;
|
|
|
|
import java.io.BufferedReader;
|
|
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.concurrent.Flow;
|
|
import java.util.concurrent.Flow.Subscriber;
|
|
import java.util.concurrent.SubmissionPublisher;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
/**
|
|
*
|
|
* @author ahren
|
|
*/
|
|
public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|
|
|
static final int timeout = 60000;
|
|
private static final int PORT = 35000;
|
|
|
|
private static Logger lg = Logger.getLogger("netz");
|
|
|
|
protected Socket socket;
|
|
protected BufferedReader reader;
|
|
protected PrintWriter writer;
|
|
|
|
private Nachricht nachricht;
|
|
private boolean laufend;
|
|
private Thread receive;
|
|
private SubmissionPublisher<Nachricht> textPublisher;
|
|
|
|
public Transmitter()
|
|
{
|
|
nachricht = new Nachricht("");
|
|
laufend = false;
|
|
receive = null;
|
|
textPublisher = new SubmissionPublisher<>();
|
|
}
|
|
|
|
public void addWertSubscription(Subscriber<Nachricht> subscriber)
|
|
{
|
|
textPublisher.subscribe(subscriber);
|
|
}
|
|
|
|
public abstract void connect() throws IOException;
|
|
|
|
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("Server: Initialisierung abgeschlossen");
|
|
startempfangen();
|
|
lg.info("Server: 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(String string){
|
|
|
|
writer.println(string);
|
|
writer.flush();
|
|
lg.info("Nachricht gesendet");
|
|
textPublisher.submit(new Nachricht(string));
|
|
//
|
|
// 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();
|
|
}
|
|
|
|
@Override
|
|
public void run() {
|
|
while(laufend){
|
|
try
|
|
{
|
|
String receivedString = reader.readLine(); // ACHTUNG blockiert
|
|
textPublisher.submit(new Nachricht(receivedString));
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
|
}
|
|
lg.info("Client: Serverbestätigung erhalten");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onSubscribe(Flow.Subscription subscription) {
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
}
|
|
|
|
@Override
|
|
public void onNext(String item) {
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
}
|
|
|
|
@Override
|
|
public void onError(Throwable throwable) {
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
}
|
|
|
|
@Override
|
|
public void onComplete() {
|
|
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
|
}
|
|
|
|
private void startempfangen()
|
|
{
|
|
laufend = true;
|
|
if (receive == null)
|
|
{
|
|
receive = new Thread(this);
|
|
receive.start();
|
|
}
|
|
|
|
}
|
|
|
|
}
|