-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
102 lines (90 loc) · 1.8 KB
/
server.c
File metadata and controls
102 lines (90 loc) · 1.8 KB
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
#include "ServerClientHeader.h"
int main(int argc, char** argv)
{
srand(time(NULL));
int database, middleware, web;
// create server-fifos
if(mkfifo(DATABASE_SERVER, O_CREAT | 0666) == -1)
{
perror("mkfifo database");
exit(EXIT_FAILURE);
}
if(mkfifo(MIDDLEWARE_SERVER, O_CREAT | 0666) == -1)
{
perror("mkfifo middleware");
exit(EXIT_FAILURE);
}
if(mkfifo(WEB_SERVER, O_CREAT | 0666) == -1)
{
perror("mkfifo web");
exit(EXIT_FAILURE);
}
database = open(DATABASE_SERVER, O_WRONLY);
middleware = open(MIDDLEWARE_SERVER, O_WRONLY);
web = open(WEB_SERVER, O_WRONLY);
while(true)
{
// sending messages
int seconds = rand() % 5 + 2;
int server = rand() % 3 + 1;
switch(server)
{
case 1:
if(write(database, DATABASE_MESSAGE, strlen(DATABASE_MESSAGE) + 1) == -1)
{
perror("write database");
exit(EXIT_FAILURE);
}
break;
case 2:
if(write(middleware, MIDDLEWARE_MESSAGE, strlen(MIDDLEWARE_MESSAGE) + 1) == -1)
{
perror("write middleware");
exit(EXIT_FAILURE);
}
break;
case 3:
if(write(web, WEB_MESSAGE, strlen(WEB_MESSAGE) + 1) == -1)
{
perror("write web");
exit(EXIT_FAILURE);
}
break;
default:
perror("switch unexpected case");
exit(EXIT_FAILURE);
}
sleep(seconds);
}
if(close(database))
{
perror("close database");
exit(EXIT_FAILURE);
}
if(close(middleware))
{
perror("close middleware");
exit(EXIT_FAILURE);
}
if(close(web))
{
perror("close web");
exit(EXIT_FAILURE);
}
if(unlink(DATABASE_SERVER) == -1)
{
perror("unlink database");
exit(EXIT_FAILURE);
}
if(unlink(MIDDLEWARE_SERVER) == -1)
{
perror("unlink middleware");
exit(EXIT_FAILURE);
}
if(unlink(WEB_SERVER) == -1)
{
perror("unlink web");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}