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.

Transmitter.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  3. * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
  4. */
  5. package ChatProgramm.model;
  6. import ChatProgramm.view.ChatView;
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStream;
  12. import java.io.OutputStreamWriter;
  13. import java.io.PrintWriter;
  14. import java.io.UnsupportedEncodingException;
  15. import java.net.Socket;
  16. import java.util.concurrent.ExecutorService;
  17. import java.util.concurrent.Executors;
  18. import java.util.concurrent.Flow;
  19. import java.util.concurrent.Flow.Subscriber;
  20. import java.util.concurrent.SubmissionPublisher;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. /**
  24. *
  25. * @author ahren
  26. */
  27. public abstract class Transmitter implements Runnable, Subscriber<String>, TransmitterInterface {
  28. static final int timeout = 60000;
  29. private static final int PORT = 35000;
  30. private static Logger lg = Logger.getLogger("netz");
  31. protected Socket socket;
  32. protected BufferedReader reader;
  33. protected PrintWriter writer;
  34. private Nachricht nachricht;
  35. private String txtNachricht;
  36. private boolean laufend;
  37. private SubmissionPublisher<Nachricht> textPublisher;
  38. private ExecutorService eService;
  39. private String receivedString;
  40. private ChatView view;
  41. private ReceiveAdapter receiveAdapter;
  42. public Transmitter(ChatView view)
  43. {
  44. socket = new Socket();
  45. eService = null;
  46. receiveAdapter = new ReceiveAdapter(view);
  47. textPublisher = new SubmissionPublisher<>();
  48. this.view = view;
  49. addWertSubscription(receiveAdapter);
  50. nachricht = new Nachricht("");
  51. }
  52. public void addWertSubscription(Subscriber<Nachricht> subscriber)
  53. {
  54. textPublisher.subscribe(subscriber);
  55. }
  56. public abstract void connect() throws IOException;
  57. public void initIO() {
  58. try {
  59. lg.info("Initialisiere reader und writer");
  60. InputStream is = socket.getInputStream();
  61. OutputStream os = socket.getOutputStream();
  62. InputStreamReader isr = new InputStreamReader(is, "UTF-8");
  63. OutputStreamWriter osr = new OutputStreamWriter(os, "UTF-8");
  64. reader = new BufferedReader(isr);
  65. writer = new PrintWriter(osr);
  66. lg.info("Reader / Writer Initialisierung abgeschlossen");
  67. startempfangen();
  68. lg.info("Warte auf Nachricht");
  69. } catch (UnsupportedEncodingException ex) {
  70. Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
  71. } catch (IOException ex) {
  72. Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
  73. }
  74. }
  75. public void send(String txtNachricht){
  76. writer.println(txtNachricht);
  77. writer.flush();
  78. lg.info("Nachricht gesendet");
  79. nachricht.setNachricht("\nDu: " + txtNachricht);
  80. textPublisher.submit(nachricht);
  81. }
  82. public Nachricht receive(){
  83. try {
  84. txtNachricht = reader.readLine();
  85. if(!txtNachricht.isEmpty()){
  86. lg.info("Nachricht erhalten");
  87. nachricht.setNachricht("\nEr / Sie: " + txtNachricht);
  88. return nachricht;
  89. }
  90. } catch (IOException e) {
  91. throw new RuntimeException(e);
  92. }
  93. return nachricht;
  94. }
  95. // public void disconnect (){
  96. // in.close();
  97. // out.close();
  98. // s.close();
  99. // }
  100. @Override
  101. public void run() {
  102. while (true) {
  103. lg.info("Warte auf Nachricht");
  104. if(laufend) {
  105. nachricht = receive();
  106. if(!nachricht.getNachricht().isEmpty()){
  107. textPublisher.submit(nachricht);
  108. }
  109. }
  110. else{
  111. break;
  112. }
  113. }
  114. }
  115. @Override
  116. public void onSubscribe(Flow.Subscription subscription) {
  117. throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
  118. }
  119. @Override
  120. public void onNext(String item) {
  121. throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
  122. }
  123. @Override
  124. public void onError(Throwable throwable) {
  125. throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
  126. }
  127. @Override
  128. public void onComplete() {
  129. throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
  130. }
  131. private void startempfangen()
  132. {
  133. synchronized (this){
  134. laufend = true;
  135. }
  136. if (eService == null){
  137. eService = Executors.newSingleThreadExecutor();
  138. eService.execute(this);
  139. }
  140. lg.info("Starte Chat");
  141. }
  142. }