-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminal.c
109 lines (89 loc) · 2.81 KB
/
Terminal.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "lectureEcriture.h"
#include "message.h"
#include "alea.h"
#include "annuaire.h"
/**
* Programme de gestion du terminal
*/
int main(int argc, char **argv){
int fd_DemandeServeur;
int fd_ReponseServeur;
int decoupeOk;
char emetteur[255], type[255], valeur[255];
char *msg;
int err;
char *rep ;
int clientRandom;
int i,j;
AnnuaireClients *an;
//----------------------------------------------------------------------
fd_DemandeServeur = atoi(argv[1]);
if (fd_DemandeServeur < 0) {
perror("Terminal.c - FD Demande serveur invalide");
exit(0);
}
fd_ReponseServeur = atoi(argv[2]);
if (fd_ReponseServeur < 0) {
perror("Terminal.c - FD Reponse serveur invalide");
exit(0);
}
//----------------------------------------------------------------------
printf("tube d'entree à aquisition %d\n\n",fd_DemandeServeur);
printf("tube de sortie d'aquistion : %d\n\n",fd_ReponseServeur);
aleainit();
while(1){
//printf("%d\n", i);
// ON LIT L'ENTREE (ici, on crée une valeur aléatoire pour chaque cb)
sprintf(valeur,"%d",alea(1,50000));
an = annuaire("annuaire.an");
if(an == NULL){
fprintf(stderr,"%s ne peut lire l'annuaire depuis le fichier%s\n", argv[0], "Annu");
exit(0);
}
clientRandom = alea(1,an->nbClients);
for(j = 0; j < clientRandom ; j++){
strcpy(emetteur , an->donnees[j].CB);
}
// ON TRANSMET AU SERVEUR
msg = message(emetteur, "Demande", valeur);
printf("%s", msg);
if (msg == NULL) {
perror("Terminal.c - Ordre de paiement invalide");
exit(0);
}
err = ecritLigne(fd_DemandeServeur, msg);
if (err == 0) {
perror("fd_DemandeServeur - ecritLigne");
exit(0);
}
printf("Terminal : message envoyé\n");
// ON ATTEND LA REPONSE DU SERVEUR
rep = litLigne(fd_ReponseServeur) ;
// ON RETRANSMET LA REPONSE A L'ORDRE
decoupeOk = decoupe(rep, emetteur, type, valeur);
if (!decoupeOk) {
printf("Erreur de découpage!!\n");
perror("TestMessage (decoupe)");
exit(0);
}
printf("%s\n",rep);
if (atoi(valeur) == 1){
// 0 = NON AUTORISEE -- 1 = AUTORISEE
printf("%s", "AUTORISATION ACCORDEE\n\n");
}
else
printf("%s", "AUTORISATION NON ACCORDEE\n\n");
}
printf("ARRET DU TERMINAL\n");
err = ecritLigne(fd_DemandeServeur, "STOP\n");
if (err == 0) {
perror("fd_DemandeServeur - ecritLigne");
exit(0);
}
return 0;
}