Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. // This file contains material supporting section 3.7 of the textbook:
  2. // "Object Oriented Software Engineering" and is issued under the open-source
  3. // license found at www.lloseng.com
  4.  
  5. package client;
  6.  
  7. import ocsf.client.*;
  8. import common.*;
  9. import java.io.*;
  10.  
  11. /**
  12. * This class overrides some of the methods defined in the abstract
  13. * superclass in order to give more functionality to the client.
  14. *
  15. * @author Dr Timothy C. Lethbridge
  16. * @author Dr Robert Laganiè
  17. * @author François Bélanger
  18. * @version July 2000
  19. */
  20. public class ChatClient extends AbstractClient
  21. {
  22. //Instance variables **********************************************
  23.  
  24. /**
  25. * The interface type variable. It allows the implementation of
  26. * the display method in the client.
  27. */
  28. ChatIF clientUI;
  29.  
  30.  
  31. //Constructors ****************************************************
  32.  
  33. /**
  34. * Constructs an instance of the chat client.
  35. *
  36. * @param host The server to connect to.
  37. * @param port The port number to connect on.
  38. * @param clientUI The interface type variable.
  39. */
  40.  
  41. public ChatClient(String host, int port, ChatIF clientUI)
  42. throws IOException
  43. {
  44. super(host, port); //Call the superclass constructor
  45. this.clientUI = clientUI;
  46. openConnection();
  47. }
  48.  
  49.  
  50. //Instance methods ************************************************
  51.  
  52. /**
  53. * This method handles all data that comes in from the server.
  54. *
  55. * @param msg The message from the server.
  56. */
  57. public void handleMessageFromServer(Object msg)
  58. {
  59. clientUI.display(msg.toString());
  60. }
  61.  
  62. /**
  63. * This method handles all data coming from the UI
  64. *
  65. * @param message The message from the UI.
  66. */
  67. public void handleMessageFromClientUI(String message)
  68. {
  69. try
  70. {
  71. sendToServer(message);
  72. }
  73. catch(IOException e)
  74. {
  75. clientUI.display
  76. ("Could not send message to server. Terminating client.");
  77. quit();
  78. }
  79. }
  80. //*** Change for E49 O.A.
  81. //* Message to be outputted once connection is closed
  82.  
  83. public void connectionClosed() {
  84.  
  85. clientUI.display
  86. ("Server shutdown. Terminating client.");
  87.  
  88. }
  89.  
  90.  
  91. //*** Change for E49 O.A.
  92. //* Exception that needs to be catched when the server shutsdown
  93. public void connectionException(Exception exception) {
  94. if(exception instanceof IOException) {
  95. quit();
  96. }
  97. }
  98.  
  99.  
  100. /**
  101. * This method terminates the client.
  102. */
  103. public void quit()
  104. {
  105. try
  106. {
  107. closeConnection();
  108. }
  109. catch(IOException e) {}
  110. System.exit(0);
  111. }
  112. }
  113. //End of ChatClient class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement