-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.h
executable file
·179 lines (161 loc) · 4.52 KB
/
interface.h
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
/*****************************************************************
* FILENAME : interface.h
*
* Functions and datastructures for the interface
* between users and the client program.
*
* DO NOT MODIFY THIS FILE. I CAN DO WHATEVER I WANT, OKAY!?
*
* Version: 1.0.3 // I've made 2 changes, 1 bugfix
******************************************************************/
#ifndef INTERFACE_H_
#define INTERFACE_H_
// #include <ctype.h>
#include <stdio.h>
#include <string.h>
// Maximum size of data for the communication using TCP/IP
#define MAX_DATA 256
// Maximum number of clients per Chat Server
#define MAX_CLIENTS 128
// Maximum number of Chat Servers per machine
#define MAX_ROOMS 64
// Wether to require/enable usernames for clients
#define USE_USERNAMES 0
// Wether chatrooms should log all messages
#define LOG_MESSAGES 1
/*
* This enum represents the result of a command.
* Based on the response from the server,
* you need to figure out the status.
*/
enum Status{
SUCCESS,
FAILURE_ALREADY_EXISTS,
FAILURE_NOT_EXISTS,
FAILURE_INVALID,
FAILURE_UNKNOWN
};
/*
* Reply structure is designed to be used for displaying the
* result of the command that has been sent to the server.
* For example, in the "process_command" function, you should
* declare a variable of Reply structure and fill it based on
* the type of command and the result.
*
* - CREATE and DELETE command:
* Reply reply;
* reply.status = one of values in Status enum;
*
* - JOIN command:
* Reply reply;
* reply.status = one of values in Status enum;
* reply.num_members = # of members
* reply.port = port number;
*
* - LIST command:
* Reply reply;
* reply.status = one of values in Status enum;
* reply.list_room = list of rooms that have been create;
*
* This structure is not for communicating between server and client.
* You need to design your own rules for the communication.
*/
struct Reply{
enum Status status;
union{
// Below structure is only for the "JOIN <chatroom name>" command
struct{
// # of members that have been joined the chatroom
int num_member;
// Address of host (in case system is distributed)
char host[MAX_DATA];
// port number to join the chatroom
int port;
};
// list_room is only for the "LIST" command
// contains the list of rooms that have been created
char list_room[MAX_DATA];
};
};
/*
* DO NOT MODIFY THIS FUNCTION
* This function convert input string to uppercase.
*/
void touppercase(char* str){
int i;
for(i=0; str[i]; ++i) str[i] = toupper((unsigned char)str[i]);
}
int startswith(char* str, char* prefix){
int i;
for(i=0; str[i] && prefix[i] == str[i]; ++i);
return !prefix[i];
}
/*
* DO NOT MODIFY THIS FUNCTION
* This function displays a title, commands that user can use.
*/
void display_title(){
printf("\n========= CHAT ROOM CLIENT =========\n");
printf(" Command Lists and Format:\n");
printf(" CREATE <name>\n");
printf(" DELETE <name>\n");
printf(" JOIN <name>\n");
printf(" QUIT\n");
printf(" LIST\n");
printf("=====================================\n");
}
/*
* DO NOT MODIFY THIS FUNCTION
* This function prompts a user to enter a command.
*/
void get_command(char* comm, const int size){
printf("Command> ");
fgets(comm, size, stdin);
comm[strlen(comm) - 1] = '\0';
}
/*
* DO NOT MODIFY THIS FUNCTION
* This function prompts a user to enter a command.
*/
void get_message(char* message, const int size){
fgets(message, size, stdin);
message[strlen(message) - 1] = '\0';
}
/*
* DO NOT MODIFY THIS FUNCTION.
* You should call this function to display the message from
* other clients in a currently joined chatroom.
*/
void display_message(char* message){
printf("> %s", message);
}
void display_reply(char* comm, const struct Reply reply){
switch(reply.status) {
case SUCCESS:
printf("Command completed successfully\n");
if(strncmp(comm, "JOIN", 4) == 0) {
printf("#Members: %d\n", reply.num_member);
printf("Host: %d, Port: %d\n", reply.host, reply.port);
}
else if(strncmp(comm, "LIST", 4) == 0) {
printf("List: %s\n", reply.list_room);
}
break;
case FAILURE_ALREADY_EXISTS:
printf("Input chatroom name already exists, command failed\n");
break;
case FAILURE_NOT_EXISTS:
printf("Input chatroom name does not exists, command failed\n");
break;
case FAILURE_INVALID:
printf("Invalid command, command failed\n");
break;
case FAILURE_UNKNOWN:
printf("Unknown error, command failed\n");
break;
default:
printf("Invalid reply status, try updating your client\n");
break;
}
}
#endif // INTERFACE_H_