-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClientCSD.java
More file actions
259 lines (194 loc) · 8.12 KB
/
Copy pathChatClientCSD.java
File metadata and controls
259 lines (194 loc) · 8.12 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// @daibeal
// Polytechnic University of Valencia
import java.rmi.*;
import java.rmi.registry.*;
//
// Main Client process.
// It creates a user interface. On UI events that happen at the UI interface, this class
// methods are invoked to connect to the server, to retrieve its channels, to disconnect, etc.
//
// It also contains a basic message listener for one chat user.
//
//
public class ChatClientCSD
implements MessageListener
{
private ChatConfiguration conf;
private IChatServer srv = null; // We just connect to one single server
private IChatUser myUser = null; // Our own user
private ChatUI ui = null; // User interface for fancy UI-driven chatting.
private boolean bConnected = false;
private boolean prueba=true;
public ChatClientCSD (ChatConfiguration conf) {
this.conf = conf;
}
// Utility functions
private void setUI (ChatUI ui) {this.ui = ui;}
public String getDefaultServerName() {return conf.getServerName();}
//
// The first thing to do before chatting is to connect to a ChatServer!!
//
// For us, connect means to locate it, register a new user into it and retrieve its channel list.
// On success, returns the server channel list.
//
public String [] doConnect (String serverName, String nick) throws Exception {
// Locate server using the name service
try {
/* *********************************************/
//ACTIVITY 1. Locate ChatServer using the name server
//1.a Obtain a reference to the name server, using LocateRegistry.getRegistry.
//Store this reference in a variable of type "Registry".
Registry reg = LocateRegistry.getRegistry (conf.getNameServiceHost(),
conf.getNameServicePort());
//1.b Look for the object "ChatServer" in the name server, using the previous reference.
// Remember that the object name is stored in the variable "serverName" (input parameter of the "doConnect" method)
// Store the obtained remote reference in the variable "srv" (defined at the beginning of ChatClientCSD class)
srv = (IChatServer) reg.lookup(serverName);
//1.c Replace this previous line with the following lines for exception management:
} catch (java.rmi.ConnectException e) {
throw new Exception ("rmiregistry not found at '" +
conf.getNameServiceHost() + ":" + conf.getNameServicePort() + "'");
} catch (java.rmi.NotBoundException e) {
throw new Exception ("Server '" + serverName + "' not found.");
}
// Once we've got the server, we create a local user object and register it into the server
/* ******************************************************/
/* ACTIVITY 2. Create a local object "ChatUser" and register it in the server.
//2.a Create the ChatUser object, indicating as parameters the nick of the user and "this" as MessageListener.
*/
/* 2.b Connect the client to the ChatServer, using the method "connectUser" of ChatServer class.
// Launch an exception if there is any error.
*/
/* 2.c Obtain the list of channels, using the method "listChannels" of ChatServer. */
IChatChannel [] channels=null; // <---- 2.c Substitute "null" with the right value
if (channels == null || channels.length == 0)
throw new Exception ("Server has no channels");
// Convert channel list to string list, since we don't want the UI to know about invocable
// objects. It is a plain UI which does not depend on RMI,
String list [] = new String [channels.length];
for (int i=0; i<channels.length; i++) {
list[i] = channels[i].getName();
}
// Connected to server. Things went fine :)
bConnected = true;
return list;
}
//
// Disconnect allows server to free up resources and remove stale references.
//
public void doDisconnect () {
bConnected = false;
try {
if (myUser != null) srv.disconnectUser(myUser);
} catch (Exception e){}
}
//
// When users leave a chat lounge they exit the Channel using this method.
//
public void doLeaveChannel (String channelName) throws Exception {
IChatChannel ch = srv.getChannel (channelName);
if (ch != null) {
/* **************************** */
//ACTIVITY 3: JOIN A CHANNEL
//3.b Make that the user "myUser" leaves the channel "ch".
}
}
//
// To chat in a channel we require users to join.
// On success, returns the user list
//
public String [] doJoinChannel (String channelName) throws Exception {
IChatChannel ch = srv.getChannel (channelName);
if (ch == null) {throw new Exception ("Channel not found");}
/* **************************** */
//ACTIVITY 3: JOIN A CHANNEL
//3.a Make that the user "myUser" joins the channel "ch".
//Obtains the list of channel users
IChatUser [] users = ch.listUsers ();
if (users == null || users.length == 0)
throw new Exception ("BUG. Tell professor there are no users after joining");
String [] userList = new String [users.length];
for (int i=0; i<users.length; i++) {
userList[i] = users[i].getNick();
}
return userList;
}
//
// UI wants to send a message to a channel... lets do it creating a IChatMessage
//
public void doSendChannelMessage (String dst, String msg) throws Exception
{
try {
IChatChannel c_dst = srv.getChannel (dst);
IChatMessage c_msg = new ChatMessage(myUser, c_dst, msg);
/* **************************** */
//ACTIVITY 4: SENDING A MESSAGE TO A CHANNEL
//4.a Send the message to the destinationchannel.
} catch (Exception e) {
throw new Exception ("Cannot send message: " + e);
}
}
//
// UI wants to send a private message to some user... lets do it creating a IChatMessage
//
public void doSendPrivateMessage (String dst, String msg) throws Exception
{
try {
IChatUser u_dst = srv.getUser (dst);
IChatMessage c_msg = new ChatMessage(myUser, u_dst, msg);
if (u_dst == null) throw new Exception ("User disconnected");
/* **************************** */
//ACTIVITY 5: SENDING A MESSAGE TO A USER
//5.a Send the message to the destination user
} catch (Exception e) {
throw new Exception ("Cannot send message: " + e);
}
}
//
// On window close, try to disconnect
//
public void doTerminate () {
doDisconnect ();
System.exit (0);
}
//
// ISA MessageListener
// Messages come from a channel or from a remote user.
//
public void messageArrived (IChatMessage msg) {
try {
IChatUser src = msg.getSender();
Remote dst = msg.getDestination();
String str = msg.getText();
if (msg.isPrivate()) {
IChatUser u_dst = (IChatUser) dst;
ui.showPrivateMessage (src.getNick(), u_dst.getNick(), str);
} else {
IChatChannel c_dst = (IChatChannel) dst;
if (src == null) { // Control message from the channel itself
String nick = null;
if (str.startsWith (ChatChannel.LEAVE)) {
nick = str.substring (ChatChannel.LEAVE.length() + 1);
ui.showUserLeavesChannel (c_dst.getName(), nick);
} else if (str.startsWith (ChatChannel.JOIN)) {
nick = str.substring (ChatChannel.JOIN.length() + 1);
ui.showUserEntersChannel (c_dst.getName(), nick);
}
} else { // Normal channel message
ui.showChannelMessage (src.getNick(), c_dst.getName(), str);
}
}
} catch (Exception e) {
ui.showErrorMessage ("Error when receiving message: " + e.getMessage());
}
}
//
// Main program, just creates the Client object, the program frame and shows it.
//
public static void main (String [] args) {
ChatClientCSD cc = new ChatClientCSD (ChatConfiguration.parse (args));
ChatUI ui = new ChatUI (cc);
cc.setUI (ui);
ui.show();
}
}