Archived
3
0

tout est à refaire

This commit is contained in:
lapupe 2016-10-14 17:47:08 +02:00
parent 86722b8db1
commit 8de367100b
12 changed files with 124 additions and 91 deletions

View File

@ -160,12 +160,19 @@ int srv_get_new_process (const struct service *srv, struct process *p)
char *buf = NULL; char *buf = NULL;
size_t msize = 0; size_t msize = 0;
int ret = file_read (srv->spath, &buf, &msize); int ret = 0;
if (ret <= 0) { while (ret == 0) {
fprintf (stderr, "err: listening on %s\n", srv->spath); ret = file_read (srv->spath, &buf, &msize);
exit (1);
if (ret < 0) {
fprintf (stderr, "err: listening on %s\n", srv->spath);
exit (1);
}
} }
char *token = NULL, *saveptr = NULL; char *token = NULL, *saveptr = NULL;
char *str = NULL; char *str = NULL;
int i = 0; int i = 0;
@ -194,7 +201,7 @@ int srv_get_new_process (const struct service *srv, struct process *p)
free (buf); free (buf);
srv_process_gen (p, pid, index, version); srv_process_gen (p, pid, index, version);
return 1; return 0;
} }
int srv_read (struct process *p, char ** buf, size_t * msize) int srv_read (struct process *p, char ** buf, size_t * msize)

View File

@ -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_read (const char *path, char **buf, size_t *msize);
int file_write (const char *path, const char *buf, size_t msize); int file_write (const char *path, const char *buf, size_t msize);
//open, close, read, write
#endif #endif

36
lib/communication2.c Normal file
View File

@ -0,0 +1,36 @@
#include "communication2.h"
#include <stdio.h>
#include <time.h>
#include <errno.h>
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/<service>
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;
}

41
lib/communication2.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef __COMMUNICATION2_H__
#define __COMMUNICATION2_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cbor.h>
#include "process.h"
#include <unistd.h> // unlink
#include <fcntl.h> // open
#include <errno.h> // 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

View File

@ -19,6 +19,7 @@ struct process {
unsigned int index; unsigned int index;
char path_in [PATH_MAX]; char path_in [PATH_MAX];
char path_out [PATH_MAX]; char path_out [PATH_MAX];
int pfd;
}; };
struct process * srv_process_copy (const struct process *p); struct process * srv_process_copy (const struct process *p);

View File

@ -15,6 +15,9 @@ $(EXEC): $(OBJECTS) $(CFILES)
.c.o: .c.o:
$(CC) -c $(CFLAGS) $< -o $@ $(CC) -c $(CFLAGS) $< -o $@
$(TESTS):
valgrind --show-leak-kinds=all --leak-check=full -v --track-origins=yes ./$(basename $@).bin
clean: clean:
@-rm $(OBJECTS) @-rm $(OBJECTS)

View File

@ -66,8 +66,6 @@ void main_loop (const struct service *srv)
if (ret == -1) { if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n"); fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue; continue;
} else if (ret == 0) { // that should not happen
continue;
} }
srv_process_print (&tab_proc[cnt]); srv_process_print (&tab_proc[cnt]);
@ -88,7 +86,7 @@ void main_loop (const struct service *srv)
} }
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
pthread_join(tab_thread[cnt], NULL); pthread_join(tab_thread[i], NULL);
} }
} }

View File

@ -15,6 +15,9 @@ $(EXEC): $(OBJECTS) $(CFILES)
.c.o: .c.o:
$(CC) -c $(CFLAGS) $< -o $@ $(CC) -c $(CFLAGS) $< -o $@
$(TESTS):
valgrind --show-leak-kinds=all --leak-check=full -v --track-origins=yes ./$(basename $@).bin
clean: clean:
@-rm $(OBJECTS) @-rm $(OBJECTS)

View File

@ -27,4 +27,6 @@ do
echo "pid : ${pid}" echo "pid : ${pid}"
cat ${REP}/${pid}-1-1-in cat ${REP}/${pid}-1-1-in
echo "exit" > ${REP}${pid}-1-1-out
done done

View File

@ -1,70 +0,0 @@
#include "tcpdserver.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#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;
}

View File

@ -16,7 +16,7 @@
#define PORT 6000 #define PORT 6000
#define BUF_SIZE 1024 #define BUF_SIZE 1024
#define TMPDIR "/tmp/ipc/" #define TMPDIR "/tmp/ipc/"
#define NBCLIENT 5 #define NBCLIENT 10
#define SERVICE_TCP "tcpd" #define SERVICE_TCP "tcpd"
int init_connection(const info_request *req) int init_connection(const info_request *req)
@ -45,7 +45,7 @@ int init_connection(const info_request *req)
exit(errno); exit(errno);
} }
if(listen(sock, 5) == -1) if(listen(sock, 10) == -1)
{ {
perror("listen()"); perror("listen()");
exit(errno); exit(errno);
@ -54,9 +54,9 @@ int init_connection(const info_request *req)
return sock; 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()"); perror("send()");
exit(errno); exit(errno);
@ -201,7 +201,7 @@ void * service_thread(void * c_data) {
perror("app_read()"); perror("app_read()");
} }
printf("message from file in : %s\n", buffer ); printf("message from file in : %s\n", buffer );
write_message(clientSock, buffer); write_message(clientSock, buffer, msize);
nbMessages--; nbMessages--;
} else if (FD_ISSET(clientSock, &rdfs)) { } else if (FD_ISSET(clientSock, &rdfs)) {
@ -212,7 +212,7 @@ void * service_thread(void * c_data) {
perror("file_write"); perror("file_write");
} }
nbMessages++; nbMessages++;
} else if (n == 0 && nbMessages == 0){ } else if (strncmp(buffer, "exit", 4) == 0 && nbMessages == 0){
//message end to server //message end to server
if(app_write(&p, "exit", 4) < 0) { if(app_write(&p, "exit", 4) < 0) {
perror("file_write"); perror("file_write");
@ -451,6 +451,7 @@ int srv_get_new_request(const struct service *srv, info_request *req) {
void * client_thread(void *reqq) { void * client_thread(void *reqq) {
info_request *req = (info_request*) reqq; info_request *req = (info_request*) reqq;
char buffer[BUF_SIZE]; char buffer[BUF_SIZE];
int nbMessages = 0;
int sock = socket(AF_INET, SOCK_STREAM, 0); int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1) if(sock == -1)
@ -468,7 +469,7 @@ void * client_thread(void *reqq) {
printf("Connected to server at :\n"); printf("Connected to server at :\n");
printAddr(&req->addr); printAddr(&req->addr);
write_message(sock, "pongd 5"); write_message(sock, "pongd 5", strlen("pongd 5"));
/*sleep(1); /*sleep(1);
write_message(sock, "is it working ???"); write_message(sock, "is it working ???");
sleep(2); sleep(2);
@ -503,11 +504,15 @@ void * client_thread(void *reqq) {
} }
if (FD_ISSET(fdout, &rdfs)){ 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()"); 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)) { } 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) { if(file_write(req->p->path_in, buffer, strlen(buffer)) < 0) {
perror("file_write"); perror("file_write");
} }
nbMessages--;
} else if (n == 0){ } else if (n == 0){
//message end from server //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"); printf("------thread client shutdown----------\n");
close(fdout);
close(sock); close(sock);
return NULL; return NULL;

View File

@ -26,7 +26,7 @@ void endConnection (int sock);
void printAddr (struct sockaddr_in *csin); 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); int read_message(int sock, char *buffer);
//2 threads for listen and send data //2 threads for listen and send data