Skip to content

Commit 60fcdac

Browse files
committed
wip: handling username request
1 parent 0434aec commit 60fcdac

File tree

3 files changed

+108
-29
lines changed

3 files changed

+108
-29
lines changed

source/budget-chat/client.c

+106-27
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,98 @@ bool client_find(struct client** pc, int id)
109109
return false;
110110
}
111111

112+
bool client_validate_username(struct client* c, struct client_name_request* req)
113+
{
114+
assert(c != NULL);
115+
assert(req != NULL);
116+
117+
// TODO: change from bool to int
118+
// Create error codes for all the possible errors
119+
// To give the user meaningful error messages
120+
char* name = NULL;
121+
size_t size;
122+
req->valid = false;
123+
size = queue_pop_no_copy(c->recv_qu, &name);
124+
size--; // Loose the /r
125+
if (size < 1) {
126+
strcpy(req->invalid_name_response, "Empty username provided");
127+
return false;
128+
}
129+
if (size >= CLIENT_MAX_NAME) {
130+
sprintf(req->invalid_name_response,
131+
"Name provided exceeds limit for number of characters: %d",
132+
CLIENT_MAX_NAME);
133+
return false;
134+
}
135+
136+
for (size_t k = 0; k < size; k++) {
137+
if (!isalnum(name[k])) {
138+
strcpy(req->invalid_name_response, "Username must only contain alphanumeric characters");
139+
return false;
140+
}
141+
}
142+
143+
req->name = name;
144+
req->size = size;
145+
if (!client_name_exists(c, req)) {
146+
strcpy(req->invalid_name_response, "Username is already taken");
147+
return false;
148+
}
149+
150+
memcpy(c->name, name, size);
151+
c->name[size + 1] = 0;
152+
return true;
153+
}
154+
155+
int client_handle_newclient(struct client* c)
156+
{
157+
assert(c != NULL);
158+
159+
struct client_name_request req;
160+
if (!client_validate_username(c, &req)) {
161+
// sendall(req.invalid_name_response)
162+
return -1;
163+
}
164+
165+
// Collect list of all names in chat
166+
// client broadcast message from
167+
// client_send(c, WELCOME_MESSAGE)
168+
return 1;
169+
}
170+
112171
int client_handle_request(struct client* c)
113172
{
114173
// tokenize the messages
115174
// TODO: what to do with more than one message
116175
if (c->name[0] == 0) {
117-
if (!client_set_name(c))
118-
return -1;
119-
120-
// client broadcast message from
121-
// client_send(c, WELCOME_MESSAGE)
176+
return client_handle_newclient(c);
122177
}
123178

124-
char *msg;
179+
char* msg;
125180
size_t size;
126181
size = queue_pop_no_copy(c->recv_qu, &msg);
127182
}
128183

129-
void client_collect_list_of_names_other_names(struct client* c) {}
184+
/*void client_on_valid_username(struct client* c)*/
185+
/*{*/
186+
/* // To *c*/
187+
/* // - The room contains: etc..*/
188+
/* // To everybody else:*/
189+
/* // - *c has entered the room*/
190+
/*}*/
191+
192+
void client_on_exit(struct client* c)
193+
{
194+
// To everybody else:
195+
// - *c has entered the room
196+
}
197+
198+
void client_collect_list_of_names_other_names(struct client* c)
199+
{
200+
assert(*c != NULL);
201+
202+
//
203+
}
130204

131205
void client_broadcast_message_to_all(struct client* c, char* msg, size_t size)
132206
{
@@ -140,8 +214,32 @@ void client_broadcast_message_from(struct client* c, char* msg, size_t size)
140214
// sendall(msg, size);
141215
}
142216

143-
void client_name_exists(struct client* c, struct client_name_request* name_req)
217+
bool client_name_exists(struct client* c, struct client_name_request* name_req)
144218
{
219+
assert(c != NULL);
220+
assert(name_req != NULL);
221+
222+
// Does this username exists?
223+
// for each client
224+
// if (client->id == c->id) continue;
225+
// if (strcmp(client->name, c->name) == 0) return false;
226+
struct client* me = c;
227+
client_first(&c);
228+
struct client* next = c->next;
229+
struct client* last = c;
230+
do {
231+
if ((last->id != me->id)
232+
&& (strncmp(last->name, name_req->name, name_req->size) == 0))
233+
{
234+
name_req->valid = false;
235+
return false;
236+
}
237+
238+
last = next;
239+
next = next->next;
240+
} while (next != NULL);
241+
242+
return true;
145243
}
146244

147245
void client_send_welcome_prompt(struct client* c)
@@ -151,22 +249,3 @@ void client_send_welcome_prompt(struct client* c)
151249
assert(res == 0);
152250
assert(l == CLIENT_WELCOME_PROMPT_SIZE);
153251
}
154-
155-
bool client_set_name(struct client* c)
156-
{
157-
char* name;
158-
size_t size;
159-
size = queue_pop_no_copy(c->recv_qu, &name);
160-
if (size < 1)
161-
return false;
162-
if (size > CLIENT_MAX_NAME)
163-
return false;
164-
165-
for (size_t k = 0; k < size; k++) {
166-
if (!isalnum(name[k])
167-
return false;
168-
}
169-
memcpy(c->name, name, size);
170-
c->name[size + 1] = 0;
171-
return true;
172-
}

source/budget-chat/client.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ int client_handle_request(struct client *c);
2929
void client_collect_list_of_names_other_names(struct client *c);
3030
void client_broadcast_message_to_all(struct client *c, char *msg, size_t size);
3131
void client_broadcast_message_from(struct client *c, char *msg, size_t size);
32-
void client_name_exists(struct client *c, struct client_name_request *name_req);
32+
bool client_name_exists(struct client *c, struct client_name_request *name_req);
3333
void client_send_welcome_prompt(struct client *c);
34-
bool client_set_name(struct client *c);

source/budget-chat/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ int main()
159159
continue;
160160

161161
// Handle closing request received
162+
client_close(&c);
162163
log_info("main epoll loop:: closing connection");
163164
if (fd_poll_del_and_close(&epci) == -1) {
164165
perror("epoll_ctl: recv 0");

0 commit comments

Comments
 (0)