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