run und publish möchtegernmäßig implementiert
This commit is contained in:
parent
96ac335267
commit
0548fee0a4
@ -1,18 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.controller;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author ahren
|
|
||||||
*/
|
|
||||||
class Nachricht
|
|
||||||
{
|
|
||||||
public Nachricht()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.controller;
|
|
||||||
|
|
||||||
import java.util.concurrent.Flow;
|
|
||||||
import java.util.concurrent.Flow.Subscriber;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author ahren
|
|
||||||
*/
|
|
||||||
public class ReceiveAdapter implements Subscriber<Nachricht>
|
|
||||||
{
|
|
||||||
public ReceiveAdapter()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSubscribe(Flow.Subscription subscription)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNext(Nachricht item)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(Throwable throwable)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onComplete()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,14 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package ChatProgramm.model;
|
package ChatProgramm.model;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
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.net.ServerSocket;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
@ -23,29 +16,11 @@ import java.util.logging.*;
|
|||||||
public class Client extends Transmitter {
|
public class Client extends Transmitter {
|
||||||
|
|
||||||
private static Logger lg = Logger.getLogger("netz");
|
private static Logger lg = Logger.getLogger("netz");
|
||||||
|
|
||||||
// private Socket socket;
|
|
||||||
// private BufferedReader reader;
|
|
||||||
// private PrintWriter writer;
|
|
||||||
|
|
||||||
private static final int PORT = 35000; //lt. iana port > 2¹⁵
|
private static final int PORT = 35000; //lt. iana port > 2¹⁵
|
||||||
private static final String IP = "127.0.0.1";
|
private static final String IP = "127.0.0.1";
|
||||||
|
|
||||||
public Client() throws IOException {
|
public Client() throws IOException {
|
||||||
connect();
|
connect();
|
||||||
|
|
||||||
// out.println("Hallo Du Server - ich bin ein Client");
|
|
||||||
// out.flush();
|
|
||||||
//
|
|
||||||
// lg.info("Client: warte auf Serverantwort");
|
|
||||||
//
|
|
||||||
// String nachricht = in.readLine(); // ACHTUNG blockiert
|
|
||||||
// lg.info("Client: Serverbestätigung erhalten");
|
|
||||||
//
|
|
||||||
// lg.info("Client: fertig");
|
|
||||||
// in.close();
|
|
||||||
// out.close();
|
|
||||||
// s.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -55,20 +30,8 @@ public class Client extends Transmitter {
|
|||||||
lg.info("Client: Verbindung wird aufgebaut");
|
lg.info("Client: Verbindung wird aufgebaut");
|
||||||
socket = new Socket(IP, PORT);
|
socket = new Socket(IP, PORT);
|
||||||
lg.info("Client: Verbindung aufgebaut");
|
lg.info("Client: Verbindung aufgebaut");
|
||||||
|
initIO();
|
||||||
// lg.info("Client: 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("Client: Initialisierung abgeschlossen");
|
|
||||||
initIO();
|
|
||||||
lg.info("Client: warte auf Nachricht");
|
lg.info("Client: warte auf Nachricht");
|
||||||
|
|
||||||
} catch (java.io.InterruptedIOException e) {
|
} catch (java.io.InterruptedIOException e) {
|
||||||
lg.warning("Timeout" + "(" + timeout / 1000 + "s)");
|
lg.warning("Timeout" + "(" + timeout / 1000 + "s)");
|
||||||
}
|
}
|
||||||
|
39
src/ChatProgramm/model/Nachricht.java
Normal file
39
src/ChatProgramm/model/Nachricht.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author ahren
|
||||||
|
*/
|
||||||
|
class Nachricht
|
||||||
|
{
|
||||||
|
private String nachricht;
|
||||||
|
//private int id;
|
||||||
|
|
||||||
|
public Nachricht(String nachricht)
|
||||||
|
{
|
||||||
|
this.nachricht = nachricht;
|
||||||
|
//this.id = id;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the id
|
||||||
|
*/
|
||||||
|
// public int getId() {
|
||||||
|
// return id;
|
||||||
|
// }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the nachricht
|
||||||
|
*/
|
||||||
|
public String getNachricht() {
|
||||||
|
return nachricht;
|
||||||
|
}
|
||||||
|
}
|
43
src/ChatProgramm/model/ReceiveAdapter.java
Normal file
43
src/ChatProgramm/model/ReceiveAdapter.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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 java.util.concurrent.Flow;
|
||||||
|
import java.util.concurrent.Flow.Subscriber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author ahren
|
||||||
|
*/
|
||||||
|
public class ReceiveAdapter implements Subscriber<Nachricht> {
|
||||||
|
|
||||||
|
private ChatView view;
|
||||||
|
private Flow.Subscription subscription;
|
||||||
|
|
||||||
|
public ReceiveAdapter(ChatView view) {
|
||||||
|
this.view = view;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSubscribe(Flow.Subscription subscription) {
|
||||||
|
this.subscription = subscription;
|
||||||
|
this.subscription.request(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNext(Nachricht item) {
|
||||||
|
view.getTxtChat().setText(item.getNachricht());
|
||||||
|
this.subscription.request(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Throwable throwable) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete(){
|
||||||
|
}
|
||||||
|
}
|
@ -5,16 +5,8 @@
|
|||||||
|
|
||||||
package ChatProgramm.model;
|
package ChatProgramm.model;
|
||||||
|
|
||||||
import ChatProgramm.model.Transmitter;
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
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.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,12 +15,7 @@ import java.util.logging.*;
|
|||||||
*/
|
*/
|
||||||
public class Server extends Transmitter
|
public class Server extends Transmitter
|
||||||
{
|
{
|
||||||
private static Logger lg = Logger.getLogger("netz");
|
private static Logger lg = Logger.getLogger("netz");
|
||||||
|
|
||||||
private Socket socket;
|
|
||||||
private BufferedReader reader;
|
|
||||||
private PrintWriter writer;
|
|
||||||
|
|
||||||
private static final int PORT = 35000; //lt. iana port > 2¹⁵
|
private static final int PORT = 35000; //lt. iana port > 2¹⁵
|
||||||
|
|
||||||
public void connect() throws IOException
|
public void connect() throws IOException
|
||||||
@ -40,57 +27,14 @@ public class Server extends Transmitter
|
|||||||
lg.info("Server: warte auf Verbindung");
|
lg.info("Server: warte auf Verbindung");
|
||||||
socket = sSocket.accept();
|
socket = sSocket.accept();
|
||||||
lg.info("Server: Verbindung akzeptiert");
|
lg.info("Server: Verbindung akzeptiert");
|
||||||
|
initIO();
|
||||||
lg.info("Server: 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");
|
|
||||||
|
|
||||||
lg.info("Server: warte auf Nachricht");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch ( java.io.InterruptedIOException e )
|
catch ( java.io.InterruptedIOException e )
|
||||||
{
|
{
|
||||||
lg.warning("Timeout"+"("+timeout/1000+"s)");
|
lg.warning("Timeout"+"("+timeout/1000+"s)");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public Server() throws IOException
|
public Server() throws IOException{
|
||||||
{
|
|
||||||
connect();
|
connect();
|
||||||
|
|
||||||
// String nachricht = in.readLine(); // ACHTUNG blockiert
|
|
||||||
// lg.info("Server: Nachricht erhalten");
|
|
||||||
// System.out.println("Server: NACHRICHT = " + nachricht);
|
|
||||||
//
|
|
||||||
// // ACHTUNG: blockiert NICHT!!!!
|
|
||||||
// out.println("Server an Client: Nachricht erhalten");
|
|
||||||
// out.flush(); // wichtig
|
|
||||||
//
|
|
||||||
// lg.info("Server: fertig");
|
|
||||||
// in.close();
|
|
||||||
// out.close();
|
|
||||||
// s.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param args the command line arguments
|
|
||||||
*/
|
|
||||||
public static void main(String[] args)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
new Server();
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import java.net.ServerSocket;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.concurrent.Flow;
|
import java.util.concurrent.Flow;
|
||||||
import java.util.concurrent.Flow.Subscriber;
|
import java.util.concurrent.Flow.Subscriber;
|
||||||
|
import java.util.concurrent.SubmissionPublisher;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -30,12 +31,26 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
|
|
||||||
private static Logger lg = Logger.getLogger("netz");
|
private static Logger lg = Logger.getLogger("netz");
|
||||||
|
|
||||||
private Socket socket;
|
protected Socket socket;
|
||||||
private BufferedReader reader;
|
protected BufferedReader reader;
|
||||||
private PrintWriter writer;
|
protected PrintWriter writer;
|
||||||
|
|
||||||
public Transmitter() {
|
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 abstract void connect() throws IOException;
|
||||||
@ -52,7 +67,7 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
reader = new BufferedReader(isr);
|
reader = new BufferedReader(isr);
|
||||||
writer = new PrintWriter(osr);
|
writer = new PrintWriter(osr);
|
||||||
lg.info("Server: Initialisierung abgeschlossen");
|
lg.info("Server: Initialisierung abgeschlossen");
|
||||||
|
startempfangen();
|
||||||
lg.info("Server: warte auf Nachricht");
|
lg.info("Server: warte auf Nachricht");
|
||||||
|
|
||||||
} catch (UnsupportedEncodingException ex) {
|
} catch (UnsupportedEncodingException ex) {
|
||||||
@ -61,9 +76,42 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, 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
|
@Override
|
||||||
public void run() {
|
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
|
@Override
|
||||||
@ -86,4 +134,15 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user