Skip to content

Commit

Permalink
Se crea el callback para la desconexión de los clientes
Browse files Browse the repository at this point in the history
  • Loading branch information
nforla committed Jul 16, 2019
1 parent 66fbeb2 commit 371a5d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
30 changes: 30 additions & 0 deletions chatroom_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ void init_app(){

void on_menu_connect_click(GtkWidget *widget, gpointer *data);
void on_connect_btn_clicked(GtkWidget *widget, ConnectionRequest *conn_request);
void on_disconnect_btn_clicked(GtkWidget *widget, gpointer *data);
void on_send_btn_clicked(GtkWidget *widget, gpointer *data);

int main(int argc, char* argv[]) {
Expand Down Expand Up @@ -173,6 +174,20 @@ void update_chat_view(char* msg_display){
gtk_text_buffer_insert(buffer, &iter, "\n", 1); //Inserts a new line
}

void clear_chat_view(){

GtkTextIter* start = malloc(sizeof(GtkTextIter));
GtkTextIter* end = malloc(sizeof(GtkTextIter));

GtkTextView *chat_view = GTK_TEXT_VIEW(gtk_builder_get_object(app_builder, "chatroom_view"));
GtkTextBuffer *buffer = gtk_text_view_get_buffer(chat_view);
gtk_text_buffer_get_bounds(buffer, start, end);
gtk_text_buffer_delete(buffer, start, end);

free(start);
free(end);
}

void* chat_listener_thread(void* param){

printf("Chat thread initialized\n");
Expand Down Expand Up @@ -209,6 +224,8 @@ void create_chat_thread(){
pthread_attr_t atributes;
pthread_attr_init(&atributes);
pthread_attr_setdetachstate(&atributes, PTHREAD_CREATE_DETACHED); //Creates detached thread that frees resources when finish
int status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
printf("Stat th: %d\n",status );
pthread_create(chat_data->chat_listener, &atributes, chat_listener_thread, NULL);

}
Expand Down Expand Up @@ -245,6 +262,19 @@ void on_connect_btn_clicked(GtkWidget *widget, ConnectionRequest *conn_request){
free(conn_request);
}

void on_disconnect_btn_clicked(GtkWidget *widget, gpointer *data){

printf("Disconnect clicked\n");

pthread_cancel(*chat_data->chat_listener); // Cancels thread listening incoming msgs.

close(chat_data->sockfd); // Closes connection

clear_chat_view();

printf("Connection closed\n");
}

void on_send_btn_clicked(GtkWidget *widget, gpointer *data){

printf("Send click\n");
Expand Down
1 change: 1 addition & 0 deletions chatroom_client_UI.glade
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
<signal name="button-press-event" handler="on_disconnect_btn_clicked" swapped="no"/>
</object>
</child>
</object>
Expand Down

0 comments on commit 371a5d0

Please sign in to comment.