awarbler / yashd_project

0 stars 0 forks source link

Change Debug Gabriels files #9

Closed awarbler closed 1 month ago

awarbler commented 1 month ago

okay so here are the changes I made in the client

Do you want me to make the changes to the file? debugged-branch-gabriel

for the communication thread // Communication thread to handle communication with the server void commmunication_thread(void args){ // buffer for receiving response char buf[BUFFER_SIZE] = {0}; char msg_buf[BUFFER_SIZE] = {0}; int bytesRead; int message_len = 0;

while(1) {

    cleanup(buf); // clear the buffer

    // read response from the server
    bytesRead = recv(sockfd, buf, BUFFER_SIZE -1, 0);
    if (bytesRead < 0) {
        perror("Error receiving response from server");
        pthread_exit(NULL);
    } else if (bytesRead > 0) {
        buf[bytesRead] = '\0'; //  null terminate the received data

        // append the new data to the accumulated message buffer 
        strncat(msg_buf, buf, bytesRead);
        message_len += bytesRead;

        // check if the delimiter '\n\ is in the accumulate message bufffer
        char *delimter_pos = strstr(msg_buf, "\n# ");
        if (delimter_pos != NULL) {
            // we found the delimiter extract the complete message 
            *delimter_pos = '\0';

            // process teh completee message (display the user)
            printf(" %s" , msg_buf);

            // shift any remaining unprocessed data to the beginning of the buffer 
            int remaining_data_len = message_len - (delimter_pos - msg_buf + 3); //  +3 for the "\n # "
            memmove(msg_buf, delimter_pos + 3, remaining_data_len);
            msg_buf[remaining_data_len]= '\0';

            // set the prompt mode since we have received the full message 
            pthread_mutex_lock(&lock); // wrap cs in lock 
            promptMode = 1;
            pthread_mutex_unlock(&lock); // unlock 

        }

        /*if (strcmp(buf, "\n# ") == 0) {
            // server sent prompt
            pthread_mutex_lock(&lock); // wrap CS in lock ...
            promptMode = 1;
            pthread_mutex_unlock(&lock); // ... unlock
            printf("switched promptMode: %d\n", promptMode);
            fflush(stdout);
        }*/
        //printf("%s", buf); // print the servers response
        fflush(stdout);
        //printf("5promptMode: %d\n", promptMode);
        //fflush(stdout);
    } else {
        // server has closed the connection
        printf("Server closed the connection\n");
        close(sockfd);
        exit(EXIT_FAILURE); // Exit the entire program
        // break;
    }
}
pthread_exit(NULL);

}

awarbler commented 1 month ago

What I did was removed duplicated code, accumulation logic : the character based parsing logic that accumulates data in the msg_buf and checks for the\n# deliminter is intact

the prompt mode: once the full message is received and processed, we switch to prompt mode = 1 and we handle errors as well for the socket failures.

I cant test it because I am not getting an output for ls, date, and etc.

I haven't had a chance to debug your code so I am working blindly here.

awarbler commented 1 month ago

if you think I need to make changes please let me know gabriel.

I will move on to the next item that needs to get done which is The thread should intercept the messages and act depending on the type of message (CMD/CTL/TEXT) Implement character based parsing to find message delimiters [TODO]

unless you have something else you need me to do first