-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
227 lines (193 loc) · 7.44 KB
/
App.java
File metadata and controls
227 lines (193 loc) · 7.44 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
import java.util.*;
import java.net.*;
import java.io.*;
import java.util.concurrent.ConcurrentHashMap;
/*
*
* This contains the code for the App class, which handles logic like adding users,
* sending messages and more. Each internal data structure is shared between each instance of the app,
* and we use concurrent hash maps since each client is on a new thread
*
* Every method is designed to run in O(1) time to ensure quick loading
*
*/
public class App {
HashSet<Long> knownUsers = new HashSet<>();
ConcurrentHashMap<Long, String> userNames = new ConcurrentHashMap<>();
static ConcurrentHashMap<Long, String> idMap = new ConcurrentHashMap<>();
static ConcurrentHashMap<Long, Channel> channelMap = new ConcurrentHashMap<>();
static ConcurrentHashMap<Channel, HashSet<Long>> userLists = new ConcurrentHashMap<>();
static ConcurrentHashMap<Long, HashSet<Long>> userChannelAccess = new ConcurrentHashMap<>();
static long updateFromServer = -1;
//To send to the server, basic routing table
ConcurrentHashMap<String, Object> clientMaps = new ConcurrentHashMap<>();
String hostname = "localhost";
int port = 1234;
public App() throws IOException{}
/*
* Add a new user with their associated username
*/
public void addUser(Long userID, String userName) throws IOException{
idMap.put(userID, userName);
if(!userChannelAccess.containsKey(userID)){
userChannelAccess.put(userID, new HashSet<Long>());
}
knownUsers.add(userID);
userNames.put(userID, userName);
}
/*
* Add a message to a specific chat channel
*/
public void addMessage(long user, Long channelID, String channelMessage) throws IOException{
if(!channelMap.containsKey(channelID)){
channelMap.put(channelID, new Channel(channelID, new ArrayList<Message>()));
}
updateAddChannel(user, channelMap.get(channelID), channelMessage);
}
private void updateAddChannel(long user, Channel c, String s) throws IOException{
c.textMessages.add(new Message(user, s));
}
/*
* Print out all the text in a channel with the correct formatting for who is viewing
*/
public void printChannel(long channelID, long viewerID) throws IOException{
Channel c = channelMap.get(channelID);
long lastID = -1;
for(Message s : c.textMessages){
if(lastID != s.sender){
System.out.println();
if(s.sender == viewerID){
System.out.print("You: ");
}
else{
System.out.print(idMap.get(s.sender) + ": ");
}
}
System.out.println(s.message);
lastID = s.sender;
}
}
/*
* Allow a user to be able to view and send messages to a chat
*/
public void addUserToChannel(long channelID, long userID) throws IOException{
if(!userLists.containsKey(channelMap.get(channelID))){
System.out.println("Channel not found");
return;
}
userLists.get(channelMap.get(channelID)).add(userID);
if(!userChannelAccess.containsKey(userID)){
userChannelAccess.put(userID, new HashSet<Long>());
}
userChannelAccess.get(userID).add(channelID);
}
/*
* List all users that are in a chat
*/
public HashSet<Long> getUsersInChannel(long channelID) throws IOException{
return userLists.get(channelMap.get(channelID));
}
/*
* Create a new channel and add the creator to channel
*/
public void createChannel(long creator, long id) throws IOException{
Channel c = new Channel(id, new ArrayList<Message>());
channelMap.put(id, c);
userLists.put(c, new HashSet<>());
addUserToChannel(id, creator);
}
/*
* Send our map of data to the server
*/
public void updateServer() throws IOException{
try (Socket socket = new Socket(hostname, port);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
) {
// Send the ConcurrentHashMaps to the server
oos.writeObject(clientMaps);
oos.flush();
} catch (Exception e) {
System.out.println("Unknown host: " + e.getMessage());
}
}
/*
* Ping the server to request data, which leads to a data send along the stream
* that this function takes and casts to the correct maps
*/
public void updateClient() throws IOException{
try (Socket socket = new Socket(hostname, port)){
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(new Integer(1));
oos.flush();
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ConcurrentHashMap<String, Object> updatedMaps = (ConcurrentHashMap<String, Object>) ois.readObject();
for(Map.Entry<String, Object> entry : updatedMaps.entrySet()){
String key = entry.getKey();
Object value = entry.getValue();
if(key.equals("knownUsers")){
knownUsers = (HashSet<Long>) value;
}
else if(key.equals("userNames")){
userNames = (ConcurrentHashMap<Long, String>) value;
}
else if(key.equals("idMap")){
idMap = (ConcurrentHashMap<Long, String>) value;
}
else if(key.equals("channelMap")){
channelMap = (ConcurrentHashMap<Long, Channel>) value;
}
else if(key.equals("userLists")){
userLists = (ConcurrentHashMap<Channel, HashSet<Long>>) value;
}
else if(key.equals("userChannelAccess")){
userChannelAccess = (ConcurrentHashMap<Long, HashSet<Long>>) value;
}
else if(key.equals("update")){
updateFromServer = (long) value;
}
}
}catch(SocketTimeoutException e){
System.out.println("couldn't receive info");
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
/*
* Helper function to update all of our individual hashmaps
*/
public void updateClientMaps(){
clientMaps.put("knownUsers", knownUsers);
clientMaps.put("userNames", userNames);
clientMaps.put("idMap", idMap);
clientMaps.put("userLists", userLists);
clientMaps.put("userChannelAccess", userChannelAccess);
clientMaps.put("channelMap", channelMap);
}
}
/*
* Channel object. Each channel has a channel ID and a list of all the messages sent
* in the channel, which is decrypted on the client side by the print function
*/
class Channel implements Serializable{
private static final long serialVersionUID = 1L;
long id;
ArrayList<Message> textMessages;
public Channel(long id, ArrayList<Message> m){
this.id = id;
this.textMessages = m;
}
}
/*
* Message object. Stores the sender and message text, allowing it to be displayed
* differently depending on who is viewing it.
*/
class Message implements Serializable{
private static final long serialVersionUID = 1L;
long sender;
String message;
public Message(long s, String m){
this.sender = s;
this.message = m;
}
}