Browse Source

Adapter

Jens
Jens Schuhmann 1 year ago
parent
commit
7cd668f8ce

+ 0
- 18
src/ChatProgramm/controller/Nachricht.java View File

/*
* 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()
{
}
}

+ 0
- 41
src/ChatProgramm/controller/ReceiveAdapter.java View File

/*
* 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()
{
}
}

+ 9
- 13
src/ChatProgramm/model/Client.java View File

import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.logging.*; import java.util.logging.*;


public Client() throws IOException { public Client() throws IOException {
connect(); connect();
initIO();


// out.println("Hallo Du Server - ich bin ein Client"); // out.println("Hallo Du Server - ich bin ein Client");
// out.flush(); // out.flush();
socket = new Socket(IP, PORT); socket = new Socket(IP, PORT);
lg.info("Client: Verbindung aufgebaut"); lg.info("Client: Verbindung aufgebaut");


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");

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)");
} }
} }

@Override
public void onNext(Nachricht item) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
} }

+ 39
- 0
src/ChatProgramm/model/Nachricht.java View File

/*
* 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
- 0
src/ChatProgramm/model/ReceiveAdapter.java View File

/*
* 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() {
}
}

+ 43
- 59
src/ChatProgramm/model/Server.java View File

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/ */

package ChatProgramm.model; package ChatProgramm.model;


import ChatProgramm.model.Transmitter; import ChatProgramm.model.Transmitter;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;
import java.util.logging.*; import java.util.logging.*;


/** /**
* Builder Class * Builder Class
*
* @author le * @author le
*/ */
public class Server extends Transmitter
{
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¹⁵
public void connect() throws IOException
{
try
{
ServerSocket sSocket = new ServerSocket(PORT);
sSocket.setSoTimeout(timeout);
lg.info("Server: warte auf Verbindung");
socket = sSocket.accept();
lg.info("Server: Verbindung akzeptiert");
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 )
{
lg.warning("Timeout"+"("+timeout/1000+"s)");
}
}
public Server() throws IOException
{
connect();
public class Server extends Transmitter {

private static Logger lg = Logger.getLogger("netz");


public void connect() throws IOException {
try {
ServerSocket sSocket = new ServerSocket(PORT);
sSocket.setSoTimeout(timeout);
lg.info("Server: warte auf Verbindung");
socket = sSocket.accept();
lg.info("Server: Verbindung akzeptiert");

} catch (java.io.InterruptedIOException e) {
lg.warning("Timeout" + "(" + timeout / 1000 + "s)");
}

}



public Server() throws IOException {
connect();
initIO();

// String nachricht = in.readLine(); // ACHTUNG blockiert // String nachricht = in.readLine(); // ACHTUNG blockiert
// lg.info("Server: Nachricht erhalten"); // lg.info("Server: Nachricht erhalten");
// System.out.println("Server: NACHRICHT = " + nachricht); // System.out.println("Server: NACHRICHT = " + nachricht);
// in.close(); // in.close();
// out.close(); // out.close();
// s.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);

/**
* @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);
}
} }
}



} }

+ 19
- 10
src/ChatProgramm/model/Transmitter.java View File

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;


* *
* @author ahren * @author ahren
*/ */
public abstract class Transmitter implements Runnable, Subscriber<String> {
public abstract class Transmitter implements Runnable, Subscriber<Nachricht> {


static final int timeout = 60000; static final int timeout = 60000;
private static final int PORT = 35000;
protected static final int PORT = 35000;
protected static final String IP = "127.0.0.1";


private static Logger lg = Logger.getLogger("netz"); private static Logger lg = Logger.getLogger("netz");


private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
protected Socket socket;
protected BufferedReader reader;
protected PrintWriter writer;

private SubmissionPublisher<Nachricht> textPublisher;


public Transmitter() { public Transmitter() {




reader = new BufferedReader(isr); reader = new BufferedReader(isr);
writer = new PrintWriter(osr); writer = new PrintWriter(osr);
lg.info("Server: Initialisierung abgeschlossen");
lg.info("Initialisierung abgeschlossen");


lg.info("Server: warte auf Nachricht");
lg.info("Warte auf Nachricht");


} catch (UnsupportedEncodingException ex) { } catch (UnsupportedEncodingException ex) {
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
} }
} }


public void addWertSubscription(Subscriber<Nachricht> subscriber) {
textPublisher.subscribe(subscriber);
}

@Override @Override
public void run() { public void run() {
} }
public void onSubscribe(Flow.Subscription subscription) { public void onSubscribe(Flow.Subscription subscription) {
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
} }


@Override @Override
public void onNext(String item) {
public void onError(Throwable throwable) {
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
} }


@Override @Override
public void onError(Throwable throwable) {
public void onComplete() {
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
} }


@Override @Override
public void onComplete() {
public void onNext(Nachricht item) {
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
} }



Loading…
Cancel
Save