-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.c
189 lines (141 loc) · 4.75 KB
/
output.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "output.h"
#include "control.h"
#include "threadsafelist.h"
static pthread_t s_threadOutput;
static bool s_threadHasExited = false;
static pthread_cond_t s_messageReceivedCondition = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t s_messageReceivedMutex = PTHREAD_MUTEX_INITIALIZER;
// Free any remaining memory and ensure mutex is unlocked
static void cleanup(void* args) {
char** receivedMessageAddress = args;
char* receivedMessage = *receivedMessageAddress;
if (receivedMessage != NULL) {
free(receivedMessage);
}
pthread_mutex_trylock(&s_messageReceivedMutex);
pthread_mutex_unlock(&s_messageReceivedMutex);
return;
}
// The thread to print output to the terminal
static void* outputThread(void* args) {
int status = 0;
OutputThreadArguments* outputArguments = args;
List* pReceivedMessagesList = outputArguments->pReceivedMessagesList;
bool isFirstSegment = true;
char* receivedMessage = NULL;
char** receivedMessageAddress = &receivedMessage;
pthread_cleanup_push(cleanup, receivedMessageAddress);
while (1) {
// If there are no received messages, wait until one arrives
if (ThreadSafeList_count(pReceivedMessagesList) == 0) {
status = pthread_mutex_lock(&s_messageReceivedMutex);
if (status) {
fputs("[Error]: could not lock message received mutex\n", stdout);
exit(1);
}
status = pthread_cond_wait(&s_messageReceivedCondition, &s_messageReceivedMutex);
if (status) {
fputs("[Error]: could not wait on message received condition variable\n", stdout);
exit(1);
}
status = pthread_mutex_unlock(&s_messageReceivedMutex);
if (status) {
fputs("[Error]: could not unlock message received mutex\n", stdout);
exit(1);
}
}
// Get message from received messages queue
receivedMessage = ThreadSafeList_trim(pReceivedMessagesList);
if (receivedMessage == NULL) {
fputs("[Error]: received message was null\n", stdout);
exit(1);
}
// Detect if the received message is the last part of an existing line
bool isLastSegment = (receivedMessage[MESSAGE_MAX_SIZE - 2] == '\0' || receivedMessage[MESSAGE_MAX_SIZE - 2] == '\n');
// Prints the received message to the terminal
// Also detects if the program should be terminated
if (isFirstSegment) {
fputs("[Remote]: ", stdout);
fputs(receivedMessage, stdout);
if (strcmp(receivedMessage, TERMINATE) == 0) {
fputs("[The remote user has sent the exit command]\n", stdout);
s_threadHasExited = true;
} else if (!isLastSegment) {
isFirstSegment = false;
}
} else {
fputs(receivedMessage, stdout);
if (isLastSegment) {
isFirstSegment = true;
}
}
fflush(stdout);
free(receivedMessage);
receivedMessage = NULL;
if (s_threadHasExited) {
break;
}
}
pthread_cleanup_pop(1);
s_threadHasExited = true;
// Signal the main thread that the program should terminate
Control_signalTermination();
return NULL;
}
// Signals the output thread that there is a received message
void Output_signalMessageReceived() {
int status = 0;
status = pthread_mutex_lock(&s_messageReceivedMutex);
if (status) {
fputs("[Error]: could not lock message received mutex\n", stdout);
exit(1);
}
status = pthread_cond_signal(&s_messageReceivedCondition);
if (status) {
fputs("[Error]: could not signal message received condition variable\n", stdout);
exit(1);
}
status = pthread_mutex_unlock(&s_messageReceivedMutex);
if (status) {
fputs("[Error]: could not unlock message received mutex\n", stdout);
exit(1);
}
return;
}
// Initializes the output thread
void Output_init(OutputThreadArguments* pOutputArguments) {
int status = 0;
status = pthread_create(&s_threadOutput, NULL, outputThread, pOutputArguments);
if (status) {
fputs("[Error]: could not create output thread\n", stdout);
exit(1);
}
return;
}
// Shutdowns the output thread and performs necessary cleanup
void Output_shutdown() {
int status = 0;
if (!s_threadHasExited) {
status = pthread_cancel(s_threadOutput);
}
if (status) {
fputs("[Error]: could not cancel output thread\n", stdout);
}
status = pthread_join(s_threadOutput, NULL);
if (status) {
fputs("[Error]: could not join with output thread\n", stdout);
}
status = pthread_cond_destroy(&s_messageReceivedCondition);
if (status) {
fputs("[Error]: could not destroy message received condition variable\n", stdout);
}
status = pthread_mutex_destroy(&s_messageReceivedMutex);
if (status) {
fputs("[Error]: could not destroy message received mutex\n", stdout);
}
return;
}