Obsolete
/
libipc-old
Archived
3
0
Fork 0

alors ou est tu passé ???

more_to_read
lapupe 2016-11-03 22:44:35 +01:00
parent 59db09d6f4
commit 5db548c78e
9 changed files with 115 additions and 125 deletions

View File

@ -241,6 +241,7 @@ int app_srv_connection (struct service *srv, const char *connectionstr, size_t m
srv->service_fd = sfd;
return srv_write(srv, connectionstr, msize);
}
int proc_connection(struct process *p) {

View File

@ -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;
}

View File

@ -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

View File

@ -3,7 +3,7 @@ CFLAGS=-Wall -g
LDFLAGS= -pthread
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/*.c)
SOURCES=$(wildcard ../lib/communication.c ../lib/process.c)
OBJECTS=$(SOURCES:.c=.o)
TESTS=$(addsuffix .test, $(EXEC))

View File

@ -10,7 +10,8 @@
/* control the file descriptor*/
void * pongd_thread(void * pdata) {
struct process *proc = (struct process*) pdata;
//struct process *proc = (struct process*) pdata;
int *sockclient = (int*) pdata;
// about the message
char *buf = malloc(BUFSIZ);
@ -22,7 +23,7 @@ void * pongd_thread(void * pdata) {
int nbytes;
//init unix socket
int sfd, cfd;
/*int sfd, cfd;
struct sockaddr_un peer_addr;
socklen_t peer_addr_size;
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);
if (cfd == -1)
handle_error("accept");
proc->proc_fd = cfd;
proc->proc_fd = cfd;*/
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);
}
if (nbytes == 0 || strncmp ("exit", buf, 4) == 0){
printf("------thread shutdown------------\n");
close(cfd);
close(sfd);
//close(cfd);
//close(sfd);
free(buf);
break;
}else {
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);
}
}
@ -178,7 +179,7 @@ void main_loop (struct service *srv)
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) {
perror("pthread_create()");
exit(errno);

View File

@ -3,7 +3,7 @@ CFLAGS=-Wall -g
LDFLAGS= -pthread
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/*.c)
SOURCES=$(wildcard ../lib/communication.c ../lib/process.c)
OBJECTS=$(SOURCES:.c=.o)
TESTS=$(addsuffix .test, $(EXEC))

View File

@ -11,6 +11,6 @@ fi
for pid in `seq 1 ${NB}`
do
./tcpdtest.bin &
./tcpdtest.bin
done

View File

@ -130,18 +130,18 @@ void * service_thread(void * c_data) {
}
free(piv);
struct process p;
/*struct process p;
app_create(&p, getpid(), cda->index, version);
srv_process_print(&p);
sleep(1);
printf("%s\n",p.path_proc );
if (proc_connection(&p) == -1){
srv_process_print(&p);*/
//sleep(1);
//printf("%s\n",p.path_proc );
/*if (proc_connection(&p) == -1){
handle_error("proc_connection");
}
}*/
//utilisation du select() pour surveiller la socket du client et fichier in
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" );
while(1) {
@ -151,7 +151,7 @@ void * service_thread(void * c_data) {
FD_SET(clientSock, &rdfs);
//add in file
FD_SET(p.proc_fd, &rdfs);
FD_SET(srv.service_fd, &rdfs);
if(select(max + 1, &rdfs, NULL, NULL, NULL) == -1)
{
@ -159,8 +159,8 @@ void * service_thread(void * c_data) {
exit(errno);
}
if (FD_ISSET(p.proc_fd, &rdfs)){
nbytes = app_read(&p, &buffer);
if (FD_ISSET(srv.service_fd, &rdfs)){
nbytes = file_read(srv.service_fd, &buffer);
if(nbytes < 0) {
perror("app_read()");
}
@ -172,7 +172,7 @@ void * service_thread(void * c_data) {
nbytes = read_message(clientSock, buffer);
if(nbytes > 0 && strncmp(buffer, "exit", 4) != 0) {
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");
}
@ -180,7 +180,7 @@ void * service_thread(void * c_data) {
}
if (strncmp(buffer, "exit", 4) == 0 && nbMessages == 0){
//message end to server
if(app_write(&p, "exit", 4) < 0) {
if(file_write(srv.service_fd, "exit", 4) < 0) {
perror("file_write");
}
@ -191,7 +191,7 @@ void * service_thread(void * c_data) {
}
}
//close the files descriptors
close(p.proc_fd);
close(srv.service_fd);
close(clientSock);
free(buffer);
@ -440,7 +440,7 @@ void * client_thread(void *reqq) {
write_message(sock, "pongd 5", strlen("pongd 5"));
//init socket unix for server
/*//init socket unix for server
int sfd;
struct sockaddr_un peer_addr;
socklen_t peer_addr_size;
@ -448,7 +448,7 @@ void * client_thread(void *reqq) {
sfd = set_listen_socket(req->p->path_proc);
if (sfd == -1){
handle_error("set_listen_socket");
}
}*/
/* master file descriptor list */
fd_set master;
@ -458,9 +458,9 @@ void * client_thread(void *reqq) {
/* maximum file descriptor number */
int fdmax;
/* listening socket descriptor */
int listener = sfd;
int listener = req->p->proc_fd;
/* newly accept()ed socket descriptor */
int newfd;
//int newfd;
/* clear the master and temp sets */
FD_ZERO(&master);
@ -485,30 +485,30 @@ void * client_thread(void *reqq) {
printf("select...OK\n");
if(FD_ISSET(listener, &read_fds)) {
/* handle new connections */
peer_addr_size = sizeof(struct sockaddr_un);
newfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
if (newfd == -1) {
handle_error("accept");
}
else
{
printf("Server-accept() is OK...\n");
FD_SET(newfd, &master); /* add to master set */
if(newfd > fdmax)
{ /* keep track of the maximum */
fdmax = newfd;
}
req->p->proc_fd = newfd;
}
}
// if(FD_ISSET(listener, &read_fds)) {
// /* handle new connections */
// peer_addr_size = sizeof(struct sockaddr_un);
// newfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
// if (newfd == -1) {
// handle_error("accept");
// }
// else
// {
// printf("Server-accept() is OK...\n");
// FD_SET(newfd, &master); /* add to master set */
// if(newfd > fdmax)
// { /* keep track of the maximum */
// fdmax = newfd;
// }
// req->p->proc_fd = newfd;
// }
// }
/*
* TODO:
* Que se passe-t-il si on reçoit un message du serveur avant l'app client?
* 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);
if(n > 0) {
printf("Client : message (%d bytes) : %s\n", n, buffer);
@ -546,7 +546,7 @@ void * client_thread(void *reqq) {
}
printf("------thread client shutdown----------\n");
close(sfd);
close(listener);
close(sock);
free(buffer);
@ -665,6 +665,7 @@ void main_loop (struct service *srv) {
tab_req[nbclient].p = malloc(sizeof(struct process));
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_request (buf, &tab_req[nbclient]);
tab_req[nbclient].p->proc_fd = i;
if (ret == -1) {
perror("srv_get_new_request()");
exit(1);

64
remote/tcpdtest.c Normal file
View 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;
}