à revoir beaucoup beacoup beacoup
This commit is contained in:
parent
5db548c78e
commit
7678a8ab7a
@ -4,7 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <cbor.h>
|
//#include <cbor.h>
|
||||||
|
|
||||||
#include "process.h"
|
#include "process.h"
|
||||||
#include <unistd.h> // unlink
|
#include <unistd.h> // unlink
|
||||||
|
@ -6,11 +6,12 @@ EXEC=$(basename $(wildcard *.c))
|
|||||||
SOURCES=$(wildcard ../lib/communication.c ../lib/process.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))
|
||||||
|
LCBOR=-lbor
|
||||||
|
|
||||||
all: $(SOURCES) $(EXEC)
|
all: $(SOURCES) $(EXEC)
|
||||||
|
|
||||||
$(EXEC): $(OBJECTS) $(CFILES)
|
$(EXEC): $(OBJECTS) $(CFILES)
|
||||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
|
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
$(CC) -c $(CFLAGS) $< -o $@
|
$(CC) -c $(CFLAGS) $< -o $@
|
||||||
|
@ -48,7 +48,7 @@ void * pongd_thread(void * pdata) {
|
|||||||
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(*sockclient);
|
||||||
free(buf);
|
free(buf);
|
||||||
break;
|
break;
|
||||||
}else {
|
}else {
|
||||||
@ -147,21 +147,52 @@ void main_loop (struct service *srv)
|
|||||||
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;
|
||||||
}
|
//}
|
||||||
}
|
nbytes = file_read (newfd, &buf);
|
||||||
|
if ( nbytes == -1) {
|
||||||
|
handle_error("file_read");
|
||||||
} else {
|
} else {
|
||||||
|
buf[BUFSIZ - 1] = '\0';
|
||||||
|
printf ("msg received (%d) : %s\n", nbytes, buf);
|
||||||
|
if (strncmp ("exit", buf, 4) == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -1 : error, 0 = no new process, 1 = new process
|
||||||
|
ret = srv_get_new_process (buf, &tab_proc[cnt]);
|
||||||
|
|
||||||
|
if (ret == -1) {
|
||||||
|
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
srv_process_print (&tab_proc[cnt]);
|
||||||
|
|
||||||
|
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &newfd);
|
||||||
|
if (ret) {
|
||||||
|
perror("pthread_create()");
|
||||||
|
exit(errno);
|
||||||
|
} else {
|
||||||
|
printf ("\n-------New thread created---------\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf ("%d applications to serve\n",cnt);
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*else {
|
||||||
nbytes = file_read (i, &buf);
|
nbytes = file_read (i, &buf);
|
||||||
if ( nbytes == -1) {
|
if ( nbytes == -1) {
|
||||||
handle_error("file_read");
|
handle_error("file_read");
|
||||||
} else if( nbytes == 0) {
|
} else if( nbytes == 0) {*/
|
||||||
/* close it... */
|
/* close it... */
|
||||||
close(i);
|
//close(i);
|
||||||
/* remove from master set */
|
/* remove from master set */
|
||||||
FD_CLR(i, &master);
|
/*FD_CLR(i, &master);
|
||||||
}else {
|
}else {
|
||||||
buf[BUFSIZ - 1] = '\0';
|
buf[BUFSIZ - 1] = '\0';
|
||||||
printf ("msg received (%d) : %s\n", nbytes, buf);
|
printf ("msg received (%d) : %s\n", nbytes, buf);
|
||||||
@ -189,7 +220,7 @@ void main_loop (struct service *srv)
|
|||||||
|
|
||||||
printf ("%d applications to serve\n",cnt);
|
printf ("%d applications to serve\n",cnt);
|
||||||
cnt++;
|
cnt++;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -207,6 +238,7 @@ void main_loop (struct service *srv)
|
|||||||
close(sfd);
|
close(sfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* service ping-pong
|
* service ping-pong
|
||||||
*
|
*
|
||||||
|
@ -6,11 +6,12 @@ EXEC=$(basename $(wildcard *.c))
|
|||||||
SOURCES=$(wildcard ../lib/communication.c ../lib/process.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))
|
||||||
|
LCBOR=-lbor
|
||||||
|
|
||||||
all: $(SOURCES) $(EXEC)
|
all: $(SOURCES) $(EXEC)
|
||||||
|
|
||||||
$(EXEC): $(OBJECTS) $(CFILES)
|
$(EXEC): $(OBJECTS) $(CFILES)
|
||||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
|
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
$(CC) -c $(CFLAGS) $< -o $@
|
$(CC) -c $(CFLAGS) $< -o $@
|
||||||
|
@ -511,7 +511,7 @@ void * client_thread(void *reqq) {
|
|||||||
/*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 from server(%d bytes) : %s\n", n, buffer);
|
||||||
if(app_write(req->p, buffer, strlen(buffer)) < 0) {
|
if(app_write(req->p, buffer, strlen(buffer)) < 0) {
|
||||||
perror("file_write");
|
perror("file_write");
|
||||||
}
|
}
|
||||||
@ -526,7 +526,7 @@ void * client_thread(void *reqq) {
|
|||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
nbytes = app_read (req->p, &buffer);
|
nbytes = app_read (req->p, &buffer);
|
||||||
printf("Client : size message %d : %s\n",nbytes, buffer );
|
printf("Client : message from app %d : %s\n",nbytes, buffer );
|
||||||
if ( nbytes == -1) {
|
if ( nbytes == -1) {
|
||||||
handle_error("file_read");
|
handle_error("file_read");
|
||||||
} else if( nbytes == 0) {
|
} else if( nbytes == 0) {
|
||||||
@ -640,14 +640,13 @@ void main_loop (struct service *srv) {
|
|||||||
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;
|
||||||
}
|
//}
|
||||||
}
|
|
||||||
} else {
|
nbytes = file_read (newfd, &buf);
|
||||||
nbytes = file_read (i, &buf);
|
|
||||||
if ( nbytes == -1) {
|
if ( nbytes == -1) {
|
||||||
handle_error("file_read");
|
handle_error("file_read");
|
||||||
} else if( nbytes == 0) {
|
} else if( nbytes == 0) {
|
||||||
@ -662,6 +661,57 @@ void main_loop (struct service *srv) {
|
|||||||
break;
|
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++;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} /*else {
|
||||||
|
nbytes = file_read (i, &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_server", buf, 4) == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
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]);
|
||||||
@ -696,7 +746,7 @@ void main_loop (struct service *srv) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} //i == listener
|
}*/ //i == listener
|
||||||
} //if FDISSET
|
} //if FDISSET
|
||||||
}//boucle for
|
}//boucle for
|
||||||
if (strncmp ("exit", buf, 4) == 0) {
|
if (strncmp ("exit", buf, 4) == 0) {
|
||||||
|
@ -37,12 +37,14 @@ int main(int argc, char * argv[]) {
|
|||||||
perror("connect()");
|
perror("connect()");
|
||||||
exit(errno);
|
exit(errno);
|
||||||
}
|
}
|
||||||
|
//printf("connected...\n");
|
||||||
file_write(sfd, srv_message, strlen(srv_message));
|
file_write(sfd, srv_message, strlen(srv_message));
|
||||||
file_write(sfd, proc_message, strlen(srv_message));
|
//printf("%s\n", proc_message);
|
||||||
|
//sleep(1);
|
||||||
|
file_write(sfd, proc_message, strlen(proc_message));
|
||||||
file_read(sfd, &buffer);
|
file_read(sfd, &buffer);
|
||||||
printf("%s\n", buffer);
|
printf("%s\n", buffer);
|
||||||
|
//sleep(1);
|
||||||
file_write(sfd, "exit", 4);
|
file_write(sfd, "exit", 4);
|
||||||
|
|
||||||
close(sfd);
|
close(sfd);
|
||||||
|
Reference in New Issue
Block a user