send versucht zu implementieren
This commit is contained in:
parent
efecd40ab4
commit
e617b11f86
@ -6,9 +6,6 @@
|
|||||||
package ChatProgramm;
|
package ChatProgramm;
|
||||||
|
|
||||||
import ChatProgramm.controller.CommandController;
|
import ChatProgramm.controller.CommandController;
|
||||||
import ChatProgramm.model.Client;
|
|
||||||
import ChatProgramm.model.Server;
|
|
||||||
import ChatProgramm.model.Transmitter;
|
|
||||||
import ChatProgramm.view.ChatView;
|
import ChatProgramm.view.ChatView;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
@ -23,10 +20,8 @@ public class Start
|
|||||||
{
|
{
|
||||||
//Transmitter transmitter = new Transmitter();
|
//Transmitter transmitter = new Transmitter();
|
||||||
ChatView view = new ChatView();
|
ChatView view = new ChatView();
|
||||||
Server server = new Server();
|
|
||||||
Client client = new Client();
|
|
||||||
//CommandController controller_commands = new CommandController(view, transmitter);
|
//CommandController controller_commands = new CommandController(view, transmitter);
|
||||||
CommandController controller_commands = new CommandController(view, server, client);
|
CommandController controller_commands = new CommandController(view);
|
||||||
controller_commands.registerEvents();
|
controller_commands.registerEvents();
|
||||||
controller_commands.registerCommands();
|
controller_commands.registerCommands();
|
||||||
view.setVisible(true);
|
view.setVisible(true);
|
||||||
|
@ -25,8 +25,6 @@ public class CommandController implements ActionListener{
|
|||||||
private ChatView view;
|
private ChatView view;
|
||||||
//private Transmitter transmitter;
|
//private Transmitter transmitter;
|
||||||
private CommandInvoker invoker;
|
private CommandInvoker invoker;
|
||||||
private Server server;
|
|
||||||
private Client client;
|
|
||||||
|
|
||||||
// public CommandController(ChatView view, Transmitter transmitter){
|
// public CommandController(ChatView view, Transmitter transmitter){
|
||||||
// this.view = view;
|
// this.view = view;
|
||||||
@ -34,11 +32,9 @@ public class CommandController implements ActionListener{
|
|||||||
// this.invoker = new CommandInvoker();
|
// this.invoker = new CommandInvoker();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public CommandController(ChatView view, Server server, Client client){
|
public CommandController(ChatView view){
|
||||||
this.view = view;
|
this.view = view;
|
||||||
this.invoker = new CommandInvoker();
|
this.invoker = new CommandInvoker();
|
||||||
this.client = client;
|
|
||||||
this.server = server;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerEvents(){
|
public void registerEvents(){
|
||||||
@ -47,8 +43,9 @@ public class CommandController implements ActionListener{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void registerCommands(){
|
public void registerCommands(){
|
||||||
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, server, client));
|
CommandSend commandSend = new CommandSend(view);
|
||||||
invoker.addCommand(view.getTfNachricht(), new CommandSend(view, server, client));
|
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, commandSend));
|
||||||
|
invoker.addCommand(view.getTfNachricht(), commandSend);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,6 +10,7 @@ import ChatProgramm.model.Server;
|
|||||||
import ChatProgramm.util.OhmLogger;
|
import ChatProgramm.util.OhmLogger;
|
||||||
import ChatProgramm.view.ChatView;
|
import ChatProgramm.view.ChatView;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import javax.swing.JDialog;
|
import javax.swing.JDialog;
|
||||||
import javax.swing.JRadioButton;
|
import javax.swing.JRadioButton;
|
||||||
@ -24,16 +25,18 @@ public class CommandConnect implements CommandInterface
|
|||||||
private JRadioButton rBtnClient;
|
private JRadioButton rBtnClient;
|
||||||
private JDialog dialogFenster;
|
private JDialog dialogFenster;
|
||||||
private static Logger lg = OhmLogger.getLogger();
|
private static Logger lg = OhmLogger.getLogger();
|
||||||
private final Client client;
|
private CommandSend commandSend;
|
||||||
private final Server server;
|
private ChatView view;
|
||||||
|
|
||||||
public CommandConnect(ChatView view, Server server, Client client)
|
public CommandConnect(ChatView view, CommandInterface value)
|
||||||
{
|
{
|
||||||
rBtnServer = view.getBtnServer();
|
rBtnServer = view.getBtnServer();
|
||||||
rBtnClient = view.getBtnClient();
|
rBtnClient = view.getBtnClient();
|
||||||
dialogFenster = view.getjDialog1();
|
dialogFenster = view.getjDialog1();
|
||||||
this.client = client;
|
|
||||||
this.server = server;
|
commandSend = (CommandSend) value;
|
||||||
|
|
||||||
|
this.view = view;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -42,7 +45,7 @@ public class CommandConnect implements CommandInterface
|
|||||||
if(rBtnServer.isSelected()){
|
if(rBtnServer.isSelected()){
|
||||||
lg.info("Server ausgewählt");
|
lg.info("Server ausgewählt");
|
||||||
try {
|
try {
|
||||||
server.connect();
|
commandSend.transmitterInterface = new Server(view);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
lg.info("Die Verbindung zum Server ist Fehlgeschlagen");
|
lg.info("Die Verbindung zum Server ist Fehlgeschlagen");
|
||||||
}
|
}
|
||||||
@ -51,7 +54,7 @@ public class CommandConnect implements CommandInterface
|
|||||||
if(rBtnClient.isSelected()){
|
if(rBtnClient.isSelected()){
|
||||||
lg.info("Client ausgewählt");
|
lg.info("Client ausgewählt");
|
||||||
try {
|
try {
|
||||||
client.connect();
|
commandSend.transmitterInterface = new Client(view);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
lg.info("Die Verbindung zum Client ist Fehlgeschlagen");
|
lg.info("Die Verbindung zum Client ist Fehlgeschlagen");
|
||||||
|
|
||||||
@ -71,4 +74,4 @@ public class CommandConnect implements CommandInterface
|
|||||||
public void undo()
|
public void undo()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,10 +7,12 @@ package ChatProgramm.controller.commands;
|
|||||||
|
|
||||||
import ChatProgramm.model.Client;
|
import ChatProgramm.model.Client;
|
||||||
import ChatProgramm.model.Server;
|
import ChatProgramm.model.Server;
|
||||||
|
import ChatProgramm.model.TransmitterInterface;
|
||||||
import ChatProgramm.util.OhmLogger;
|
import ChatProgramm.util.OhmLogger;
|
||||||
import ChatProgramm.view.ChatView;
|
import ChatProgramm.view.ChatView;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import javax.swing.JTextField;
|
import javax.swing.JTextField;
|
||||||
|
import ChatProgramm.model.Nachricht;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -19,28 +21,33 @@ import javax.swing.JTextField;
|
|||||||
public class CommandSend implements CommandInterface
|
public class CommandSend implements CommandInterface
|
||||||
{
|
{
|
||||||
private static Logger lg = OhmLogger.getLogger();
|
private static Logger lg = OhmLogger.getLogger();
|
||||||
private JTextField tfNachricht;
|
private JTextField eingabeFeld;
|
||||||
private String nachricht;
|
private String nachricht;
|
||||||
private final Client client;
|
private ChatView view;
|
||||||
private final Server server;
|
public TransmitterInterface transmitterInterface;
|
||||||
|
public Server server;
|
||||||
|
public Client client;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public CommandSend(ChatView view, Server server, Client client)
|
|
||||||
|
public CommandSend(ChatView view)
|
||||||
{
|
{
|
||||||
tfNachricht = view.getTfNachricht();
|
this.view = view;
|
||||||
nachricht = tfNachricht.getText();
|
this.eingabeFeld = view.getTfNachricht();
|
||||||
this.client = client;
|
transmitterInterface = null;
|
||||||
this.server = server;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute()
|
||||||
{
|
{
|
||||||
if(server.isConnected())
|
if(transmitterInterface != null && !eingabeFeld.getText().isEmpty()){
|
||||||
server.send(nachricht);
|
transmitterInterface.send(new Nachricht("Du: " + eingabeFeld.getText()));
|
||||||
if(client.isConnected())
|
eingabeFeld.setText("");
|
||||||
client.send(nachricht);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isUndoable()
|
public boolean isUndoable()
|
||||||
{
|
{
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package ChatProgramm.model;
|
package ChatProgramm.model;
|
||||||
|
|
||||||
|
import ChatProgramm.view.ChatView;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
@ -20,7 +21,11 @@ public class Client extends Transmitter {
|
|||||||
private static final String IP = "127.0.0.1";
|
private static final String IP = "127.0.0.1";
|
||||||
|
|
||||||
|
|
||||||
public Client(){}
|
public Client(ChatView view) throws IOException {
|
||||||
|
super(view);
|
||||||
|
connect();
|
||||||
|
initIO();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -29,8 +34,6 @@ 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();
|
|
||||||
connected = true;
|
|
||||||
} catch (java.io.InterruptedIOException e) {
|
} catch (java.io.InterruptedIOException e) {
|
||||||
lg.warning("Timeout" + "(" + timeout / 1000 + "s)");
|
lg.warning("Timeout" + "(" + timeout / 1000 + "s)");
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ package ChatProgramm.model;
|
|||||||
*
|
*
|
||||||
* @author ahren
|
* @author ahren
|
||||||
*/
|
*/
|
||||||
class Nachricht
|
public class Nachricht
|
||||||
{
|
{
|
||||||
private String nachricht;
|
private String nachricht;
|
||||||
//private int id;
|
//private int id;
|
||||||
@ -36,4 +36,9 @@ class Nachricht
|
|||||||
public String getNachricht() {
|
public String getNachricht() {
|
||||||
return nachricht;
|
return nachricht;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setNachricht(String nachricht) {
|
||||||
|
this.nachricht = nachricht;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -29,7 +29,8 @@ public class ReceiveAdapter implements Subscriber<Nachricht> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNext(Nachricht item) {
|
public void onNext(Nachricht item) {
|
||||||
view.getTxtChat().setText(item.getNachricht());
|
|
||||||
|
view.getTxtChat().append("\n" + item.getNachricht());
|
||||||
this.subscription.request(1);
|
this.subscription.request(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
package ChatProgramm.model;
|
package ChatProgramm.model;
|
||||||
|
|
||||||
|
import ChatProgramm.view.ChatView;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
@ -18,6 +19,7 @@ public class Server extends Transmitter
|
|||||||
private static Logger lg = Logger.getLogger("netz");
|
private static Logger lg = Logger.getLogger("netz");
|
||||||
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
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -27,13 +29,17 @@ 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();
|
|
||||||
connected = true;
|
|
||||||
}
|
}
|
||||||
catch ( java.io.InterruptedIOException e )
|
catch ( java.io.InterruptedIOException e )
|
||||||
{
|
{
|
||||||
lg.warning("Timeout"+"("+timeout/1000+"s)");
|
lg.warning("Timeout"+"("+timeout/1000+"s)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public Server() {}
|
public Server(ChatView view) throws IOException {
|
||||||
|
super(view);
|
||||||
|
connect();
|
||||||
|
initIO();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
package ChatProgramm.model;
|
package ChatProgramm.model;
|
||||||
|
|
||||||
|
import ChatProgramm.view.ChatView;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -14,6 +15,8 @@ import java.io.PrintWriter;
|
|||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
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.concurrent.SubmissionPublisher;
|
||||||
@ -24,7 +27,7 @@ import java.util.logging.Logger;
|
|||||||
*
|
*
|
||||||
* @author ahren
|
* @author ahren
|
||||||
*/
|
*/
|
||||||
public abstract class Transmitter implements Runnable, Subscriber<String> {
|
public abstract class Transmitter implements Runnable, Subscriber<String>, TransmitterInterface {
|
||||||
|
|
||||||
static final int timeout = 60000;
|
static final int timeout = 60000;
|
||||||
private static final int PORT = 35000;
|
private static final int PORT = 35000;
|
||||||
@ -34,22 +37,23 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
protected Socket socket;
|
protected Socket socket;
|
||||||
protected BufferedReader reader;
|
protected BufferedReader reader;
|
||||||
protected PrintWriter writer;
|
protected PrintWriter writer;
|
||||||
protected boolean connected;
|
|
||||||
|
|
||||||
private Nachricht nachricht;
|
private Nachricht nachricht;
|
||||||
private boolean laufend;
|
private boolean laufend;
|
||||||
private Thread receive;
|
|
||||||
private SubmissionPublisher<Nachricht> textPublisher;
|
private SubmissionPublisher<Nachricht> textPublisher;
|
||||||
|
private ExecutorService eService;
|
||||||
private String receivedString;
|
private String receivedString;
|
||||||
|
private ChatView view;
|
||||||
|
private ReceiveAdapter receiveAdapter;
|
||||||
|
|
||||||
public Transmitter()
|
public Transmitter(ChatView view)
|
||||||
{
|
{
|
||||||
nachricht = new Nachricht("");
|
socket = new Socket();
|
||||||
laufend = false;
|
eService = null;
|
||||||
receive = null;
|
receiveAdapter = new ReceiveAdapter(view);
|
||||||
textPublisher = new SubmissionPublisher<>();
|
textPublisher = new SubmissionPublisher<>();
|
||||||
connected = false;
|
this.view = view;
|
||||||
receivedString = "";
|
addWertSubscription(receiveAdapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addWertSubscription(Subscriber<Nachricht> subscriber)
|
public void addWertSubscription(Subscriber<Nachricht> subscriber)
|
||||||
@ -58,10 +62,6 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public abstract void connect() throws IOException;
|
public abstract void connect() throws IOException;
|
||||||
|
|
||||||
public boolean isConnected(){
|
|
||||||
return connected;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initIO() {
|
public void initIO() {
|
||||||
try {
|
try {
|
||||||
@ -85,12 +85,12 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(String string){
|
public void send(Nachricht nachricht){
|
||||||
|
|
||||||
writer.println(string);
|
writer.println(nachricht.getNachricht());
|
||||||
writer.flush();
|
writer.flush();
|
||||||
lg.info("Nachricht gesendet");
|
lg.info("Nachricht gesendet");
|
||||||
textPublisher.submit(new Nachricht(string));
|
textPublisher.submit(nachricht);
|
||||||
//
|
//
|
||||||
// String nachricht = in.readLine(); // ACHTUNG blockiert
|
// String nachricht = in.readLine(); // ACHTUNG blockiert
|
||||||
// lg.info("Client: Serverbestätigung erhalten");
|
// lg.info("Client: Serverbestätigung erhalten");
|
||||||
@ -105,25 +105,43 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
// out.close();
|
// out.close();
|
||||||
// s.close();
|
// s.close();
|
||||||
}
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return nachricht;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while(laufend){
|
while (true) {
|
||||||
|
lg.info("Warte auf Nachricht");
|
||||||
|
if(laufend) {
|
||||||
|
Nachricht eingehendeNachricht = receive();
|
||||||
|
|
||||||
try
|
if(!eingehendeNachricht.getNachricht().isEmpty()){
|
||||||
{
|
textPublisher.submit(eingehendeNachricht);
|
||||||
receivedString = reader.readLine(); // ACHTUNG blockiert
|
}
|
||||||
textPublisher.submit(new Nachricht(receivedString));
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
lg.info("Nachricht erhalten:" + receivedString);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
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
|
||||||
@ -146,12 +164,17 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
|
|||||||
|
|
||||||
private void startempfangen()
|
private void startempfangen()
|
||||||
{
|
{
|
||||||
laufend = true;
|
synchronized (this){
|
||||||
if (receive == null)
|
laufend = true;
|
||||||
{
|
}
|
||||||
receive = new Thread(this);
|
|
||||||
receive.start();
|
if (eService == null){
|
||||||
}
|
eService = Executors.newSingleThreadExecutor();
|
||||||
|
eService.execute(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
lg.info("Starte Chat");
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
src/ChatProgramm/model/TransmitterInterface.java
Normal file
17
src/ChatProgramm/model/TransmitterInterface.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
|
||||||
|
*/
|
||||||
|
package ChatProgramm.model;
|
||||||
|
|
||||||
|
import ChatProgramm.model.Nachricht;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author ahren
|
||||||
|
*/
|
||||||
|
public interface TransmitterInterface
|
||||||
|
{
|
||||||
|
public void send(Nachricht nachricht);
|
||||||
|
public Nachricht receive();
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user