-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatService.java
185 lines (129 loc) · 3.96 KB
/
ChatService.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
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
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* A Class for managing chat rooms.
* Assign correct id from both users and rooms.
* Map user id from username.
* Map room id from room name.
* Record users in which room.
* Provide broadcast a message to all user in same room.
*/
public class ChatService {
// For getting corresponding socket from user id.
private Map<Integer, SocketChannel> userChannels;
private int nextChatRoomId;
private int nextUserId;
// For getting user id from username.
private Map<String, Integer> userIdMap;
// For getting room id from room name.
private Map<String, Integer> chatRoomIdMap;
// Record which user in which room.
private Map<Integer, List<Integer>> userInRoom;
public ChatService()
{
// Initialize all data objects.
chatRoomIdMap = new HashMap<>();
userIdMap = new HashMap<>();
userInRoom = new HashMap<>();
userChannels = new HashMap<>();
nextChatRoomId = 0;
nextUserId = 0;
}
// Broadcast a message to all user in same room.
public int broadcast(int roomId, int userId, String msg) throws IOException {
// ByteBuffer byteBuffer = ByteBuffer.wrap(msg.getBytes());
List<Integer> userIds = userInRoom.get(roomId);
// Send to all user in this room.
for(Integer id : userIds)
{
ByteBuffer byteBuffer = ByteBuffer.wrap(msg.getBytes());
userChannels.get(id).write(byteBuffer);
}
return 0;
}
// Get room id from room name.
public int getRoomId(String room)
{
if(chatRoomIdMap.containsKey(room))
{
return -3;
}
return userIdMap.get(room);
}
// Get user id from username.
public int getUserId(String username)
{
if(!userIdMap.containsKey(username))
{
return -2;
}
return userIdMap.get(username);
}
// If a new user join in the chat room, firstly gets an ID.
public int generateUserId(String username)
{
// If a same username has existed.
if(!userIdMap.containsKey(username))
{
userIdMap.put(username, nextUserId);
++nextUserId;
}
return userIdMap.get(username);
}
// A user leaves room.
public int leaveRoom(int roomId, int userId)
{
List<Integer> userList = userInRoom.get(roomId);
int idx = 0;
// Find out the user.
for(Integer id : userList)
{
if(id.equals(userId))
{
break;
}
++idx;
}
// User doesn't stay in this room.
if(idx == userList.size())
{
// Return error code.
return -4;
}
// Remove this user from this room.
userList.remove(idx);
String key = null;
// Remove this user's socket.
for(Map.Entry<String, Integer> entry : userIdMap.entrySet())
{
if(entry.getValue() == userId)
{
key = entry.getKey();
break;
}
}
userIdMap.remove(key);
return 0;
}
// Join a room.
public int joinRoom(String roomName, int userId, SocketChannel channel)
{
// If the room doesn't exist, then create new one.
if(!chatRoomIdMap.containsKey(roomName))
{
chatRoomIdMap.put(roomName, nextChatRoomId);
++nextChatRoomId;
userInRoom.put(chatRoomIdMap.get(roomName), new LinkedList<>());
}
// Record the socket.
userChannels.put(userId, channel);
// Add user to corresponding room.
userInRoom.get(chatRoomIdMap.get(roomName)).add(userId);
return chatRoomIdMap.get(roomName);
}
}