-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevc.c
162 lines (127 loc) · 5.06 KB
/
evc.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
#include "network.h"
#include "messagerie.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#define ACTION_EDIT_ID 1
#define ACTION_SHOW 2
#define ACTION_LEAVE 9
void main(int argc, char * const * argv) {
if (argc != 4) {
printf("Usage : %s server_ip server_port train_id\n", argv[0]);
exit(EXIT_FAILURE);
}
int serverPort = atoi(argv[2]);
char * serverIp = argv[1];
char trainId[MAX_ID_LENGTH + 1];
strncpy(trainId, argv[3], MAX_ID_LENGTH);
trainId[MAX_ID_LENGTH + 1] = '\0';
int mainSocket;
struct sockaddr_in serverAddr, clientAddr;
// Création du socket
mainSocket = socket(AF_INET, SOCK_STREAM, 0);
CHECK_ERR("Unable to open socket", mainSocket, -1);
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = 0;
clientAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // INADDR_ANY;
int clientAddrLength = sizeof(clientAddr);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(serverPort);
serverAddr.sin_addr.s_addr = inet_addr(serverIp);
printf("Server settings : %s:%d\n", serverIp, ntohs(serverAddr.sin_port));
// Connexion au socket
CHECK_ERR("Unable to connect to socket", bind(mainSocket, (const struct sockaddr * ) &clientAddr, sizeof(clientAddr)), -1);
CHECK_ERR("Unable to get socket name", getsockname(mainSocket, (struct sockaddr *) &clientAddr, &clientAddrLength), -1);
printf("Bind to socket %s:%d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
CHECK_ERR("Unable to connect to server", connect(mainSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)), -1);
// char recptBuffer[MAXOCTETS+1]; // buffer de reception
// int lengthRecept ; // Nb octets recus
T_trainData train = {"", 0, 0};
strcpy(train.id, trainId);
printf("[EVC Connected]\n");
printf("\n");
printf("%d Changer l'identifiant du train\n", ACTION_EDIT_ID);
printf("%d Afficher les données du train\n", ACTION_SHOW);
printf("%d Quitter\n", ACTION_LEAVE);
printf("\n");
printf("%d Rejoindre le réseau\n", REQ_ADDTRAIN);
printf("%d Quitter le réseau\n", REQ_DELTRAIN);
printf("%d Envoyer position\n", REQ_MVTRAIN);
printf("%d Demande d'autorisation de mouvement\n", REQ_ASKMOVE);
printf("\n");
int action = 0;
char actionBuffer[4];
T_IndusMsg * resMsg = (T_IndusMsg *) malloc(sizeof(T_IndusMsg));
while (1) {
printf("> ");
fgets(actionBuffer, MAXOCTETS, stdin);
action = atoi(actionBuffer);
T_IndusMsg reqMsg = {0, 0, train};
T_canton newLocation;
switch (action) {
case ACTION_EDIT_ID:
printf("? WIP\n");
break;
case ACTION_SHOW:
printf("[%s] (%d|%d)\n", train.id, train.canton, train.eoa);
break;
case ACTION_LEAVE:
close(mainSocket);
exit(EXIT_SUCCESS);
printf("! Left\n");
break;
case REQ_ADDTRAIN:
reqMsg.type = REQ_ADDTRAIN;
write(mainSocket, &reqMsg, sizeof(T_IndusMsg));
printf("...\n");
read(mainSocket, resMsg, sizeof(T_IndusMsg));
handle_client(&train, *resMsg);
break;
case REQ_DELTRAIN:
reqMsg.type = REQ_DELTRAIN;
write(mainSocket, &reqMsg, sizeof(T_IndusMsg));
printf("...\n");
read(mainSocket, resMsg, sizeof(T_IndusMsg));
handle_client(&train, *resMsg);
break;
case REQ_MVTRAIN:
printf("Nouvelle position (1-%d): ", train.eoa);
scanf("%d",&newLocation);
getchar();
if (newLocation < 1) {
printf("! Nouvelle position invalide (<1)\n");
break;
}
reqMsg.type = REQ_MVTRAIN;
reqMsg.data.canton = newLocation;
write(mainSocket, &reqMsg, sizeof(T_IndusMsg));
printf("...\n");
read(mainSocket, resMsg, sizeof(T_IndusMsg));
handle_client(&train, *resMsg);
break;
case REQ_ASKMOVE:
printf("Position demandée (1-%d): ", train.eoa);
scanf("%d",&newLocation);
getchar();
if (newLocation < 1) {
printf("! Position invalide (<1)\n");
break;
}
reqMsg.type = REQ_ASKMOVE;
reqMsg.data.canton = newLocation;
write(mainSocket, &reqMsg, sizeof(T_IndusMsg));
printf("...\n");
read(mainSocket, resMsg, sizeof(T_IndusMsg));
handle_client(&train, *resMsg);
break;
default:
printf("! Action inconnue\n");
break;
}
}
}