Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. package main;
  2.  
  3. //This file contains material supporting section 3.7 of the textbook:
  4. //"Object Oriented Software Engineering" and is issued under the open-source
  5. //license found at www.lloseng.com
  6.  
  7. import java.io.*;
  8. import com.lloseng.ocsf.server.*;
  9.  
  10. import common.ChatIF;
  11.  
  12. /**
  13. * This class overrides some of the methods in the abstract superclass in order
  14. * to give more functionality to the server.
  15. *
  16. * @author Dr Timothy C. Lethbridge
  17. * @author Dr Robert Laganière
  18. * @author François Bélanger
  19. * @author Paul Holden
  20. * @version July 2000
  21. */
  22. public class EchoServer extends AbstractServer {
  23. //Class variables *************************************************
  24.  
  25. /**
  26. * The default port to listen on.
  27. */
  28. final public static int DEFAULT_PORT = 5555;
  29. ChatIF serverUI;
  30.  
  31. //Constructors ****************************************************
  32.  
  33. /**
  34. * Constructs an instance of the echo server.
  35. *
  36. * @param port The port number to connect on.
  37. */
  38. public EchoServer(int port) {
  39. super(port);
  40. }
  41.  
  42. public EchoServer(int port, ChatIF serverUI) throws IOException {
  43. super(port);
  44. this.serverUI = serverUI;
  45. }
  46.  
  47. //Instance methods ************************************************
  48.  
  49. /**
  50. * This method handles any messages received from the client.
  51. *
  52. * @param msg The message received from the client.
  53. * @param client The connection from which the message originated.
  54. */
  55. public void handleMessageFromClient(Object msg, ConnectionToClient client) {
  56. System.out.println("Message received: " + msg + " from " + client);
  57. this.sendToAllClients(msg);
  58. }
  59.  
  60. /**
  61. * This method overrides the one in the superclass. Called when the server
  62. * starts listening for connections.
  63. */
  64. protected void serverStarted() {
  65. System.out.println("Server listening for connections on port " + getPort());
  66. }
  67.  
  68. /**
  69. * This method overrides the one in the superclass. Called when the server stops
  70. * listening for connections.
  71. */
  72. protected void serverStopped() {
  73. System.out.println("Server has stopped listening for connections.");
  74. }
  75.  
  76. /**
  77. * This method overrides the one in the superclass. Called when the server
  78. * stops.
  79. *
  80. */
  81.  
  82. /**
  83. * This method overrides the one in the superclass. Called when the server a
  84. * client is connected to the server.
  85. */
  86. protected void clientConnected(ConnectionToClient client) {
  87. System.out.println("Client " + client + " connected to the server.");
  88. }
  89.  
  90. /**
  91. * This method overrides the one in the superclass. Called when the server a
  92. * client is disconnected from the server.
  93. */
  94. synchronized protected void clientDisconnected(ConnectionToClient client) {
  95. System.out.println("Client disconnected from the server.");
  96. }
  97.  
  98. //Class methods ***************************************************
  99.  
  100. public void handleMessageFromServerUI(String message) {
  101. try {
  102. String[] splitMessage = message.split(" ");
  103. if (message.equals("#quit")) {
  104. this.quit();
  105. } else if (message.equals("#stop")) {
  106. this.stopListening();
  107. } else if (message.equals("#close")) {
  108. this.close();
  109. } else if (message.equals("#start")) {
  110. if (this.isListening() == false) {
  111. this.listen();
  112. } else {
  113. serverUI.display("The server is already listening");
  114. }
  115.  
  116. } else if (message.equals("#getport")) {
  117. serverUI.display(Integer.toString(this.getPort()));
  118. } else if (splitMessage[0].equals("#setport")) {
  119. if (this.isClosed()) {
  120. this.setPort(Integer.parseInt(splitMessage[0]));
  121. } else {
  122. serverUI.display("The port cannot be changed because the server is not closed");
  123. }
  124. } else {
  125. serverUI.display(message);
  126. sendToAllClients("SERVER MSG > " + message);
  127. }
  128. } catch (Exception e) {
  129.  
  130. serverUI.display("Cannot send message to clients");
  131. }
  132.  
  133. }
  134.  
  135. /**
  136. * This method is responsible for the creation of the server instance (there is
  137. * no UI in this phase).
  138. *
  139. * @param args[0] The port number to listen on. Defaults to 5555 if no argument
  140. * is entered.
  141. */
  142. public static void main(String[] args) {
  143. int port = 0; // Port to listen on
  144.  
  145. try {
  146. port = Integer.parseInt(args[0]); // Get port from command line
  147. } catch (Throwable t) {
  148. port = DEFAULT_PORT; // Set port to 5555
  149. }
  150.  
  151. EchoServer sv = new EchoServer(port);
  152.  
  153. try {
  154. sv.listen(); // Start listening for connections
  155. } catch (Exception ex) {
  156. System.out.println("ERROR - Could not listen for clients!");
  157. }
  158. }
  159.  
  160. public void quit() {
  161. try {
  162. close();
  163. } catch (IOException e) {
  164. }
  165. System.exit(0);
  166. }
  167.  
  168. }
  169. //End of EchoServer class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement