Skip to content

Commit

Permalink
Merge branch 'master' of gitlab.ucode.world:connect-khpi/connect-ucha…
Browse files Browse the repository at this point in the history
…t/pbalazhy-2
  • Loading branch information
YaroslavChuiko committed Feb 6, 2022
2 parents f84deca + e222959 commit f8350c5
Show file tree
Hide file tree
Showing 26 changed files with 252 additions and 337 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ OBJS = $(OBJDIR)/*.o
all: $(LIBMX) $(UTILSLIB) $(CJSON) $(SQLITE) $(SERVER) $(CLIENT)

$(LIBMX):
# make -sC $(LIBMX_DIR)
make -sC $(LIBMX_DIR)

$(UTILSLIB):
make -sC $(UTILSLIB_DIR)

$(CJSON):
# make -sC $(CJSON_DIR)
make -sC $(CJSON_DIR)

$(SQLITE):
# make -sC $(SQLITE_DIR)
make -sC $(SQLITE_DIR)

$(SERVER):
make -sC $(SERVER_DIR)
Expand All @@ -51,9 +51,9 @@ uninstall:
make -sC $(SERVER_DIR) $@
make -sC $(CLIENT_DIR) $@
make -sC $(UTILSLIB_DIR) $@
# make -sC $(LIBMX_DIR) $@
# make -sC $(CJSON_DIR) $@
# make -sC $(SQLITE_DIR) $@
make -sC $(LIBMX_DIR) $@
make -sC $(CJSON_DIR) $@
make -sC $(SQLITE_DIR) $@
make clean
rm -f $(UCHAT)

Expand Down
2 changes: 1 addition & 1 deletion client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CFLAGS = -std=gnu11 -pthread -g
# -Wall -Wextra -Werror -Wpedantic
# CFLAGS = -std=c11 -Wall -Wextra -Werror -Wpedantic -pthread -g

UTIL_DIRS = logs data/avatars
UTIL_DIRS = data/avatars
OBJDIR = obj
SRCDIR = src src/request_handlers src/gui
UCHAT = ../uchat
Expand Down
17 changes: 6 additions & 11 deletions client/inc/client.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#ifndef CLIENT_HEADER
#define CLIENT_HEADER

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <gtk/gtk.h>
#include <cairo.h>
// #include <unistd.h>

#ifdef __MACH__
#include "../../libraries/openssl/openssl/ssl.h"
Expand All @@ -26,9 +22,10 @@
#include "../../libraries/cjson/inc/cJSON.h"
#include "../../libraries/libmx/inc/libmx.h"
#include "../../utils/inc/utils.h"

#include "const.h"
#include "types.h"
#include "const.h"

extern GtkWidget *main_window;

unsigned long get_current_time();
char* get_string_time(unsigned long seconds);
Expand Down Expand Up @@ -164,8 +161,8 @@ t_response_code get_response_code(cJSON* json);
t_request_type get_request_type(cJSON* json);
void update_last_chat_msg(t_chat* chat_to_update, t_msg* new_msg);

t_response_code handle_get_user_image(int user_id, char** avatar_path);
t_response_code handle_update_user_image(char *path);
// t_response_code handle_get_user_image(int user_id, char** avatar_path);
// t_response_code handle_update_user_image(char *path);

void client_init(int server_fd, SSL *ssl, SSL_CTX* ctx);
void client_cleanup(bool is_client_exit);
Expand All @@ -174,8 +171,6 @@ void handle_arg_errors(char** argv);

void init_ssl(SSL_CTX **ctx);
void connect_ssl(SSL **ssl, int* server_fd, SSL_CTX **ctx);
void client_log(const char* info, t_info_type type);
char* get_log_name();

void build_rightbar_chat();
void delete_message(GtkWidget *widget, GdkEventButton *event, t_msg *message);
Expand Down
2 changes: 1 addition & 1 deletion client/inc/const.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CLIENT_CONST_H
#define CLIENT_CONST_H

#define IP_ADDRESS "127.0.0.1" // remove later
#define IP_ADDRESS "127.0.0.1" // default

#define WINDOW_WIDTH 880
#define WINDOW_HEIGHT 760
Expand Down
5 changes: 0 additions & 5 deletions client/inc/types.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#ifndef CLIENT_TYPES_H
#define CLIENT_TYPES_H

#include <gtk/gtk.h>

extern GtkWidget *main_window;

// Type for the user saved from the server
typedef struct s_user {
int user_id;
Expand All @@ -23,7 +19,6 @@ typedef struct s_client_utils {
t_user* current_user;
t_chat* current_chat;
t_chat* chatlist;
char* log_name;
// for suspending server updates during request handling
bool is_suspended;
} t_client_utils;
Expand Down
2 changes: 1 addition & 1 deletion client/src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main(int argc, char **argv) {
SSL_CTX *ctx = NULL;
SSL *ssl = NULL;

connect_to_server(argv[1], atoi(argv[1]), &server_socket, &ctx, &ssl);
connect_to_server(argv[1], atoi(argv[2]), &server_socket, &ctx, &ssl);
client_init(server_socket, ssl, ctx);

gtk_init(&argc, &argv);
Expand Down
1 change: 0 additions & 1 deletion client/src/client_cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ void client_cleanup(bool is_client_exit) {
pthread_mutex_lock(&utils->lock);
user_cleanup(&utils->current_user);
mx_clear_chat_list(&utils->chatlist);
mx_strdel(&utils->log_name);
pthread_mutex_unlock(&utils->lock);

if (is_client_exit) {
Expand Down
26 changes: 2 additions & 24 deletions client/src/client_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ void client_init(int server_fd, SSL *ssl, SSL_CTX* ctx) {
utils->current_user = NULL;
utils->current_chat = NULL;
utils->chatlist = NULL;
utils->log_name = NULL;
utils->is_suspended = true;
pthread_mutex_unlock(&utils->lock);

Expand All @@ -27,7 +26,7 @@ void client_init(int server_fd, SSL *ssl, SSL_CTX* ctx) {
// Check if command line arguments are valid
void handle_arg_errors(char** argv) {

if (argv[1] == NULL /*|| argv[2] == NULL*/) {
if (argv[1] == NULL || argv[2] == NULL) {
mx_printerr("usage: ./uchat [ip] [port]\n");
exit(EXIT_FAILURE);
}
Expand All @@ -42,7 +41,7 @@ void connect_to_server(const char* ip_address, int port, int* server_fd, SSL_CTX
init_ssl(ctx);

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
server_addr.sin_addr.s_addr = inet_addr(ip_address);
server_addr.sin_port = htons(port);

if ((*server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
Expand All @@ -58,27 +57,6 @@ void connect_to_server(const char* ip_address, int port, int* server_fd, SSL_CTX
connect_ssl(ssl, server_fd, ctx);
}

void client_log(const char* info, t_info_type type) {

FILE* fd = fopen(utils->log_name, "a+");
fprintf(fd, "%s: ", type == ERROR_LOG ? "ERROR" : "INFO");
fprintf(fd, "%s\n", info);
fclose(fd);

}

char* get_log_name() {

char* log_name = mx_strnew(mx_strlen(CLIENTLOG_NAME) + 5);
char* id_str = mx_itoa(utils->current_user->user_id);
mx_strcat(log_name, CLIENTLOG_NAME);
mx_strcat(log_name, id_str);
mx_strcat(log_name, ".log");
mx_strdel(&id_str);
return log_name;

}

void handle_error(const char* error) {

char* err_str = mx_strjoin(error, "\n");
Expand Down
6 changes: 3 additions & 3 deletions client/src/gui/build_change_avatar_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ void change_avatar_btn_click(GtkWidget *widget, gpointer data) {

char *new_avatar = (char*)gtk_entry_get_text(GTK_ENTRY(path_field));

handle_update_user_image(new_avatar);
usleep(4000000);
handle_get_user_image(utils->current_user->user_id, &utils->current_user->avatar_path);
// handle_update_user_image(new_avatar);
// usleep(4000000);
// handle_get_user_image(utils->current_user->user_id, &utils->current_user->avatar_path);

gtk_widget_destroy(toplevel);
}
Expand Down
1 change: 0 additions & 1 deletion client/src/request_handlers/get_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ t_response_code handle_get_chats_request() {
utils->current_chat = mx_get_chat_by_id(utils->chatlist, curr_chat_id);
}

mx_print_chat_list(utils->chatlist);
return R_SUCCESS;

}
114 changes: 57 additions & 57 deletions client/src/request_handlers/get_user_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,70 +20,70 @@ static char* get_file_path_for(int user_id) {

}

t_response_code handle_get_user_image(int user_id, char** avatar_path) {
// t_response_code handle_get_user_image(int user_id, char** avatar_path) {

utils->is_suspended = true;
// Send request for receiving user avatar
cJSON *json = cJSON_CreateObject();
cJSON_AddNumberToObject(json, "type", REQ_GET_USER_IMAGE);
cJSON_AddNumberToObject(json, "user_id", user_id);
char* json_str = cJSON_PrintUnformatted(json);
cJSON_Delete(json);
// utils->is_suspended = true;
// // Send request for receiving user avatar
// cJSON *json = cJSON_CreateObject();
// cJSON_AddNumberToObject(json, "type", REQ_GET_USER_IMAGE);
// cJSON_AddNumberToObject(json, "user_id", user_id);
// char* json_str = cJSON_PrintUnformatted(json);
// cJSON_Delete(json);

send_to_server(utils->ssl, json_str);
free(json_str);
// send_to_server(utils->ssl, json_str);
// free(json_str);

// Creating temp file
FILE *fp;
char* file_path = get_file_path_for(user_id);
if ((fp = fopen(file_path, "wb")) == NULL) {
handle_error("Cannot open image file");
mx_strdel(&file_path);
return R_FILE_ERROR;
}
// // Creating temp file
// FILE *fp;
// char* file_path = get_file_path_for(user_id);
// if ((fp = fopen(file_path, "wb")) == NULL) {
// handle_error("Cannot open image file");
// mx_strdel(&file_path);
// return R_FILE_ERROR;
// }

// Reciving len of encoded image
int len_encoded = 0;
usleep(500000);
if(recv(utils->server_fd, &len_encoded, sizeof(int), 0) == 0) {
handle_error("Error while receiving length");
mx_strdel(&file_path);
return R_FILE_ERROR;
}
// // Reciving len of encoded image
// int len_encoded = 0;
// usleep(500000);
// if(recv(utils->server_fd, &len_encoded, sizeof(int), 0) == 0) {
// handle_error("Error while receiving length");
// mx_strdel(&file_path);
// return R_FILE_ERROR;
// }

// Reciving encoded image
unsigned char *encoded = malloc((sizeof(char) * len_encoded));
mx_memset(encoded, 0, len_encoded);
recv_image_from_server(&utils->server_fd, &encoded, len_encoded);
// // Reciving encoded image
// unsigned char *encoded = malloc((sizeof(char) * len_encoded));
// mx_memset(encoded, 0, len_encoded);
// recv_image_from_server(&utils->server_fd, &encoded, len_encoded);

// Decoding image
unsigned int flen = b64d_size(len_encoded);
unsigned char *decoded = malloc( (sizeof(char) * flen) + 1);
flen = b64_decode(encoded, len_encoded, decoded);
free(encoded);
// // Decoding image
// unsigned int flen = b64d_size(len_encoded);
// unsigned char *decoded = malloc( (sizeof(char) * flen) + 1);
// flen = b64_decode(encoded, len_encoded, decoded);
// free(encoded);

// Writing decoded image to created file
fwrite(decoded, flen, 1, fp);
if (ferror(fp)) {
handle_error("fwrite() failed");
mx_strdel(&file_path);
return R_FILE_ERROR;
}
int r;
if ((r = fclose(fp)) == EOF) {
handle_error("Cannot close file handler");
mx_strdel(&file_path);
return R_FILE_ERROR;
}
// // Writing decoded image to created file
// fwrite(decoded, flen, 1, fp);
// if (ferror(fp)) {
// handle_error("fwrite() failed");
// mx_strdel(&file_path);
// return R_FILE_ERROR;
// }
// int r;
// if ((r = fclose(fp)) == EOF) {
// handle_error("Cannot close file handler");
// mx_strdel(&file_path);
// return R_FILE_ERROR;
// }

free(decoded);
// free(decoded);

if (*avatar_path) {
mx_strdel(avatar_path);
}
*avatar_path = mx_strdup(file_path);
mx_strdel(&file_path);
utils->is_suspended = false;
return R_SUCCESS;
// if (*avatar_path) {
// mx_strdel(avatar_path);
// }
// *avatar_path = mx_strdup(file_path);
// mx_strdel(&file_path);
// utils->is_suspended = false;
// return R_SUCCESS;

}
// }
1 change: 0 additions & 1 deletion client/src/request_handlers/login.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ t_response_code handle_login_request(const char* user_name, const char* user_pas

// handle_get_user_image(utils->current_user->user_id, &utils->current_user->avatar_path);
handle_get_chats_request();
utils->log_name = get_log_name();

}

Expand Down
2 changes: 0 additions & 2 deletions client/src/request_handlers/search_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ t_chat* handle_search_chats_request(const char* search_str) {
}
free(response);

mx_print_chat_list(chat_list);

utils->is_suspended = false;
return chat_list;

Expand Down
8 changes: 1 addition & 7 deletions client/src/request_handlers/server_updates.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ static int handle_new_message(t_chat* curr_chat, int message_id, bool is_current

if (is_current && message_id > curr_last_msg_id) {

client_log("You're reading an incoming message", INFO_LOG);
if (!curr_chat->new_messages) {

// handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path);
Expand All @@ -70,7 +69,6 @@ static int handle_new_message(t_chat* curr_chat, int message_id, bool is_current

} else if (message_id > last_new_msg_id) {

client_log("You have an incoming message", INFO_LOG);
// handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path);
mx_msg_push_back(&curr_chat->new_messages, new_msg);
curr_chat->last_new_msg = mx_get_last_msg_node(curr_chat->new_messages);
Expand All @@ -79,11 +77,7 @@ static int handle_new_message(t_chat* curr_chat, int message_id, bool is_current
}

update_chatlist_item_info(curr_chat);

char str[200];
sprintf(str, "This is a t_msg msg:\n\ttext: %s, chat_id: %d, sender_id: %d, sender_name: %s, date: %s, avatar: %d\n",
new_msg->text, new_msg->chat_id, new_msg->sender_id, new_msg->sender_name, new_msg->date_str, new_msg->avatar_color);
client_log(str, INFO_LOG);

g_usleep(100000);
return 0;

Expand Down
Loading

0 comments on commit f8350c5

Please sign in to comment.