alors ou est tu passé ???
This commit is contained in:
parent
59db09d6f4
commit
5db548c78e
@ -241,6 +241,7 @@ int app_srv_connection (struct service *srv, const char *connectionstr, size_t m
|
|||||||
srv->service_fd = sfd;
|
srv->service_fd = sfd;
|
||||||
|
|
||||||
return srv_write(srv, connectionstr, msize);
|
return srv_write(srv, connectionstr, msize);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int proc_connection(struct process *p) {
|
int proc_connection(struct process *p) {
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
#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
|
|
@ -3,7 +3,7 @@ CFLAGS=-Wall -g
|
|||||||
LDFLAGS= -pthread
|
LDFLAGS= -pthread
|
||||||
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
|
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
|
||||||
EXEC=$(basename $(wildcard *.c))
|
EXEC=$(basename $(wildcard *.c))
|
||||||
SOURCES=$(wildcard ../lib/*.c)
|
SOURCES=$(wildcard ../lib/communication.c ../lib/process.c)
|
||||||
OBJECTS=$(SOURCES:.c=.o)
|
OBJECTS=$(SOURCES:.c=.o)
|
||||||
TESTS=$(addsuffix .test, $(EXEC))
|
TESTS=$(addsuffix .test, $(EXEC))
|
||||||
|
|
||||||
|
@ -10,7 +10,8 @@
|
|||||||
|
|
||||||
/* control the file descriptor*/
|
/* control the file descriptor*/
|
||||||
void * pongd_thread(void * pdata) {
|
void * pongd_thread(void * pdata) {
|
||||||
struct process *proc = (struct process*) pdata;
|
//struct process *proc = (struct process*) pdata;
|
||||||
|
int *sockclient = (int*) pdata;
|
||||||
|
|
||||||
// about the message
|
// about the message
|
||||||
char *buf = malloc(BUFSIZ);
|
char *buf = malloc(BUFSIZ);
|
||||||
@ -22,7 +23,7 @@ void * pongd_thread(void * pdata) {
|
|||||||
int nbytes;
|
int nbytes;
|
||||||
|
|
||||||
//init unix socket
|
//init unix socket
|
||||||
int sfd, cfd;
|
/*int sfd, cfd;
|
||||||
struct sockaddr_un peer_addr;
|
struct sockaddr_un peer_addr;
|
||||||
socklen_t peer_addr_size;
|
socklen_t peer_addr_size;
|
||||||
printf("%s\n", proc->path_proc);
|
printf("%s\n", proc->path_proc);
|
||||||
@ -37,22 +38,22 @@ void * pongd_thread(void * pdata) {
|
|||||||
cfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
|
cfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
|
||||||
if (cfd == -1)
|
if (cfd == -1)
|
||||||
handle_error("accept");
|
handle_error("accept");
|
||||||
proc->proc_fd = cfd;
|
proc->proc_fd = cfd;*/
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if ((nbytes = app_read (proc, &buf)) == -1) {
|
if ((nbytes = file_read (*sockclient, &buf)) == -1) {
|
||||||
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", nbytes);
|
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", nbytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nbytes == 0 || strncmp ("exit", buf, 4) == 0){
|
if (nbytes == 0 || strncmp ("exit", buf, 4) == 0){
|
||||||
printf("------thread shutdown------------\n");
|
printf("------thread shutdown------------\n");
|
||||||
close(cfd);
|
//close(cfd);
|
||||||
close(sfd);
|
//close(sfd);
|
||||||
free(buf);
|
free(buf);
|
||||||
break;
|
break;
|
||||||
}else {
|
}else {
|
||||||
printf ("read, size %d : %s\n", nbytes, buf);
|
printf ("read, size %d : %s\n", nbytes, buf);
|
||||||
if ((nbytes = app_write (proc, buf, nbytes)) == -1) {
|
if ((nbytes = file_write (*sockclient, buf, nbytes)) == -1) {
|
||||||
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", nbytes);
|
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", nbytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,7 +179,7 @@ void main_loop (struct service *srv)
|
|||||||
|
|
||||||
srv_process_print (&tab_proc[cnt]);
|
srv_process_print (&tab_proc[cnt]);
|
||||||
|
|
||||||
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &tab_proc[cnt]);
|
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &i);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
perror("pthread_create()");
|
perror("pthread_create()");
|
||||||
exit(errno);
|
exit(errno);
|
||||||
|
@ -3,7 +3,7 @@ CFLAGS=-Wall -g
|
|||||||
LDFLAGS= -pthread
|
LDFLAGS= -pthread
|
||||||
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
|
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
|
||||||
EXEC=$(basename $(wildcard *.c))
|
EXEC=$(basename $(wildcard *.c))
|
||||||
SOURCES=$(wildcard ../lib/*.c)
|
SOURCES=$(wildcard ../lib/communication.c ../lib/process.c)
|
||||||
OBJECTS=$(SOURCES:.c=.o)
|
OBJECTS=$(SOURCES:.c=.o)
|
||||||
TESTS=$(addsuffix .test, $(EXEC))
|
TESTS=$(addsuffix .test, $(EXEC))
|
||||||
|
|
||||||
|
@ -11,6 +11,6 @@ fi
|
|||||||
|
|
||||||
for pid in `seq 1 ${NB}`
|
for pid in `seq 1 ${NB}`
|
||||||
do
|
do
|
||||||
./tcpdtest.bin &
|
./tcpdtest.bin
|
||||||
|
|
||||||
done
|
done
|
||||||
|
@ -130,18 +130,18 @@ void * service_thread(void * c_data) {
|
|||||||
}
|
}
|
||||||
free(piv);
|
free(piv);
|
||||||
|
|
||||||
struct process p;
|
/*struct process p;
|
||||||
app_create(&p, getpid(), cda->index, version);
|
app_create(&p, getpid(), cda->index, version);
|
||||||
srv_process_print(&p);
|
srv_process_print(&p);*/
|
||||||
sleep(1);
|
//sleep(1);
|
||||||
printf("%s\n",p.path_proc );
|
//printf("%s\n",p.path_proc );
|
||||||
if (proc_connection(&p) == -1){
|
/*if (proc_connection(&p) == -1){
|
||||||
handle_error("proc_connection");
|
handle_error("proc_connection");
|
||||||
}
|
}*/
|
||||||
//utilisation du select() pour surveiller la socket du client et fichier in
|
//utilisation du select() pour surveiller la socket du client et fichier in
|
||||||
fd_set rdfs;
|
fd_set rdfs;
|
||||||
|
|
||||||
int max = clientSock > p.proc_fd ? clientSock : p.proc_fd;
|
int max = clientSock > srv.service_fd ? clientSock : srv.service_fd;
|
||||||
|
|
||||||
printf("Waitting for new messages...\n" );
|
printf("Waitting for new messages...\n" );
|
||||||
while(1) {
|
while(1) {
|
||||||
@ -151,7 +151,7 @@ void * service_thread(void * c_data) {
|
|||||||
FD_SET(clientSock, &rdfs);
|
FD_SET(clientSock, &rdfs);
|
||||||
|
|
||||||
//add in file
|
//add in file
|
||||||
FD_SET(p.proc_fd, &rdfs);
|
FD_SET(srv.service_fd, &rdfs);
|
||||||
|
|
||||||
if(select(max + 1, &rdfs, NULL, NULL, NULL) == -1)
|
if(select(max + 1, &rdfs, NULL, NULL, NULL) == -1)
|
||||||
{
|
{
|
||||||
@ -159,8 +159,8 @@ void * service_thread(void * c_data) {
|
|||||||
exit(errno);
|
exit(errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FD_ISSET(p.proc_fd, &rdfs)){
|
if (FD_ISSET(srv.service_fd, &rdfs)){
|
||||||
nbytes = app_read(&p, &buffer);
|
nbytes = file_read(srv.service_fd, &buffer);
|
||||||
if(nbytes < 0) {
|
if(nbytes < 0) {
|
||||||
perror("app_read()");
|
perror("app_read()");
|
||||||
}
|
}
|
||||||
@ -172,7 +172,7 @@ void * service_thread(void * c_data) {
|
|||||||
nbytes = read_message(clientSock, buffer);
|
nbytes = read_message(clientSock, buffer);
|
||||||
if(nbytes > 0 && strncmp(buffer, "exit", 4) != 0) {
|
if(nbytes > 0 && strncmp(buffer, "exit", 4) != 0) {
|
||||||
printf("Server : message (%ld bytes) : %s\n", nbytes, buffer);
|
printf("Server : message (%ld bytes) : %s\n", nbytes, buffer);
|
||||||
if(app_write(&p, buffer, nbytes) < 0) {
|
if(file_write(srv.service_fd, buffer, nbytes) < 0) {
|
||||||
perror("file_write");
|
perror("file_write");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,7 +180,7 @@ void * service_thread(void * c_data) {
|
|||||||
}
|
}
|
||||||
if (strncmp(buffer, "exit", 4) == 0 && nbMessages == 0){
|
if (strncmp(buffer, "exit", 4) == 0 && nbMessages == 0){
|
||||||
//message end to server
|
//message end to server
|
||||||
if(app_write(&p, "exit", 4) < 0) {
|
if(file_write(srv.service_fd, "exit", 4) < 0) {
|
||||||
perror("file_write");
|
perror("file_write");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,7 +191,7 @@ void * service_thread(void * c_data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//close the files descriptors
|
//close the files descriptors
|
||||||
close(p.proc_fd);
|
close(srv.service_fd);
|
||||||
close(clientSock);
|
close(clientSock);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|
||||||
@ -440,7 +440,7 @@ void * client_thread(void *reqq) {
|
|||||||
|
|
||||||
write_message(sock, "pongd 5", strlen("pongd 5"));
|
write_message(sock, "pongd 5", strlen("pongd 5"));
|
||||||
|
|
||||||
//init socket unix for server
|
/*//init socket unix for server
|
||||||
int sfd;
|
int sfd;
|
||||||
struct sockaddr_un peer_addr;
|
struct sockaddr_un peer_addr;
|
||||||
socklen_t peer_addr_size;
|
socklen_t peer_addr_size;
|
||||||
@ -448,7 +448,7 @@ void * client_thread(void *reqq) {
|
|||||||
sfd = set_listen_socket(req->p->path_proc);
|
sfd = set_listen_socket(req->p->path_proc);
|
||||||
if (sfd == -1){
|
if (sfd == -1){
|
||||||
handle_error("set_listen_socket");
|
handle_error("set_listen_socket");
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/* master file descriptor list */
|
/* master file descriptor list */
|
||||||
fd_set master;
|
fd_set master;
|
||||||
@ -458,9 +458,9 @@ void * client_thread(void *reqq) {
|
|||||||
/* maximum file descriptor number */
|
/* maximum file descriptor number */
|
||||||
int fdmax;
|
int fdmax;
|
||||||
/* listening socket descriptor */
|
/* listening socket descriptor */
|
||||||
int listener = sfd;
|
int listener = req->p->proc_fd;
|
||||||
/* newly accept()ed socket descriptor */
|
/* newly accept()ed socket descriptor */
|
||||||
int newfd;
|
//int newfd;
|
||||||
|
|
||||||
/* clear the master and temp sets */
|
/* clear the master and temp sets */
|
||||||
FD_ZERO(&master);
|
FD_ZERO(&master);
|
||||||
@ -485,30 +485,30 @@ void * client_thread(void *reqq) {
|
|||||||
printf("select...OK\n");
|
printf("select...OK\n");
|
||||||
|
|
||||||
|
|
||||||
if(FD_ISSET(listener, &read_fds)) {
|
// if(FD_ISSET(listener, &read_fds)) {
|
||||||
/* handle new connections */
|
// /* handle new connections */
|
||||||
peer_addr_size = sizeof(struct sockaddr_un);
|
// peer_addr_size = sizeof(struct sockaddr_un);
|
||||||
newfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
|
// newfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
|
||||||
if (newfd == -1) {
|
// if (newfd == -1) {
|
||||||
handle_error("accept");
|
// handle_error("accept");
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
printf("Server-accept() is OK...\n");
|
// printf("Server-accept() is OK...\n");
|
||||||
FD_SET(newfd, &master); /* add to master set */
|
// FD_SET(newfd, &master); /* add to master set */
|
||||||
if(newfd > fdmax)
|
// if(newfd > fdmax)
|
||||||
{ /* keep track of the maximum */
|
// { /* keep track of the maximum */
|
||||||
fdmax = newfd;
|
// fdmax = newfd;
|
||||||
}
|
// }
|
||||||
req->p->proc_fd = newfd;
|
// req->p->proc_fd = newfd;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
/*
|
/*
|
||||||
* TODO:
|
* TODO:
|
||||||
* Que se passe-t-il si on reçoit un message du serveur avant l'app client?
|
* Que se passe-t-il si on reçoit un message du serveur avant l'app client?
|
||||||
* Ou ecrit-on le message ??
|
* Ou ecrit-on le message ??
|
||||||
*/
|
*/
|
||||||
else if (FD_ISSET(sock, &read_fds)) {
|
/*else*/ if (FD_ISSET(sock, &read_fds)) {
|
||||||
int n = read_message(sock, buffer);
|
int n = read_message(sock, buffer);
|
||||||
if(n > 0) {
|
if(n > 0) {
|
||||||
printf("Client : message (%d bytes) : %s\n", n, buffer);
|
printf("Client : message (%d bytes) : %s\n", n, buffer);
|
||||||
@ -546,7 +546,7 @@ void * client_thread(void *reqq) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("------thread client shutdown----------\n");
|
printf("------thread client shutdown----------\n");
|
||||||
close(sfd);
|
close(listener);
|
||||||
close(sock);
|
close(sock);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|
||||||
@ -665,6 +665,7 @@ void main_loop (struct service *srv) {
|
|||||||
tab_req[nbclient].p = malloc(sizeof(struct process));
|
tab_req[nbclient].p = malloc(sizeof(struct process));
|
||||||
// -1 : error, 0 = no new process, 1 = new process
|
// -1 : error, 0 = no new process, 1 = new process
|
||||||
ret = srv_get_new_request (buf, &tab_req[nbclient]);
|
ret = srv_get_new_request (buf, &tab_req[nbclient]);
|
||||||
|
tab_req[nbclient].p->proc_fd = i;
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
perror("srv_get_new_request()");
|
perror("srv_get_new_request()");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
64
remote/tcpdtest.c
Normal file
64
remote/tcpdtest.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include "../lib/communication.h"
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
|
||||||
|
#define PONGD_SERVICE_NAME "pongd"
|
||||||
|
#define LISTEN_BACKLOG 50
|
||||||
|
#define handle_error(msg) \
|
||||||
|
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
||||||
|
|
||||||
|
int main(int argc, char * argv[]) {
|
||||||
|
char *srv_message = malloc(1024);
|
||||||
|
char *pidfile = malloc(1024);
|
||||||
|
char *buffer = malloc(1024);
|
||||||
|
|
||||||
|
snprintf(srv_message , 1024, "%s %d 1 1" ,"connect 127.0.0.1 6000" , getpid());
|
||||||
|
snprintf(pidfile, 1024,"%s%d-1-1", "/tmp/ipc/", getpid());
|
||||||
|
|
||||||
|
char *proc_message = "hello frero";
|
||||||
|
|
||||||
|
int sfd;
|
||||||
|
struct sockaddr_un my_addr;
|
||||||
|
socklen_t peer_addr_size;
|
||||||
|
|
||||||
|
sfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (sfd == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
memset(&my_addr, 0, sizeof(struct sockaddr_un));
|
||||||
|
// Clear structure
|
||||||
|
my_addr.sun_family = AF_UNIX;
|
||||||
|
strncpy(my_addr.sun_path, "/tmp/ipc/tcpd", sizeof(my_addr.sun_path) - 1);
|
||||||
|
|
||||||
|
peer_addr_size = sizeof(struct sockaddr_un);
|
||||||
|
if(connect(sfd,(struct sockaddr *) &my_addr, peer_addr_size) == -1)
|
||||||
|
{
|
||||||
|
perror("connect()");
|
||||||
|
exit(errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
file_write(sfd, srv_message, strlen(srv_message));
|
||||||
|
file_write(sfd, proc_message, strlen(srv_message));
|
||||||
|
file_read(sfd, &buffer);
|
||||||
|
printf("%s\n", buffer);
|
||||||
|
|
||||||
|
file_write(sfd, "exit", 4);
|
||||||
|
|
||||||
|
close(sfd);
|
||||||
|
|
||||||
|
// //sleep(1);
|
||||||
|
// cfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
// if (sfd == -1)
|
||||||
|
// return -1;
|
||||||
|
// strncpy(my_addr.sun_path, pidfile, sizeof(my_addr.sun_path) - 1);
|
||||||
|
// if(connect(cfd,(struct sockaddr *) &my_addr, peer_addr_size) == -1)
|
||||||
|
// {
|
||||||
|
// perror("connect()");
|
||||||
|
// exit(errno);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// close(cfd);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user