forked from TimLethbridge/Lloseng
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServerConsole.java
More file actions
171 lines (141 loc) · 3.95 KB
/
ServerConsole.java
File metadata and controls
171 lines (141 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// 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 client.*;
import common.*;
import java.util.Arrays;
import java.util.List;
/**
* This class constructs the UI for a chat client. It implements the
* chat interface in order to activate the display() method.
* Warning: Some of the code here is cloned in ServerConsole
*
* @author François Bélanger
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganière
* @version July 2000
*/
public class ServerConsole implements ChatIF
{
//Class variables *************************************************
/**
* The default port to connect on.
*/
final public static int DEFAULT_PORT = 5555;
//Instance variables **********************************************
/**
* The instance of the client that created this ConsoleChat.
*/
ChatClient client;
/**
* The instance of EchoServer for this Console.
*/
static EchoServer echo;
//Constructors ****************************************************
/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ServerConsole(int port)
{
try
{
echo = new EchoServer(port);
}
catch(Exception e)
{
System.out.println("SYSTEM ::: Could not setup server");
System.exit(1);
}
}
//Instance methods ************************************************
/**
* This method waits for input from the console. Once it is
* received, it sends it to the client's message handler.
*/
public void accept()
{
try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;
while (true)
{
message = fromConsole.readLine();
message = "SERVER MSG> " + message;
//This is where I decide what to do with the code.
if ( message.charAt(12) == '#' ){
echo.serverCommand(message);}
else {echo.handleMessageFromServerConsole(message);}
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}
}
/**
* This method overrides the method in the ChatIF interface. It
* displays a message onto the screen.
*
* @param message The string to be displayed.
*/
public void display(String message)
{
System.out.println("> " + message);
}
/**
* Hook method called after the connection has been closed. The default
* implementation does nothing. The method may be overriden by subclasses to
* perform special processing such as cleaning up and terminating, or
* attempting to reconnect.
*/
protected void connectionClosed() {
System.out.println("The server has shutdown.");
}
/**
* Hook method called each time an exception is thrown by the client's
* thread that is waiting for messages from the server. The method may be
* overridden by subclasses.
*
* @param exception
* the exception raised.
*/
protected void connectionException(Exception exception) {
connectionClosed();
}
//Class methods ***************************************************
/**
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
*/
public static void main(String[] args)
{
int port = 0; //The port number
try
{
port = Integer.parseInt(args[0]);
}
catch(ArrayIndexOutOfBoundsException e)
{
port = DEFAULT_PORT;
}
ServerConsole chat= new ServerConsole(port);
try
{
echo.listen(); //Start listening for connections
}
catch (Exception ex)
{
System.out.println("ERROR - Could not listen for clients!");
}
chat.accept(); //Wait for console data
}
}
//End of ConsoleChat class