From 82843f35b9b9bc7736ac7815c5bf2fdbc0accea8 Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Tue, 23 May 2006 06:31:56 +0000 Subject: [PATCH] Parte 2 (RPC) terminada por 3ra vez!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (!!!) --- practicas/practica3/parte1/gentest.py | 24 ++++ practicas/practica3/parte1/test.sh | 6 + practicas/practica3/parte2/Makefile | 47 +++++++ practicas/practica3/parte2/gentest.py | 24 ++++ practicas/practica3/parte2/set.x | 12 ++ practicas/practica3/parte2/set_client.cpp | 112 +++++++++++++++++ practicas/practica3/parte2/set_server.cpp | 147 ++++++++++++++++++++++ practicas/practica3/parte2/test.sh | 6 + 8 files changed, 378 insertions(+) create mode 100755 practicas/practica3/parte1/gentest.py create mode 100755 practicas/practica3/parte1/test.sh create mode 100644 practicas/practica3/parte2/Makefile create mode 100755 practicas/practica3/parte2/gentest.py create mode 100644 practicas/practica3/parte2/set.x create mode 100644 practicas/practica3/parte2/set_client.cpp create mode 100644 practicas/practica3/parte2/set_server.cpp create mode 100755 practicas/practica3/parte2/test.sh diff --git a/practicas/practica3/parte1/gentest.py b/practicas/practica3/parte1/gentest.py new file mode 100755 index 0000000..ea3f89f --- /dev/null +++ b/practicas/practica3/parte1/gentest.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# vim: set encoding=utf-8 : + +iteraciones = 10 +palabras = ('hola', 'chau', 'bueno', 'malo', 'cosa', 'prueba', 'socket') +operaciones = ('put', 'find', 'del') + +from random import randint, shuffle +from sys import argv + +try: + iteraciones = int(argv[1]) +except: + pass + +for i in xrange(iteraciones): + p = list(palabras) + shuffle(p) + p = p[0:randint(1, len(p)-1)] + print operaciones[randint(0, len(operaciones)-1)], + for pal in p: + print pal, + print + diff --git a/practicas/practica3/parte1/test.sh b/practicas/practica3/parte1/test.sh new file mode 100755 index 0000000..37d1659 --- /dev/null +++ b/practicas/practica3/parte1/test.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +./gentest.py | ./client & +./gentest.py | ./client & +./gentest.py | ./client & +./gentest.py | ./client & diff --git a/practicas/practica3/parte2/Makefile b/practicas/practica3/parte2/Makefile new file mode 100644 index 0000000..1e25bae --- /dev/null +++ b/practicas/practica3/parte2/Makefile @@ -0,0 +1,47 @@ + +# This is a template Makefile generated by rpcgen + +# Parameters + +CLIENT = set_client +SERVER = set_server + +SOURCES_CLNT.c = +SOURCES_CLNT.h = +SOURCES_SVC.c = +SOURCES_SVC.h = +SOURCES.x = set.x + +TARGETS_SVC.c = set_svc.c set_server.cpp set_xdr.c +TARGETS_CLNT.c = set_clnt.c set_client.cpp set_xdr.c +TARGETS = set.h set_xdr.c set_clnt.c set_svc.c set_server.c set_client.c + +OBJECTS_CLNT = $(SOURCES_CLNT.c:%.c=%.o) $(TARGETS_CLNT.c:%.c=%.o) +OBJECTS_SVC = $(SOURCES_SVC.c:%.c=%.o) $(TARGETS_SVC.c:%.c=%.o) +# Compiler flags + +CFLAGS += -g -Wall -I../common +LDLIBS += -lnsl ../common/common.a +CC=g++ +RPCGENFLAGS = -a -N + +# Targets + +all : $(CLIENT) $(SERVER) + +$(TARGETS) : $(SOURCES.x) + rpcgen $(RPCGENFLAGS) $(SOURCES.x) + +$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) $(TARGETS_CLNT.c) + +$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) $(TARGETS_SVC.c) + +$(CLIENT) : $(OBJECTS_CLNT) + $(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) $(LDLIBS) + +$(SERVER) : $(OBJECTS_SVC) + $(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS) + +#clean: +# $(RM) core $(TARGETS) $(OBJECTS_CLNT) $(OBJECTS_SVC) $(CLIENT) $(SERVER) + diff --git a/practicas/practica3/parte2/gentest.py b/practicas/practica3/parte2/gentest.py new file mode 100755 index 0000000..ea3f89f --- /dev/null +++ b/practicas/practica3/parte2/gentest.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# vim: set encoding=utf-8 : + +iteraciones = 10 +palabras = ('hola', 'chau', 'bueno', 'malo', 'cosa', 'prueba', 'socket') +operaciones = ('put', 'find', 'del') + +from random import randint, shuffle +from sys import argv + +try: + iteraciones = int(argv[1]) +except: + pass + +for i in xrange(iteraciones): + p = list(palabras) + shuffle(p) + p = p[0:randint(1, len(p)-1)] + print operaciones[randint(0, len(operaciones)-1)], + for pal in p: + print pal, + print + diff --git a/practicas/practica3/parte2/set.x b/practicas/practica3/parte2/set.x new file mode 100644 index 0000000..c1ece18 --- /dev/null +++ b/practicas/practica3/parte2/set.x @@ -0,0 +1,12 @@ +/* + * Programa para manipular un set con RPC + */ +program SETPRG { + version SETVERS { + /* importa solo el retorno del ultimo mensaje */ + int PUT(string payload, int id, int end) = 1; + int FIND(string payload, int id, int end) = 2; + int DEL(string payload, int id, int end) = 3; + int QUIT(int id) = 4; + } = 1; +} = 0x20000099; diff --git a/practicas/practica3/parte2/set_client.cpp b/practicas/practica3/parte2/set_client.cpp new file mode 100644 index 0000000..d721451 --- /dev/null +++ b/practicas/practica3/parte2/set_client.cpp @@ -0,0 +1,112 @@ +/* + * This is sample code generated by rpcgen. + * These are only templates and you can use them + * as a guideline for developing your own functions. + */ + +#include "set.h" +#include "common.h" +#include +#include +#include + +enum { OK, NOT_FOUND, EXISTS }; + +int +main(int argc, char *argv[]) +{ + int client_id; + char *server_name; + char *localhost = "localhost"; + pid_t pid; + CLIENT* cl; + const char *pname = argv[0]; + + // mostrar el pid del cliente + pid = getpid(); + print_msg(stdout, "%s Cliente con pid = %d\n", pname, pid); + + if (argc > 1) + server_name = argv[1]; + else + server_name = localhost; + + // id del cliente, por default calculado en base al pid + if (argc > 2) + client_id = atoi(argv[2]); + else + client_id = pid % 32; + + + /* Crear el "handle" del cliente para llamar MESSAGEPROG en el + * server. Usamos "udp" para la comunicacion. */ + cl = clnt_create(server_name, SETPRG, SETVERS, "udp"); + if (cl == NULL) + { + /* No se pudo hacer contacto error y adios. */ + clnt_pcreateerror(server_name); + exit(1); + } + + std::string line; + int* (*rem_proc)(char*, int, int, CLIENT*); + while (std::getline(std::cin, line)) + { + std::istringstream iss(line); + std::string token; + if (!(iss >> token)) return 0; + if (token == "put") + { + rem_proc = put_1; + print_msg(stdout, "%s (%d): llamando a PUT:", pname, pid); + } + else if (token == "find") + { + rem_proc = find_1; + print_msg(stdout, "%s (%d): llamando a FIND:", pname, pid); + } + else if (token == "del") + { + rem_proc = del_1; + print_msg(stdout, "%s (%d): llamando a DEL:", pname, pid); + } + else + { + print_msg(stderr, "%s (%d): Invalid token %s!\n", pname, pid, token.c_str()); + exit(1); + } + while (iss >> token) + { + /* llamar al procedure en el server */ + int* r = rem_proc((char*)token.c_str(), client_id, 0, cl); + if (r == NULL) + { + /* error durante la comunicacion. */ + clnt_perror(cl, server_name); + exit(1); + } + print_msg(stdout, " %s", token.c_str()); + } + print_msg(stdout, "\n"); + int* r = rem_proc("", client_id, 1, cl); + if (r == NULL) + { + /* error durante la comunicacion. */ + clnt_perror(cl, server_name); + exit(1); + } + print_msg(stdout, "%s (%d): el server responde %d\n", pname, pid, *r); + } + + int* r = quit_1(client_id, cl); + if (r == NULL) + { + /* error durante la comunicacion. */ + clnt_perror(cl, server_name); + exit(1); + } + + exit (0); +} + +// vim: set et sw=4 sts=4 : diff --git a/practicas/practica3/parte2/set_server.cpp b/practicas/practica3/parte2/set_server.cpp new file mode 100644 index 0000000..2a1d29f --- /dev/null +++ b/practicas/practica3/parte2/set_server.cpp @@ -0,0 +1,147 @@ +/* + * This is sample code generated by rpcgen. + * These are only templates and you can use them + * as a guideline for developing your own functions. + */ + +#include "set.h" +#include "common.h" +#include +#include +#include +#include +#include + +std::map< int, std::set< std::string > > sets; + +enum { OK, NOT_FOUND, EXISTS }; + +static +void +get_file_name(int id, char* fname) +{ + sprintf(fname, "cliente_%d", id); +} + +static +FILE* +open_file(int id, char* mode) +{ + char fname[32]; + get_file_name(id, fname); + FILE* fp = fopen(fname, mode); + if (!fp) + { + perror("Error al abrir archivo"); + exit(1); + } + return fp; +} + +static +void +write_token(int id, const char* token) +{ + FILE* fp = open_file(id, "a"); + if (fprintf(fp, "%s ", token) != (signed)strlen(token)+1) + { + perror("Error al escribir archivo"); + exit(1); + } + fclose(fp); +} + +int * +put_1_svc(char *payload, int id, int end, struct svc_req *rqstp) +{ + static int result; + + result = OK; + write_token(id, payload); + print_msg(stdout, "%d: recibido PUT '%s', end=%d\n", id, payload, end); + if (end) + { + FILE* fp = open_file(id, "r"); + std::string buffer; + char token[256]; + while (fscanf(fp, "%s", token) != EOF) + buffer += token; + fclose(fp); + print_msg(stdout, "%d: procesando PUT '%s'\n", id, buffer.c_str()); + if (sets[id].find(buffer) == sets[id].end()) + sets[id].insert(buffer); + else + result = EXISTS; + get_file_name(id, token); + remove(token); // Elimino archivo temporal + } + + return &result; +} + +int * +find_1_svc(char *payload, int id, int end, struct svc_req *rqstp) +{ + static int result; + + result = OK; + write_token(id, payload); + print_msg(stdout, "%d: recibido FIND '%s', end=%d\n", id, payload, end); + if (end) + { + FILE* fp = open_file(id, "r"); + std::string buffer; + char token[256]; + while (fscanf(fp, "%s", token) != EOF) + buffer += token; + fclose(fp); + print_msg(stdout, "%d: procesando FIND '%s'\n", id, buffer.c_str()); + if (sets[id].find(buffer) == sets[id].end()) + result = NOT_FOUND; + get_file_name(id, token); + remove(token); // Elimino archivo temporal + } + + return &result; +} + +int * +del_1_svc(char *payload, int id, int end, struct svc_req *rqstp) +{ + static int result; + + result = OK; + write_token(id, payload); + print_msg(stdout, "%d: recibido DEL '%s', end=%d\n", id, payload, end); + if (end) + { + FILE* fp = open_file(id, "r"); + std::string buffer; + char token[256]; + while (fscanf(fp, "%s", token) != EOF) + buffer += token; + fclose(fp); + print_msg(stdout, "%d: procesando DEL '%s'\n", id, buffer.c_str()); + if (!sets[id].erase(buffer)) + result = NOT_FOUND; + get_file_name(id, token); + remove(token); // Elimino archivo temporal + } + + return &result; +} + +int * +quit_1_svc(int id, struct svc_req *rqstp) +{ + static int result; + + result = OK; + print_msg(stdout, "%d: recibido QUIT, limpiando set\n", id); + if (!sets.erase(id)) + result = NOT_FOUND; + + return &result; +} + +// vim: set et sw=4 sts=4 : diff --git a/practicas/practica3/parte2/test.sh b/practicas/practica3/parte2/test.sh new file mode 100755 index 0000000..d671aea --- /dev/null +++ b/practicas/practica3/parte2/test.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +./gentest.py | ./set_client & +./gentest.py | ./set_client & +./gentest.py | ./set_client & +./gentest.py | ./set_client & -- 2.43.0