|
|
|
@@ -32,32 +32,35 @@ void *input(void *arg) {
|
|
|
|
|
int i;
|
|
|
|
|
CONTEXT *context = (CONTEXT *)arg;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
while (1) {
|
|
|
|
|
/* Get a message from the user */
|
|
|
|
|
msg = malloc(512);
|
|
|
|
|
memset(msg, 0, 512);
|
|
|
|
|
i = 0;
|
|
|
|
|
ncplane_printf_yx(context->plane_prompt, 0, 0, "Type a message: ");
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&context->mut_render);
|
|
|
|
|
pthread_mutex_lock(context->mut_render);
|
|
|
|
|
ncplane_printf_yx(context->plane_prompt, 0, 0, "Type a message: ");
|
|
|
|
|
notcurses_render(context->ctx_nc);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_render);
|
|
|
|
|
pthread_mutex_unlock(context->mut_render);
|
|
|
|
|
|
|
|
|
|
/* Loop to get input */
|
|
|
|
|
while (1) {
|
|
|
|
|
character = notcurses_get(context->ctx_nc, NULL, &ni);
|
|
|
|
|
if (character == NCKEY_ENTER) {
|
|
|
|
|
character = notcurses_get(context->ctx_nc, NULL, &ni);
|
|
|
|
|
|
|
|
|
|
if (character == NCKEY_ENTER) {
|
|
|
|
|
/* Add to sendlist when user presses enter */
|
|
|
|
|
pthread_mutex_lock(&context->mut_send);
|
|
|
|
|
pthread_mutex_lock(context->mut_send);
|
|
|
|
|
ListAppend(context->ctx_sendlist, msg);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_send);
|
|
|
|
|
pthread_mutex_unlock(context->mut_send);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(context->mut_render);
|
|
|
|
|
ncplane_erase_region(context->plane_prompt, 0, 16, 1,
|
|
|
|
|
context->ctx_cols);
|
|
|
|
|
context->ctx_cols);
|
|
|
|
|
pthread_mutex_unlock(context->mut_render);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&context->mut_render);
|
|
|
|
|
pthread_mutex_lock(context->mut_render);
|
|
|
|
|
notcurses_render(context->ctx_nc);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_render);
|
|
|
|
|
pthread_mutex_unlock(context->mut_render);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
@@ -71,18 +74,23 @@ void *input(void *arg) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print the message as the user types it */
|
|
|
|
|
pthread_mutex_lock(context->mut_render);
|
|
|
|
|
ncplane_erase_region(context->plane_prompt, 0, 0, 1, context->ctx_cols);
|
|
|
|
|
ncplane_printf_yx(context->plane_prompt, 0, 0, "Type a message: %s", msg);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&context->mut_render);
|
|
|
|
|
notcurses_render(context->ctx_nc);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_render);
|
|
|
|
|
pthread_mutex_unlock(context->mut_render);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Wake up thread if it was waiting to have a msg to send */
|
|
|
|
|
pthread_mutex_lock(context->mut_sendcv);
|
|
|
|
|
if (ListCount(context->ctx_sendlist) == 1) {
|
|
|
|
|
pthread_cond_signal(&context->cv_send);
|
|
|
|
|
pthread_cond_signal(context->cv_send);
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_unlock(context->mut_sendcv);
|
|
|
|
|
/* s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
|
|
|
|
if (s != 0)
|
|
|
|
|
handle_error_en(s, "pthread_setcancelstate");*/
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@@ -94,13 +102,18 @@ void *sender(void *arg) {
|
|
|
|
|
CONTEXT *context = (CONTEXT *)arg;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
/*
|
|
|
|
|
s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
|
|
|
|
if (s != 0)
|
|
|
|
|
handle_error_en(s, "pthread_setcancelstate");
|
|
|
|
|
*/
|
|
|
|
|
/* Check if the list has any content to send
|
|
|
|
|
* If not, wait for some to arrive */
|
|
|
|
|
pthread_mutex_lock(context->mut_sendcv);
|
|
|
|
|
if (ListCount(context->ctx_sendlist) == 0) {
|
|
|
|
|
pthread_mutex_lock(&context->mut_sendcv);
|
|
|
|
|
pthread_cond_wait(&context->cv_send, &context->mut_sendcv);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_sendcv);
|
|
|
|
|
pthread_cond_wait(context->cv_send, context->mut_sendcv);
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_unlock(context->mut_sendcv);
|
|
|
|
|
|
|
|
|
|
msg = malloc(512);
|
|
|
|
|
header = malloc(519);
|
|
|
|
@@ -110,11 +123,11 @@ void *sender(void *arg) {
|
|
|
|
|
/* Obtain mutex, get and then remove message
|
|
|
|
|
* from the list, unlock mutex,
|
|
|
|
|
* send message. */
|
|
|
|
|
pthread_mutex_unlock(&context->mut_send);
|
|
|
|
|
pthread_mutex_lock(context->mut_send);
|
|
|
|
|
ListFirst(context->ctx_sendlist);
|
|
|
|
|
strcpy(msg, (char*)ListCurr(context->ctx_sendlist));
|
|
|
|
|
ListRemove(context->ctx_sendlist);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_send);
|
|
|
|
|
pthread_mutex_unlock(context->mut_send);
|
|
|
|
|
|
|
|
|
|
bytes = send(context->ctx_confd, msg, 512, 0);
|
|
|
|
|
|
|
|
|
@@ -123,21 +136,28 @@ void *sender(void *arg) {
|
|
|
|
|
pthread_cancel(*context->thread_in);
|
|
|
|
|
pthread_cancel(*context->thread_out);
|
|
|
|
|
pthread_cancel(*context->thread_recv);
|
|
|
|
|
pthread_exit(&context->ctx_closed);
|
|
|
|
|
pthread_exit(context->ctx_closed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Append message to header and add to print list */
|
|
|
|
|
strcpy(header, "You: ");
|
|
|
|
|
strncat(header, msg, strlen(msg));
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&context->mut_recv);
|
|
|
|
|
/* Add to receive list. This is done to print the received and
|
|
|
|
|
* sent messages in order */
|
|
|
|
|
pthread_mutex_lock(context->mut_recv);
|
|
|
|
|
ListAppend(context->ctx_recvlist, header);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_recv);
|
|
|
|
|
pthread_mutex_unlock(context->mut_recv);
|
|
|
|
|
|
|
|
|
|
/* Wake thread waiting for message to print */
|
|
|
|
|
if (ListCount(context->ctx_recvlist) == 1) {
|
|
|
|
|
pthread_cond_signal(&context->cv_recv);
|
|
|
|
|
}
|
|
|
|
|
pthread_cond_signal(context->cv_recv);
|
|
|
|
|
|
|
|
|
|
free(msg);
|
|
|
|
|
/*
|
|
|
|
|
s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
|
|
|
|
if (s != 0)
|
|
|
|
|
handle_error_en(s, "pthread_setcancelstate");
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@@ -150,9 +170,9 @@ void *receiver(void *arg) {
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
msg = malloc(512);
|
|
|
|
|
header = malloc(strlen(context->ip) + 514);
|
|
|
|
|
header = malloc(strlen(context->ip) + 515);
|
|
|
|
|
memset(msg, 0, 512);
|
|
|
|
|
memset(header, 0, strlen(context->ip) + 514);
|
|
|
|
|
memset(header, 0, strlen(context->ip) + 515);
|
|
|
|
|
|
|
|
|
|
bytes = recv(context->ctx_confd, msg, 512, 0);
|
|
|
|
|
|
|
|
|
@@ -161,7 +181,7 @@ void *receiver(void *arg) {
|
|
|
|
|
pthread_cancel(*context->thread_send);
|
|
|
|
|
pthread_cancel(*context->thread_in);
|
|
|
|
|
pthread_cancel(*context->thread_out);
|
|
|
|
|
pthread_exit(&context->ctx_closed);
|
|
|
|
|
pthread_exit(context->ctx_closed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strncpy(header, context->ip, strlen(context->ip));
|
|
|
|
@@ -170,14 +190,14 @@ void *receiver(void *arg) {
|
|
|
|
|
|
|
|
|
|
/* Add message to list for other thread to print */
|
|
|
|
|
if (bytes != -1) {
|
|
|
|
|
pthread_mutex_lock(&context->mut_recv);
|
|
|
|
|
pthread_mutex_lock(context->mut_recv);
|
|
|
|
|
ListAppend(context->ctx_recvlist, header);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_recv);
|
|
|
|
|
pthread_mutex_unlock(context->mut_recv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Signal output thread, if it was waiting for something to print */
|
|
|
|
|
if (ListCount(context->ctx_recvlist) == 1) {
|
|
|
|
|
pthread_cond_signal(&context->cv_recv);
|
|
|
|
|
pthread_cond_signal(context->cv_recv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
@@ -189,27 +209,28 @@ void *output(void *arg) {
|
|
|
|
|
CONTEXT *context = (CONTEXT *)arg;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
msg = malloc(512 + strlen(context->ip));
|
|
|
|
|
|
|
|
|
|
/* Block if there's nothing to output */
|
|
|
|
|
if (ListCount(context->ctx_recvlist) == 0) {
|
|
|
|
|
pthread_mutex_lock(&context->mut_recvcv);
|
|
|
|
|
pthread_cond_wait(&context->cv_recv, &context->mut_recvcv);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_recvcv);
|
|
|
|
|
pthread_mutex_lock(context->mut_recvcv);
|
|
|
|
|
pthread_cond_wait(context->cv_recv, context->mut_recvcv);
|
|
|
|
|
pthread_mutex_unlock(context->mut_recvcv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg = malloc(515 + strlen(context->ip));
|
|
|
|
|
/* Remove a message from the list and print it to chatlog */
|
|
|
|
|
pthread_mutex_lock(&context->mut_recv);
|
|
|
|
|
pthread_mutex_lock(context->mut_recv);
|
|
|
|
|
ListFirst(context->ctx_recvlist);
|
|
|
|
|
strcpy(msg, (char*)ListCurr(context->ctx_recvlist));
|
|
|
|
|
ListRemove(context->ctx_recvlist);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_recv);
|
|
|
|
|
pthread_mutex_unlock(context->mut_recv);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(context->mut_render);
|
|
|
|
|
ncplane_printf(context->plane_log, "%s\n", msg);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&context->mut_render);
|
|
|
|
|
notcurses_render(context->ctx_nc);
|
|
|
|
|
pthread_mutex_unlock(&context->mut_render);
|
|
|
|
|
pthread_mutex_unlock(context->mut_render);
|
|
|
|
|
|
|
|
|
|
free(msg);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@@ -236,9 +257,15 @@ int main(int argc, char* argv[]) {
|
|
|
|
|
int yes = 1;
|
|
|
|
|
|
|
|
|
|
/* threading stuff */
|
|
|
|
|
CONTEXT *context = malloc(sizeof(CONTEXT));
|
|
|
|
|
LIST *receivelist, *sendlist;
|
|
|
|
|
pthread_t input_thread, send_thread, receive_thread, output_thread;
|
|
|
|
|
pthread_mutex_t sendmut, receivemut, sendcvmut, receivecvmut, rendermut;
|
|
|
|
|
pthread_mutex_t sendmut,
|
|
|
|
|
receivemut,
|
|
|
|
|
sendcvmut,
|
|
|
|
|
receivecvmut,
|
|
|
|
|
rendermut,
|
|
|
|
|
savedmut;
|
|
|
|
|
pthread_cond_t sendcv, receivecv;
|
|
|
|
|
char closed[] = "Connection closed.";
|
|
|
|
|
|
|
|
|
@@ -389,6 +416,10 @@ int main(int argc, char* argv[]) {
|
|
|
|
|
fprintf(stderr, "Error: render mutex init failed\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (pthread_mutex_init(&savedmut, NULL) != 0) {
|
|
|
|
|
fprintf(stderr, "Error: saved mutex init failed\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (pthread_cond_init(&sendcv, NULL) != 0) {
|
|
|
|
|
fprintf(stderr, "Error: send CV init failed\n");
|
|
|
|
|
return -1;
|
|
|
|
@@ -428,39 +459,43 @@ int main(int argc, char* argv[]) {
|
|
|
|
|
}
|
|
|
|
|
ncplane_set_scrolling(chatlog, true);
|
|
|
|
|
|
|
|
|
|
CONTEXT context = {
|
|
|
|
|
.ctx_recvlist = receivelist,
|
|
|
|
|
.ctx_sendlist = sendlist,
|
|
|
|
|
/* Lists to store messages */
|
|
|
|
|
context->ctx_recvlist = receivelist;
|
|
|
|
|
context->ctx_sendlist = sendlist;
|
|
|
|
|
|
|
|
|
|
.thread_in = &input_thread,
|
|
|
|
|
.thread_out = &output_thread,
|
|
|
|
|
.thread_recv = &receive_thread,
|
|
|
|
|
.thread_send = &send_thread,
|
|
|
|
|
/* Threads */
|
|
|
|
|
context->thread_in = &input_thread;
|
|
|
|
|
context->thread_out = &output_thread;
|
|
|
|
|
context->thread_recv = &receive_thread;
|
|
|
|
|
context->thread_send = &send_thread;
|
|
|
|
|
|
|
|
|
|
.mut_send = sendmut,
|
|
|
|
|
.mut_recv = receivemut,
|
|
|
|
|
.mut_sendcv = sendcvmut,
|
|
|
|
|
.mut_recvcv = receivecvmut,
|
|
|
|
|
.mut_render = rendermut,
|
|
|
|
|
/* Mutexes */
|
|
|
|
|
context->mut_send = &sendmut;
|
|
|
|
|
context->mut_recv = &receivemut;
|
|
|
|
|
context->mut_sendcv = &sendcvmut;
|
|
|
|
|
context->mut_recvcv = &receivecvmut;
|
|
|
|
|
context->mut_render = &rendermut;
|
|
|
|
|
|
|
|
|
|
.cv_send = sendcv,
|
|
|
|
|
.cv_recv = receivecv,
|
|
|
|
|
/* Condition variables */
|
|
|
|
|
context->cv_send = &sendcv;
|
|
|
|
|
context->cv_recv = &receivecv;
|
|
|
|
|
|
|
|
|
|
.ip = s,
|
|
|
|
|
.ctx_confd = confd,
|
|
|
|
|
/* Networking stuff */
|
|
|
|
|
context->ip = s;
|
|
|
|
|
context->ctx_confd = confd;
|
|
|
|
|
|
|
|
|
|
.ctx_nc = nc,
|
|
|
|
|
.plane_log = chatlog,
|
|
|
|
|
.plane_prompt = prompt,
|
|
|
|
|
.ctx_rows = rows,
|
|
|
|
|
.ctx_cols = cols,
|
|
|
|
|
.ctx_closed = closed,
|
|
|
|
|
};
|
|
|
|
|
/* Notcurses rendering stuff */
|
|
|
|
|
context->ctx_nc = nc;
|
|
|
|
|
context->plane_log = chatlog;
|
|
|
|
|
context->plane_prompt = prompt;
|
|
|
|
|
context->ctx_rows = rows;
|
|
|
|
|
context->ctx_cols = cols;
|
|
|
|
|
context->ctx_closed = closed;
|
|
|
|
|
|
|
|
|
|
pthread_create(&input_thread, NULL, input, &context);
|
|
|
|
|
pthread_create(&send_thread, NULL, sender, &context);
|
|
|
|
|
pthread_create(&output_thread, NULL, output, &context);
|
|
|
|
|
pthread_create(&receive_thread, NULL, receiver, &context);
|
|
|
|
|
pthread_create(&input_thread, NULL, input, context);
|
|
|
|
|
pthread_create(&send_thread, NULL, sender, context);
|
|
|
|
|
pthread_create(&output_thread, NULL, output, context);
|
|
|
|
|
pthread_create(&receive_thread, NULL, receiver, context);
|
|
|
|
|
|
|
|
|
|
pthread_join(input_thread, NULL);
|
|
|
|
|
pthread_join(output_thread, NULL);
|
|
|
|
|