This repository was archived by the owner on Jun 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
237 lines (216 loc) · 6.2 KB
/
Client.java
File metadata and controls
237 lines (216 loc) · 6.2 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
package client;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.swing.JOptionPane;
import server.ServerIF;
import common.*;
public class Client extends UnicastRemoteObject implements ClientIF {
private static final long serialVersionUID = 7468891722773409712L;
ClientMainGUI chatGUI;
User me;
private ReadWriteLock guiLock = new ReentrantReadWriteLock();
private static String hostName = "localhost";
private static int registryPort = 1099;
private static String serviceName = "SunnyChatService";
protected ServerIF serverIF;
protected boolean connectionProblem = false;
public Client() throws Exception {
super();
serverIF = (ServerIF) Naming.lookup(String.format("rmi://%s:%d/%s", hostName, registryPort, serviceName));
}
public boolean login(String userName, String password) {
guiLock.writeLock().lock();
boolean success = false;
try {
me = serverIF.login(userName, password, this);
chatGUI = new ClientMainGUI(this);
success = true;
} catch (ObjectNotFoundException e) {
success = false;
} catch (RemoteException e) {
raiseFatalError(e);
} finally {
guiLock.writeLock().unlock();
}
return success;
}
public boolean register(String userName, String password) {
try {
serverIF.register(userName, password);
return true;
} catch (RemoteException e) {
raiseFatalError(e);
} catch (DuplicatedObjectException e) {
return false;
}
return false;
}
public boolean joinGroup(int groupNumber) {
try {
serverIF.joinGroup(groupNumber, me);
return true;
} catch (RemoteException | InvalidSessionException e) {
raiseFatalError(e);
} catch (DuplicatedObjectException e) {
return false;
}
return false;
}
public void sendMessage(int selectedChatRoomID, String message) {
try {
serverIF.sendMessage(me, selectedChatRoomID, message);
} catch (RemoteException | InvalidSessionException e) {
raiseFatalError(e);
}
}
public String[] getChatRoomMembers(int cid) {
try {
return serverIF.getChatRoomMembers(me, cid);
} catch (RemoteException | InvalidSessionException e) {
raiseFatalError(e);
}
return new String[] {};
}
public ArrayList<User> getOnlineUsers() {
try {
return serverIF.getOnlineUsers(me);
} catch (RemoteException | InvalidSessionException e) {
raiseFatalError(e);
}
return new ArrayList<User>();
}
/**
* Send an invitation to a online user.
*
* @param user
* @return Status:
* <P/>
* - 0: Success
* <P/>
* - 1: The user is not online
* <P/>
* - 2: The user is already a friend
*/
public int sendFriendInvitation(User user) {
try {
serverIF.sendFriendInvitation(me, user.uid);
return 0;
} catch (RemoteException | InvalidSessionException e) {
raiseFatalError(e);
} catch (ObjectNotFoundException e) {
return 1;
} catch (DuplicatedObjectException e) {
return 2;
}
return 1;
}
public void sendFile(int cid, File file) throws IOException {
byte[] rawFile = new byte[(int) file.length()];
BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
is.read(rawFile);
is.close();
try {
serverIF.uploadFile(me, cid, file.getName(), rawFile);
} catch (RemoteException | InvalidSessionException e) {
raiseFatalError(e);
}
}
public void logout() {
try {
serverIF.logout(me);
} catch (RemoteException e) {
raiseFatalError(e);
} catch (InvalidSessionException e) {
// Do nothing, since the client is closing
}
}
/**
* Receive a new ChatRoom from server
*/
@Override
public void receiveChatRoom(ChatRoom room) throws RemoteException {
guiLock.readLock().lock();
try {
chatGUI.addChatRoom(room);
} finally {
guiLock.readLock().unlock();
}
}
@Override
public void receiveMessage(ChatMessage message) throws RemoteException {
guiLock.readLock().lock();
try {
chatGUI.addMessage(message);
} finally {
guiLock.readLock().unlock();
}
}
@Override
public void receiveFriendInvitation(User user) throws RemoteException {
new Thread(new Runnable() {
public void run() {
int result = JOptionPane.showConfirmDialog(null,
String.format("User %s wants to become your friend, accept or not?", user.userName), "Question",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.NO_OPTION)
return;
try {
serverIF.acceptFriendInvitation(me, user.uid);
} catch (RemoteException | InvalidSessionException e) {
raiseFatalError(e);
}
}
}).start();
}
@Override
public void receiveFile(String fileName, byte[] fileContent) throws RemoteException {
new Thread(new Runnable() {
public void run() {
File f = new File("./" + fileName);
try {
if (!f.exists())
f.createNewFile();
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
os.write(fileContent);
os.close();
JOptionPane.showMessageDialog(null,
String.format("Successfully get the file and saved in:\n%s", f.getCanonicalPath()), "Hint",
JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Failed to get the file from server :(", "Hint",
JOptionPane.ERROR_MESSAGE);
}
}
}).start();
}
public void raiseFatalError(Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Fatal Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
public static void main(String[] args) {
try {
Client client;
if (args.length == 2) {
hostName = args[0];
registryPort = Integer.parseInt(args[1]);
System.out.printf("Get arguments: hostName=%s, registryPort=%d\n", hostName, registryPort);
}
client = new Client();
new ClientLoginGUI(client);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "The server is currently unavailable, please try again later!", "Error",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
}