123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
- package netz.model;
-
-
- import ohmlogger.OhmLogger;
-
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- 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.SubmissionPublisher;
- import java.util.logging.Level;
- import java.util.logging.Logger;
-
-
- /**
- *
- * @author hd, chris
- */
- public class ChatModel implements Runnable
- {
- private static Logger lg = OhmLogger.getLogger();
-
- private ExecutorService eService;
- private SubmissionPublisher<String> iPublisher;
- private volatile boolean laufend;
-
- String nachricht;
-
- BufferedReader in;
- PrintWriter out;
-
- private Socket socket;
-
- public ChatModel()
- {
- laufend = false;
- iPublisher = new SubmissionPublisher<>();
- eService = Executors.newSingleThreadExecutor();
- }
-
- public void addSubscription(Flow.Subscriber<String> subscriber)
- {
- iPublisher.subscribe(subscriber);
- }
-
- public synchronized void start(){
- laufend = true;
- eService.submit(this);
- this.notifyAll();//muss sync
- lg.info("startet");
- }
-
- @Override
- public void run()
- {
- while(true){
- try {
- nachricht = in.readLine();
- lg.info("Nachricht empfangen: " + nachricht);
- iPublisher.submit(nachricht);//wenn neue Nachricht
- } catch (IOException ex) {
- lg.log(Level.SEVERE, ex.toString());
- }
- }
- }
-
- public void setSocket(Socket s) throws IOException {
- InputStream iStream = s.getInputStream();
- OutputStream oStream = s.getOutputStream();
-
- InputStreamReader isr = new InputStreamReader(iStream, "UTF-8");
- OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8");
-
- in = new BufferedReader(isr);
- //BufferedWriter out = new BufferedWriter(osr);
- out = new PrintWriter(osr);
- start();
- }
- public void setClient(int PORT, String IP_ADRESSE) throws IOException
- {
- Thread connectThread = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- lg.info("Client: verbinde ...");
- Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
- lg.info("Client: Verbindung hergestellt");
- setSocket(s);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- connectThread.start();
- }
- public void setServer(int PORT) throws IOException
- {
- Thread connectThread = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- ServerSocket sSocket = new ServerSocket(PORT);
- lg.info("Server: Warte auf Verbindung ...");
- Socket s = sSocket.accept(); // Achtung: blockiert!
- lg.info("Server: Verbindung akzeptiert");
- setSocket(s);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- connectThread.start();
- }
-
- public void sendMessage(String msg)
- {
- if(laufend){
- lg.log(Level.INFO, "Sende Nachricht: " + msg);
- out.println(msg);
- out.flush();
- }
- else{
- lg.log(Level.INFO, "Keine Verbindung!");
- }
- }
- }
|