Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/tcp/app/tcpd.c

801 lines
23 KiB
C
Raw Normal View History

#include "tcpd.h"
2016-09-18 00:17:47 +02:00
#include <sys/types.h>
#include <sys/socket.h>
2016-10-28 13:58:04 +02:00
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h> // mkfifo
#include <linux/limits.h>
#define PORT 6000
#define TMPDIR "/tmp/ipc/"
2016-10-14 17:47:08 +02:00
#define NBCLIENT 10
#define SERVICE_TCP "tcpd"
2016-10-28 13:58:04 +02:00
#define LISTEN_BACKLOG 50
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
2016-09-29 19:02:05 +02:00
int init_connection(const info_request *req)
{
int yes = 1;
2016-09-26 14:15:56 +02:00
int sock = socket(AF_INET, SOCK_STREAM, 0);
2016-09-29 19:02:05 +02:00
//struct sockaddr_in sin = { 0 };
2016-09-26 14:15:56 +02:00
if(sock == -1)
{
perror("socket()");
exit(errno);
}
/*"address already in use" error message */
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
{
perror("Server-setsockopt() error lol!");
exit(1);
}
printf("Server-setsockopt() is OK...\n");
2016-09-26 14:15:56 +02:00
2016-09-29 19:02:05 +02:00
if(bind(sock,(struct sockaddr *) &req->addr, sizeof(req->addr)) == -1)
2016-09-26 14:15:56 +02:00
{
perror("bind()");
exit(errno);
}
2016-10-14 17:47:08 +02:00
if(listen(sock, 10) == -1)
2016-09-26 14:15:56 +02:00
{
perror("listen()");
exit(errno);
}
return sock;
}
2016-10-14 17:47:08 +02:00
void write_message(int sock, const char *buffer, size_t size_buf)
{
2016-10-14 17:47:08 +02:00
if(send(sock, buffer, size_buf, 0) < 0)
2016-09-26 14:15:56 +02:00
{
perror("send()");
exit(errno);
}
}
int read_message(int sock, char *buffer)
{
2016-10-28 13:58:04 +02:00
return read(sock, buffer, BUFSIZ);
}
2016-09-22 18:38:24 +02:00
void endConnection(int sock) {
2016-09-26 14:15:56 +02:00
close(sock);
}
void printAddr(struct sockaddr_in *csin) {
2016-09-26 14:15:56 +02:00
printf("IP Addr : %s\n", inet_ntoa(csin->sin_addr));
printf("Port : %u\n", ntohs(csin->sin_port));
}
2016-10-28 13:58:04 +02:00
/*
* Chaque client correspond à un thread service
* Le 1er message du client indiquera le service voulu et sa version
* Etablir la connection avec le service
* Ecouter ensuite sur la socket client et socket unix pour traiter les messages
*/
void * service_thread(void * c_data) {
2016-09-26 14:15:56 +02:00
client_data *cda = (client_data*) c_data;
char *service;
int version;
int clientSock = cda->sfd;
int nbMessages = 0;
2016-09-26 14:15:56 +02:00
2016-10-28 13:58:04 +02:00
//buffer for message
size_t nbytes = 0;
char *buffer = malloc (BUFSIZ);
2016-10-06 19:44:53 +02:00
if (buffer == NULL) {
perror("malloc()");
return NULL;
}
2016-10-28 13:58:04 +02:00
memset(buffer, 0, BUFSIZ);
2016-10-06 19:44:53 +02:00
2016-09-26 14:15:56 +02:00
if (read_message(clientSock, buffer) == -1) {
perror("read_message()");
return NULL;
}else {
parseServiceVersion(buffer, &service, &version);
}
/* TODO : service correspond au service que le client veut utiliser
** il faut comparer service à un tableau qui contient les services
** disponibles
*/
//pid index version
char * piv = malloc(PATH_MAX);
2016-09-26 14:15:56 +02:00
memset(piv , 0, PATH_MAX);
if (piv == NULL) {
perror("malloc()");
}
makePivMessage(&piv, getpid(), cda->index, version);
2016-10-06 19:44:53 +02:00
struct service srv;
memset (&srv, 0, sizeof (struct service));
srv->index = 0;
srv->version = 0;
2016-10-06 19:44:53 +02:00
srv_init (0, NULL, NULL, &srv, service, NULL);
2016-10-28 13:58:04 +02:00
if (app_srv_connection(&srv, piv, strlen(piv)) == -1) {
handle_error("app_srv_connection\n");
2016-09-26 14:15:56 +02:00
}
2016-10-28 13:58:04 +02:00
free(piv);
2016-10-06 19:44:53 +02:00
2016-11-03 22:44:35 +01:00
/*struct process p;
2016-10-06 19:44:53 +02:00
app_create(&p, getpid(), cda->index, version);
2016-11-03 22:44:35 +01:00
srv_process_print(&p);*/
//sleep(1);
//printf("%s\n",p.path_proc );
/*if (proc_connection(&p) == -1){
2016-10-28 13:58:04 +02:00
handle_error("proc_connection");
2016-11-03 22:44:35 +01:00
}*/
2016-09-26 14:15:56 +02:00
//utilisation du select() pour surveiller la socket du client et fichier in
fd_set rdfs;
2016-11-03 22:44:35 +01:00
int max = clientSock > srv.service_fd ? clientSock : srv.service_fd;
2016-09-26 14:15:56 +02:00
printf("Waitting for new messages...\n" );
2016-09-26 14:15:56 +02:00
while(1) {
FD_ZERO(&rdfs);
//add client's socket
FD_SET(clientSock, &rdfs);
//add in file
2016-11-03 22:44:35 +01:00
FD_SET(srv.service_fd, &rdfs);
2016-09-26 14:15:56 +02:00
if(select(max + 1, &rdfs, NULL, NULL, NULL) == -1)
{
perror("select()");
exit(errno);
}
2016-11-03 22:44:35 +01:00
if (FD_ISSET(srv.service_fd, &rdfs)){
nbytes = file_read(srv.service_fd, &buffer);
2016-10-28 13:58:04 +02:00
if(nbytes < 0) {
2016-10-06 19:44:53 +02:00
perror("app_read()");
2016-09-26 14:15:56 +02:00
}
2016-10-28 13:58:04 +02:00
printf("message from file : %s\n", buffer );
write_message(clientSock, buffer, nbytes);
nbMessages--;
2016-09-29 19:02:05 +02:00
2016-10-28 13:58:04 +02:00
} else if (FD_ISSET(clientSock, &rdfs)) {
nbytes = read_message(clientSock, buffer);
if(nbytes > 0 && strncmp(buffer, "exit", 4) != 0) {
printf("Server : message (%ld bytes) : %s\n", nbytes, buffer);
2016-11-03 22:44:35 +01:00
if(file_write(srv.service_fd, buffer, nbytes) < 0) {
perror("file_write");
}
2016-10-28 13:58:04 +02:00
nbMessages++;
2016-10-28 13:58:04 +02:00
}
if (strncmp(buffer, "exit", 4) == 0 && nbMessages == 0){
//message end to server
2016-11-03 22:44:35 +01:00
if(file_write(srv.service_fd, "exit", 4) < 0) {
perror("file_write");
}
2016-09-26 14:15:56 +02:00
printf("------thread %d shutdown----------\n\n", cda->index );
break;
2016-10-28 13:58:04 +02:00
}
2016-09-26 14:15:56 +02:00
}
}
2016-10-28 13:58:04 +02:00
//close the files descriptors
2016-11-03 22:44:35 +01:00
close(srv.service_fd);
2016-10-28 13:58:04 +02:00
close(clientSock);
free(buffer);
2016-09-26 14:15:56 +02:00
2016-10-28 13:58:04 +02:00
//release the resources
pthread_detach(pthread_self());
2016-09-26 14:15:56 +02:00
return NULL;
}
2016-09-18 00:17:47 +02:00
void parseServiceVersion(char * buf, char ** service, int *version) {
2016-09-26 14:15:56 +02:00
char *token = NULL, *saveptr = NULL;
char *str = NULL;
int i = 0;
for (str = buf, i = 1; ; str = NULL, i++) {
token = strtok_r(str, " ", &saveptr);
if (token == NULL)
break;
if (i == 1) {
*service = token;
}
else if (i == 2) {
*version = strtoul(token, NULL, 10);
}
}
}
int fifo_create (char * path)
{
int ret;
if ((ret = mkfifo (path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) {
switch (errno) {
case EACCES :
printf ("file %s : EACCES\n", path);
return 1;
case EEXIST :
printf ("file %s : EEXIST\n", path);
break;
case ENAMETOOLONG :
printf ("file %s : ENAMETOOLONG\n", path);
return 2;
case ENOENT :
printf ("file %s : ENOENT\n", path);
return 3;
case ENOSPC :
printf ("file %s : ENOSPC\n", path);
return 4;
case ENOTDIR :
printf ("file %s : ENOTDIR\n", path);
return 5;
case EROFS :
printf ("file %s : EROFS\n", path);
return 6;
default :
printf ("err file %s unknown\n", path);
return 7;
}
}
return ret;
}
void inOutPathCreate(char ** pathname, int index, int version) {
2016-09-29 17:45:22 +02:00
snprintf(pathname[0] , PATH_MAX, "%s%d-%d-%d-in" , TMPDIR, getpid(), index, version);
snprintf(pathname[1] , PATH_MAX, "%s%d-%d-%d-out", TMPDIR, getpid(), index, version);
}
void makePivMessage (char ** piv, int pid, int index, int version) {
2016-09-29 17:45:22 +02:00
snprintf(*piv , PATH_MAX, "%d %d %d" , getpid(), index, version);
}
2016-10-28 13:58:04 +02:00
/*
* lancer le serveur, ecouter sur une l'adresse et port
* A chaque nouveau client lance un thread service
*/
2016-09-29 19:02:05 +02:00
void * server_thread(void * reqq) {
info_request *req = (info_request*) reqq;
2016-09-29 17:45:22 +02:00
//client
client_data tab_client[NBCLIENT];
pthread_t tab_service_threads[NBCLIENT];
int actual = 0;
int i;
2016-09-29 19:02:05 +02:00
int sock = init_connection(req);
2016-09-29 17:45:22 +02:00
fd_set rdfs;
printf("Waitting for new clients :\n" );
while(1) {
FD_ZERO(&rdfs);
/* add STDIN_FILENO */
FD_SET(STDIN_FILENO, &rdfs);
//add listener's socket
FD_SET(sock, &rdfs);
2016-09-29 19:02:05 +02:00
if(select(sock + 1, &rdfs, NULL, NULL, NULL) == -1)
2016-09-29 17:45:22 +02:00
{
perror("select()");
exit(errno);
}
/* something from standard input : i.e keyboard */
if(FD_ISSET(STDIN_FILENO, &rdfs))
{
/* stop process when type on keyboard */
for (i = 0; i < actual; i++) {
if (pthread_cancel(tab_service_threads[i]) != 0) {
printf("Aucun thread correspond \n");
}
}
2016-09-29 17:45:22 +02:00
printf("server shutdown\n");
break;
}
else if (FD_ISSET(sock, &rdfs)){
//New client
socklen_t sinsize = sizeof (struct sockaddr_in);
tab_client[actual].sfd = accept(sock, (struct sockaddr *)&tab_client[actual].c_addr, &sinsize);
if(tab_client[actual].sfd == -1)
{
perror("accept()");
close(sock);
exit(errno);
}
printf("New client :\n");
printAddr(&tab_client[actual].c_addr);
2016-09-29 17:45:22 +02:00
tab_client[actual].index = actual;
int ret = pthread_create( &tab_service_threads[actual], NULL, &service_thread, (void *) &tab_client[actual]);
if (ret) {
perror("pthread_create()");
endConnection(sock);
exit(errno);
} else {
printf("\n----------Creation of listen thread %d ------------\n", actual);
}
actual++;
}
}
for (i = 0; i < actual; i++) {
pthread_join(tab_service_threads[i], NULL);
}
2016-10-28 13:58:04 +02:00
printf("--------------server thread shutdown--------------- \n");
2016-09-29 17:45:22 +02:00
endConnection(sock);
return NULL;
}
2016-09-29 19:02:05 +02:00
/*
2016-10-28 13:58:04 +02:00
* user can send 2 types of request to server : listen or connect
2016-09-29 19:02:05 +02:00
* listen = server for a service such as pongd
* connect = connect to a server
*/
2016-10-28 13:58:04 +02:00
int srv_get_new_request(char *buf, info_request *req) {
2016-09-29 17:45:22 +02:00
2016-09-29 19:02:05 +02:00
char *token = NULL, *saveptr = NULL;
char *str = NULL;
int i = 0;
2016-09-29 17:45:22 +02:00
//for a "connect" request
pid_t pid = 0;
int index = 0;
int version = 0;
2016-09-29 19:02:05 +02:00
for (str = buf, i = 1; ; str = NULL, i++) {
token = strtok_r(str, " ", &saveptr);
if (token == NULL)
break;
2016-09-26 14:15:56 +02:00
2016-09-29 19:02:05 +02:00
if (i == 1) {
if(strncmp("exit", token, 4) == 0 ) {
2016-10-28 13:58:04 +02:00
strncpy(req->request, token, 4);
free(str);
return 0;
}
2016-09-29 19:02:05 +02:00
req->request = token;
}
else if (i == 2){
2016-09-29 19:02:05 +02:00
req->addr.sin_addr.s_addr = inet_addr(token);
}
else if (i == 3) {
req->addr.sin_port = htons(strtoul(token, NULL, 10));
}
else if (i == 4 && (strcmp("connect", req->request)) == 0){
pid = strtoul(token, NULL, 10);
}
else if (i == 5 && (strcmp("connect", req->request)) == 0) {
index = strtoul(token, NULL, 10);
}
else if (i == 6 && (strcmp("connect", req->request)) == 0) {
version = strtoul(token, NULL, 10);
}
2016-09-29 19:02:05 +02:00
}
2016-09-26 14:15:56 +02:00
2016-09-29 19:02:05 +02:00
req->addr.sin_family = AF_INET;
2016-09-26 14:15:56 +02:00
if (strcmp("connect", req->request) == 0) {
srv_process_gen (req->p, pid, index, version);
}
2016-09-29 19:02:05 +02:00
return 1;
}
2016-09-26 14:15:56 +02:00
2016-10-28 13:58:04 +02:00
/*
* client thread est lancé suite à une requete "connect"
* connecter à une adresse, port le 1er message indiquera le service et le version voulus
* Se mettre ensuite sur l'écoute de la socket serveur et le fichier client pour traiter les messages
*/
void * client_thread(void *reqq) {
info_request *req = (info_request*) reqq;
2016-10-28 13:58:04 +02:00
/* buffer for client data */
int nbytes;
char *buffer= malloc(BUFSIZ);
if (buffer == NULL)
{
handle_error("malloc");
}
memset(buffer, 0, BUFSIZ);;
2016-10-14 17:47:08 +02:00
int nbMessages = 0;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1)
{
perror("socket()");
exit(errno);
}
if(connect(sock,(struct sockaddr *) &req->addr, sizeof(struct sockaddr)) == -1)
{
perror("connect()");
exit(errno);
}
printf("Connected to server at :\n");
printAddr(&req->addr);
2016-10-14 17:47:08 +02:00
write_message(sock, "pongd 5", strlen("pongd 5"));
2016-10-28 13:58:04 +02:00
2016-11-03 22:44:35 +01:00
/*//init socket unix for server
2016-10-28 13:58:04 +02:00
int sfd;
struct sockaddr_un peer_addr;
socklen_t peer_addr_size;
sfd = set_listen_socket(req->p->path_proc);
if (sfd == -1){
handle_error("set_listen_socket");
2016-11-03 22:44:35 +01:00
}*/
2016-10-28 13:58:04 +02:00
/* master file descriptor list */
fd_set master;
/* temp file descriptor list for select() */
fd_set read_fds;
2016-10-28 13:58:04 +02:00
/* maximum file descriptor number */
int fdmax;
/* listening socket descriptor */
2016-11-03 22:44:35 +01:00
int listener = req->p->proc_fd;
2016-10-28 13:58:04 +02:00
/* newly accept()ed socket descriptor */
2016-11-03 22:44:35 +01:00
//int newfd;
2016-10-28 13:58:04 +02:00
/* clear the master and temp sets */
FD_ZERO(&master);
FD_ZERO(&read_fds);
2016-10-28 13:58:04 +02:00
/* add the listener to the master set */
FD_SET(listener, &master);
FD_SET(sock, &master);
2016-10-28 13:58:04 +02:00
/* keep track of the biggest file descriptor */
fdmax = sock > listener ? sock : listener;
2016-10-28 13:58:04 +02:00
while(1) {
/* copy it */
read_fds = master;
if(select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1)
{
perror("select()");
exit(errno);
}
2016-10-28 13:58:04 +02:00
printf("select...OK\n");
2016-11-03 22:44:35 +01:00
// 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;
// }
// }
2016-10-28 13:58:04 +02:00
/*
* TODO:
* Que se passe-t-il si on reçoit un message du serveur avant l'app client?
* Ou ecrit-on le message ??
*/
2016-11-03 22:44:35 +01:00
/*else*/ if (FD_ISSET(sock, &read_fds)) {
int n = read_message(sock, buffer);
if(n > 0) {
2016-11-04 16:51:17 +01:00
printf("Client : message from server(%d bytes) : %s\n", n, buffer);
2016-10-28 13:58:04 +02:00
if(app_write(req->p, buffer, strlen(buffer)) < 0) {
perror("file_write");
}
2016-10-14 17:47:08 +02:00
nbMessages--;
} else if (n == 0){
//message end from server
printf("server down\n");
printf("------thread client shutdown----------\n");
break;
}
2016-10-28 13:58:04 +02:00
}else {
nbytes = app_read (req->p, &buffer);
2016-11-04 16:51:17 +01:00
printf("Client : message from app %d : %s\n",nbytes, buffer );
2016-10-28 13:58:04 +02:00
if ( nbytes == -1) {
handle_error("file_read");
} else if( nbytes == 0) {
/* close it... */
close(req->p->proc_fd);
/* remove from master set */
FD_CLR(req->p->proc_fd, &master);
break;
}else {
//printf("Client : size message %d \n",nbytes );
write_message(sock, buffer, nbytes);
if (strncmp(buffer, "exit", 4) != 0) {
nbMessages++;
}
}
}
}
2016-10-06 19:44:53 +02:00
printf("------thread client shutdown----------\n");
2016-11-03 22:44:35 +01:00
close(listener);
close(sock);
2016-10-28 13:58:04 +02:00
free(buffer);
2016-10-28 13:58:04 +02:00
//release the resources
pthread_detach(pthread_self());
return NULL;
}
2016-09-29 19:02:05 +02:00
void request_print (const info_request *req) {
printf("%s \n",req->request);
2016-09-29 19:02:05 +02:00
}
2016-09-26 14:15:56 +02:00
2016-10-28 13:58:04 +02:00
/*
*Surveiller le fichier tmp/ipc/service
*Accepter 2 types de requetes :
* listen : lancer un serveur, ecouter sur un port ie "listen 127.0.0.1 6000"
* connect : connecter à une adresse, port ie "connect 127.0.0.1 6000 ${pid} 1 1"
*/
void main_loop (struct service *srv) {
2016-09-29 19:02:05 +02:00
//request
2016-10-28 13:58:04 +02:00
info_request tab_req[NBCLIENT];
2016-09-29 19:02:05 +02:00
int ret;
2016-10-28 14:24:15 +02:00
int i;
2016-10-28 13:58:04 +02:00
//pid server
pthread_t pid_s;
//pid client
pthread_t tab_client[NBCLIENT];
int nbclient = 0;
//init socket unix for server
int sfd;
struct sockaddr_un peer_addr;
socklen_t peer_addr_size;
sfd = set_listen_socket(srv->spath);
if (sfd == -1){
handle_error("set_listen_socket");
}
2016-09-29 19:02:05 +02:00
2016-10-28 13:58:04 +02:00
/* master file descriptor list */
fd_set master;
/* temp file descriptor list for select() */
fd_set read_fds;
/* maximum file descriptor number */
int fdmax;
/* listening socket descriptor */
int listener = sfd;
/* newly accept()ed socket descriptor */
int newfd;
/* buffer for client data */
char *buf = malloc(BUFSIZ);
if (buf == NULL)
{
handle_error("malloc");
}
memset(buf, 0, BUFSIZ);
int nbytes;
/* clear the master and temp sets */
FD_ZERO(&master);
FD_ZERO(&read_fds);
/* add the listener to the master set */
FD_SET(listener, &master);
//FD_SET(sfd, &master);
/* keep track of the biggest file descriptor */
fdmax = listener; /* so far, it's this one*/
for(;;) {
/* copy it */
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
{
perror("Server-select() error lol!");
2016-09-29 19:02:05 +02:00
exit(1);
2016-09-26 14:15:56 +02:00
}
2016-10-28 13:58:04 +02:00
//printf("Server-select...OK\n");
2016-10-28 14:24:15 +02:00
for (i = 0; i <= fdmax; i++) {
if(FD_ISSET(i, &read_fds)) {
if(i == listener) {
/* 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");
2016-10-28 13:58:04 +02:00
}
2016-10-28 14:24:15 +02:00
else
{
printf("Server-accept() is OK...\n");
2016-11-04 16:51:17 +01:00
//FD_SET(newfd, &master); /* add to master set */
//if(newfd > fdmax)
//{ /* keep track of the maximum */
//fdmax = newfd;
//}
nbytes = file_read (newfd, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else if( nbytes == 0) {
/* close it... */
close(i);
/* remove from master set */
FD_CLR(i, &master);
}else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
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 = newfd;
if (ret == -1) {
perror("srv_get_new_request()");
exit(1);
} else if (ret == 0) {
break;
}
request_print(&tab_req[nbclient]);
if (strcmp("listen", tab_req[nbclient].request) == 0) {
int ret = pthread_create( &pid_s, NULL, &server_thread, (void *) &tab_req[nbclient]);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf("\n----------Creation of server thread ------------\n");
}
nbclient++;
}else {
int ret = pthread_create( &tab_client[nbclient], NULL, &client_thread, (void *) &tab_req[nbclient]);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf("\n----------Creation of client thread ------------\n");
}
nbclient++;
}
}
2016-10-28 13:58:04 +02:00
}
2016-11-04 16:51:17 +01:00
} /*else {
2016-10-28 14:24:15 +02:00
nbytes = file_read (i, &buf);
if ( nbytes == -1) {
handle_error("file_read");
2016-11-04 16:51:17 +01:00
} else if( nbytes == 0) {*/
2016-10-28 14:24:15 +02:00
/* close it... */
2016-11-04 16:51:17 +01:00
//close(i);
2016-10-28 14:24:15 +02:00
/* remove from master set */
2016-11-04 16:51:17 +01:00
/*FD_CLR(i, &master);
2016-10-28 14:24:15 +02:00
}else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
2016-11-04 16:51:17 +01:00
if (strncmp ("exit_server", buf, 4) == 0) {
2016-10-28 14:24:15 +02:00
break;
}
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]);
2016-11-03 22:44:35 +01:00
tab_req[nbclient].p->proc_fd = i;
2016-10-28 14:24:15 +02:00
if (ret == -1) {
perror("srv_get_new_request()");
exit(1);
} else if (ret == 0) {
break;
}
request_print(&tab_req[nbclient]);
if (strcmp("listen", tab_req[nbclient].request) == 0) {
int ret = pthread_create( &pid_s, NULL, &server_thread, (void *) &tab_req[nbclient]);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf("\n----------Creation of server thread ------------\n");
}
nbclient++;
}else {
int ret = pthread_create( &tab_client[nbclient], NULL, &client_thread, (void *) &tab_req[nbclient]);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf("\n----------Creation of client thread ------------\n");
}
nbclient++;
}
}
2016-11-04 16:51:17 +01:00
}*/ //i == listener
2016-10-28 14:24:15 +02:00
} //if FDISSET
}//boucle for
if (strncmp ("exit", buf, 4) == 0) {
break;
2016-09-26 14:15:56 +02:00
}
2016-10-28 14:24:15 +02:00
}//boucle while
2016-10-28 13:58:04 +02:00
if (pthread_cancel(pid_s) != 0) {
printf("Aucun thread correspond \n");
}
pthread_join(pid_s, NULL);
2016-10-28 14:24:15 +02:00
2016-10-28 13:58:04 +02:00
/*for (i = 0; i < nbclient; i++) {
pthread_join(tab_client[i], NULL);
}*/
for (i = 0; i < nbclient; i++) {
free(tab_req[i].p);
}
free(buf);
close(sfd);
2016-09-29 19:02:05 +02:00
}
2016-09-26 14:15:56 +02:00
2016-09-29 19:02:05 +02:00
int main(int argc, char * argv[], char **env) {
struct service srv;
srv_init (argc, argv, env, &srv, SERVICE_TCP, NULL);
printf ("Listening on %s.\n", srv.spath);
// creates the service named pipe, that listens to client applications
int ret;
if ((ret = srv_create (&srv))) {
fprintf(stdout, "error service_create %d\n", ret);
exit (1);
2016-09-26 14:15:56 +02:00
}
2016-09-29 19:02:05 +02:00
printf("MAIN: server created\n" );
2016-09-26 14:15:56 +02:00
2016-09-29 19:02:05 +02:00
// the service will loop until the end of time, a specific message, a signal
main_loop (&srv);
// the application will shut down, and remove the service named pipe
if ((ret = srv_close (&srv))) {
fprintf(stdout, "error service_close %d\n", ret);
exit (1);
}
2016-09-26 14:15:56 +02:00
return 0;
2016-09-22 18:38:24 +02:00
}