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 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 kommunikation.transmitter;
  7. import java.util.Observable;
  8. import java.util.logging.Logger;
  9. import javax.swing.JOptionPane;
  10. import kommunikation.logger.OhmLogger;
  11. /**
  12. *
  13. * @author Alexander_Christoph
  14. */
  15. public class Transmitter extends Observable implements Runnable
  16. {
  17. protected static final Logger lg = OhmLogger.getLogger();
  18. private Kommunikationspartner kom;
  19. private Boolean verbindung;
  20. private Thread thd;
  21. public Transmitter(int modus)
  22. {
  23. if (modus == 0)
  24. {
  25. kom = new Client();
  26. }
  27. if (modus == 1)
  28. {
  29. kom = new Server();
  30. }
  31. verbindung = false;
  32. thd = null;
  33. }
  34. public void start()
  35. {
  36. if (thd == null)
  37. {
  38. thd = new Thread(this);
  39. thd.start();
  40. }
  41. }
  42. @Override
  43. public void run()
  44. {
  45. while (true)
  46. {
  47. if (!verbindung)
  48. {
  49. kom.verbinde();
  50. verbindung = true;
  51. }
  52. synchronized (this)
  53. {
  54. kom.empfangen();
  55. }
  56. lg.info("empfangen");
  57. this.setChanged();
  58. this.notifyObservers();
  59. }
  60. }
  61. public void setIpPort(String ip, int port)
  62. {
  63. kom.setPort(port);
  64. kom.setIp(ip);
  65. }
  66. public void senden(Object nachricht)
  67. {
  68. if (verbindung)
  69. {
  70. kom.senden(nachricht);
  71. }
  72. else
  73. {
  74. JOptionPane.showMessageDialog(null, "Bauen Sie zuerst eine Verbindung auf", "Keine Verbindung", JOptionPane.INFORMATION_MESSAGE);
  75. }
  76. }
  77. public Object getNachricht()
  78. {
  79. return kom.getNachricht();
  80. }
  81. }