/
ChatClient.cpp
205 lines (183 loc) · 8.16 KB
/
ChatClient.cpp
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
#include "ChatClient.h"
int ChatClient::run(int argc, char *argv[]) {
messagesSent = 0;
char* my_string;
char* p;
bool safe_to_connect = false;
char *sendline = new char[MAXLINE];
char recvline[MAXLINE];
size_t num_bytes = 100;
int bytes_read;
int sockfd;
struct hostent* lh;
struct in_addr** addr_list;
struct sockaddr_in servaddr;
char* username;
bool waitingForResponse = false;
Message* response = NULL;
bool hasError = false;
//display a nice interface
printf("%s\n\n", "");
printf("%s\n", "-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-");
printf("%s\n", "\tWelcome to the Chat Room: Client Side!");
printf("%s\n", "\t A Simple Chat Application by Team 6");
printf("%s\n", "-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-");
printf("%s\n", "");
//make sure the correct number of arguments are specified
if (argc < 3) {
fprintf(stderr, "\tusage: %s <host> <port>\n", argv[0]);
fprintf(stderr, "\t<host>\t\t\t- IP Address or hostname of server\n");
fprintf(stderr, "\t<port>\t\t\t- port to connect on\n");
exit(1);
}
// sets the hostname and the port to communicate with
hostname = argv[1];
port = atoi(argv[2]);
//create an object for accessing the data link layer
NetworkLayer* networkLayer = new NetworkLayer();
if(!networkLayer->initialize(port)) {
perror("Error, could not connect to internal services layers\n");
delete networkLayer;
exit(1);
}
printf("To enter the chat room, use the 'join' command.\n");
while(1) {
//read from stdin
printf("Enter a command: ");
my_string = (char*) malloc(num_bytes + 1);
bytes_read = getline(&my_string, &num_bytes, stdin);
strcpy(sendline, my_string);
p = strchr(sendline, '\n');
if (p != NULL) {
*p = '\0'; // get rid of the '\n'
}
//see if any new messages have arrived since we sent the last message
response = networkLayer->checkForMessages(hasError);
if (response != NULL) {
printf("successfully received message: \n%s\n", response->data);
receive_message(response);
}
//If the client receives a join command, start a connection to the server
if(strncasecmp(sendline, "join ", 5) == 0) {
if(!safe_to_connect) {
printf("Connecting to server...\n");
safe_to_connect = true;
username = sendline+5;
networkLayer->sendMessage(createConnectToServerMessage(hostname, username, 2666));
//Message *m = new Message(Message_Join, sendline, strlen(sendline), messagesSent++, username, (char*)"server");
//networkLayer->sendMessage(m);
}
else {
printf("You already connected!!!\n");
}
}
//make sure we are connected to the server before allowing the user to execute other commands
if(safe_to_connect) {
if(strncasecmp(sendline, "speak ", 6) == 0) {
Message *m = new Message(Message_Speak, sendline, strlen(sendline), messagesSent++, username, (char*)"server");
networkLayer->sendMessage(m);
}
else if(strncasecmp(sendline, "whisper ", 8) == 0) {
Message *m = new Message(Message_Whisper, sendline, strlen(sendline), messagesSent++, username, (char*)"server");
networkLayer->sendMessage(m);
}
else if(strncasecmp(sendline, "kick ", 5) == 0) {
Message *m = new Message(Message_Kick, sendline, strlen(sendline), messagesSent++, username, (char*)"server");
networkLayer->sendMessage(m);
}
else if(strncasecmp(sendline, "list", 4) == 0) {
Message *m = new Message(Message_List, sendline, strlen(sendline), messagesSent++, username, (char*)"server");
networkLayer->sendMessage(m);
}
else if(strncasecmp(sendline, "sendfile", 8) == 0) {
//TODO: Implement
Message *m = new Message(Message_SendFile, sendline, strlen(sendline), messagesSent++, username, (char*)"server");
networkLayer->sendMessage(m);
}
else if(strncasecmp(sendline, "quit", 4) == 0) {
Message *m = new Message(Message_Quit, sendline, strlen(sendline), messagesSent++, username, (char*)"server");
networkLayer->sendMessage(m);
exit(0);
}
else if(strncasecmp(sendline, "help", 4) == 0) {
show_help();
}
else {
//catch all invalid commands, and make sure the user doesn't type 'join' again
if(strncasecmp(sendline, "join ", 5) != 0) {
printf("Invalid command. Type 'help' to see a list of valid commands.\n");
}
}
}
else {
//the user cannot use the commands until they are connected
printf("You have to join the chat room first!\n");
}
printf("\n");
}
delete networkLayer;
printf("Done.\n");
return 0;
}
void ChatClient::receive_message(Message* m) {
if(m->type == Message_Join) {
printf("%s joined the chat!\n", m->sourceName);
}
else if(m->type == Message_Speak) {
printf("%s:\n %s\n", m->sourceName, m->data);
}
else if(m->type == Message_Kick) {
printf("%s is being removed from the chat room by %s!\n", m->targetName, m->sourceName);
}
else if(m->type == Message_Whisper) {
printf("Private Message from %s:\n %s\n", m->sourceName, m->data);
}
else if(m->type == Message_List) {
printf("Listing users currently connected to chat room...\n");
}
else if(m->type == Message_SendFile) {
printf("Sending file...\n");
}
else if(m->type == Message_Quit) {
printf("%s has left the room.\n", m->sourceName);
}
}
/**
* This function displays a list of command usages for the supported
* chat room commands.
*/
void ChatClient::show_help() {
printf("List of valid commands for the chat room:\n\n");
printf("\t- Join - Allow a user to join the chat room. Must specify a user name.\n");
printf("\t\tExample: \n");
printf("\t\t\tjoin Trevor\n\n");
printf("\t- Kick - Forcibly remove a user from the chat room by specifying their username.\n");
printf("\t\tExample: \n");
printf("\t\t\tkick James\n\n");
printf("\t- Speak - Send a message to all users in the chat room.\n");
printf("\t\tExample: \n");
printf("\t\t\tspeak Hello there!\n\n");
printf("\t- Whisper - Send a private message to a single user in the chat room by specifying their name.\n");
printf("\t\tExample: \n");
printf("\t\t\twhisper Peiwei Hello there!\n\n");
printf("\t- List - Display a list of all users in the chat room.\n");
printf("\t\tExample: \n");
printf("\t\t\tlist\n\n");
printf("\t- Sendfile - Display a list of all users in the chat room.\n");
printf("\t\tExample: \n");
printf("\t\t\tsendfile /home/Desktop/chat.log\n\n");
printf("\t- Help - Display these instructions about how to use the chat room commands.\n");
printf("\t\tExample: \n");
printf("\t\t\thelp\n\n");
printf("\t- Quit - Allow a user to exit the chat room on their own free will.\n");
printf("\t\tExample: \n");
printf("\t\t\tquit\n\n");
}
/**
* This is the main function which creates a chat client object
* and runs the chat.
*/
int main(int argc, char* argv[]) {
ChatClient cc = ChatClient();
cc.run(argc, argv);
}