forked from Dracotz/SDE-hUberT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresto.c
167 lines (112 loc) · 3.17 KB
/
resto.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
#include <stdio.h>
#include <stdlib.h>
#include "message.h"
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include "resto.h"
#include <signal.h>
#include "struct.h"
#include "shared.h"
#include <time.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
int getCmd(char *text){
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
if(poll(&mypoll,1,TIMEOUT_STDIN)){
fgets(text,SIZE_TEXTBUFF,stdin);
return 1;
}
// printf("Stdin empty\n");
// char *p = strchr(text, '\n');
// if(p) *p = 0;
return 0;
}
int main(){
signal(SIGUSR1, signal_handle);
signal(SIGINT, signal_end);
int i, tailleMenu;
MENU menu;
//INITIALISATION MUTEX
int mutex = semaphore_create(KEY_MUTEX);
semaphore_init(mutex, 1);
filePrivee = connectToFilePrivate(&connected,KEY_PUBLIC_RESTO);
filePublic = getFilePublic(KEY_PUBLIC_RESTO);
if(filePublic<0) connected = 0;
else connected =1;
MsgConnexion msgCo;
msgCo.type= 2;
msgCo.id=0;
signal(2,deco);
MsgText messageTxt;
messageTxt.type = 1;
char text[SIZE_TEXTBUFF];
printf("%s , combien de plats voulez-vous dans votre Menu ?\n", NomRestaurant);
tailleMenu = -1;
while(tailleMenu < 1) {
scanf("%d", &tailleMenu);
viderTampon();
}
/*****************************************************************/
menu = (MENU)malloc(tailleMenu*sizeof(repas));
initialisationMenu(menu, tailleMenu);
//INITIALISATION MEMOIRE PARTAGEE
nettoyageSegment();
id=shmget(KEY_MEM, tailleMenu*sizeof(repas), 0660 | IPC_CREAT | IPC_EXCL);//creation de la memoire
menu = (MENU) shmat(id, 0, 0); //attachement
pid=fork();
if (pid==0) { //FILS
while(travail) {
pause();
printf("La cuisine se met au boulot pour préparer ce qu'il faut.\n");
rechargeStock(menu, mutex, tailleMenu);
}
//FIN FILS
} else {
//PERE
down(mutex);
for(i=0; i<tailleMenu; i++) {
printf("Nom du plat : ");
scanf("%32s",menu[i].nom);
viderTampon();
printf("Quantite : ");
scanf("%d", &menu[i].quantite);
viderTampon();
}
up(mutex);
/*******************Preparation Connexion avec hUberT*************************/
while(connected == 1){
if(fileEmpty(filePublic)!=1){
printf("ID attendu %d\n",filePrivee);
msgrcv(filePublic,&msgCo,sizeof(msgCo.id),filePrivee,IPC_NOWAIT);
if(msgCo.id==filePrivee){
connected =0;
printf("Client deconnected by hubert\n");
}
}
if(getCmd(text)==1){ //return 1 si text sinon 0
if(strcmp(text,"EXIT\n")==0){
printf("EXIT DEMANDE\n");
disconnect(filePrivee,filePublic);
}
else{
strcpy(messageTxt.cmd,text);
if( msgsnd(filePrivee,&messageTxt,sizeof(messageTxt.cmd),1)!=0){
printf("Error sending message file : %d",filePrivee);
perror("msgsnd");
}
}
}
}
}
//FIN PERE
shmdt(menu);;
shmctl(id, IPC_RMID, 0); // destruction du segment
semaphore_remove(mutex);
//if(pid!=0) {free(menu);}
printf("\n\n\n\n***THE END***\n");
exit(0);
}