/
centro_svc.c
331 lines (270 loc) · 9.16 KB
/
centro_svc.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
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
/*
* Universidad Simón Bolivar
* CI4835 - Redes de Computadoras
* Proyecto 2
*
* Archivo: centro_svc.c
* Autor: Alexandra Paredes 05-38680
* Javier Rodriguez 05-38838
* Descripción: Programa que implemente las funciones del centro de distriucion (servidor)
*
*/
/*
* Please do not edit this file.
* It was generated using rpcgen.
*/
#include "centro.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "gestion_centro.h"
#ifndef SIG_PF
#define SIG_PF void(*)(int)
#endif
char entryLog[256];
////////////////////////// INICIO FUNCIONES MANEJO DE LA LISTA ////////////////////////////////////
/* Función: crear
Entrada:
lista: Apuntador a la dirección en memoria de la lista
Salida:
N/A
Función que crea la lista que almacena tipo bomba
*/
void crear(bomba* lista){
strcpy(lista->nombre,"NIL");
strcpy(lista->desafio,"NIL");
lista->tiempoVencimiento = 0;
lista->siguiente = NULL;
}
///////////////////////////// FIN FUNCIONES MANEJO DE LA LISTA ////////////////////////////////////
/* Función: centro_prog_1
Entrada:
rqstp: Apuntador a la structura svc_req
transp: Apuntador al registro SVCXPRT
Salida:
N/A
Función que gestiona las funciones del RPC del lado del servidor
*/
static void centro_prog_1(struct svc_req *rqstp, register SVCXPRT *transp){
union {
datos solicitar_desafio_1_arg;
datos respodnder_desafio_1_arg;
char *solicitar_tiempo_1_arg;
char *solicitar_gasolina_1_arg;
} argument;
char *result;
xdrproc_t _xdr_argument, _xdr_result;
char *(*local)(char *, struct svc_req *);
switch (rqstp->rq_proc) {
case NULLPROC:
(void) svc_sendreply (transp, (xdrproc_t) xdr_void, (char *)NULL);
return;
case SOLICITAR_DESAFIO:
_xdr_argument = (xdrproc_t) xdr_datos;
_xdr_result = (xdrproc_t) xdr_wrapstring;
local = (char *(*)(char *, struct svc_req *)) solicitar_desafio_1_svc;
break;
case RESPODNDER_DESAFIO:
_xdr_argument = (xdrproc_t) xdr_datos;
_xdr_result = (xdrproc_t) xdr_wrapstring;
local = (char *(*)(char *, struct svc_req *)) respodnder_desafio_1_svc;
break;
case SOLICITAR_TIEMPO:
_xdr_argument = (xdrproc_t) xdr_wrapstring;
_xdr_result = (xdrproc_t) xdr_int;
local = (char *(*)(char *, struct svc_req *)) solicitar_tiempo_1_svc;
break;
case SOLICITAR_GASOLINA:
_xdr_argument = (xdrproc_t) xdr_wrapstring;
_xdr_result = (xdrproc_t) xdr_int;
local = (char *(*)(char *, struct svc_req *)) solicitar_gasolina_1_svc;
break;
default:
svcerr_noproc (transp);
return;
}
memset ((char *)&argument, 0, sizeof (argument));
if (!svc_getargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
svcerr_decode (transp);
return;
}
result = (*local)((char *)&argument, rqstp);
if (result != NULL && !svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
svcerr_systemerr (transp);
}
if (!svc_freeargs (transp, (xdrproc_t) _xdr_argument, (caddr_t) &argument)) {
fprintf (stderr, "%s", "unable to free arguments");
exit (1);
}
return;
}
/* Función: verificarArgs
Entrada:
cantArg: Cantidad de parametros introducidos.
args: Arreglo con los parametros introducidos.
Salida:
N/A
Función que permite verificar los parametros de entrada al llamar
el programa. En caso de diferencias con el formato de
entrada se muestra el mensaje de uso del progama
*/
void verificarArgs(int cantArg,char*args[]){
int opt;
struct option longopts[] = {
{ "cp", required_argument, NULL, 'm'},
{ 0, 0, 0, 0}
};
if(cantArg == 11){
/* Ciclo que toma los arguemtos de cada uno de los flags sin importar el orden en
el cual son colocado los flags al invocar el programa */
while ((opt = getopt_long_only(cantArg, args, "n:m:i:t:s:",longopts,NULL)) != -1){
switch (opt) {
case 'n':
// Nombre del Centro
if (nombreServidor != NULL)
free(nombreServidor);
if (asprintf(&nombreServidor, "%s", optarg) == -1) {
fatalerror("Error copiando string para manejo de argumentos",
EXIT_BAD_ARG);
}
//printf("Nombre: %s\n",nombreCentro);
break;
case 'm':
// Capacidad Máxima
capacidadMaximaServidor = atoi(optarg);
//printf("Capacidad: %d\n",capacidadMaxima);
break;
case 'i':
// Inventario
inventarioServidor = atoi(optarg);
//printf("Inventario: %d\n",inventario);
break;
case 't':
// Tiempo de respuesta
tiempoRespuestaServidor = atoi(optarg);
//printf("Tiempo: %d\n",tiempo);
break;
case 's':
// Suministro
suministroServidor = atoi(optarg);
//printf("Suministro: %d\n",suministro);
break;
default:
fatalerror(usoCentro,EXIT_BAD_ARG);
}
}
if(capacidadMaximaServidor < 38000 || capacidadMaximaServidor > 3800000){
fatalerror(usoCentro,EXIT_BAD_ARG);
}
if(inventarioServidor < 0 || inventarioServidor > capacidadMaximaServidor){
fatalerror(usoCentro,EXIT_BAD_ARG);
}
if(suministroServidor < 0 || suministroServidor > 10000){
fatalerror(usoCentro,EXIT_BAD_ARG);
}
if(tiempoRespuestaServidor < 0 || tiempoRespuestaServidor > 180){
fatalerror(usoCentro,EXIT_BAD_ARG);
}
}else{
fatalerror(usoCentro,EXIT_BAD_ARG);
}
}
/* Función: finHilo
Entrada:
signal: Entero que representa la señal de retorno del hilo
Salida:
N/A
Función que verfica si el tanque esta o no full
*/
static void finHilo(int signal){
if (fclose(logServer) == 0){
printf("Termino actividad del servidor.\n");
}
exit(0);
}
/* Función: ejecucionCentro
Entrada:
N/A
Salida:
N/A
Función que se encarga de hacer la simulación del tiempo.
Verifica que el tanque este lleno o vacío y hace el aumento de gasolina, cuando sea necesario.
*/
void *ejecucionCentro(){
while(tiempoEjecucionServidor<=480){
if(inventarioServidor == capacidadMaximaServidor){
sprintf(entryLog, "Tanque full: %d\n", tiempoEjecucionServidor);
fprintf(logServer,"%s",entryLog);
fflush(logServer);
}
if(inventarioServidor <= 0){
sprintf(entryLog, "Tanque vacio: %d\n", tiempoEjecucionServidor);
fprintf(logServer,"%s",entryLog);
fflush(logServer);
}
tiempoEjecucionServidor++;
if((inventarioServidor = inventarioServidor + suministroServidor) > capacidadMaximaServidor){
inventarioServidor = capacidadMaximaServidor;
}
//printf("Inventario %d\n",inventarioServidor);
usleep(100*1000);
}
raise(SIGUSR1);
}
int main (int argc, char **argv)
{
pthread_t threadPrincipal = (pthread_t *) malloc(sizeof (pthread_t));
struct sigaction accion;
memset(&accion,'\0',sizeof(accion));
accion.sa_handler = &finHilo;
tiempoEjecucionServidor = 0;
programname = argv[0]; // Guardar nombre del programa para errores
if(sigaction(SIGUSR1, &accion, NULL) < 0) {
fatalerror("Manejando la señal",EXIT_ERROR);
return 1;
}
verificarArgs(argc,argv);
listaBombas = (bomba *) malloc(sizeof(bomba));
crear(listaBombas);
// Crear y abrir archivo en modo de escritura
char log_name[128];
sprintf(log_name, "log_%s.txt", nombreServidor);
logServer = fopen(log_name, "w");
fprintf(logServer, "\0");
fclose(logServer);
logServer = fopen(log_name, "a+");
// Escribir estado inicial del centro en el log
sprintf(entryLog, "Estado incial: %d\n", inventarioServidor);
fprintf(logServer,"%s",entryLog);
fflush(logServer);
int tError;
if((tError=pthread_create(&threadPrincipal,NULL,ejecucionCentro,NULL)) < 0)
fatalerror("Error al crear el thread",tError);
register SVCXPRT *transp;
pmap_unset (CENTRO_PROG, CENTRO_VERS);
transp = svcudp_create(RPC_ANYSOCK);
if (transp == NULL) {
fprintf (stderr, "%s", "cannot create udp service.");
exit(1);
}
if (!svc_register(transp, CENTRO_PROG, CENTRO_VERS, centro_prog_1, IPPROTO_UDP)) {
fprintf (stderr, "%s", "unable to register (CENTRO_PROG, CENTRO_VERS, udp).");
exit(1);
}
transp = svctcp_create(RPC_ANYSOCK, 0, 0);
if (transp == NULL) {
fprintf (stderr, "%s", "cannot create tcp service.");
exit(1);
}
if (!svc_register(transp, CENTRO_PROG, CENTRO_VERS, centro_prog_1, IPPROTO_TCP)) {
fprintf (stderr, "%s", "unable to register (CENTRO_PROG, CENTRO_VERS, tcp).");
exit(1);
}
svc_run ();
fprintf (stderr, "%s", "svc_run returned");
exit(EXIT_SUCCESS);
}