+/*
+ * 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 <set>
+#include <map>
+#include <string>
+#include <cstdlib>
+#include <cstdio>
+
+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 :