Compare commits
3 Commits
5bc6c575fc
...
9c87de7196
Author | SHA1 | Date | |
---|---|---|---|
9c87de7196 | |||
34861b4644 | |||
a83338e8db |
38
main.c
38
main.c
@@ -94,7 +94,16 @@ int main() {
|
|||||||
}
|
}
|
||||||
strcat(data,"\0");
|
strcat(data,"\0");
|
||||||
|
|
||||||
if (strcmp(strtok(data," \r\n"), "SENDING") == 0) {
|
buffer[0] = '\0'; /* Clear the buffer */
|
||||||
|
|
||||||
|
char* send_or_receive = malloc(10);
|
||||||
|
int k=0;
|
||||||
|
while (*(data + k) != '\n') {
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
memcpy(send_or_receive,data,k);
|
||||||
|
|
||||||
|
if (strstr(send_or_receive, "SENDING") != NULL) {
|
||||||
if (from_user[i] == NULL) {
|
if (from_user[i] == NULL) {
|
||||||
from_user[i] = fetch_user(fetch_from_string(data,"IAM"));
|
from_user[i] = fetch_user(fetch_from_string(data,"IAM"));
|
||||||
if (from_user[i] == NULL) {
|
if (from_user[i] == NULL) {
|
||||||
@@ -123,7 +132,7 @@ int main() {
|
|||||||
return -10;
|
return -10;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Your message is: %s\n",message_string);
|
printf("Your message is: \n------------------\n%s\n------------------\n",message_string);
|
||||||
|
|
||||||
if (user_to_index(to_user[i]) < 0) {
|
if (user_to_index(to_user[i]) < 0) {
|
||||||
printf("Recipient user does not exist!\n");
|
printf("Recipient user does not exist!\n");
|
||||||
@@ -135,7 +144,7 @@ int main() {
|
|||||||
|
|
||||||
printf("Message has been pushed onto stack for %s\n",to_user[i]->username);
|
printf("Message has been pushed onto stack for %s\n",to_user[i]->username);
|
||||||
|
|
||||||
} else if (strcmp(strtok(buffer," \r\n"), "SENDING") == 0) {
|
} else if (strstr(send_or_receive, "RECEIVING") != NULL) {
|
||||||
|
|
||||||
if (from_user[i] == NULL) {
|
if (from_user[i] == NULL) {
|
||||||
from_user[i] = fetch_user(fetch_from_string(data,"IAM"));
|
from_user[i] = fetch_user(fetch_from_string(data,"IAM"));
|
||||||
@@ -155,7 +164,7 @@ int main() {
|
|||||||
printf("Invalid GET request.\n");
|
printf("Invalid GET request.\n");
|
||||||
return -30;
|
return -30;
|
||||||
} else {
|
} else {
|
||||||
if (strstr(num_of_messages_string,"ALL") == 0) {
|
if (strstr(num_of_messages_string,"ALL") != NULL) {
|
||||||
num_of_messages = stack_size(message_stack[user_to_index(from_user[i])]);
|
num_of_messages = stack_size(message_stack[user_to_index(from_user[i])]);
|
||||||
} else {
|
} else {
|
||||||
num_of_messages = atoi(num_of_messages_string);
|
num_of_messages = atoi(num_of_messages_string);
|
||||||
@@ -166,7 +175,7 @@ int main() {
|
|||||||
num_of_messages = stack_size(message_stack[user_to_index(from_user[i])]);
|
num_of_messages = stack_size(message_stack[user_to_index(from_user[i])]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i=0;i<num_of_messages;i++) {
|
for (int k=0;k<num_of_messages;k++) {
|
||||||
Message* message = stack_pop(message_stack[user_to_index(from_user[i])]);
|
Message* message = stack_pop(message_stack[user_to_index(from_user[i])]);
|
||||||
printf("You have a message from %s"
|
printf("You have a message from %s"
|
||||||
", sent at %s"
|
", sent at %s"
|
||||||
@@ -176,19 +185,17 @@ int main() {
|
|||||||
message->text);
|
message->text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
printf("You are not sending or receiving.\n");
|
printf("You are not sending or receiving.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
close(conn_sockets[i]);
|
close(conn_sockets[i]);
|
||||||
|
conn_sockets[i] = -1;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
continue_for_loop:
|
continue_for_loop:
|
||||||
data[0] = '\0'; /* This effectively clears the string, since the first element is null. */
|
memset(data,0,DATA_SIZE);
|
||||||
|
memset(buffer,0,BUFFER_SIZE);
|
||||||
to_user[i] = NULL;
|
to_user[i] = NULL;
|
||||||
|
from_user[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,15 +209,10 @@ int main() {
|
|||||||
return 130;
|
return 130;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message message = new_message("Hello, this is a text message",users[0],users[1]);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// printf("Message was: %s\nSentfrom: %s\nSent to: %s\nWith password: %s\nSent at: %s\n",message.text, message.sender.username, message.recipient.username, message.recipient.password, asctime(&message.timeinfo));
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,6 +245,8 @@ User** create_user_list(char* filename) {
|
|||||||
|
|
||||||
void sigint_handler(int dummy) {
|
void sigint_handler(int dummy) {
|
||||||
stop_running = true;
|
stop_running = true;
|
||||||
|
printf("sigint_handler is stopping...\n");
|
||||||
|
exit(130);
|
||||||
}
|
}
|
||||||
|
|
||||||
User* fetch_user(char* username) {
|
User* fetch_user(char* username) {
|
||||||
|
@@ -50,14 +50,24 @@ char* fetch_message_string(char* message) {
|
|||||||
num_of_terminators = 1;
|
num_of_terminators = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int message_length = end_index - start_index;
|
end_index -= num_of_terminators;
|
||||||
char* message_string = malloc(message_length + 1);
|
|
||||||
|
|
||||||
for (int i=0;i < message_length-num_of_terminators; i++) { /* The reason the upper-bound is message_length-1 is because the last
|
int message_length = end_index - start_index;
|
||||||
|
char* message_string = malloc(message_length + 2);
|
||||||
|
|
||||||
|
printf("Message length is %d\n",message_length);
|
||||||
|
|
||||||
|
for (int i=0; i<message_length; i++) { /* The reason the upper-bound is message_length-1 is because the last
|
||||||
character is a new-line, which the user would not have typed. */
|
character is a new-line, which the user would not have typed. */
|
||||||
|
|
||||||
*(message_string + i) = *(message + start_index + i);
|
*(message_string + i) = *(message + start_index + i);
|
||||||
|
printf("%c",*(message_string + i));
|
||||||
}
|
}
|
||||||
|
|
||||||
*(message_string + (message_length - num_of_terminators)) = '\0';
|
strcat(message_string,"\0");
|
||||||
|
|
||||||
|
// printf("%s\n",message_string);
|
||||||
|
|
||||||
|
// *(message_string + (message_length - num_of_terminators)) = '\0';
|
||||||
return message_string;
|
return message_string;
|
||||||
}
|
}
|
||||||
|
2
stack.c
2
stack.c
@@ -35,7 +35,7 @@ void stack_push(Stack* stack,void* element) {
|
|||||||
|
|
||||||
void* stack_pop(Stack* stack) {
|
void* stack_pop(Stack* stack) {
|
||||||
assert( !(stack_isEmpty(stack)) );
|
assert( !(stack_isEmpty(stack)) );
|
||||||
free(stack->data + stack->top_index);
|
// free(stack->data + stack->top_index);
|
||||||
stack->top_index--;
|
stack->top_index--;
|
||||||
void* to_return = *(stack->data + stack->top_index);
|
void* to_return = *(stack->data + stack->top_index);
|
||||||
*(stack->data + stack->top_index) = NULL;
|
*(stack->data + stack->top_index) = NULL;
|
||||||
|
Reference in New Issue
Block a user