You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatModel.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package netz.model;
  7. import ohmlogger.OhmLogger;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.OutputStream;
  13. import java.io.OutputStreamWriter;
  14. import java.io.PrintWriter;
  15. import java.net.ServerSocket;
  16. import java.net.Socket;
  17. import java.util.concurrent.ExecutorService;
  18. import java.util.concurrent.Executors;
  19. import java.util.concurrent.Flow;
  20. import java.util.concurrent.SubmissionPublisher;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. /**
  24. *
  25. * @author hd, chris
  26. */
  27. public class ChatModel implements Runnable
  28. {
  29. private static Logger lg = OhmLogger.getLogger();
  30. private ExecutorService eService;
  31. private SubmissionPublisher<String> iPublisher;
  32. private volatile boolean laufend;
  33. String nachricht;
  34. BufferedReader in;
  35. PrintWriter out;
  36. private Socket socket;
  37. public ChatModel()
  38. {
  39. laufend = false;
  40. iPublisher = new SubmissionPublisher<>();
  41. eService = Executors.newSingleThreadExecutor();
  42. }
  43. public void addSubscription(Flow.Subscriber<String> subscriber)
  44. {
  45. iPublisher.subscribe(subscriber);
  46. }
  47. public synchronized void start(){
  48. laufend = true;
  49. eService.submit(this);
  50. this.notifyAll();//muss sync
  51. lg.info("startet");
  52. }
  53. @Override
  54. public void run()
  55. {
  56. while(true){
  57. try {
  58. nachricht = in.readLine();
  59. lg.info("Nachricht empfangen: " + nachricht);
  60. iPublisher.submit(nachricht);//wenn neue Nachricht
  61. } catch (IOException ex) {
  62. lg.log(Level.SEVERE, ex.toString());
  63. }
  64. }
  65. }
  66. public void setSocket(Socket s) throws IOException {
  67. InputStream iStream = s.getInputStream();
  68. OutputStream oStream = s.getOutputStream();
  69. InputStreamReader isr = new InputStreamReader(iStream, "UTF-8");
  70. OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8");
  71. in = new BufferedReader(isr);
  72. //BufferedWriter out = new BufferedWriter(osr);
  73. out = new PrintWriter(osr);
  74. start();
  75. }
  76. public void setClient(int PORT, String IP_ADRESSE) throws IOException
  77. {
  78. Thread connectThread = new Thread(new Runnable() {
  79. @Override
  80. public void run() {
  81. try {
  82. lg.info("Client: verbinde ...");
  83. Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
  84. lg.info("Client: Verbindung hergestellt");
  85. setSocket(s);
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. });
  91. connectThread.start();
  92. }
  93. public void setServer(int PORT) throws IOException
  94. {
  95. Thread connectThread = new Thread(new Runnable() {
  96. @Override
  97. public void run() {
  98. try {
  99. ServerSocket sSocket = new ServerSocket(PORT);
  100. lg.info("Server: Warte auf Verbindung ...");
  101. Socket s = sSocket.accept(); // Achtung: blockiert!
  102. lg.info("Server: Verbindung akzeptiert");
  103. setSocket(s);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. });
  109. connectThread.start();
  110. }
  111. public void sendMessage(String msg)
  112. {
  113. if(laufend){
  114. lg.log(Level.INFO, "Sende Nachricht: " + msg);
  115. out.println(msg);
  116. out.flush();
  117. }
  118. else{
  119. lg.log(Level.INFO, "Keine Verbindung!");
  120. }
  121. }
  122. }