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.

ConnectController.java 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 chatprogramm.controller;
  7. import chatprogramm.logger.OhmLogger;
  8. import chatprogramm.model.Transmitter;
  9. import chatprogramm.view.ChatView;
  10. import java.io.IOException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.JOptionPane;
  14. /**
  15. *
  16. * @author Gerhard
  17. */
  18. public class ConnectController
  19. {
  20. private ChatView view;
  21. private Transmitter model;
  22. private static final Logger logger = OhmLogger.getLogger();
  23. private String ip = null;
  24. private int port = 0;
  25. public ConnectController(ChatView view, Transmitter model)
  26. {
  27. this.view = view;
  28. this.model = model;
  29. chooseConnection();
  30. }
  31. void chooseConnection()
  32. {
  33. Object[] options = {"Client", "Server"};
  34. int choice = JOptionPane.showOptionDialog(view, "Wähle deine Verbindungsart:", "Client oder Server", 0, 1, null, options, null);
  35. if(choice == 1)
  36. {
  37. logger.info("Server");
  38. String port = JOptionPane.showInputDialog(view, "PORT eingeben");
  39. logger.info("Port für Server ist: localhost:" + port);
  40. startServer();
  41. }
  42. else
  43. {
  44. logger.info("Client");
  45. port = Integer.parseInt(JOptionPane.showInputDialog(view, "PORT eingeben"));
  46. ip = JOptionPane.showInputDialog(view, "IP vom Server bitte");
  47. logger.info("Client IP Adresse und Port ist: " + ip + ":" + port);
  48. startClient();
  49. }
  50. }
  51. public void startServer()
  52. {
  53. try
  54. {
  55. model.createServer(port);
  56. }
  57. catch (IOException ex)
  58. {
  59. Logger.getLogger(ConnectController.class.getName()).log(Level.SEVERE, null, ex);
  60. }
  61. }
  62. public void startClient()
  63. {
  64. try
  65. {
  66. model.createClient(port, ip);
  67. }
  68. catch (IOException ex)
  69. {
  70. Logger.getLogger(ConnectController.class.getName()).log(Level.SEVERE, null, ex);
  71. }
  72. }
  73. }