From 8de367100b8754922bd2d063b0308f2c2a89e7ec Mon Sep 17 00:00:00 2001 From: lapupe Date: Fri, 14 Oct 2016 17:47:08 +0200 Subject: [PATCH] =?UTF-8?q?tout=20est=20=C3=A0=20refaire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/communication.c | 17 +++++++--- lib/communication.h | 2 ++ lib/communication2.c | 36 ++++++++++++++++++++ lib/communication2.h | 41 +++++++++++++++++++++++ lib/process.h | 1 + pingpong/Makefile | 3 ++ pingpong/pingpong.c | 6 ++-- remote/Makefile | 3 ++ remote/script/testTcpd.sh | 2 ++ remote/tcpdclient.c | 70 --------------------------------------- remote/tcpdserver.c | 32 ++++++++++++------ remote/tcpdserver.h | 2 +- 12 files changed, 124 insertions(+), 91 deletions(-) create mode 100644 lib/communication2.c create mode 100644 lib/communication2.h delete mode 100644 remote/tcpdclient.c diff --git a/lib/communication.c b/lib/communication.c index 40b882c..cb8e5fe 100644 --- a/lib/communication.c +++ b/lib/communication.c @@ -160,11 +160,18 @@ int srv_get_new_process (const struct service *srv, struct process *p) char *buf = NULL; size_t msize = 0; - int ret = file_read (srv->spath, &buf, &msize); - if (ret <= 0) { - fprintf (stderr, "err: listening on %s\n", srv->spath); - exit (1); + int ret = 0; + while (ret == 0) { + ret = file_read (srv->spath, &buf, &msize); + + if (ret < 0) { + fprintf (stderr, "err: listening on %s\n", srv->spath); + exit (1); + } + } + + char *token = NULL, *saveptr = NULL; char *str = NULL; @@ -194,7 +201,7 @@ int srv_get_new_process (const struct service *srv, struct process *p) free (buf); srv_process_gen (p, pid, index, version); - return 1; + return 0; } int srv_read (struct process *p, char ** buf, size_t * msize) diff --git a/lib/communication.h b/lib/communication.h index 85e2f8d..08db661 100644 --- a/lib/communication.h +++ b/lib/communication.h @@ -68,4 +68,6 @@ int app_write (struct process *, char * buf, size_t); int file_read (const char *path, char **buf, size_t *msize); int file_write (const char *path, const char *buf, size_t msize); +//open, close, read, write + #endif diff --git a/lib/communication2.c b/lib/communication2.c new file mode 100644 index 0000000..fdb532d --- /dev/null +++ b/lib/communication2.c @@ -0,0 +1,36 @@ +#include "communication2.h" +#include +#include +#include + +int srv_init (int argc, char **argv, char **env, struct service *srv, const char *sname, int (*cb)(int argc, char **argv, char **env, struct service *srv, const char *sname)) +{ + if (srv == NULL) + return ER_PARAMS; + + // TODO + // use the argc, argv and env parameters + // it will be useful to change some parameters transparently + // ex: to get resources from other machines, choosing the + // remote with environment variables + + argc = argc; + argv = argv; + env = env; + + // gets the service path, such as /tmp/ + memset (srv->spath, 0, PATH_MAX); + strncat (srv->spath, TMPDIR, PATH_MAX -1); + strncat (srv->spath, sname, PATH_MAX -1); + + srv->version = COMMUNICATION_VERSION; + srv->index = 0; // TODO + + if (cb != NULL) { + int ret = (*cb) (argc, argv, env, srv, sname); + if (ret != 0) + return ret; + } + + return 0; +} \ No newline at end of file diff --git a/lib/communication2.h b/lib/communication2.h new file mode 100644 index 0000000..58c5a30 --- /dev/null +++ b/lib/communication2.h @@ -0,0 +1,41 @@ +#ifndef __COMMUNICATION2_H__ +#define __COMMUNICATION2_H__ + +#include +#include +#include +#include + +#include "process.h" +#include // unlink + +#include // open + +#include // error numbers + +#define COMMUNICATION_VERSION 1 + +#define ER_FILE_OPEN 1 +#define ER_FILE_CLOSE 2 +#define ER_FILE_READ 3 +#define ER_FILE_WRITE 4 +#define ER_FILE_WRITE_PARAMS 5 + +#define ER_MEM_ALLOC 100 +#define ER_PARAMS 101 + + +struct service { + unsigned int version; + unsigned int index; + char spath[PATH_MAX]; +}; + +int srv_init (int argc, char **argv, char **env + , struct service *srv, const char *sname + , int (*cb)(int argc, char **argv, char **env + , struct service *srv, const char *sname)); + + + +#endif \ No newline at end of file diff --git a/lib/process.h b/lib/process.h index d707c81..4d5b8c1 100644 --- a/lib/process.h +++ b/lib/process.h @@ -19,6 +19,7 @@ struct process { unsigned int index; char path_in [PATH_MAX]; char path_out [PATH_MAX]; + int pfd; }; struct process * srv_process_copy (const struct process *p); diff --git a/pingpong/Makefile b/pingpong/Makefile index d0864dc..dc40348 100644 --- a/pingpong/Makefile +++ b/pingpong/Makefile @@ -15,6 +15,9 @@ $(EXEC): $(OBJECTS) $(CFILES) .c.o: $(CC) -c $(CFLAGS) $< -o $@ +$(TESTS): + valgrind --show-leak-kinds=all --leak-check=full -v --track-origins=yes ./$(basename $@).bin + clean: @-rm $(OBJECTS) diff --git a/pingpong/pingpong.c b/pingpong/pingpong.c index 8a46937..e5c6d1d 100644 --- a/pingpong/pingpong.c +++ b/pingpong/pingpong.c @@ -66,9 +66,7 @@ void main_loop (const struct service *srv) if (ret == -1) { fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n"); continue; - } else if (ret == 0) { // that should not happen - continue; - } + } srv_process_print (&tab_proc[cnt]); @@ -88,7 +86,7 @@ void main_loop (const struct service *srv) } for (i = 0; i < cnt; i++) { - pthread_join(tab_thread[cnt], NULL); + pthread_join(tab_thread[i], NULL); } } diff --git a/remote/Makefile b/remote/Makefile index a4597d9..efa6ad2 100644 --- a/remote/Makefile +++ b/remote/Makefile @@ -15,6 +15,9 @@ $(EXEC): $(OBJECTS) $(CFILES) .c.o: $(CC) -c $(CFLAGS) $< -o $@ +$(TESTS): + valgrind --show-leak-kinds=all --leak-check=full -v --track-origins=yes ./$(basename $@).bin + clean: @-rm $(OBJECTS) diff --git a/remote/script/testTcpd.sh b/remote/script/testTcpd.sh index 15340ea..689f87f 100755 --- a/remote/script/testTcpd.sh +++ b/remote/script/testTcpd.sh @@ -27,4 +27,6 @@ do echo "pid : ${pid}" cat ${REP}/${pid}-1-1-in + echo "exit" > ${REP}${pid}-1-1-out + done diff --git a/remote/tcpdclient.c b/remote/tcpdclient.c deleted file mode 100644 index 6926077..0000000 --- a/remote/tcpdclient.c +++ /dev/null @@ -1,70 +0,0 @@ -#include "tcpdserver.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define BUF_SIZE 1024 -#define PORT 6000 - -void write_message(int sock, const char *buffer) -{ - if(send(sock, buffer, strlen(buffer), 0) < 0) - { - perror("send()"); - exit(errno); - } -} - -int read_message(int sock, char *buffer) -{ - int n = 0; - - if((n = recv(sock, buffer, BUF_SIZE - 1, 0)) < 0) - { - perror("recv()"); - /* if recv error we disonnect the client */ - n = 0; - } - - buffer[n] = 0; - - return n; -} - - -int main(int argc, char ** argv) { - int sock = socket(AF_INET, SOCK_STREAM, 0); - if(sock == -1) - { - perror("socket()"); - exit(errno); - } - - struct sockaddr_in sin = {0}; - sin.sin_addr.s_addr = inet_addr("127.0.0.1"); - sin.sin_port = htons(PORT); /* on utilise htons pour le port */ - sin.sin_family = AF_INET; - - if(connect(sock,(struct sockaddr *) &sin, sizeof(struct sockaddr)) == -1) - { - perror("connect()"); - exit(errno); - } - - write_message(sock, "pongd 5"); - sleep(1); - write_message(sock, "is it working ???"); - sleep(2); - write_message(sock, "is it working ???"); - - close(sock); - - return 0; -} - diff --git a/remote/tcpdserver.c b/remote/tcpdserver.c index a3036c8..0a88089 100644 --- a/remote/tcpdserver.c +++ b/remote/tcpdserver.c @@ -16,7 +16,7 @@ #define PORT 6000 #define BUF_SIZE 1024 #define TMPDIR "/tmp/ipc/" -#define NBCLIENT 5 +#define NBCLIENT 10 #define SERVICE_TCP "tcpd" int init_connection(const info_request *req) @@ -45,7 +45,7 @@ int init_connection(const info_request *req) exit(errno); } - if(listen(sock, 5) == -1) + if(listen(sock, 10) == -1) { perror("listen()"); exit(errno); @@ -54,9 +54,9 @@ int init_connection(const info_request *req) return sock; } -void write_message(int sock, const char *buffer) +void write_message(int sock, const char *buffer, size_t size_buf) { - if(send(sock, buffer, strlen(buffer), 0) < 0) + if(send(sock, buffer, size_buf, 0) < 0) { perror("send()"); exit(errno); @@ -201,7 +201,7 @@ void * service_thread(void * c_data) { perror("app_read()"); } printf("message from file in : %s\n", buffer ); - write_message(clientSock, buffer); + write_message(clientSock, buffer, msize); nbMessages--; } else if (FD_ISSET(clientSock, &rdfs)) { @@ -212,7 +212,7 @@ void * service_thread(void * c_data) { perror("file_write"); } nbMessages++; - } else if (n == 0 && nbMessages == 0){ + } else if (strncmp(buffer, "exit", 4) == 0 && nbMessages == 0){ //message end to server if(app_write(&p, "exit", 4) < 0) { perror("file_write"); @@ -451,6 +451,7 @@ int srv_get_new_request(const struct service *srv, info_request *req) { void * client_thread(void *reqq) { info_request *req = (info_request*) reqq; char buffer[BUF_SIZE]; + int nbMessages = 0; int sock = socket(AF_INET, SOCK_STREAM, 0); if(sock == -1) @@ -468,7 +469,7 @@ void * client_thread(void *reqq) { printf("Connected to server at :\n"); printAddr(&req->addr); - write_message(sock, "pongd 5"); + write_message(sock, "pongd 5", strlen("pongd 5")); /*sleep(1); write_message(sock, "is it working ???"); sleep(2); @@ -503,11 +504,15 @@ void * client_thread(void *reqq) { } if (FD_ISSET(fdout, &rdfs)){ - if(read(fdout, &buffer, BUF_SIZE) < 0) { + size_t n_read = read(fdout, &buffer, BUF_SIZE); + if(n_read < 0) { perror("read()"); } - write_message(sock, buffer); - + printf("Client : size message %ld \n",n_read ); + write_message(sock, buffer, n_read); + if (strncmp(buffer, "exit", 4) != 0) { + nbMessages++; + } } else if (FD_ISSET(sock, &rdfs)) { @@ -518,6 +523,7 @@ void * client_thread(void *reqq) { if(file_write(req->p->path_in, buffer, strlen(buffer)) < 0) { perror("file_write"); } + nbMessages--; } else if (n == 0){ //message end from server @@ -533,10 +539,14 @@ void * client_thread(void *reqq) { } } + + if (strncmp(buffer, "exit", 4) == 0 && nbMessages == 0) { + break; + } } printf("------thread client shutdown----------\n"); - + close(fdout); close(sock); return NULL; diff --git a/remote/tcpdserver.h b/remote/tcpdserver.h index 72c3426..3ee3843 100644 --- a/remote/tcpdserver.h +++ b/remote/tcpdserver.h @@ -26,7 +26,7 @@ void endConnection (int sock); void printAddr (struct sockaddr_in *csin); -void write_message(int sock, const char *buffer); +void write_message(int sock, const char *buffer, size_t size); int read_message(int sock, char *buffer); //2 threads for listen and send data