Browse Source

send versucht zu implementieren

Toni
ahren 1 year ago
parent
commit
efecd40ab4

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

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.model.Transmitter;
import ChatProgramm.view.ChatView; import ChatProgramm.view.ChatView;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
{ {
//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);
CommandController controller_commands = new CommandController(view, server, client);
controller_commands.registerEvents(); controller_commands.registerEvents();
controller_commands.registerCommands(); controller_commands.registerCommands();
view.setVisible(true); view.setVisible(true);

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

import ChatProgramm.controller.commands.CommandConnect; import ChatProgramm.controller.commands.CommandConnect;
import ChatProgramm.controller.commands.CommandInvoker; import ChatProgramm.controller.commands.CommandInvoker;
import ChatProgramm.controller.commands.CommandSend; import ChatProgramm.controller.commands.CommandSend;
import ChatProgramm.model.Client;
import ChatProgramm.model.Server;
import ChatProgramm.model.Transmitter; import ChatProgramm.model.Transmitter;
import ChatProgramm.view.ChatView; import ChatProgramm.view.ChatView;
import java.awt.Component; import java.awt.Component;
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;
// this.invoker = new CommandInvoker(); // this.invoker = new CommandInvoker();
// } // }
public CommandController(ChatView view){
public CommandController(ChatView view, Server server, Client client){
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(){
} }
public void registerCommands(){ 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

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;
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 final Server server;
public CommandConnect(ChatView view)
public CommandConnect(ChatView view, Server server, Client client)
{ {
rBtnServer = view.getBtnServer(); rBtnServer = view.getBtnServer();
rBtnClient = view.getBtnClient(); rBtnClient = view.getBtnClient();
dialogFenster = view.getjDialog1(); dialogFenster = view.getjDialog1();
this.client = client;
this.server = server;
} }


@Override @Override
if(rBtnServer.isSelected()){ if(rBtnServer.isSelected()){
lg.info("Server ausgewählt"); lg.info("Server ausgewählt");
try { try {
new Server();
server.connect();
} catch (IOException ex) { } catch (IOException ex) {
lg.info("Die Verbindung zum Server ist Fehlgeschlagen"); lg.info("Die Verbindung zum Server ist Fehlgeschlagen");
} }
if(rBtnClient.isSelected()){ if(rBtnClient.isSelected()){
lg.info("Client ausgewählt"); lg.info("Client ausgewählt");
try { try {
new Client();
client.connect();
} catch (IOException ex) { } catch (IOException ex) {
lg.info("Die Verbindung zum Client ist Fehlgeschlagen"); lg.info("Die Verbindung zum Client ist Fehlgeschlagen");



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



package ChatProgramm.controller.commands; package ChatProgramm.controller.commands;


import ChatProgramm.model.Client;
import ChatProgramm.model.Server;
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;
{ {
private static Logger lg = OhmLogger.getLogger(); private static Logger lg = OhmLogger.getLogger();
private JTextField tfNachricht; 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(); tfNachricht = view.getTfNachricht();
nachricht = tfNachricht.getText();
this.client = client;
this.server = server;
} }


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


@Override @Override

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

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¹⁵
private static final String IP = "127.0.0.1"; private static final String IP = "127.0.0.1";


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




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

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

socket = sSocket.accept(); socket = sSocket.accept();
lg.info("Server: Verbindung akzeptiert"); lg.info("Server: Verbindung akzeptiert");
initIO(); 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() throws IOException{
connect();
}
public Server() {}
} }

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

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 Thread receive;
private SubmissionPublisher<Nachricht> textPublisher; private SubmissionPublisher<Nachricht> textPublisher;
private String receivedString;
public Transmitter() public Transmitter()
{ {
laufend = false; laufend = false;
receive = null; receive = null;
textPublisher = new SubmissionPublisher<>(); textPublisher = new SubmissionPublisher<>();
connected = false;
receivedString = "";
} }
public void addWertSubscription(Subscriber<Nachricht> subscriber) public void addWertSubscription(Subscriber<Nachricht> subscriber)
} }


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 {


reader = new BufferedReader(isr); reader = new BufferedReader(isr);
writer = new PrintWriter(osr); writer = new PrintWriter(osr);
lg.info("Server: Initialisierung abgeschlossen");
lg.info("Reader / Writer Initialisierung abgeschlossen");
startempfangen(); startempfangen();
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);
@Override @Override
public void run() { public void run() {
while(laufend){ while(laufend){

try try
{ {
String receivedString = reader.readLine(); // ACHTUNG blockiert
receivedString = reader.readLine(); // ACHTUNG blockiert
textPublisher.submit(new Nachricht(receivedString)); textPublisher.submit(new Nachricht(receivedString));
} }
catch (IOException ex) catch (IOException ex)
{ {
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, 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