Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- //This file contains material supporting section 3.7 of the textbook:
- //"Object Oriented Software Engineering" and is issued under the open-source
- //license found at www.lloseng.com
- import java.io.*;
- import com.lloseng.ocsf.server.*;
- import common.ChatIF;
- /**
- * This class overrides some of the methods in the abstract superclass in order
- * to give more functionality to the server.
- *
- * @author Dr Timothy C. Lethbridge
- * @author Dr Robert Laganière
- * @author François Bélanger
- * @author Paul Holden
- * @version July 2000
- */
- public class EchoServer extends AbstractServer {
- //Class variables *************************************************
- /**
- * The default port to listen on.
- */
- final public static int DEFAULT_PORT = 5555;
- ChatIF serverUI;
- //Constructors ****************************************************
- /**
- * Constructs an instance of the echo server.
- *
- * @param port The port number to connect on.
- */
- public EchoServer(int port) {
- super(port);
- }
- public EchoServer(int port, ChatIF serverUI) throws IOException {
- super(port);
- this.serverUI = serverUI;
- }
- //Instance methods ************************************************
- /**
- * This method handles any messages received from the client.
- *
- * @param msg The message received from the client.
- * @param client The connection from which the message originated.
- */
- public void handleMessageFromClient(Object msg, ConnectionToClient client) {
- System.out.println("Message received: " + msg + " from " + client);
- this.sendToAllClients(msg);
- }
- /**
- * This method overrides the one in the superclass. Called when the server
- * starts listening for connections.
- */
- protected void serverStarted() {
- System.out.println("Server listening for connections on port " + getPort());
- }
- /**
- * This method overrides the one in the superclass. Called when the server stops
- * listening for connections.
- */
- protected void serverStopped() {
- System.out.println("Server has stopped listening for connections.");
- }
- /**
- * This method overrides the one in the superclass. Called when the server
- * stops.
- *
- */
- /**
- * This method overrides the one in the superclass. Called when the server a
- * client is connected to the server.
- */
- protected void clientConnected(ConnectionToClient client) {
- System.out.println("Client " + client + " connected to the server.");
- }
- /**
- * This method overrides the one in the superclass. Called when the server a
- * client is disconnected from the server.
- */
- synchronized protected void clientDisconnected(ConnectionToClient client) {
- System.out.println("Client disconnected from the server.");
- }
- //Class methods ***************************************************
- public void handleMessageFromServerUI(String message) {
- try {
- String[] splitMessage = message.split(" ");
- if (message.equals("#quit")) {
- this.quit();
- } else if (message.equals("#stop")) {
- this.stopListening();
- } else if (message.equals("#close")) {
- this.close();
- } else if (message.equals("#start")) {
- if (this.isListening() == false) {
- this.listen();
- } else {
- serverUI.display("The server is already listening");
- }
- } else if (message.equals("#getport")) {
- serverUI.display(Integer.toString(this.getPort()));
- } else if (splitMessage[0].equals("#setport")) {
- if (this.isClosed()) {
- this.setPort(Integer.parseInt(splitMessage[0]));
- } else {
- serverUI.display("The port cannot be changed because the server is not closed");
- }
- } else {
- serverUI.display(message);
- sendToAllClients("SERVER MSG > " + message);
- }
- } catch (Exception e) {
- serverUI.display("Cannot send message to clients");
- }
- }
- /**
- * This method is responsible for the creation of the server instance (there is
- * no UI in this phase).
- *
- * @param args[0] The port number to listen on. Defaults to 5555 if no argument
- * is entered.
- */
- public static void main(String[] args) {
- int port = 0; // Port to listen on
- try {
- port = Integer.parseInt(args[0]); // Get port from command line
- } catch (Throwable t) {
- port = DEFAULT_PORT; // Set port to 5555
- }
- EchoServer sv = new EchoServer(port);
- try {
- sv.listen(); // Start listening for connections
- } catch (Exception ex) {
- System.out.println("ERROR - Could not listen for clients!");
- }
- }
- public void quit() {
- try {
- close();
- } catch (IOException e) {
- }
- System.exit(0);
- }
- }
- //End of EchoServer class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement