-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·228 lines (188 loc) · 8.09 KB
/
main.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "conf.h"
#include "client.h"
#include "log.h"
#include "server.h"
#include "utils.h"
#include "communication.h"
#include <signal.h>
//------------------------------------------------------------------------------------------------
uint32_t executionTimeRequested; // secs
static struct timespec executionTimeActualStart, executionTimeActualFinish;
pthread_t communicationThreads[COMMUNICATION_WORKERS_MAX];
uint8_t communicationThreadsAvailable = COMMUNICATION_WORKERS_MAX;
static pthread_t pollingThread, producerThread, datetimeListenerThread;
pthread_mutex_t messagesBufferLock, activeDevicesLock, availableThreadsLock, messagesStatsLock, logLock, logEventLock;
MessagesStats messagesStats;
uint32_t CLIENT_AEM;
uint32_t setupDatetimeAem;
// Communication time for each device
struct timeval CLIENT_AEM_CONN_START_LIST[CLIENT_AEM_LIST_LENGTH][MAX_CONNECTIONS_WITH_SAME_CLIENT] = {0, 0};
struct timeval CLIENT_AEM_CONN_END_LIST[CLIENT_AEM_LIST_LENGTH][MAX_CONNECTIONS_WITH_SAME_CLIENT] = {0, 0};
uint8_t CLIENT_AEM_CONN_N_LIST[CLIENT_AEM_LIST_LENGTH] = {0};
//------------------------------------------------------------------------------------------------
extern messages_head_t messagesHead;
extern messages_head_t inboxHead;
/// \brief Handler of SIGALRM signal. Used to terminate execution when MAX_EXECUTION_TIME finishes.
/// \param signo
/// \return void - Actually this function terminates program execution.
static void onAlarm(int signo);
/// \brief Handler of SIGALRM signal. Used to terminate setup process if exceeds timeout.
/// \param signo
/// \return void - This function terminates program execution.
static void onSetupAlarm(int signo);
/// \brief
/// \example ./Final [MAX_EXECUTION_TIME] [SETUP_DATE_TIME_AEM]
/// \param argc
/// \param argv
/// \return
int main( int argc, char **argv )
{
// Message message;
// messagesStats.produced = 0;
// messagesStats.received = 0;
// messagesStats.received_for_me = 0;
// messagesStats.transmitted = 0;
// messagesStats.transmitted_to_recipient = 0;
//
// log_tearUp( "session1.json" );
// log_event_start( "connnection", 9026, 8600 );
//
// generateRandomMessage( &message );
// log_event_message( "received", &message );
// generateRandomMessage( &message );
// log_event_message( "received", &message );
//
// generateRandomMessage( &message );
// log_event_message( "transmitted", &message );
// generateRandomMessage( &message );
// log_event_message( "transmitted", &message );
//
// log_event_stop();
// log_tearDown( 0.0 );
//
// return 1;
int status;
// Set max execution time ( in seconds )
executionTimeRequested = ( argc < 2 ) ? MAX_EXECUTION_TIME :
(uint32_t) strtol( argv[1], (char **)NULL, STRSEP_BASE_10 );
// Initialize RNG
srand((unsigned int) time( NULL ));
// Initialize Locks
status = pthread_mutex_init( &messagesBufferLock, NULL );
if ( status != 0 )
error( status, "\tmain(): pthread_mutex_init( messagesBufferLock ) failed" );
status = pthread_mutex_init( &activeDevicesLock, NULL );
if ( status != 0 )
error( status, "\tmain(): pthread_mutex_init( activeDevicesLock ) failed" );
status = pthread_mutex_init( &availableThreadsLock, NULL );
if ( status != 0 )
error( status, "\tmain(): pthread_mutex_init( activeDevicesLock ) failed" );
status = pthread_mutex_init( &messagesStatsLock, NULL );
if ( status != 0 )
error( status, "\tmain(): pthread_mutex_init( messagesStatsLock ) failed" );
status = pthread_mutex_init( &logEventLock, NULL );
if ( status != 0 )
error( status, "\tmain(): pthread_mutex_init( logEventLock ) failed" );
// Get AEM of running device
CLIENT_AEM = getClientAem("wlan0");
printf( "AEM = %d\n", CLIENT_AEM );
// Initialize types
messagesHead = 0;
inboxHead = 0;
// Initialize logger
log_tearUp( "session1.json" );
messagesStats.produced = 0;
messagesStats.received = 0;
messagesStats.received_for_me = 0;
messagesStats.transmitted = 0;
messagesStats.transmitted_to_recipient = 0;
// Setup datetime
if ( 1 == SYNC_DATETIME )
{
setupDatetimeAem = ( argc < 3 ) ? SETUP_DATETIME_AEM : (uint32_t) strtol( argv[1], (char **)NULL, STRSEP_BASE_10 );
if ( setupDatetimeAem > 0 )
{
if ( CLIENT_AEM == setupDatetimeAem )
{
// Start datetime transmitter server ( in a new thread )
status = pthread_create( &datetimeListenerThread, NULL, (void *) communication_datetime_listener_worker, NULL );
if ( status != 0 )
error( status, "\tmain(): pthread_create( datetimeListenerThread ) failed" );
}
else
{
// Setup alarm for setup
alarm( SETUP_DATETIME_TIMEOUT );
signal( SIGALRM, onSetupAlarm );
// Receive & set datetime from datetime server
if ( false == communication_datetime_receiver() )
error( -1, "\tmain(): communication_datetime_receiver() failed" );
}
}
}
// Start recording actual time
clock_gettime(CLOCK_REALTIME, &executionTimeActualStart);
// Setup alarm
alarm( executionTimeRequested );
signal( SIGALRM, onAlarm );
// Start polling client ( in a new thread )
status = pthread_create(&pollingThread, NULL, (void *) polling_worker, NULL);
if ( status != 0 )
error( status, "\tmain(): pthread_create( pollingThread ) failed" );
// Start producer client ( in a new thread )
status = pthread_create(&producerThread, NULL, (void *) producer_worker, NULL);
if ( status != 0 )
error( status, "\tmain(): pthread_create( producerThread ) failed" );
// Start listening server ( main thread )
listening_worker();
return EXIT_SUCCESS;
}
static void onAlarm( int signo )
{
int status;
fprintf( stdout, "Caught the SIGALRM signal ( signo = %d )", signo );
// Kill Producer Thread
status = pthread_cancel( producerThread );
if ( status != 0 )
error( status, "\tonAlarm(): pthread_cancel() on producerThread failed" );
status = pthread_join( producerThread, NULL );
if ( status != 0 )
error( status, "\tonAlarm(): pthread_join() on producerThread failed" );
// Kill Polling Thread
status = pthread_cancel( pollingThread );
if ( status != 0 )
error( status, "\tonAlarm(): pthread_cancel() on pollingThread failed" );
status = pthread_join( pollingThread, NULL );
if ( status != 0 )
error( status, "\tonAlarm(): pthread_join() on pollingThread failed" );
// Kill Datetime Listener Thread
if ( CLIENT_AEM == setupDatetimeAem )
{
status = pthread_cancel( datetimeListenerThread );
if ( status != 0 )
error( status, "\tonAlarm(): pthread_cancel() on datetimeListenerThread failed" );
status = pthread_join( datetimeListenerThread, NULL );
if ( status != 0 )
error( status, "\tonAlarm(): pthread_join() on datetimeListenerThread failed" );
}
// Find actual execution time
clock_gettime(CLOCK_REALTIME, &executionTimeActualFinish);
long executionTimeActualSeconds = executionTimeActualFinish.tv_sec - executionTimeActualStart.tv_sec;
long executionTimeActualNanoSeconds = executionTimeActualFinish.tv_nsec - executionTimeActualStart.tv_nsec;
if (executionTimeActualStart.tv_nsec > executionTimeActualFinish.tv_nsec) // clock underflow
{
--executionTimeActualSeconds;
executionTimeActualNanoSeconds += 1e9;
}
double executionTimeActual = (double)executionTimeActualSeconds + (double)executionTimeActualNanoSeconds/(double)1e9;
// Close logger
messagesStats.producedDelayAvg /= ( float ) messagesStats.produced; // avg
messagesStats.producedDelayAvg /= 60.0; // sec --> min
log_tearDown(executionTimeActual);
exit( EXIT_SUCCESS );
}
static void onSetupAlarm( int signo )
{
fprintf( stderr, "onSetupAlarm(): Setup timeout (%d sec) reached! Exiting...\n", SETUP_DATETIME_TIMEOUT );
exit( EXIT_FAILURE );
}