-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatClient.java
94 lines (81 loc) · 2.14 KB
/
ChatClient.java
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
// 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
package client;
import ocsf.client.*;
import common.*;
import java.io.*;
/**
* This class overrides some of the methods defined in the abstract
* superclass in order to give more functionality to the client.
*
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganiè
* @author François Bélanger
* @version July 2000
*/
public class ChatClient extends AbstractClient
{
//Instance variables **********************************************
/**
* The interface type variable. It allows the implementation of
* the display method in the client.
*/
ChatIF clientUI;
//Constructors ****************************************************
/**
* Constructs an instance of the chat client.
*
* @param host The server to connect to.
* @param port The port number to connect on.
* @param clientUI The interface type variable.
*/
public ChatClient(String host, int port, ChatIF clientUI)
throws IOException
{
super(host, port); //Call the superclass constructor
this.clientUI = clientUI;
openConnection();
}
//Instance methods ************************************************
/**
* This method handles all data that comes in from the server.
*
* @param msg The message from the server.
*/
public void handleMessageFromServer(Object msg)
{
clientUI.display(msg.toString());
}
/**
* This method handles all data coming from the UI
*
* @param message The message from the UI.
*/
public void handleMessageFromClientUI(String message)
{
try
{
sendToServer(message);
}
catch(IOException e)
{
clientUI.display
("Could not send message to server. Terminating client.");
quit();
}
}
/**
* This method terminates the client.
*/
public void quit()
{
try
{
closeConnection();
}
catch(IOException e) {}
System.exit(0);
}
}
//End of ChatClient class