-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputerSystem.cpp
421 lines (357 loc) · 10.2 KB
/
ComputerSystem.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <pthread.h>
#include <sys/dispatch.h>
pthread_attr_t attr;
typedef struct _pulse msg_header_t;
#define R_TO_CS "radar"
#define OC_TO_CS "operator_console"
#define CS_TO_D "data_display"
// Airspace
#define MIN_X 0
#define MAX_X 100000
#define MIN_Y 0
#define MAX_Y 100000
#define MIN_Z 15000
#define MAX_Z 40000
// Constraints
#define DXY 3000
#define DZ 1000
// Structure to store data coming from Radar
typedef struct Aircraft {
msg_header_t hdr;
float time;
int id;
float x, y, z;
float speedX, speedY, speedZ;
}aircraft_data_t;
aircraft_data_t aircraft_data[10];
typedef struct OperatorMessage {
msg_header_t hdr;
int id;
float speedX, speedY, speedZ;
}oc_msg_t;
typedef struct Timeframe {
msg_header_t hdr;
int n;
}time_msg_t;
time_msg_t timeframe;
// Server Thread for Client Radar
void *threadRadar(void *arg) {
printf("[Computer System] Running server...\n\n");
name_attach_t *attach;
int rcvid;
int server_coid;
if ((server_coid = name_open(CS_TO_D, 0)) == -1) {
pthread_exit(NULL);
}
if ((attach = name_attach(NULL, R_TO_CS, 0)) == NULL) {
pthread_exit(NULL);
}
while (1) {
for (int i=0; i<10; i++) {
rcvid = MsgReceive(attach->chid, &aircraft_data[i], sizeof(aircraft_data[i]), NULL);
// Error condition, exit
if (rcvid == -1)
break;
// Pulse received
if (rcvid == 0) {
switch (aircraft_data[i].hdr.code) {
case _PULSE_CODE_DISCONNECT:
ConnectDetach(aircraft_data[i].hdr.scoid);
break;
case _PULSE_CODE_UNBLOCK:
break;
default:
break;
}
continue;
}
//name_open() sends a connect message, must EOK this
if (aircraft_data[i].hdr.type == _IO_CONNECT ) {
MsgReply( rcvid, EOK, NULL, 0 );
continue;
}
// Some other QNX IO message was received; reject it
if (aircraft_data[i].hdr.type > _IO_BASE && aircraft_data[i].hdr.type <= _IO_MAX ) {
MsgError( rcvid, ENOSYS );
continue;
}
// Getting Messages from Client
if (aircraft_data[i].hdr.type == 0x00) {
if (aircraft_data[i].hdr.subtype == 0x01) {
//printf("%f\t %d\t %f\t %f\t %f\t %f\t %f\t %f\n", aircraft_data[i].time, aircraft_data[i].id, aircraft_data[i].x, aircraft_data[i].y, aircraft_data[i].z, aircraft_data[i].speedX, aircraft_data[i].speedY, aircraft_data[i].speedZ);
}
}
MsgReply(rcvid, EOK, 0, 0);
}
}
/* Remove the name from the space */
name_detach(attach, 0);
pthread_exit(NULL);
}
// Thread to check for collision
void *threadCheckCollision(void *arg) {
name_attach_t *attach;
int rcvid;
//aircraft_data_t aircraft_i_after_t[10], aircraft_j_after_t[10];
if ((attach = name_attach(NULL, R_TO_CS, 0)) == NULL)
pthread_exit(NULL);
while (1) {
for (int i=0; i<10; i++) {
rcvid = MsgReceive(attach->chid, &aircraft_data[i], sizeof(aircraft_data[i]), NULL);
// Error condition, exit
if (rcvid == -1)
break;
// Pulse received
if (rcvid == 0) {
switch (aircraft_data[i].hdr.code) {
case _PULSE_CODE_DISCONNECT:
ConnectDetach(aircraft_data[i].hdr.scoid);
break;
case _PULSE_CODE_UNBLOCK:
break;
default:
break;
}
continue;
}
//name_open() sends a connect message, must EOK this
if (aircraft_data[i].hdr.type == _IO_CONNECT ) {
MsgReply( rcvid, EOK, NULL, 0 );
continue;
}
// Some other QNX IO message was received; reject it
if (aircraft_data[i].hdr.type > _IO_BASE && aircraft_data[i].hdr.type <= _IO_MAX ) {
MsgError( rcvid, ENOSYS );
continue;
}
if (aircraft_data[i].hdr.type == 0x00) {
if (aircraft_data[i].hdr.subtype == 0x01) {
for (int j=i+1; j<10; j++) {
// Check for minimum separation
float distance = sqrt(pow(aircraft_data[i].x - aircraft_data[j].x, 2) + pow(aircraft_data[i].y - aircraft_data[j].y, 2) + pow(aircraft_data[i].z - aircraft_data[j].z, 2));
float dxy = sqrt(pow(aircraft_data[i].x - aircraft_data[j].x, 2) + pow(aircraft_data[i].y - aircraft_data[j].y, 2));
float dz = abs(aircraft_data[i].z - aircraft_data[j].z);
if (distance < 50) {
printf("\n!!! Collision Alert !!! \n");
printf("Aircraft #%d and Aircraft #%d collided at T = %f seconds.\n", aircraft_data[i].id, aircraft_data[j].id, aircraft_data[i].time);
}
if (dxy < DXY && dz < DZ) {
printf("\n!!! Safety Violation !!!\n");
printf("Aircraft #%d and Aircraft #%d\n", aircraft_data[i].id, aircraft_data[j].id);
}
timeframe.n = 180;
for (int t=0; t<timeframe.n; t++) {
float x1 = aircraft_data[i].x + (aircraft_data[i].speedX * t);
float y1 = aircraft_data[i].y + (aircraft_data[i].speedY * t);
float z1 = aircraft_data[i].z + (aircraft_data[i].speedZ * t);
float x2 = aircraft_data[j].x + (aircraft_data[j].speedX * t);
float y2 = aircraft_data[j].y + (aircraft_data[j].speedY * t);
float z2 = aircraft_data[j].z + (aircraft_data[j].speedZ * t);
if (x1 == x2 && y1 == y2 && z1 == z2) {
printf("\n!!! COLLISION DETECTED !!!\n");
printf("Aircraft #%d and Aircraft #%d will be DEAD!\n", aircraft_data[i].id, aircraft_data[j].id);
printf("Collision bound to happen in %f seconds\n", t);
}
}
}
}
}
MsgReply(rcvid, EOK, 0, 0);
}
}
// Remove the name from the space
name_detach(attach, 0);
pthread_exit(NULL);
}
// Thread to send aircraft data to Display
void *threadDisplayAircraft(void *arg) {
int server_coid;
if ((server_coid = name_open(CS_TO_D, 0)) == -1) {
pthread_exit(NULL);
}
for (int i=0; i<10; i++) {
aircraft_data[i].hdr.type = 0x00;
aircraft_data[i].hdr.subtype = 0x01;
}
while (1) {
for(int i=0; i<10; i++) {
if(MsgSend(server_coid, &aircraft_data[i], sizeof(aircraft_data[i]), NULL, 0) == -1)
break;
sleep(0.1);
}
sleep(1);
}
name_close(server_coid);
pthread_exit(NULL);
}
// Thread to get speed and id from operator console
void *threadGetUpdateSpeed(void *arg) {
name_attach_t *attach;
oc_msg_t msg;
int rcvid;
if((attach = name_attach(NULL, OC_TO_CS, 0)) == NULL) {
pthread_exit(NULL);
}
while(1) {
// Wait for a message on the connection
rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
// Check for errors
if (rcvid == -1) {
perror("MsgReceive Failed\n");
break;
}
if (rcvid == 0) {
switch (msg.hdr.code) {
case _PULSE_CODE_DISCONNECT:
ConnectDetach(msg.hdr.scoid);
break;
case _PULSE_CODE_UNBLOCK:
break;
default:
break;
}
continue;
}
/* name_open() sends a connect message, must EOK this */
if (msg.hdr.type == _IO_CONNECT ) {
MsgReply( rcvid, EOK, NULL, 0 );
continue;
}
/* Some other QNX IO message was received; reject it */
if (msg.hdr.type > _IO_BASE && msg.hdr.type <= _IO_MAX ) {
MsgError( rcvid, ENOSYS );
continue;
}
if (msg.hdr.type == 0x00) {
if (msg.hdr.subtype == 0x01) {
printf("Msg received from OC...\n");
}
}
MsgReply(rcvid, EOK, 0, 0);
}
name_detach(attach, 0);
pthread_exit(NULL);
}
// Thread to post data to radar to update speed
void *threadPostUpdateSpeed(void *arg) {
oc_msg_t msg;
int server_coid;
if((server_coid = name_open(R_TO_CS, 0)) == -1)
pthread_exit(NULL);
msg.hdr.type = 0x00;
msg.hdr.subtype = 0x02;
if(MsgSend(server_coid, &msg, sizeof(msg), NULL, 0) == -1)
printf("Error sending message...\n");
name_close(server_coid);
pthread_exit(NULL);
}
// Thread to get timeframe from operator console
void *threadGetTimeframe(void *arg) {
name_attach_t *attach;
int rcvid;
if((attach = name_attach(NULL, OC_TO_CS, 0)) == NULL) {
pthread_exit(NULL);
}
while(1) {
// Wait for a message on the connection
rcvid = MsgReceive(attach->chid, &timeframe, sizeof(timeframe), NULL);
// Check for errors
if (rcvid == -1) {
perror("MsgReceive Failed\n");
break;
}
if (rcvid == 0) {
switch (timeframe.hdr.code) {
case _PULSE_CODE_DISCONNECT:
ConnectDetach(timeframe.hdr.scoid);
break;
case _PULSE_CODE_UNBLOCK:
break;
default:
break;
}
continue;
}
/* name_open() sends a connect message, must EOK this */
if (timeframe.hdr.type == _IO_CONNECT ) {
MsgReply( rcvid, EOK, NULL, 0 );
continue;
}
/* Some other QNX IO message was received; reject it */
if (timeframe.hdr.type > _IO_BASE && timeframe.hdr.type <= _IO_MAX ) {
MsgError( rcvid, ENOSYS );
continue;
}
if (timeframe.hdr.type == 0x00) {
if (timeframe.hdr.subtype == 0x02) {
printf("Msg received from OC...\n");
}
}
MsgReply(rcvid, EOK, 0, 0);
}
name_detach(attach, 0);
pthread_exit(NULL);
}
// Main Thread
int main(int argc, char *argv[]) {
// Creating threads for different clients
pthread_t thread1, thread2, thread3, thread4, thread5, thread6;
int rc;
/* Initialize attributes */
rc = pthread_attr_init(&attr);
if (rc)
printf("ERROR, RC from pthread_attr_init() is %d \n", rc);
/* Set detach state */
rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (rc)
printf("ERROR; RC from pthread_attr_setdetachstate() is %d \n", rc);
// Creating Thread1
rc = pthread_create(&thread1, NULL, threadRadar, NULL);
if (rc != 0) {
printf("Error creating thread\n");
return EXIT_FAILURE;
}
// Creating Thread2
rc = pthread_create(&thread2, NULL, threadDisplayAircraft, NULL);
if (rc != 0) {
printf("Error creating thread\n");
return EXIT_FAILURE;
}
// Creating Thread3
rc = pthread_create(&thread3, NULL, threadCheckCollision, NULL);
if (rc != 0) {
printf("Error creating thread\n");
return EXIT_FAILURE;
}
// Creating Thread4
rc = pthread_create(&thread4, NULL, threadGetUpdateSpeed, NULL);
if(rc != 0) {
printf("Error creating thread\n");
return EXIT_FAILURE;
}
// Creating Thread5
rc = pthread_create(&thread5, NULL, threadPostUpdateSpeed, NULL);
if(rc != 0) {
printf("Error creating thread\n");
return EXIT_FAILURE;
}
// Creating Thread6
rc = pthread_create(&thread6, NULL, threadGetTimeframe, NULL);
if(rc != 0) {
printf("Error creating thread\n");
return EXIT_FAILURE;
}
/* Synchronize all threads */
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
pthread_join(thread5, NULL);
pthread_join(thread6, NULL);
return EXIT_SUCCESS;
}