From 0682b592e477e2e9df0016f378eb1a840ffa33c9 Mon Sep 17 00:00:00 2001 From: stormrider1982 Date: Mon, 23 Feb 2004 21:30:30 +0000 Subject: [PATCH] le queueInit qui marche? --- src/ids.c | 16 ++++++++++++++ src/ids.h | 4 ++++ src/queueInit.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/src/ids.c b/src/ids.c index 403a13a..3582789 100644 --- a/src/ids.c +++ b/src/ids.c @@ -102,5 +102,21 @@ int msgQueueSemIdIntern( return 0; } +int msgQueueIdIntern( + msgQueueId dest, + msgSpaceId externId, + int queueIdx){ + if (strlen(externId)>MSGSPACE_ID_LEN){ + return -1; + } +#ifdef _NZG_REALFILEID + sprintf(dest,"/tmp/nzgQueue-%s-%d",(char *)externId,queueIdx); +#else + sprintf(dest,"/nzgQueue-%s-%d",(char *)externId,queueIdx); +#endif + return 0; +} + + #endif diff --git a/src/ids.h b/src/ids.h index d6c4e58..7c87d5f 100644 --- a/src/ids.h +++ b/src/ids.h @@ -12,5 +12,9 @@ int msgQueueSemIdIntern( msgQueueSemId dest, msgSpaceId externId, int queueIdx); +int msgQueueIdIntern( + msgQueueId dest, + msgSpaceId externId, + int queueIdx); #endif diff --git a/src/queueInit.c b/src/queueInit.c index e69de29..9c53ae7 100644 --- a/src/queueInit.c +++ b/src/queueInit.c @@ -0,0 +1,56 @@ +#include "libnazgul.h" +#include "ids.h" + +msgQueue * queueInit(msgSpaceId externId, int queueIdx) { + int queueFd; + msgQueue * queue; + sem_t * semaphore; + msgQueueId queueId; + + queue = NULL; + + if(msgQueueIdIntern(queueId, externId, queueIdx) < 0) { + return NULL; + } + + queueFd = shm_open(queueId, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, MSGSPACE_DEFAULT_MODE); + if(queueFd == -1) { + fprintf(stderr, "queueInit : %s initialisation failed: %s\n", queueId, strerror(errno)); + return NULL; + } + + if(ftruncate(queueFd, sizeof(* queue)) == -1) { + fprintf( stderr, "Queue resizing failed: %s\n", + strerror( errno ) ); + return NULL; + } + + + /* on remplit la structure msgQueue */ + queue->elemCounter = 0; + //queue->head = NULL; + //queue->tail = NULL; + + + + // creation du semaphore + semaphore = sem_open(queueId, O_CREAT|O_EXCL, SEM_DEFAULT_MODE, 1); + if(semaphore == SEM_FAILED) { + NZG_ERROR("sem_open", queueId); + goto ERROR; + } + +if(sem_wait(semaphore)==-1){ + NZG_ERROR("sem_wait",queueId); + goto ERROR; + } + + /* on ferme tout ce qu'il faut */ + sem_close(semaphore); + close(queueFd); + + return queue; +ERROR: + return NULL; + +}