public void registerCommands(){ | public void registerCommands(){ | ||||
invoker.addCommand(view.getBtnConnect(), new CommandConnect(view)); | invoker.addCommand(view.getBtnConnect(), new CommandConnect(view)); | ||||
invoker.addCommand(view.getTfNachricht(), new CommandSend()); | |||||
invoker.addCommand(view.getTfNachricht(), new CommandSend(view)); | |||||
} | } | ||||
/** | /** |
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||||
*/ | */ | ||||
package ChatProgramm.model; | |||||
package ChatProgramm.controller; | |||||
/** | /** | ||||
* | * | ||||
* @author ahren | * @author ahren | ||||
*/ | */ | ||||
class Nachricht | |||||
public class Nachricht | |||||
{ | { | ||||
private String nachricht; | private String nachricht; | ||||
//private int id; | //private int id; |
* 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/Class.java to edit this template | * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | ||||
*/ | */ | ||||
package ChatProgramm.model; | |||||
package ChatProgramm.controller; | |||||
import ChatProgramm.view.ChatView; | import ChatProgramm.view.ChatView; | ||||
import java.util.concurrent.Flow; | import java.util.concurrent.Flow; |
package ChatProgramm.controller.commands; | package ChatProgramm.controller.commands; | ||||
import ChatProgramm.controller.Nachricht; | |||||
import ChatProgramm.model.Client; | |||||
import ChatProgramm.model.Server; | |||||
import ChatProgramm.model.Transmitter; | |||||
import ChatProgramm.util.OhmLogger; | |||||
import ChatProgramm.view.ChatView; | |||||
import java.util.logging.Logger; | |||||
import javax.swing.JTextField; | |||||
/** | /** | ||||
* | * | ||||
* @author ahren | * @author ahren | ||||
*/ | */ | ||||
public class CommandSend implements CommandInterface | public class CommandSend implements CommandInterface | ||||
{ | { | ||||
public CommandSend() | |||||
private static Logger lg = OhmLogger.getLogger(); | |||||
private ChatView view; | |||||
private JTextField eingabeFeld; | |||||
public Server server; | |||||
public Client client; | |||||
public CommandSend(ChatView view) | |||||
{ | { | ||||
this.view = view; | |||||
this.eingabeFeld = view.getTfNachricht(); | |||||
} | } | ||||
@Override | @Override | ||||
public void execute() | public void execute() | ||||
{ | { | ||||
//Transmitter.send(new Nachricht(eingabeFeld.getText())); | |||||
lg.info("Sende Nachricht"); | |||||
} | } | ||||
@Override | @Override |
*/ | */ | ||||
package ChatProgramm.model; | package ChatProgramm.model; | ||||
import ChatProgramm.controller.Nachricht; | |||||
import java.io.BufferedReader; | import java.io.BufferedReader; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
private static Logger lg = Logger.getLogger("netz"); | 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¹⁵ | |||||
private static final String IP = "127.0.0.1"; | |||||
public Client() throws IOException { | public Client() throws IOException { | ||||
connect(); | connect(); | ||||
} | } | ||||
} | } | ||||
@Override | |||||
public void onNext(Nachricht item) { | |||||
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody | |||||
} | |||||
} | } |
private static Logger lg = Logger.getLogger("netz"); | private static Logger lg = Logger.getLogger("netz"); | ||||
@Override | |||||
public void connect() throws IOException { | public void connect() throws IOException { | ||||
try { | try { | ||||
ServerSocket sSocket = new ServerSocket(PORT); | ServerSocket sSocket = new ServerSocket(PORT); | ||||
public Server() throws IOException { | public Server() throws IOException { | ||||
super(); | |||||
connect(); | connect(); | ||||
initIO(); | initIO(); | ||||
*/ | */ | ||||
package ChatProgramm.model; | package ChatProgramm.model; | ||||
import ChatProgramm.controller.Nachricht; | |||||
import java.io.BufferedReader; | import java.io.BufferedReader; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
*/ | */ | ||||
public abstract class Transmitter implements Runnable, Subscriber<Nachricht> { | public abstract class Transmitter implements Runnable, Subscriber<Nachricht> { | ||||
static final int timeout = 60000; | |||||
static final int timeout = 10000; | |||||
protected static final int PORT = 35000; | protected static final int PORT = 35000; | ||||
protected static final String IP = "127.0.0.1"; | protected static final String IP = "127.0.0.1"; | ||||
protected PrintWriter writer; | protected PrintWriter writer; | ||||
private SubmissionPublisher<Nachricht> textPublisher; | private SubmissionPublisher<Nachricht> textPublisher; | ||||
private Thread thd; | |||||
private boolean laufend; | |||||
public Transmitter() { | public Transmitter() { | ||||
socket = new Socket(); | |||||
} | } | ||||
public abstract void connect() throws IOException; | public abstract void connect() throws IOException; | ||||
public void addWertSubscription(Subscriber<Nachricht> subscriber) { | |||||
textPublisher.subscribe(subscriber); | |||||
} | |||||
public void initIO() { | public void initIO() { | ||||
try { | try { | ||||
lg.info("Initialisiere reader und writer"); | lg.info("Initialisiere reader und writer"); | ||||
} | } | ||||
} | } | ||||
public void addWertSubscription(Subscriber<Nachricht> subscriber) { | |||||
textPublisher.subscribe(subscriber); | |||||
public void send(Nachricht nachricht) { | |||||
lg.info("Nachricht wird gesendet"); | |||||
writer.println(nachricht.getNachricht()); | |||||
writer.flush(); | |||||
lg.info("Nachricht wird angezeigt"); | |||||
textPublisher.submit(nachricht); | |||||
} | } | ||||
// public void startServer() { | |||||
// laufend = true; | |||||
// if (thd == null) { | |||||
// thd = new Thread(this); | |||||
// thd.start(); | |||||
// } | |||||
// | |||||
// synchronized (thd) { | |||||
// thd.notify(); | |||||
// } | |||||
// | |||||
// } | |||||
@Override | @Override | ||||
public void run() { | public void run() { | ||||
try { | |||||
Nachricht nachricht = new Nachricht(reader.readLine()); | |||||
textPublisher.submit(nachricht); | |||||
} catch (IOException ex) { | |||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex); | |||||
} | |||||
} | } | ||||
@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 | ||||
} | } | ||||
@Override | @Override | ||||
public void onError(Throwable throwable) { | public void onError(Throwable throwable) { |