Browse Source

send versucht zu implementieren

Toni
ahren 1 year ago
parent
commit
efecd40ab4

+ 5
- 1
src/ChatProgramm/Start.java View File

@@ -6,6 +6,8 @@
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;
@@ -21,8 +23,10 @@ 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);
CommandController controller_commands = new CommandController(view, server, client);
controller_commands.registerEvents();
controller_commands.registerCommands();
view.setVisible(true);

+ 9
- 3
src/ChatProgramm/controller/CommandController.java View File

@@ -8,6 +8,8 @@ package ChatProgramm.controller;
import ChatProgramm.controller.commands.CommandConnect;
import ChatProgramm.controller.commands.CommandInvoker;
import ChatProgramm.controller.commands.CommandSend;
import ChatProgramm.model.Client;
import ChatProgramm.model.Server;
import ChatProgramm.model.Transmitter;
import ChatProgramm.view.ChatView;
import java.awt.Component;
@@ -23,6 +25,8 @@ 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;
@@ -30,9 +34,11 @@ public class CommandController implements ActionListener{
// this.invoker = new CommandInvoker();
// }
public CommandController(ChatView view){
public CommandController(ChatView view, Server server, Client client){
this.view = view;
this.invoker = new CommandInvoker();
this.client = client;
this.server = server;
}
public void registerEvents(){
@@ -41,8 +47,8 @@ public class CommandController implements ActionListener{
}
public void registerCommands(){
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view));
invoker.addCommand(view.getTfNachricht(), new CommandSend());
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view, server, client));
invoker.addCommand(view.getTfNachricht(), new CommandSend(view, server, client));
}

/**

+ 7
- 4
src/ChatProgramm/controller/commands/CommandConnect.java View File

@@ -10,7 +10,6 @@ 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;
@@ -25,12 +24,16 @@ 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;
public CommandConnect(ChatView view)
public CommandConnect(ChatView view, Server server, Client client)
{
rBtnServer = view.getBtnServer();
rBtnClient = view.getBtnClient();
dialogFenster = view.getjDialog1();
this.client = client;
this.server = server;
}

@Override
@@ -39,7 +42,7 @@ public class CommandConnect implements CommandInterface
if(rBtnServer.isSelected()){
lg.info("Server ausgewählt");
try {
new Server();
server.connect();
} catch (IOException ex) {
lg.info("Die Verbindung zum Server ist Fehlgeschlagen");
}
@@ -48,7 +51,7 @@ public class CommandConnect implements CommandInterface
if(rBtnClient.isSelected()){
lg.info("Client ausgewählt");
try {
new Client();
client.connect();
} catch (IOException ex) {
lg.info("Die Verbindung zum Client ist Fehlgeschlagen");


+ 14
- 1
src/ChatProgramm/controller/commands/CommandSend.java View File

@@ -5,6 +5,8 @@

package ChatProgramm.controller.commands;

import ChatProgramm.model.Client;
import ChatProgramm.model.Server;
import ChatProgramm.util.OhmLogger;
import ChatProgramm.view.ChatView;
import java.util.logging.Logger;
@@ -18,14 +20,25 @@ public class CommandSend implements CommandInterface
{
private static Logger lg = OhmLogger.getLogger();
private JTextField tfNachricht;
public CommandSend(ChatView view)
private String nachricht;
private final Client client;
private final Server server;
public CommandSend(ChatView view, Server server, Client client)
{
tfNachricht = view.getTfNachricht();
nachricht = tfNachricht.getText();
this.client = client;
this.server = server;
}

@Override
public void execute()
{
if(server.isConnected())
server.send(nachricht);
if(client.isConnected())
client.send(nachricht);
}

@Override

+ 3
- 4
src/ChatProgramm/model/Client.java View File

@@ -18,10 +18,9 @@ public class Client extends Transmitter {
private static Logger lg = Logger.getLogger("netz");
private static final int PORT = 35000; //lt. iana port > 2¹⁵
private static final String IP = "127.0.0.1";

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


@Override
@@ -31,7 +30,7 @@ public class Client extends Transmitter {
socket = new Socket(IP, PORT);
lg.info("Client: Verbindung aufgebaut");
initIO();
lg.info("Client: warte auf Nachricht");
connected = true;
} catch (java.io.InterruptedIOException e) {
lg.warning("Timeout" + "(" + timeout / 1000 + "s)");
}

+ 2
- 3
src/ChatProgramm/model/Server.java View File

@@ -28,13 +28,12 @@ public class Server extends Transmitter
socket = sSocket.accept();
lg.info("Server: Verbindung akzeptiert");
initIO();
connected = true;
}
catch ( java.io.InterruptedIOException e )
{
lg.warning("Timeout"+"("+timeout/1000+"s)");
}
}
public Server() throws IOException{
connect();
}
public Server() {}
}

+ 14
- 4
src/ChatProgramm/model/Transmitter.java View File

@@ -34,11 +34,13 @@ 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 String receivedString;
public Transmitter()
{
@@ -46,6 +48,8 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
laufend = false;
receive = null;
textPublisher = new SubmissionPublisher<>();
connected = false;
receivedString = "";
}
public void addWertSubscription(Subscriber<Nachricht> subscriber)
@@ -54,6 +58,10 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
}

public abstract void connect() throws IOException;
public boolean isConnected(){
return connected;
}

public void initIO() {
try {
@@ -66,9 +74,9 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {

reader = new BufferedReader(isr);
writer = new PrintWriter(osr);
lg.info("Server: Initialisierung abgeschlossen");
lg.info("Reader / Writer Initialisierung abgeschlossen");
startempfangen();
lg.info("Server: warte auf Nachricht");
lg.info("Warte auf Nachricht");

} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
@@ -101,16 +109,18 @@ public abstract class Transmitter implements Runnable, Subscriber<String> {
@Override
public void run() {
while(laufend){

try
{
String receivedString = reader.readLine(); // ACHTUNG blockiert
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");
lg.info("Nachricht erhalten:" + receivedString);
}
}


Loading…
Cancel
Save