fix some issues thanks to valgrind but it isn't perfect yet, need more work on it
This commit is contained in:
parent
4fda0231ce
commit
f3f7927d25
@ -22,7 +22,7 @@ void * pongd_thread(void * pdata) {
|
|||||||
printf ("read, size %ld : %s\n", msize, buf);
|
printf ("read, size %ld : %s\n", msize, buf);
|
||||||
|
|
||||||
if(msize > 0) {
|
if(msize > 0) {
|
||||||
printf ("before proc write\n");
|
//printf ("before proc write\n");
|
||||||
if ((ret = srv_write (proc, buf, msize)) == -1) {
|
if ((ret = srv_write (proc, buf, msize)) == -1) {
|
||||||
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", ret);
|
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", ret);
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,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
|
||||||
|
|
||||||
int init_connection(void)
|
int init_connection(void)
|
||||||
{
|
{
|
||||||
@ -84,12 +85,12 @@ void printClientAddr(struct sockaddr_in *csin) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void * service_thread(void * pdata) {
|
void * service_thread(void * c_data) {
|
||||||
p_data *pda = (p_data*) pdata;
|
client_data *cda = (client_data*) c_data;
|
||||||
char buffer[BUF_SIZE];
|
char buffer[BUF_SIZE];
|
||||||
char *service;
|
char *service;
|
||||||
int version;
|
int version;
|
||||||
int clientSock = pda->sfd;
|
int clientSock = cda->sfd;
|
||||||
|
|
||||||
if (read_message(clientSock, buffer) == -1) {
|
if (read_message(clientSock, buffer) == -1) {
|
||||||
perror("read_message()");
|
perror("read_message()");
|
||||||
@ -104,13 +105,19 @@ void * service_thread(void * pdata) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
//path service
|
//path service
|
||||||
char * servicePath = malloc(sizeof PATH_MAX);
|
char * servicePath = (char*) calloc((strlen(TMPDIR) + strlen(service) + 1), sizeof(char));
|
||||||
|
if (servicePath == NULL) {
|
||||||
|
perror("calloc()");
|
||||||
|
}
|
||||||
strcat(servicePath, TMPDIR);
|
strcat(servicePath, TMPDIR);
|
||||||
strcat(servicePath, service);
|
strcat(servicePath, service);
|
||||||
|
|
||||||
//pid index version
|
//pid index version
|
||||||
char * piv = malloc(sizeof PATH_MAX);
|
char * piv = (char*) calloc(PATH_MAX, sizeof(char));
|
||||||
makePivMessage(&piv, getpid(), pda->index, version);
|
if (piv == NULL) {
|
||||||
|
perror("calloc()");
|
||||||
|
}
|
||||||
|
makePivMessage(&piv, getpid(), cda->index, version);
|
||||||
printf("piv : %s\n",piv );
|
printf("piv : %s\n",piv );
|
||||||
|
|
||||||
//write pid index version in T/I/S of service
|
//write pid index version in T/I/S of service
|
||||||
@ -124,10 +131,15 @@ void * service_thread(void * pdata) {
|
|||||||
|
|
||||||
// gets the service path, such as /tmp/ipc/pid-index-version-in/out
|
// gets the service path, such as /tmp/ipc/pid-index-version-in/out
|
||||||
char *pathname[2];
|
char *pathname[2];
|
||||||
inOutPathCreate(pathname, pda->index, version);
|
pathname[0] = (char*) calloc(PATH_MAX, sizeof(char));
|
||||||
|
if (pathname[0] == NULL) {
|
||||||
// printf("pathname 1: %s\n",pathname[0] );
|
perror("calloc()");
|
||||||
// printf("pathname 2: %s\n",pathname[1] );
|
}
|
||||||
|
pathname[1] = (char*) calloc(PATH_MAX, sizeof(char));
|
||||||
|
if (pathname[1] == NULL) {
|
||||||
|
perror("calloc()");
|
||||||
|
}
|
||||||
|
inOutPathCreate(pathname, cda->index, version);
|
||||||
|
|
||||||
//create in out files
|
//create in out files
|
||||||
if(fifo_create(pathname[0]) != 0) {
|
if(fifo_create(pathname[0]) != 0) {
|
||||||
@ -175,18 +187,18 @@ void * service_thread(void * pdata) {
|
|||||||
if(FD_ISSET(STDIN_FILENO, &rdfs))
|
if(FD_ISSET(STDIN_FILENO, &rdfs))
|
||||||
{
|
{
|
||||||
/* stop process when type on keyboard */
|
/* stop process when type on keyboard */
|
||||||
|
printf("thread %d shutdown\n", cda->index );
|
||||||
break;
|
break;
|
||||||
}else /*if (FD_ISSET(fdin, &rdfs))*/{
|
}else /*if (FD_ISSET(fdin, &rdfs))*/{
|
||||||
if (FD_ISSET(clientSock, &rdfs)) {
|
if (FD_ISSET(clientSock, &rdfs)) {
|
||||||
if(read_message(clientSock, buffer) > 0) {
|
if(read_message(clientSock, buffer) > 0) {
|
||||||
printf("message : %s\n",buffer );
|
printf("message : %s\n",buffer );
|
||||||
}
|
|
||||||
|
|
||||||
if(file_write(pathname[1], buffer, strlen(buffer)) < 0) {
|
if(file_write(pathname[1], buffer, strlen(buffer)) < 0) {
|
||||||
perror("file_write");
|
perror("file_write");
|
||||||
}
|
}
|
||||||
printf("ok\n");
|
printf("ok\n");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (FD_ISSET(fdin, &rdfs)) {
|
if (FD_ISSET(fdin, &rdfs)) {
|
||||||
|
|
||||||
@ -197,8 +209,14 @@ void * service_thread(void * pdata) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//free
|
||||||
free(servicePath);
|
free(servicePath);
|
||||||
free(piv);
|
free(piv);
|
||||||
|
free(service);
|
||||||
|
free(pathname[0]);
|
||||||
|
free(pathname[1]);
|
||||||
|
|
||||||
|
|
||||||
//close the files descriptors
|
//close the files descriptors
|
||||||
close(fdin);
|
close(fdin);
|
||||||
@ -224,6 +242,7 @@ void parseServiceVersion(char * buf, char ** service, int *version) {
|
|||||||
*version = strtoul(token, NULL, 10);
|
*version = strtoul(token, NULL, 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int fifo_create (char * path)
|
int fifo_create (char * path)
|
||||||
@ -262,9 +281,6 @@ int fifo_create (char * path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void inOutPathCreate(char ** pathname, int index, int version) {
|
void inOutPathCreate(char ** pathname, int index, int version) {
|
||||||
pathname[0] = malloc(sizeof PATH_MAX);
|
|
||||||
pathname[1] = malloc(sizeof PATH_MAX);
|
|
||||||
|
|
||||||
int length = snprintf( NULL, 0, "%d", version );
|
int length = snprintf( NULL, 0, "%d", version );
|
||||||
char* versionStr = malloc( length + 2 );
|
char* versionStr = malloc( length + 2 );
|
||||||
snprintf( versionStr, length + 1, "%d", version );
|
snprintf( versionStr, length + 1, "%d", version );
|
||||||
@ -292,6 +308,10 @@ void inOutPathCreate(char ** pathname, int index, int version) {
|
|||||||
strcat(pathname[1], "-");
|
strcat(pathname[1], "-");
|
||||||
strcat(pathname[1], versionStr);
|
strcat(pathname[1], versionStr);
|
||||||
strcat(pathname[1], "-out");
|
strcat(pathname[1], "-out");
|
||||||
|
|
||||||
|
free(pidprocess);
|
||||||
|
free(indexStr);
|
||||||
|
free(versionStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void makePivMessage (char ** piv, int pid, int index, int version) {
|
void makePivMessage (char ** piv, int pid, int index, int version) {
|
||||||
@ -312,42 +332,82 @@ void makePivMessage (char ** piv, int pid, int index, int version) {
|
|||||||
strcat(*piv, indexStr);
|
strcat(*piv, indexStr);
|
||||||
strcat(*piv, " ");
|
strcat(*piv, " ");
|
||||||
strcat(*piv, versionStr);
|
strcat(*piv, versionStr);
|
||||||
|
|
||||||
|
free(pidprocess);
|
||||||
|
free(indexStr);
|
||||||
|
free(versionStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char * argv[], char **env) {
|
int main(int argc, char * argv[], char **env) {
|
||||||
|
//client
|
||||||
|
client_data tab_client[NBCLIENT];
|
||||||
|
pthread_t tab_service_threads[NBCLIENT];
|
||||||
|
int actual = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
int sock = init_connection();
|
int sock = init_connection();
|
||||||
// char buffer[BUF_SIZE];
|
fd_set rdfs;
|
||||||
|
int max = sock;
|
||||||
|
|
||||||
struct sockaddr_in csin = { 0 };
|
printf("Waitting for new clients :\n" );
|
||||||
socklen_t sinsize = sizeof csin;
|
while(1) {
|
||||||
int sfd = accept(sock, (struct sockaddr *)&csin, &sinsize);
|
FD_ZERO(&rdfs);
|
||||||
if(sfd == -1)
|
|
||||||
|
/* add STDIN_FILENO */
|
||||||
|
FD_SET(STDIN_FILENO, &rdfs);
|
||||||
|
|
||||||
|
//add client's socket
|
||||||
|
FD_SET(sock, &rdfs);
|
||||||
|
|
||||||
|
if(select(max + 1, &rdfs, NULL, NULL, NULL) == -1)
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
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()");
|
perror("accept()");
|
||||||
close(sock);
|
close(sock);
|
||||||
exit(errno);
|
exit(errno);
|
||||||
}
|
}
|
||||||
|
printClientAddr(&tab_client[actual].c_addr);
|
||||||
|
|
||||||
printClientAddr(&csin);
|
tab_client[actual].index = actual;
|
||||||
printf("%d \n",getpid());
|
|
||||||
|
|
||||||
pthread_t listenPid;
|
int ret = pthread_create( &tab_service_threads[actual], NULL, &service_thread, (void *) &tab_client[actual]);
|
||||||
p_data *p_d_listen = malloc (sizeof(p_data));
|
|
||||||
//p_d_listen.c_sock = NULL;
|
|
||||||
p_d_listen->sfd = sfd;
|
|
||||||
p_d_listen->index = 0;
|
|
||||||
|
|
||||||
int ret = pthread_create( &listenPid, NULL, &service_thread, (void *) p_d_listen);
|
|
||||||
if (ret) {
|
if (ret) {
|
||||||
perror("pthread_create()");
|
perror("pthread_create()");
|
||||||
endConnection(sock);
|
endConnection(sock);
|
||||||
exit(errno);
|
exit(errno);
|
||||||
} else {
|
} else {
|
||||||
printf("Creation of listen thread \n");
|
printf("Creation of listen thread %d\n", actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_join(listenPid, NULL);
|
max = tab_client[actual].sfd > max ? tab_client[actual].sfd : max;
|
||||||
|
actual++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < actual; i++) {
|
||||||
|
pthread_join(tab_service_threads[i], NULL);
|
||||||
|
}
|
||||||
|
|
||||||
endConnection(sock);
|
endConnection(sock);
|
||||||
|
|
||||||
|
@ -7,10 +7,10 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
struct sockaddr_in c_sock;
|
struct sockaddr_in c_addr;
|
||||||
int sfd;
|
int sfd;
|
||||||
int index;
|
int index;
|
||||||
} p_data;
|
} client_data;
|
||||||
|
|
||||||
int initConnection ();
|
int initConnection ();
|
||||||
void endConnection (int sock);
|
void endConnection (int sock);
|
||||||
|
Reference in New Issue
Block a user