-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_module.c
154 lines (139 loc) · 5.03 KB
/
events_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Created by georgii on 12.04.2021.
//
#include <malloc.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include "events_module.h"
#include "ui_module.h"
list_item_t* find_transfer_progress(list_item_t* transfer_list, transfer_progress_t *tp) {
list_item_t *item = transfer_list;
while (item != NULL && tp != NULL) {
transfer_progress_t *item_data = item->data;
if (item_data &&
0 == strcmp(item_data->triplet.filename, tp->triplet.filename) &&
0 == strncmp(item_data->triplet.hash, tp->triplet.hash, 32)
) {
return item;
}
item = item->next;
}
return NULL;
}
void destroy_transfer_progress(transfer_progress_t *tp) {
free(tp);
}
void destroy_action_message(char *str) {
free(str);
}
list_item_t* find_download(events_module_data_t* em, transfer_progress_t *tp) {
pthread_mutex_lock(&em->download_mutex);
list_item_t *item = find_transfer_progress(em->download_list, tp);
pthread_mutex_unlock(&em->download_mutex);
return item;
}
list_item_t* find_upload(events_module_data_t* em, transfer_progress_t *tp) {
pthread_mutex_lock(&em->upload_mutex);
list_item_t *item = find_transfer_progress(em->upload_list, tp);
pthread_mutex_unlock(&em->upload_mutex);
return item;
}
void init_events_module(events_module_data_t* em) {
pthread_mutex_init(&em->actions_mutex, NULL);
pthread_mutex_init(&em->download_mutex, NULL);
pthread_mutex_init(&em->upload_mutex, NULL);
em->actions_list = calloc(1, sizeof(list_item_t));
em->download_list = calloc(1, sizeof(list_item_t));
em->upload_list = calloc(1, sizeof(list_item_t));
}
void destroy_events_module(events_module_data_t* em) {
pthread_mutex_destroy(&em->actions_mutex);
pthread_mutex_destroy(&em->download_mutex);
pthread_mutex_destroy(&em->upload_mutex);
destroy_list(em->download_list, (int (*)(void *)) destroy_transfer_progress);
destroy_list(em->upload_list, (int (*)(void *)) destroy_transfer_progress);
destroy_list(em->actions_list, (int (*)(void *)) destroy_action_message);
}
void put_download(events_module_data_t* em, transfer_progress_t *progress) {
pthread_mutex_lock(&em->download_mutex);
list_item_t *found = find_transfer_progress(em->download_list, progress);
int8_t do_clear = 0;
if (found) {
transfer_progress_t *data = found->data;
data->transferred = progress->transferred;
} else {
em->download_list = push(em->download_list, progress);
do_clear = 1;
}
render_transfer_area(em->ui_data, do_clear);
pthread_mutex_unlock(&em->download_mutex);
}
void del_download(events_module_data_t* em, transfer_progress_t *progress) {
pthread_mutex_lock(&em->download_mutex);
list_item_t *found = find_transfer_progress(em->download_list, progress);
if (found) {
em->download_list = remove_el(em->download_list, found);
if (!em->download_list) {
put_action(em, "[EVENTS-MODULE] ERROR on del download");
}
} else {
// Apparently, deleted by other thread
}
render_transfer_area(em->ui_data, 1);
pthread_mutex_unlock(&em->download_mutex);
}
void put_upload(events_module_data_t* em, transfer_progress_t *progress) {
pthread_mutex_lock(&em->upload_mutex);
list_item_t *found = find_transfer_progress(em->upload_list, progress);
int8_t do_clear = 0;
if (found) {
transfer_progress_t *data = found->data;
data->transferred = progress->transferred;
} else {
em->upload_list = push(em->upload_list, progress);
do_clear = 1;
}
render_transfer_area(em->ui_data, do_clear);
pthread_mutex_unlock(&em->upload_mutex);
}
void del_upload(events_module_data_t* em, transfer_progress_t *progress) {
pthread_mutex_lock(&em->upload_mutex);
list_item_t *found = find_transfer_progress(em->upload_list, progress);
if (found) {
em->upload_list = remove_el(em->upload_list, found);
if (!em->upload_list) {
put_action(em, "[EVENTS-MODULE] ERROR on del upload");
}
} else {
// Apparently, deleted by other thread
}
render_transfer_area(em->ui_data, 1);
pthread_mutex_unlock(&em->upload_mutex);
}
void add_time_tag(char *str) {
time_t timer;
struct tm* tm_info;
timer = time(NULL);
tm_info = localtime(&timer);
strftime(str, 29, "[%Y-%m-%d %H:%M:%S] ", tm_info);
}
void put_action(events_module_data_t* em, const char *str) {
pthread_mutex_lock(&em->actions_mutex);
char *logged_str = calloc(1, 512);
add_time_tag(logged_str);
strcat(logged_str, str);
em->actions_list = push(em->actions_list, logged_str);
render_events_log(em->ui_data, 1);
pthread_mutex_unlock(&em->actions_mutex);
}
void log_error(events_module_data_t *em, char *msg) {
char error[256] = {0};
sprintf(error, msg, errno);
put_action(em, error);
}
void log_action(events_module_data_t* em, const char *msg, const char *arg) {
char action[256] = {0};
sprintf(action, msg, arg);
put_action(em, action);
}