175 lines
5.1 KiB
Java
175 lines
5.1 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 ChatProgramm.view.ChatView;
|
|
import ChatProgramm.view.GrafikView;
|
|
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;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.Socket;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
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<Figur>, TransmitterInterface {
|
|
|
|
static final int timeout = 60000;
|
|
private static final int PORT = 35000;
|
|
|
|
private static Logger lg = Logger.getLogger("netz");
|
|
|
|
protected Socket socket;
|
|
protected ObjectInputStream reader;
|
|
protected ObjectOutputStream writer;
|
|
|
|
private Figur figur;
|
|
private SubmissionPublisher<Figur> figurPublisher;
|
|
private ExecutorService eService;
|
|
private String receivedString;
|
|
private ChatView view;
|
|
private GrafikModel model;
|
|
private ReceiveAdapter receiveAdapter;
|
|
|
|
public Transmitter(ChatView view, GrafikModel model, GrafikView gView)
|
|
{
|
|
socket = new Socket();
|
|
eService = null;
|
|
receiveAdapter = new ReceiveAdapter(view, model, gView);
|
|
figurPublisher = new SubmissionPublisher<>();
|
|
this.view = view;
|
|
addWertSubscription(receiveAdapter);
|
|
figur = new Figur();
|
|
}
|
|
|
|
public void addWertSubscription(Subscriber<Figur> subscriber)
|
|
{
|
|
figurPublisher.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();
|
|
|
|
writer = new ObjectOutputStream(os);
|
|
writer.flush();
|
|
reader = new ObjectInputStream(is);
|
|
|
|
lg.info("Reader / Writer Initialisierung abgeschlossen");
|
|
startempfangen();
|
|
|
|
} 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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @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");
|
|
figurPublisher.submit(figur);
|
|
}
|
|
public Figur receive(){
|
|
|
|
Object receivedObject;
|
|
try {
|
|
receivedObject = reader.readObject();
|
|
|
|
if (receivedObject instanceof Figur) {
|
|
lg.info("Figur erhalten");
|
|
figur = (Figur) receivedObject;
|
|
}
|
|
} catch (IOException ex) {
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
|
} catch (ClassNotFoundException ex) {
|
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
|
}
|
|
|
|
|
|
return figur;
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
public void run() {
|
|
while (true) {
|
|
lg.info("Warte auf Nachricht");
|
|
figur = receive();
|
|
if(!figur.getPunkte().isEmpty()){
|
|
figurPublisher.submit(figur);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@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(Figur 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()
|
|
{
|
|
synchronized (this){
|
|
}
|
|
if (eService == null){
|
|
eService = Executors.newSingleThreadExecutor();
|
|
eService.execute(this);
|
|
}
|
|
lg.info("Starte Chat");
|
|
}
|
|
}
|