Apply indent with -linux style

This commit is contained in:
Glenn Y. Rolland 2020-03-04 00:06:43 +01:00
parent e2692e8547
commit c5f4cd8345
64 changed files with 2552 additions and 2232 deletions

View file

@ -9,38 +9,33 @@
/* TODO: prevoir le cas des négatifs dans la taille ... */ /* TODO: prevoir le cas des négatifs dans la taille ... */
void * msgAllocate(msgSpace *space, void *msgAllocate(msgSpace * space, int pool, int taille, int option)
int pool, {
int taille, void *resultAddr = NULL;
int option int bufferFreeSize;
){ int i;
void * resultAddr=NULL; msgPoolId resultPoolId;
int bufferFreeSize; /* tableau des valeurs des semPoolCoef/pool pour identifier le pool
int i; * qui sera libéré le plus rapidement */
msgPoolId resultPoolId; float semPoolCoef[space->poolNb];
/* tableau des valeurs des semPoolCoef/pool pour identifier le pool int idxPoolOptimum;
* qui sera libéré le plus rapidement */ bool gotRessourceSem;
float semPoolCoef[space->poolNb]; msgPoolDataTabSemId ressourceSemId;
int idxPoolOptimum; sem_t *ressourceSemFd;
bool gotRessourceSem; int ressourceSemVal;
msgPoolDataTabSemId ressourceSemId;
sem_t * ressourceSemFd;
int ressourceSemVal;
int nbLockedSem; int nbLockedSem;
float minPoolCoef; float minPoolCoef;
int selectedPoolIndex; int selectedPoolIndex;
int bufferFreeIndex; int bufferFreeIndex;
msgPoolData * mSPoolDataTabAddr; msgPoolData *mSPoolDataTabAddr;
selectedPoolIndex=-1; selectedPoolIndex = -1;
mSPoolDataTabAddr = msgPoolDataTabOpen(space);
mSPoolDataTabAddr=msgPoolDataTabOpen(space); if (mSPoolDataTabAddr == NULL) {
if (mSPoolDataTabAddr==NULL){ NZG_ERROR("msgPoolDataTabOpen", space->poolDataTabId);
NZG_ERROR("msgPoolDataTabOpen",space->poolDataTabId); goto ERROR;
goto ERROR; }
}
/* /*
// verifier le premier arg du shm_open // verifier le premier arg du shm_open
mSPoolDataTabFd=shm_open(space->poolDataTabId, mSPoolDataTabFd=shm_open(space->poolDataTabId,
@ -64,152 +59,167 @@ if (mSPoolDataTabAddr==NULL){
return NULL; return NULL;
} }
*/ */
gotRessourceSem=false; gotRessourceSem = false;
/* initialisation des coefs */ /* initialisation des coefs */
for (i=0;i<(space->poolNb);i++){ for (i = 0; i < (space->poolNb); i++) {
semPoolCoef[i]=-1; semPoolCoef[i] = -1;
}
nbLockedSem=0;
if ( pool == ANYPOOL){
fprintf(stderr,"[ ALLOCATION ANYPOOL : %d ]\n",(int)getpid());
// choisir le pool au hasard (ou presque)
for(i=0; i<(space->poolNb); i++) {
printf("- boucle %d\n",i); fflush(stdout);
if(mSPoolDataTabAddr[i].bufferSize >= taille) {
printf("( buffSize > taille )\n"); fflush(stdout);
/* choisir le numero du semaphore
en fonction du nombre de lock poses / nombre de buffer */
msgPoolSemIdIntern(ressourceSemId,space->id,i);
ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
if (ressourceSemFd == SEM_FAILED){
NZG_ERROR("sem_open",ressourceSemId);
return NULL;
}
/* on remplit le tableau avec les valeurs des semaphores */
if (sem_getvalue(ressourceSemFd, &ressourceSemVal) < 0){
NZG_ERROR("sem_getvalue",ressourceSemId);
return NULL;
}
printf("RESSOURCESEMVAL %d\n",ressourceSemVal);
if (ressourceSemVal <= 0){
printf("resVal < 0 : %d\n",ressourceSemVal);
/* il y a ressourceSemVal processus qui attendent déja... */
semPoolCoef[nbLockedSem] =
(float) ((1-ressourceSemVal) / mSPoolDataTabAddr[i].bufferNb);
nbLockedSem++;
}
if(sem_trywait(ressourceSemFd)==0) {
printf("got try_wait\n");
/* choisir la 1ere pool de taille plus grande
* libre si possible */
gotRessourceSem=true;
selectedPoolIndex=i;
break;
}
if( sem_close(ressourceSemFd) <0){
NZG_ERROR("sem_getvalue",ressourceSemId);
return NULL;
}
} // if buffSize > taille
} // for
printf("ERRORDETECT outFor\n"); fflush(stdout);
if (!gotRessourceSem) {
printf("Calcul du meilleur en cas de liberation\n");
minPoolCoef= semPoolCoef[0];
idxPoolOptimum = 0;
/* on cherche le pool avec le moins de lock poses / nbre de buffer
* le numéro du pool est stocké dans idxPoolOptimum */
for(i=0; i<nbLockedSem; i++) {
printf("Coef %d : %d\n",i,(int)semPoolCoef[i]);
if ((semPoolCoef[i] != -1)
&& (semPoolCoef[i] < minPoolCoef)) {
minPoolCoef = semPoolCoef[i];
idxPoolOptimum = i;
}
}
if (minPoolCoef == -1){
/* il n'y a aucune pool dont la taille satisfait la demande */
return NULL;
} else {
selectedPoolIndex=idxPoolOptimum;
}
printf("Optimum : %d\n",selectedPoolIndex);
}
}else {
fprintf(stderr,"[ ALLOCATION : %d ]\n",(int)getpid());
selectedPoolIndex=pool;
}
if (!gotRessourceSem){
msgPoolSemIdIntern(ressourceSemId,space->externId,selectedPoolIndex);
ressourceSemFd=sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0);
if(ressourceSemFd==SEM_FAILED){
NZG_ERROR("sem_open",ressourceSemId);
return NULL;
} }
//TODO:virer la ligne suivante: nbLockedSem = 0;
sem_getvalue(ressourceSemFd, &ressourceSemVal); if (pool == ANYPOOL) {
fprintf(stderr, "[ ALLOCATION ANYPOOL : %d ]\n", (int)getpid());
printf("SemWait... %s : %d\n",ressourceSemId,ressourceSemVal); // choisir le pool au hasard (ou presque)
if (sem_wait(ressourceSemFd) < 0){ for (i = 0; i < (space->poolNb); i++) {
NZG_ERROR("sem_wait",ressourceSemId); printf("- boucle %d\n", i);
sem_close(ressourceSemFd); fflush(stdout);
return NULL; if (mSPoolDataTabAddr[i].bufferSize >= taille) {
} printf("( buffSize > taille )\n");
printf("Passed %s\n",ressourceSemId); fflush(stdout);
} /* choisir le numero du semaphore
en fonction du nombre de lock poses / nombre de buffer */
msgPoolSemIdIntern(ressourceSemId, space->id,
i);
ressourceSemFd =
sem_open(ressourceSemId, O_CREAT,
SEM_DEFAULT_MODE, 0);
if (ressourceSemFd == SEM_FAILED) {
NZG_ERROR("sem_open", ressourceSemId);
return NULL;
}
/* on remplit le tableau avec les valeurs des semaphores */
if (sem_getvalue
/* on a acqui un semaphore pour la ressouce */ (ressourceSemFd, &ressourceSemVal) < 0) {
/* on acquiert le droit de modifier les infos sur la ressource */ NZG_ERROR("sem_getvalue",
/* on protege le tableau des associations */ ressourceSemId);
msgPoolDataTabLock(space); return NULL;
}
/* on modifie maintenant les données */ printf("RESSOURCESEMVAL %d\n", ressourceSemVal);
/* - on récupere l'index du premier buffer libre */ if (ressourceSemVal <= 0) {
bufferFreeIndex = msgBufferGetFreeIndex(mSPoolDataTabAddr,selectedPoolIndex); printf("resVal < 0 : %d\n",
if (bufferFreeIndex < 0){ ressourceSemVal);
// aucun buffer libre ? /* il y a ressourceSemVal processus qui attendent déja... */
NZG_ERROR("msgBufferGetFreeIndex",""); semPoolCoef[nbLockedSem] =
goto ERROR; (float)((1 -
} ressourceSemVal) /
printf("Buffer selected : %d,%d\n",selectedPoolIndex,bufferFreeIndex); mSPoolDataTabAddr[i].
/* mapper le buffer libre dans l'esp addr du proc */ bufferNb);
strcpy(resultPoolId,mSPoolDataTabAddr[selectedPoolIndex].poolId); nbLockedSem++;
}
if (sem_trywait(ressourceSemFd) == 0) {
printf("got try_wait\n");
/* choisir la 1ere pool de taille plus grande
* libre si possible */
gotRessourceSem = true;
selectedPoolIndex = i;
break;
}
if (sem_close(ressourceSemFd) < 0) {
NZG_ERROR("sem_getvalue",
ressourceSemId);
return NULL;
}
} // if buffSize > taille
} // for
bufferFreeSize=mSPoolDataTabAddr[selectedPoolIndex].bufferSize; printf("ERRORDETECT outFor\n");
printf("BufferSize : %d\n", bufferFreeSize); fflush(stdout);
resultAddr=msgBufferMap(mSPoolDataTabAddr,selectedPoolIndex,bufferFreeIndex);
if (resultAddr==NULL){
NZG_ERROR("msgBufferMap",mSPoolDataTabAddr[selectedPoolIndex].poolId);
goto ERROR;
}
/* - on s'enregistre aupres de ce buffer */ if (!gotRessourceSem) {
msgBufferAttachProc(mSPoolDataTabAddr, printf("Calcul du meilleur en cas de liberation\n");
selectedPoolIndex, minPoolCoef = semPoolCoef[0];
bufferFreeIndex, idxPoolOptimum = 0;
resultAddr); /* on cherche le pool avec le moins de lock poses / nbre de buffer
* le numéro du pool est stocké dans idxPoolOptimum */
for (i = 0; i < nbLockedSem; i++) {
printf("Coef %d : %d\n", i,
(int)semPoolCoef[i]);
if ((semPoolCoef[i] != -1)
&& (semPoolCoef[i] < minPoolCoef)) {
minPoolCoef = semPoolCoef[i];
idxPoolOptimum = i;
}
}
// munmap(mSPoolDataTabAddr,(space->poolNb) * sizeof( msgPoolData )); if (minPoolCoef == -1) {
/* unmapper le msgPoolDataTab */ /* il n'y a aucune pool dont la taille satisfait la demande */
msgPoolDataTabClose(space,mSPoolDataTabAddr); return NULL;
msgPoolDataTabUnlock(space); } else {
selectedPoolIndex = idxPoolOptimum;
}
printf("Optimum : %d\n", selectedPoolIndex);
}
} else {
fprintf(stderr, "[ ALLOCATION : %d ]\n", (int)getpid());
selectedPoolIndex = pool;
}
printf( "alloc de %p\n", (void *)resultAddr); if (!gotRessourceSem) {
return resultAddr; msgPoolSemIdIntern(ressourceSemId, space->externId,
selectedPoolIndex);
ressourceSemFd =
sem_open(ressourceSemId, O_CREAT, SEM_DEFAULT_MODE, 0);
if (ressourceSemFd == SEM_FAILED) {
NZG_ERROR("sem_open", ressourceSemId);
return NULL;
}
//TODO:virer la ligne suivante:
sem_getvalue(ressourceSemFd, &ressourceSemVal);
ERROR: printf("SemWait... %s : %d\n", ressourceSemId, ressourceSemVal);
NZG_ERROR("msgAllocate","error processing"); if (sem_wait(ressourceSemFd) < 0) {
msgPoolDataTabUnlock(space); NZG_ERROR("sem_wait", ressourceSemId);
munmap(mSPoolDataTabAddr,(space->poolNb) * sizeof( msgPoolData )); sem_close(ressourceSemFd);
return NULL; return NULL;
}
printf("Passed %s\n", ressourceSemId);
}
/* on a acqui un semaphore pour la ressouce */
/* on acquiert le droit de modifier les infos sur la ressource */
/* on protege le tableau des associations */
msgPoolDataTabLock(space);
/* on modifie maintenant les données */
/* - on récupere l'index du premier buffer libre */
bufferFreeIndex =
msgBufferGetFreeIndex(mSPoolDataTabAddr, selectedPoolIndex);
if (bufferFreeIndex < 0) {
// aucun buffer libre ?
NZG_ERROR("msgBufferGetFreeIndex", "");
goto ERROR;
}
printf("Buffer selected : %d,%d\n", selectedPoolIndex, bufferFreeIndex);
/* mapper le buffer libre dans l'esp addr du proc */
strcpy(resultPoolId, mSPoolDataTabAddr[selectedPoolIndex].poolId);
bufferFreeSize = mSPoolDataTabAddr[selectedPoolIndex].bufferSize;
printf("BufferSize : %d\n", bufferFreeSize);
resultAddr =
msgBufferMap(mSPoolDataTabAddr, selectedPoolIndex, bufferFreeIndex);
if (resultAddr == NULL) {
NZG_ERROR("msgBufferMap",
mSPoolDataTabAddr[selectedPoolIndex].poolId);
goto ERROR;
}
/* - on s'enregistre aupres de ce buffer */
msgBufferAttachProc(mSPoolDataTabAddr,
selectedPoolIndex, bufferFreeIndex, resultAddr);
// munmap(mSPoolDataTabAddr,(space->poolNb) * sizeof( msgPoolData ));
/* unmapper le msgPoolDataTab */
msgPoolDataTabClose(space, mSPoolDataTabAddr);
msgPoolDataTabUnlock(space);
printf("alloc de %p\n", (void *)resultAddr);
return resultAddr;
ERROR:
NZG_ERROR("msgAllocate", "error processing");
msgPoolDataTabUnlock(space);
munmap(mSPoolDataTabAddr, (space->poolNb) * sizeof(msgPoolData));
return NULL;
} }

View file

@ -1,15 +1,11 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgBufferGetAttachedProcIndex( int msgBufferGetAttachedProcIndex(msgPoolData * poolDataTabAddr,
msgPoolData * poolDataTabAddr, int poolIndex, void *addr)
int poolIndex, {
void * addr
){
//TODO: parcourrir tous les index, et regarder s'il y //TODO: parcourrir tous les index, et regarder s'il y
//a une addresse qui correspond avec le meme processus... //a une addresse qui correspond avec le meme processus...
//et renvoyer l'index du buffer correspondant a l'addresse... //et renvoyer l'index du buffer correspondant a l'addresse...
return 0; return 0;
} }

View file

@ -1,41 +1,36 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgBufferAttachProc( int msgBufferAttachProc(msgPoolData * poolDataTabAddr,
msgPoolData * poolDataTabAddr, int poolIndex, int bufferIndex, void *addr)
int poolIndex,
int bufferIndex,
void * addr)
{ {
msgBufferInfoTabId bufferInfoTabId; msgBufferInfoTabId bufferInfoTabId;
int bufferInfoTabFd; int bufferInfoTabFd;
msgBufferInfo * bufferInfoTabAddr; msgBufferInfo *bufferInfoTabAddr;
int bufferInfoNb; int bufferInfoNb;
//récuperer l'ID du BufferInfoTab; //récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId); strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
bufferInfoTabFd = shm_open(bufferInfoTabId, O_RDWR, SHM_DEFAULT_MODE);
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE); if (bufferInfoTabFd < 0) {
if (bufferInfoTabFd<0){ NZG_ERROR("sem_open", bufferInfoTabId);
NZG_ERROR("sem_open",bufferInfoTabId); return -1;
return -1; }
}
/** on regarde dans le tableau d'infos de buffer **/ /** on regarde dans le tableau d'infos de buffer **/
bufferInfoNb=poolDataTabAddr[poolIndex].bufferNb; bufferInfoNb = poolDataTabAddr[poolIndex].bufferNb;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo), bufferInfoTabAddr = mmap(NULL, bufferInfoNb * sizeof(msgBufferInfo),
PROT_READ|PROT_WRITE,MAP_SHARED,bufferInfoTabFd,(off_t)0); PROT_READ | PROT_WRITE, MAP_SHARED,
printf("atta gooo %s\n",bufferInfoTabId); bufferInfoTabFd, (off_t) 0);
printf("atta gooo %s\n", bufferInfoTabId);
bufferInfoTabAddr[bufferIndex].ownerPid = getpid(); bufferInfoTabAddr[bufferIndex].ownerPid = getpid();
bufferInfoTabAddr[bufferIndex].addr = addr; bufferInfoTabAddr[bufferIndex].addr = addr;
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0) if (munmap(bufferInfoTabAddr, bufferInfoNb * sizeof(msgBufferInfo)) < 0) {
{ NZG_ERROR("munmap", bufferInfoTabId);
NZG_ERROR("munmap",bufferInfoTabId); return -1;
return -1; } }
return 0; return 0;
} }

View file

@ -1,42 +1,37 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgBufferDetachProc( int msgBufferDetachProc(msgPoolData * poolDataTabAddr,
msgPoolData * poolDataTabAddr, int poolIndex, int bufferIndex, void *addr)
int poolIndex, {
int bufferIndex, msgBufferInfoTabId bufferInfoTabId;
void * addr int bufferInfoTabFd;
){ msgBufferInfo *bufferInfoTabAddr;
msgBufferInfoTabId bufferInfoTabId; int bufferInfoNb;
int bufferInfoTabFd;
msgBufferInfo * bufferInfoTabAddr;
int bufferInfoNb;
printf("Detaching %d,%d\n",poolIndex,bufferIndex); printf("Detaching %d,%d\n", poolIndex, bufferIndex);
//récuperer l'ID du BufferInfoTab; //récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId); bufferInfoTabFd = shm_open(bufferInfoTabId, O_RDWR, SHM_DEFAULT_MODE);
if (bufferInfoTabFd < 0) {
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE); NZG_ERROR("shm_open", bufferInfoTabId);
if (bufferInfoTabFd<0){ return -1;
NZG_ERROR("shm_open",bufferInfoTabId); }
return -1;
}
/** on regarde dans le tableau d'infos de buffer **/ /** on regarde dans le tableau d'infos de buffer **/
bufferInfoNb=poolDataTabAddr[poolIndex].bufferNb; bufferInfoNb = poolDataTabAddr[poolIndex].bufferNb;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo), bufferInfoTabAddr = mmap(NULL, bufferInfoNb * sizeof(msgBufferInfo),
PROT_READ|PROT_WRITE,MAP_SHARED,bufferInfoTabFd,(off_t)0); PROT_READ | PROT_WRITE, MAP_SHARED,
bufferInfoTabFd, (off_t) 0);
bufferInfoTabAddr[bufferIndex].ownerPid = (pid_t)-1; bufferInfoTabAddr[bufferIndex].ownerPid = (pid_t) - 1;
bufferInfoTabAddr[bufferIndex].addr = NULL; bufferInfoTabAddr[bufferIndex].addr = NULL;
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0) if (munmap(bufferInfoTabAddr, bufferInfoNb * sizeof(msgBufferInfo)) < 0) {
{ NZG_ERROR("munmap", bufferInfoTabId);
NZG_ERROR("munmap",bufferInfoTabId); return -1;
return -1; }
}
return 0; return 0;
} }

View file

@ -1,39 +1,43 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgBufferGetFreeIndex(msgPoolData * poolDataTabAddr,int poolIndex){ int msgBufferGetFreeIndex(msgPoolData * poolDataTabAddr, int poolIndex)
msgBufferInfoTabId bufferInfoTabId; {
int bufferInfoTabFd; msgBufferInfoTabId bufferInfoTabId;
msgBufferInfo * bufferInfoTabAddr; int bufferInfoTabFd;
int bufferInfoNb; msgBufferInfo *bufferInfoTabAddr;
int bufferFreeIndex; int bufferInfoNb;
int i; int bufferFreeIndex;
int i;
//récuperer l'ID du BufferInfoTab; //récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId); strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE); bufferInfoTabFd = shm_open(bufferInfoTabId, O_RDWR, SHM_DEFAULT_MODE);
if (bufferInfoTabFd<0){ if (bufferInfoTabFd < 0) {
NZG_ERROR("shm_open",bufferInfoTabId); NZG_ERROR("shm_open", bufferInfoTabId);
return -1; return -1;
} }
/** on regarde dans le tableau d'infos de buffer **/ /** on regarde dans le tableau d'infos de buffer **/
bufferInfoNb=poolDataTabAddr[poolIndex].bufferNb; bufferInfoNb = poolDataTabAddr[poolIndex].bufferNb;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo), bufferInfoTabAddr = mmap(NULL, bufferInfoNb * sizeof(msgBufferInfo),
PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0); PROT_READ, MAP_SHARED, bufferInfoTabFd,
(off_t) 0);
i=0; i = 0;
while ((i<bufferInfoNb) while ((i < bufferInfoNb)
&& (bufferInfoTabAddr[i].ownerPid != (pid_t)-1)){ i++; } && (bufferInfoTabAddr[i].ownerPid != (pid_t) - 1)) {
if (i == bufferInfoNb){ return -1; } i++;
bufferFreeIndex=i; }
if (i == bufferInfoNb) {
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0) return -1;
{ }
NZG_ERROR("munmap",bufferInfoTabId); bufferFreeIndex = i;
return -1;
} if (munmap(bufferInfoTabAddr, bufferInfoNb * sizeof(msgBufferInfo)) < 0) {
NZG_ERROR("munmap", bufferInfoTabId);
return bufferFreeIndex; return -1;
}
return bufferFreeIndex;
} }

View file

@ -1,51 +1,61 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgBufferGetProcAttach( int msgBufferGetProcAttach(msgPoolData * poolDataTabAddr,
msgPoolData * poolDataTabAddr, int poolNb,
int poolNb, int *poolIndex, int *bufferIndex, void *addr)
int * poolIndex, {
int * bufferIndex, msgBufferInfoTabId bufferInfoTabId;
void * addr int bufferInfoTabFd;
){ msgBufferInfo *bufferInfoTabAddr;
msgBufferInfoTabId bufferInfoTabId; int bufferInfoNb;
int bufferInfoTabFd;
msgBufferInfo * bufferInfoTabAddr;
int bufferInfoNb;
bool found=false; bool found = false;
int pIdx=0; int pIdx = 0;
int bIdx; int bIdx;
while ((!found) && (pIdx < poolNb)){ while ((!found) && (pIdx < poolNb)) {
/* Pour chaque pool */ /* Pour chaque pool */
//récuperer l'ID du BufferInfoTab; //récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[pIdx].bufferInfoTabId); strcpy(bufferInfoTabId, poolDataTabAddr[pIdx].bufferInfoTabId);
bufferInfoNb=poolDataTabAddr[pIdx].bufferNb; bufferInfoNb = poolDataTabAddr[pIdx].bufferNb;
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE); bufferInfoTabFd =
if (bufferInfoTabFd<0) shm_open(bufferInfoTabId, O_RDWR, SHM_DEFAULT_MODE);
{ NZG_ERROR("shm_open",bufferInfoTabId); return -1; } if (bufferInfoTabFd < 0) {
NZG_ERROR("shm_open", bufferInfoTabId);
return -1;
}
/** on regarde dans le tableau d'infos de buffer **/ /** on regarde dans le tableau d'infos de buffer **/
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo), bufferInfoTabAddr =
PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0); mmap(NULL, bufferInfoNb * sizeof(msgBufferInfo), PROT_READ,
MAP_SHARED, bufferInfoTabFd, (off_t) 0);
/* on cherche dans chacun des buffers */ /* on cherche dans chacun des buffers */
bIdx=0; bIdx = 0;
while((!found) && bIdx<bufferInfoNb){ while ((!found) && bIdx < bufferInfoNb) {
if (bufferInfoTabAddr[bIdx].addr == addr){ if (bufferInfoTabAddr[bIdx].addr == addr) {
found=true; found = true;
(*bufferIndex)=bIdx; (*bufferIndex) = bIdx;
}; };
bIdx++; bIdx++;
}
if (found) {
(*poolIndex) = pIdx;
}
/* on détache le morceau de mémoire */
if (munmap
(bufferInfoTabAddr,
bufferInfoNb * sizeof(msgBufferInfo)) < 0) {
NZG_ERROR("munmap", bufferInfoTabId);
return -1;
}
close(bufferInfoTabFd);
pIdx++;
} }
if (found){ (*poolIndex)=pIdx; }
/* on détache le morceau de mémoire */
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0)
{ NZG_ERROR("munmap",bufferInfoTabId); return -1; }
close(bufferInfoTabFd);
pIdx++;
}
if (found){ return 0; } else {return -1;} if (found) {
return 0;
} else {
return -1;
}
} }

View file

@ -1,37 +1,39 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
int msgBufferInfoTabCreate( int msgBufferInfoTabCreate(msgSpaceId externId,
msgSpaceId externId, msgPoolData * poolDataTabAddr,
msgPoolData * poolDataTabAddr, int poolIdx, int bufferNb)
int poolIdx, {
int bufferNb) {
int bufferInfoTabFd; int bufferInfoTabFd;
msgBufferInfoTabId bufferInfoTabId; msgBufferInfoTabId bufferInfoTabId;
/* creation des infos sur buffers DEBUT */
if (msgBufferInfoTabIdIntern(bufferInfoTabId, externId, poolIdx) == -1) {
NZG_ERROR("msgPoolId creation", bufferInfoTabId);
return -1;
}
bufferInfoTabFd =
shm_open(bufferInfoTabId, O_RDWR | O_CREAT | O_EXCL | O_TRUNC,
MSGSPACE_DEFAULT_MODE);
if (bufferInfoTabFd == -1) {
fprintf(stderr, "msgInfoTab : %s creation failed: %s\n",
bufferInfoTabId, strerror(errno));
return -1;
}
if (ftruncate(bufferInfoTabFd, bufferNb * sizeof(msgBufferInfo)) == -1) {
fprintf(stderr, "msgBufferInfoTab resizing failed: %s\n",
strerror(errno));
return -1;
}
close(bufferInfoTabFd);
/* creation des infos sur buffers FIN */
msgBufferInfoTabInit(poolDataTabAddr, poolIdx);
/* creation des infos sur buffers DEBUT */
if (msgBufferInfoTabIdIntern(bufferInfoTabId,externId,poolIdx) == -1){
NZG_ERROR("msgPoolId creation",bufferInfoTabId);
return -1; return -1;
} NZG_ERROR("munmap", bufferInfoTabId);
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE);
if (bufferInfoTabFd == -1 ) {
fprintf( stderr, "msgInfoTab : %s creation failed: %s\n",bufferInfoTabId,
strerror( errno ) );
return -1; return -1;
}
if (ftruncate(bufferInfoTabFd, bufferNb*sizeof(msgBufferInfo)) == -1){
fprintf( stderr, "msgBufferInfoTab resizing failed: %s\n",
strerror( errno ) );
return -1;
}
close(bufferInfoTabFd);
/* creation des infos sur buffers FIN */
msgBufferInfoTabInit(poolDataTabAddr,poolIdx);
return -1;NZG_ERROR("munmap",bufferInfoTabId);
return -1;
} }

View file

@ -1,34 +1,38 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgBufferInfoTabInit(msgPoolData * poolDataTabAddr,int poolIndex){ int msgBufferInfoTabInit(msgPoolData * poolDataTabAddr, int poolIndex)
msgBufferInfoTabId bufferInfoTabId; {
int bufferInfoTabFd; msgBufferInfoTabId bufferInfoTabId;
msgBufferInfo * bufferInfoTabAddr; int bufferInfoTabFd;
int bufferInfoNb; msgBufferInfo *bufferInfoTabAddr;
int i; int bufferInfoNb;
int i;
//récuperer l'ID du BufferInfoTab; //récuperer l'ID du BufferInfoTab;
strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId); strcpy(bufferInfoTabId, poolDataTabAddr[poolIndex].bufferInfoTabId);
printf("msgBufferInfoTabId %d open >>%s<<\n",poolIndex,bufferInfoTabId); printf("msgBufferInfoTabId %d open >>%s<<\n", poolIndex,
bufferInfoTabFd=shm_open(bufferInfoTabId,O_RDWR,SHM_DEFAULT_MODE); bufferInfoTabId);
if (bufferInfoTabFd<0){ bufferInfoTabFd = shm_open(bufferInfoTabId, O_RDWR, SHM_DEFAULT_MODE);
NZG_ERROR("msgBufferInfoTabId open",bufferInfoTabId); if (bufferInfoTabFd < 0) {
return -1; NZG_ERROR("msgBufferInfoTabId open", bufferInfoTabId);
} return -1;
}
/** on regarde dans le tableau d'infos de buffer **/ /** on regarde dans le tableau d'infos de buffer **/
bufferInfoNb=poolDataTabAddr[poolIndex].bufferNb; bufferInfoNb = poolDataTabAddr[poolIndex].bufferNb;
bufferInfoTabAddr=mmap(NULL,bufferInfoNb*sizeof(msgBufferInfo), bufferInfoTabAddr = mmap(NULL, bufferInfoNb * sizeof(msgBufferInfo),
PROT_WRITE|PROT_READ,MAP_SHARED,bufferInfoTabFd,(off_t)0); PROT_WRITE | PROT_READ, MAP_SHARED,
bufferInfoTabFd, (off_t) 0);
for (i=0;i<bufferInfoNb;i++){ for (i = 0; i < bufferInfoNb; i++) {
bufferInfoTabAddr[i].ownerPid = (pid_t)-1; bufferInfoTabAddr[i].ownerPid = (pid_t) - 1;
} }
if (munmap(bufferInfoTabAddr,bufferInfoNb*sizeof(msgBufferInfo))< 0)
{ perror("munmap"); return -1; }
return 0; if (munmap(bufferInfoTabAddr, bufferInfoNb * sizeof(msgBufferInfo)) < 0) {
perror("munmap");
return -1;
}
return 0;
} }

View file

@ -1,55 +1,50 @@
#include "libnazgul.h" #include "libnazgul.h"
void *msgBufferMap(msgPoolData * poolDataTab, int poolIndex, int bufferIndex)
{
void *resultAddr;
int bufferSize, bufferNb;
int poolBufferTabFd;
msgPoolId poolBufferTabId;
printf("Mapping buffer (%d,%d)\n", poolIndex, bufferIndex);
// TODO: récuperer l'ID du BufferInfoTab;
strcpy(poolBufferTabId, poolDataTab[poolIndex].poolId);
bufferSize = poolDataTab[poolIndex].bufferSize;
bufferNb = poolDataTab[poolIndex].bufferNb;
if (bufferNb < 0) {
// do something with bufferNb
}
void * msgBufferMap(msgPoolData * poolDataTab, int poolIndex, int bufferIndex) { poolBufferTabFd = shm_open(poolBufferTabId, O_RDWR, SHM_DEFAULT_MODE);
void * resultAddr; if (poolBufferTabFd < 0) {
int bufferSize, bufferNb; NZG_ERROR("shm_open", poolBufferTabId);
int poolBufferTabFd; goto ERROR;
msgPoolId poolBufferTabId; }
printf("Mapping buffer (%d,%d)\n",poolIndex,bufferIndex); // mapper le buffer dans l'espace mémoire du processus
// TODO: récuperer l'ID du BufferInfoTab; /* on s'arrete juste derriere l'index qui nous intéresse */
strcpy(poolBufferTabId, poolDataTab[poolIndex].poolId); resultAddr = mmap(NULL, bufferSize * (bufferIndex + 1), PROT_READ | PROT_WRITE, //PROT_NONE
bufferSize = poolDataTab[poolIndex].bufferSize; MAP_SHARED, poolBufferTabFd, (off_t) 0);
bufferNb = poolDataTab[poolIndex].bufferNb; if (resultAddr == MAP_FAILED) {
if (bufferNb < 0) { NZG_ERROR("mmap", poolBufferTabId);
// do something with bufferNb goto ERROR;
} }
printf("Mapped from %p to %p\n",
(void *)resultAddr,
(void *)(resultAddr + bufferSize * (bufferIndex + 1))
);
poolBufferTabFd=shm_open(poolBufferTabId,O_RDWR,SHM_DEFAULT_MODE); resultAddr = resultAddr + (bufferSize * bufferIndex);
if (poolBufferTabFd<0){ printf("Moved to %p\n", (void *)resultAddr);
NZG_ERROR("shm_open",poolBufferTabId);
goto ERROR;
}
// mapper le buffer dans l'espace mémoire du processus /* mprotect(
/* on s'arrete juste derriere l'index qui nous intéresse */ resultAddr,
resultAddr=mmap(NULL, bufferSize,
bufferSize*(bufferIndex+1), PROT_READ|PROT_WRITE
PROT_READ|PROT_WRITE, //PROT_NONE ); */
MAP_SHARED,
poolBufferTabFd,
(off_t)0);
if(resultAddr == MAP_FAILED) {
NZG_ERROR("mmap", poolBufferTabId);
goto ERROR;
}
printf( "Mapped from %p to %p\n",
(void *)resultAddr,
(void *)(resultAddr+ bufferSize*(bufferIndex+1))
);
resultAddr=resultAddr +( bufferSize*bufferIndex); close(poolBufferTabFd);
printf( "Moved to %p\n",(void *)resultAddr );
/* mprotect( return resultAddr;
resultAddr, ERROR:
bufferSize, return NULL;
PROT_READ|PROT_WRITE
);*/
close(poolBufferTabFd);
return resultAddr;
ERROR:
return NULL;
} }

View file

@ -1,66 +1,63 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgFree(msgSpace * space, void * addr){ int msgFree(msgSpace * space, void *addr)
{
int poolIndex; int poolIndex;
int bufferIndex; int bufferIndex;
msgPoolData * poolDataTabAddr; msgPoolData *poolDataTabAddr;
sem_t * ressourceSemFd; sem_t *ressourceSemFd;
msgPoolDataTabSemId ressourceSemId; msgPoolDataTabSemId ressourceSemId;
int err; int err;
int bufferNb; int bufferNb;
int bufferSize; int bufferSize;
void * realAddr; void *realAddr;
printf("[ FREE %p ]\n",(void *)addr); printf("[ FREE %p ]\n", (void *)addr);
/* on acquiert le droit de modifier les infos sur la ressource */ /* on acquiert le droit de modifier les infos sur la ressource */
/* on protege le tableau des associations */ /* on protege le tableau des associations */
if (msgPoolDataTabLock(space) <0){ if (msgPoolDataTabLock(space) < 0) {
NZG_ERROR("msgPoolDataTabLock",space->poolDataTabSemId); NZG_ERROR("msgPoolDataTabLock", space->poolDataTabSemId);
goto ERROR; goto ERROR;
}; };
/* verifier le premier arg du shm_open */ /* verifier le premier arg du shm_open */
poolDataTabAddr = msgPoolDataTabOpen(space); poolDataTabAddr = msgPoolDataTabOpen(space);
// TODO: verouiller semaphore DataInfo ?? // TODO: verouiller semaphore DataInfo ??
poolIndex=-1; bufferIndex=-1; poolIndex = -1;
err=msgBufferGetProcAttach( bufferIndex = -1;
poolDataTabAddr, err = msgBufferGetProcAttach(poolDataTabAddr,
space->poolNb, space->poolNb,
&poolIndex, &poolIndex, &bufferIndex, addr);
&bufferIndex,
addr
);
printf("Found : %d\n",err); printf("Found : %d\n", err);
printf("Freing pool: %d, buffer: %d\n",poolIndex,bufferIndex); printf("Freing pool: %d, buffer: %d\n", poolIndex, bufferIndex);
msgBufferDetachProc(poolDataTabAddr,poolIndex,bufferIndex,addr); msgBufferDetachProc(poolDataTabAddr, poolIndex, bufferIndex, addr);
/* unmapper le buffer */ /* unmapper le buffer */
realAddr=addr; realAddr = addr;
bufferSize=poolDataTabAddr[poolIndex].bufferSize; bufferSize = poolDataTabAddr[poolIndex].bufferSize;
bufferNb=poolDataTabAddr[poolIndex].bufferNb; bufferNb = poolDataTabAddr[poolIndex].bufferNb;
realAddr=realAddr-poolIndex*bufferSize; realAddr = realAddr - poolIndex * bufferSize;
munmap(realAddr,bufferSize*bufferNb); munmap(realAddr, bufferSize * bufferNb);
msgPoolDataTabClose(space,poolDataTabAddr); msgPoolDataTabClose(space, poolDataTabAddr);
// deverouiller semaphore DataInfo // deverouiller semaphore DataInfo
msgPoolDataTabUnlock(space); msgPoolDataTabUnlock(space);
// deverouiller semaphore ressource. // deverouiller semaphore ressource.
msgPoolSemIdIntern(ressourceSemId,space->externId,poolIndex); msgPoolSemIdIntern(ressourceSemId, space->externId, poolIndex);
ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0); ressourceSemFd = sem_open(ressourceSemId, O_CREAT, SEM_DEFAULT_MODE, 0);
if (ressourceSemFd == SEM_FAILED){ if (ressourceSemFd == SEM_FAILED) {
NZG_ERROR("sem_open",ressourceSemId); NZG_ERROR("sem_open", ressourceSemId);
goto ERROR; goto ERROR;
} }
sem_post(ressourceSemFd); sem_post(ressourceSemFd);
sem_close(ressourceSemFd); sem_close(ressourceSemFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

117
src/get.c
View file

@ -2,70 +2,71 @@
#define NONBLOCK -1 #define NONBLOCK -1
void * msgGet(msgSpace * space,int queueIndex,int option){ void *msgGet(msgSpace * space, int queueIndex, int option)
void * resultAddr; {
msgQueueId queueId; void *resultAddr;
msgQueue * queue; msgQueueId queueId;
msgQueueElemId oldElemId; msgQueue *queue;
msgQueueElem * oldElem; msgQueueElemId oldElemId;
msgPoolData * poolDataTab; msgQueueElem *oldElem;
// on teste la possibilité de lecture sur la liste... msgPoolData *poolDataTab;
if (option == NONBLOCK){ // on teste la possibilité de lecture sur la liste...
if (msgQueueReadTryLock(space->externId,queueIndex) <0){ if (option == NONBLOCK) {
NZG_ERROR("msgQueueReadTryLock",space->externId); if (msgQueueReadTryLock(space->externId, queueIndex) < 0) {
goto ERROR; NZG_ERROR("msgQueueReadTryLock", space->externId);
goto ERROR;
}
} else {
if (msgQueueReadLock(space->externId, queueIndex) < 0) {
NZG_ERROR("msgQueueReadLock", space->externId);
goto ERROR;
}
} }
} else { // la lecture est possible
if (msgQueueReadLock(space->externId,queueIndex) <0){ // on essaye donc de modifier la liste
NZG_ERROR("msgQueueReadLock",space->externId); msgQueueProtLock(space->externId, queueIndex);
goto ERROR; msgQueueIdIntern(queueId, space->externId, queueIndex);
// ouvrir la file
queue = msgQueueOpen(queueId);
if (queue == NULL) {
NZG_ERROR("msgQueueOpen", queueId);
goto ERROR;
} }
} // recupérer l'id de l'ancien element...
// la lecture est possible msgQueueRem(queue, oldElemId);
// on essaye donc de modifier la liste
msgQueueProtLock(space->externId,queueIndex);
msgQueueIdIntern(queueId,space->externId,queueIndex);
// ouvrir la file
queue = msgQueueOpen(queueId);
if (queue==NULL){
NZG_ERROR("msgQueueOpen",queueId);
goto ERROR;
}
// recupérer l'id de l'ancien element...
msgQueueRem(queue, oldElemId);
oldElem = msgQueueElemOpen(oldElemId); oldElem = msgQueueElemOpen(oldElemId);
if (oldElem == NULL){ if (oldElem == NULL) {
NZG_ERROR("msgQueueElemOpen",oldElemId); NZG_ERROR("msgQueueElemOpen", oldElemId);
goto ERROR; goto ERROR;
} }
/* on récupere la taille des buffer dans la pool du buffer */ /* on récupere la taille des buffer dans la pool du buffer */
poolDataTab=msgPoolDataTabOpen(space); poolDataTab = msgPoolDataTabOpen(space);
// mapper le buffer dans l'espace mémoire du processus // mapper le buffer dans l'espace mémoire du processus
resultAddr=msgBufferMap(poolDataTab,oldElem->poolIndex,oldElem->bufferIndex); resultAddr =
// attacher au buffer... msgBufferMap(poolDataTab, oldElem->poolIndex, oldElem->bufferIndex);
if (msgBufferAttachProc(poolDataTab, // attacher au buffer...
oldElem->poolIndex, if (msgBufferAttachProc(poolDataTab,
oldElem->bufferIndex, oldElem->poolIndex,
resultAddr) <0){ oldElem->bufferIndex, resultAddr) < 0) {
NZG_ERROR("msgBufferAttachProc",oldElemId); NZG_ERROR("msgBufferAttachProc", oldElemId);
goto ERROR; goto ERROR;
} }
msgPoolDataTabClose(space,poolDataTab); msgPoolDataTabClose(space, poolDataTab);
if (msgQueueElemClose(oldElem) <0){ if (msgQueueElemClose(oldElem) < 0) {
NZG_ERROR("msgQueueElemClose",oldElemId); NZG_ERROR("msgQueueElemClose", oldElemId);
} }
shm_unlink(oldElemId); shm_unlink(oldElemId);
// fermer la file // fermer la file
msgQueueClose(queue); msgQueueClose(queue);
// on a fini de modifier la liste // on a fini de modifier la liste
msgQueueProtUnlock(space->externId,queueIndex); msgQueueProtUnlock(space->externId, queueIndex);
return resultAddr; return resultAddr;
ERROR: ERROR:
return NULL; return NULL;
} }

View file

@ -1,13 +1,13 @@
#ifndef _NZG_GLOBAL #ifndef _NZG_GLOBAL
#define _NZG_GLOBAL 1 #define _NZG_GLOBAL 1
#include <unistd.h> /* POSIX et al */ #include <unistd.h> /* POSIX et al */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <fcntl.h> /* pour O_RDWR */ #include <fcntl.h> /* pour O_RDWR */
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h> /* shm_open */ #include <sys/mman.h> /* shm_open */
#include <errno.h> #include <errno.h>
#include <semaphore.h> #include <semaphore.h>
@ -25,12 +25,10 @@
#define SEM_FAILED ((sem_t *)0) #define SEM_FAILED ((sem_t *)0)
#endif #endif
#ifndef MAP_FAILED #ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1) #define MAP_FAILED ((void *)-1)
#endif #endif
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
#define DEFAULT_MSGSPACELISTID "/tmp/nzgSpaceList" #define DEFAULT_MSGSPACELISTID "/tmp/nzgSpaceList"
#else #else

48
src/global.h~ Normal file
View file

@ -0,0 +1,48 @@
#ifndef _NZG_GLOBAL
#define _NZG_GLOBAL 1
#include <unistd.h> /* POSIX et al */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h> /* pour O_RDWR */
#include <sys/types.h>
#include <sys/mman.h> /* shm_open */
#include <errno.h>
#include <semaphore.h>
#define PAGESIZE sysconf(_SC_PAGESIZE)
#define MSGSPACE_DEFAULT_MODE 0600
#define SEM_DEFAULT_MODE 0600
#define SHM_DEFAULT_MODE 0600
#define MSGSPACE_ID_LEN 32
#define ERR_UNHANDLED "Gérer mieu les erreurs"
#define ANYPOOL -1
#define SPECIFICPOOL 0
#ifndef SEM_FAILED
#define SEM_FAILED ((sem_t *)0)
#endif
#ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1)
#endif
#ifdef _NZG_REALFILEID
#define DEFAULT_MSGSPACELISTID "/tmp/nzgSpaceList"
#else
#define DEFAULT_MSGSPACELISTID "/nzgSpaceList"
#endif
#ifdef _NZG_REALFILEID
#define DEFAULT_MSGSPACELISTSEMID "/tmp/nzgSpaceListSem"
#else
#define DEFAULT_MSGSPACELISTSEMID "/nzgSpaceListSem"
#endif
#define NZG_ERROR(func,str) fprintf(stderr,"*** %s: %s***\n%s in %s:%d\n",func,strerror(errno),str,__FILE__,__LINE__);
#endif

204
src/ids.c
View file

@ -1,158 +1,150 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src ){ int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src)
if (strlen(src)>MSGSPACE_ID_LEN){ {
return -1; if (strlen(src) > MSGSPACE_ID_LEN) {
} return -1;
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */ }
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(dest,"/tmp/nzgSpace%s",(char *)src); sprintf(dest, "/tmp/nzgSpace%s", (char *)src);
#else #else
sprintf(dest,"/nzgSpace%s",(char *)src); sprintf(dest, "/nzgSpace%s", (char *)src);
#endif #endif
return 0; return 0;
} }
int msgPoolDataTabSemIdIntern(
msgPoolSemId destSemId, int msgPoolDataTabSemIdIntern(msgPoolSemId destSemId, const msgSpaceId externId)
const msgSpaceId externId){ {
if (strlen(externId)>MSGSPACE_ID_LEN*4){ if (strlen(externId) > MSGSPACE_ID_LEN * 4) {
return -1; return -1;
} }
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */ /* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(destSemId,"/tmp/nzgPoolDTSem-%s",(char *)externId); sprintf(destSemId, "/tmp/nzgPoolDTSem-%s", (char *)externId);
#else #else
sprintf(destSemId,"/nzgPoolDTSem-%s",(char *)externId); sprintf(destSemId, "/nzgPoolDTSem-%s", (char *)externId);
#endif #endif
return 0; return 0;
} }
int msgPoolSemIdIntern(
msgPoolSemId destSemId, int msgPoolSemIdIntern(msgPoolSemId destSemId,
const msgSpaceId srcPoolId, const msgSpaceId srcPoolId, int poolIdx)
int poolIdx){ {
if (strlen(srcPoolId)>MSGSPACE_ID_LEN*4){ if (strlen(srcPoolId) > MSGSPACE_ID_LEN * 4) {
return -1; return -1;
} }
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */ /* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(destSemId,"/tmp/nzgPoolSem-%s-%d",(char *)srcPoolId,poolIdx); sprintf(destSemId, "/tmp/nzgPoolSem-%s-%d", (char *)srcPoolId, poolIdx);
#else #else
sprintf(destSemId,"/nzgPoolSem-%s-%d",(char *)srcPoolId,poolIdx); sprintf(destSemId, "/nzgPoolSem-%s-%d", (char *)srcPoolId, poolIdx);
#endif #endif
return 0; return 0;
} }
int msgPoolDataIdIntern(msgPoolDataTabId dest,const msgSpaceId src ){ int msgPoolDataIdIntern(msgPoolDataTabId dest, const msgSpaceId src)
if (strlen(src)>MSGSPACE_ID_LEN){ {
return -1; if (strlen(src) > MSGSPACE_ID_LEN) {
} return -1;
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */ }
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(dest,"/tmp/nzgPoolData-%s",(char *)src); sprintf(dest, "/tmp/nzgPoolData-%s", (char *)src);
#else #else
sprintf(dest,"/nzgPoolData-%s",(char *)src); sprintf(dest, "/nzgPoolData-%s", (char *)src);
#endif #endif
return 0; return 0;
} }
int msgPoolIdIntern(msgPoolId dest, msgPoolId src, int num)
int msgPoolIdIntern(msgPoolId dest,msgPoolId src, int num){ {
if (strlen(src)>MSGSPACE_ID_LEN){ if (strlen(src) > MSGSPACE_ID_LEN) {
return -1; return -1;
} }
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(dest,"/tmp/nzgPool-%s-%d",(char *)src,num); sprintf(dest, "/tmp/nzgPool-%s-%d", (char *)src, num);
#else #else
sprintf(dest,"/nzgPool-%s-%d",(char *)src,num); sprintf(dest, "/nzgPool-%s-%d", (char *)src, num);
#endif #endif
return 0; return 0;
} }
int msgBufferInfoTabIdIntern( int msgBufferInfoTabIdIntern(msgBufferInfoTabId dest, msgSpaceId src, int num)
msgBufferInfoTabId dest, {
msgSpaceId src, if (strlen(src) > MSGSPACE_ID_LEN) {
int num){ return -1;
if (strlen(src)>MSGSPACE_ID_LEN){ }
return -1;
}
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(dest,"/tmp/nzgBufferInfo-%s-%d",(char *)src,num); sprintf(dest, "/tmp/nzgBufferInfo-%s-%d", (char *)src, num);
#else #else
sprintf(dest,"/nzgBufferInfo-%s-%d",(char *)src,num); sprintf(dest, "/nzgBufferInfo-%s-%d", (char *)src, num);
#endif #endif
return 0; return 0;
} }
int msgQueueProtSemIdIntern( int msgQueueProtSemIdIntern(msgQueueSemId dest,
msgQueueSemId dest, msgSpaceId externId, int queueIdx)
msgSpaceId externId, {
int queueIdx){ if (strlen(externId) > MSGSPACE_ID_LEN) {
if (strlen(externId)>MSGSPACE_ID_LEN){ return -1;
return -1; }
}
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(dest,"/tmp/nzgQueueProtSem-%s-%d",(char *)externId,queueIdx); sprintf(dest, "/tmp/nzgQueueProtSem-%s-%d", (char *)externId, queueIdx);
#else #else
sprintf(dest,"/nzgQueueProtSem-%s-%d",(char *)externId,queueIdx); sprintf(dest, "/nzgQueueProtSem-%s-%d", (char *)externId, queueIdx);
#endif #endif
return 0; return 0;
} }
int msgQueueReadSemIdIntern( int msgQueueReadSemIdIntern(msgQueueSemId dest,
msgQueueSemId dest, msgSpaceId externId, int queueIdx)
msgSpaceId externId, {
int queueIdx){ if (strlen(externId) > MSGSPACE_ID_LEN) {
if (strlen(externId)>MSGSPACE_ID_LEN){ return -1;
return -1; }
}
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(dest,"/tmp/nzgQueueReadSem-%s-%d",(char *)externId,queueIdx); sprintf(dest, "/tmp/nzgQueueReadSem-%s-%d", (char *)externId, queueIdx);
#else #else
sprintf(dest,"/nzgQueueReadSem-%s-%d",(char *)externId,queueIdx); sprintf(dest, "/nzgQueueReadSem-%s-%d", (char *)externId, queueIdx);
#endif #endif
return 0; return 0;
} }
int msgQueueIdIntern( int msgQueueIdIntern(msgQueueId dest, msgSpaceId externId, int queueIdx)
msgQueueId dest, {
msgSpaceId externId, if (strlen(externId) > MSGSPACE_ID_LEN) {
int queueIdx){ return -1;
if (strlen(externId)>MSGSPACE_ID_LEN){ }
return -1;
}
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(dest,"/tmp/nzgQueue-%s-%d",(char *)externId,queueIdx); sprintf(dest, "/tmp/nzgQueue-%s-%d", (char *)externId, queueIdx);
#else #else
sprintf(dest,"/nzgQueue-%s-%d",(char *)externId,queueIdx); sprintf(dest, "/nzgQueue-%s-%d", (char *)externId, queueIdx);
#endif #endif
return 0; return 0;
} }
int msgQueueElemIdIntern( int msgQueueElemIdIntern(msgQueueElemId dest, msgQueueId src, int counter)
msgQueueElemId dest, {
msgQueueId src, if (strlen(src) > MSGSPACE_ID_LEN) {
int counter){ return -1;
if (strlen(src)>MSGSPACE_ID_LEN){ }
return -1; sprintf(dest, "%s-%d", (char *)src, counter);
} return 0;
sprintf(dest,"%s-%d",(char *)src,counter);
return 0;
} }
int msgSpaceListElemIdIntern(msgSpaceListElemId elemListId,msgSpaceId externId){ int msgSpaceListElemIdIntern(msgSpaceListElemId elemListId, msgSpaceId externId)
if (strlen(externId)>MSGSPACE_ID_LEN){ {
return -1; if (strlen(externId) > MSGSPACE_ID_LEN) {
} return -1;
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */ }
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */
#ifdef _NZG_REALFILEID #ifdef _NZG_REALFILEID
sprintf(elemListId,"/tmp/nzgSpaceListElem-%s",(char *)externId); sprintf(elemListId, "/tmp/nzgSpaceListElem-%s", (char *)externId);
#else #else
sprintf(elemListId,"/nzgSpaceListElem-%s",(char *)externId); sprintf(elemListId, "/nzgSpaceListElem-%s", (char *)externId);
#endif #endif
return 0; return 0;
} }

View file

@ -2,21 +2,13 @@
#define _NZG_IDS 1 #define _NZG_IDS 1
#include "libnazgul.h" #include "libnazgul.h"
/* nzg_ids.c */ /* nzg_ids.c */
int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src); int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolSemIdIntern(msgPoolSemId destSemId, const msgSpaceId srcPoolId, int poolIdx); int msgPoolSemIdIntern(msgPoolSemId destSemId, const msgSpaceId srcPoolId,
int poolIdx);
int msgPoolDataIdIntern(msgPoolDataTabId dest, const msgSpaceId src); int msgPoolDataIdIntern(msgPoolDataTabId dest, const msgSpaceId src);
int msgPoolIdIntern(msgPoolId dest,msgPoolId src, int num); int msgPoolIdIntern(msgPoolId dest, msgPoolId src, int num);
int msgQueueSemIdIntern( int msgQueueSemIdIntern(msgQueueSemId dest, msgSpaceId externId, int queueIdx);
msgQueueSemId dest, int msgQueueIdIntern(msgQueueId dest, msgSpaceId externId, int queueIdx);
msgSpaceId externId,
int queueIdx);
int msgQueueIdIntern(
msgQueueId dest,
msgSpaceId externId,
int queueIdx);
#endif #endif

22
src/ids.h~ Normal file
View file

@ -0,0 +1,22 @@
#ifndef _NZG_IDS
#define _NZG_IDS 1
#include "libnazgul.h"
/* nzg_ids.c */
int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolSemIdIntern(msgPoolSemId destSemId, const msgSpaceId srcPoolId, int poolIdx);
int msgPoolDataIdIntern(msgPoolDataTabId dest, const msgSpaceId src);
int msgPoolIdIntern(msgPoolId dest,msgPoolId src, int num);
int msgQueueSemIdIntern(
msgQueueSemId dest,
msgSpaceId externId,
int queueIdx);
int msgQueueIdIntern(
msgQueueId dest,
msgSpaceId externId,
int queueIdx);
#endif

View file

@ -3,88 +3,86 @@
#include "global.h" #include "global.h"
typedef enum { true=1, false=0} bool; typedef enum { true = 1, false = 0 } bool;
typedef char msgSpaceId[MSGSPACE_ID_LEN]; typedef char msgSpaceId[MSGSPACE_ID_LEN];
typedef char msgSpaceListId[4*MSGSPACE_ID_LEN]; typedef char msgSpaceListId[4 * MSGSPACE_ID_LEN];
typedef char msgSpaceListSemId[4*MSGSPACE_ID_LEN]; typedef char msgSpaceListSemId[4 * MSGSPACE_ID_LEN];
typedef char msgSpaceListElemId[4*MSGSPACE_ID_LEN]; typedef char msgSpaceListElemId[4 * MSGSPACE_ID_LEN];
typedef char msgPoolDataTabId[4*MSGSPACE_ID_LEN]; typedef char msgPoolDataTabId[4 * MSGSPACE_ID_LEN];
typedef char msgPoolDataTabSemId[4*MSGSPACE_ID_LEN]; typedef char msgPoolDataTabSemId[4 * MSGSPACE_ID_LEN];
typedef char msgBufferInfoTabId[4*MSGSPACE_ID_LEN]; typedef char msgBufferInfoTabId[4 * MSGSPACE_ID_LEN];
typedef char msgPoolId[4*MSGSPACE_ID_LEN]; typedef char msgPoolId[4 * MSGSPACE_ID_LEN];
typedef char msgPoolSemId[4*MSGSPACE_ID_LEN]; typedef char msgPoolSemId[4 * MSGSPACE_ID_LEN];
typedef char msgQueueDataId[4*MSGSPACE_ID_LEN]; typedef char msgQueueDataId[4 * MSGSPACE_ID_LEN];
typedef char msgQueueId[4*MSGSPACE_ID_LEN]; typedef char msgQueueId[4 * MSGSPACE_ID_LEN];
typedef char msgQueueElemId[4*MSGSPACE_ID_LEN]; typedef char msgQueueElemId[4 * MSGSPACE_ID_LEN];
typedef char msgQueueSemId[4*MSGSPACE_ID_LEN]; typedef char msgQueueSemId[4 * MSGSPACE_ID_LEN];
/* pid[] */ /* pid[] */
/* liste des processus demandeurs */ /* liste des processus demandeurs */
typedef struct MsgPool { typedef struct MsgPool {
int bufferSize; int bufferSize;
int bufferNb; int bufferNb;
} msgPool; } msgPool;
typedef struct MsgBufferInfo { typedef struct MsgBufferInfo {
pid_t ownerPid; pid_t ownerPid;
void * addr; void *addr;
} msgBufferInfo; } msgBufferInfo;
typedef struct MsgPoolData { typedef struct MsgPoolData {
msgPoolId poolId; msgPoolId poolId;
msgBufferInfoTabId bufferInfoTabId; msgBufferInfoTabId bufferInfoTabId;
int bufferNb; int bufferNb;
int bufferSize; int bufferSize;
int allocDispBuffer; int allocDispBuffer;
} msgPoolData; } msgPoolData;
/* TODO: queueId */ /* TODO: queueId */
typedef struct MsgSpace { typedef struct MsgSpace {
msgSpaceId id; msgSpaceId id;
msgSpaceId externId; msgSpaceId externId;
int poolNb; int poolNb;
int queueNb; int queueNb;
int pid; int pid;
msgPoolDataTabId poolDataTabId; msgPoolDataTabId poolDataTabId;
msgPoolDataTabSemId poolDataTabSemId; msgPoolDataTabSemId poolDataTabSemId;
} msgSpace; } msgSpace;
typedef struct MsgSpaceListElem { typedef struct MsgSpaceListElem {
msgSpaceListElemId id; msgSpaceListElemId id;
msgSpaceId spaceId; msgSpaceId spaceId;
msgSpaceListElemId next; msgSpaceListElemId next;
} msgSpaceListElem; } msgSpaceListElem;
typedef struct MsgSpaceList { typedef struct MsgSpaceList {
msgSpaceListId id; msgSpaceListId id;
int elemCounter; int elemCounter;
msgSpaceListElemId headId; msgSpaceListElemId headId;
msgSpaceListElemId tailId; msgSpaceListElemId tailId;
} msgSpaceList; } msgSpaceList;
typedef struct MsgQueueElem { typedef struct MsgQueueElem {
msgQueueElemId id; msgQueueElemId id;
int poolIndex; int poolIndex;
int bufferIndex; int bufferIndex;
msgQueueElemId next; msgQueueElemId next;
} msgQueueElem; } msgQueueElem;
typedef struct MsgQueue { typedef struct MsgQueue {
msgQueueId id; msgQueueId id;
int elemCounter; int elemCounter;
msgQueueElemId headId; msgQueueElemId headId;
msgQueueElemId tailId; msgQueueElemId tailId;
} msgQueue; } msgQueue;
#endif #endif

91
src/iface.h~ Normal file
View file

@ -0,0 +1,91 @@
#ifndef _NZG_IFACE
#define _NZG_IFACE 1
#include "global.h"
typedef enum { true=1, false=0} bool;
typedef char msgSpaceId[MSGSPACE_ID_LEN];
typedef char msgSpaceListId[4*MSGSPACE_ID_LEN];
typedef char msgSpaceListSemId[4*MSGSPACE_ID_LEN];
typedef char msgSpaceListElemId[4*MSGSPACE_ID_LEN];
typedef char msgPoolDataTabId[4*MSGSPACE_ID_LEN];
typedef char msgPoolDataTabSemId[4*MSGSPACE_ID_LEN];
typedef char msgBufferInfoTabId[4*MSGSPACE_ID_LEN];
typedef char msgPoolId[4*MSGSPACE_ID_LEN];
typedef char msgPoolSemId[4*MSGSPACE_ID_LEN];
typedef char msgQueueDataId[4*MSGSPACE_ID_LEN];
typedef char msgQueueId[4*MSGSPACE_ID_LEN];
typedef char msgQueueElemId[4*MSGSPACE_ID_LEN];
typedef char msgQueueSemId[4*MSGSPACE_ID_LEN];
/* pid[] */
/* liste des processus demandeurs */
typedef struct MsgPool {
int bufferSize;
int bufferNb;
} msgPool;
typedef struct MsgBufferInfo {
pid_t ownerPid;
void * addr;
} msgBufferInfo;
typedef struct MsgPoolData {
msgPoolId poolId;
msgBufferInfoTabId bufferInfoTabId;
int bufferNb;
int bufferSize;
int allocDispBuffer;
} msgPoolData;
/* TODO: queueId */
typedef struct MsgSpace {
msgSpaceId id;
msgSpaceId externId;
int poolNb;
int queueNb;
int pid;
msgPoolDataTabId poolDataTabId;
msgPoolDataTabSemId poolDataTabSemId;
} msgSpace;
typedef struct MsgSpaceListElem {
msgSpaceListElemId id;
msgSpaceId spaceId;
msgSpaceListElemId next;
} msgSpaceListElem;
typedef struct MsgSpaceList {
msgSpaceListId id;
int elemCounter;
msgSpaceListElemId headId;
msgSpaceListElemId tailId;
} msgSpaceList;
typedef struct MsgQueueElem {
msgQueueElemId id;
int poolIndex;
int bufferIndex;
msgQueueElemId next;
} msgQueueElem;
typedef struct MsgQueue {
msgQueueId id;
int elemCounter;
msgQueueElemId headId;
msgQueueElemId tailId;
} msgQueue;
#endif
/* */

8
src/libnazgul.h~ Normal file
View file

@ -0,0 +1,8 @@
#ifndef _LIBNAZGUL
#define _LIBNAZGUL 1
#include "global.h"
#include "iface.h"
#include "proto.h"
#endif

View file

@ -1,50 +1,52 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
int msgPoolCreate( int msgPoolCreate(msgSpaceId externId, int poolIdx, int buffNb, int buffSize)
msgSpaceId externId, {
int poolIdx,
int buffNb,
int buffSize
) {
int poolFd; int poolFd;
sem_t * ressourceSemFd; sem_t *ressourceSemFd;
msgPoolId poolId; msgPoolId poolId;
msgPoolSemId poolRessourceSemId; msgPoolSemId poolRessourceSemId;
/* creation des buffers DEBUT */ /* creation des buffers DEBUT */
if (msgPoolIdIntern(poolId,externId,poolIdx) == -1){ if (msgPoolIdIntern(poolId, externId, poolIdx) == -1) {
fprintf( stderr, "msgPoolId creation failed for id %s\n", (char*)poolId ); fprintf(stderr, "msgPoolId creation failed for id %s\n",
return -1; (char *)poolId);
return -1;
} }
poolFd=shm_open(poolId,O_RDWR|O_CREAT|O_EXCL|O_TRUNC,MSGSPACE_DEFAULT_MODE); poolFd =
if (poolFd == -1 ) { shm_open(poolId, O_RDWR | O_CREAT | O_EXCL | O_TRUNC,
fprintf( stderr, "msgPool : %s creation failed: %s\n",poolId, MSGSPACE_DEFAULT_MODE);
strerror( errno ) ); if (poolFd == -1) {
return -1; fprintf(stderr, "msgPool : %s creation failed: %s\n", poolId,
strerror(errno));
return -1;
} }
if (ftruncate(poolFd, (buffSize*buffNb)) == -1){ if (ftruncate(poolFd, (buffSize * buffNb)) == -1) {
fprintf( stderr, "msgPool resizing failed: %s\n", fprintf(stderr, "msgPool resizing failed: %s\n",
strerror( errno ) ); strerror(errno));
return -1; return -1;
} }
/* creation des buffers FIN */ /* creation des buffers FIN */
if (msgPoolSemIdIntern(poolRessourceSemId,externId,poolIdx) == -1){ if (msgPoolSemIdIntern(poolRessourceSemId, externId, poolIdx) == -1) {
fprintf( stderr, "msgPoolId creation failed for id %s\n", fprintf(stderr, "msgPoolId creation failed for id %s\n",
(char*)poolRessourceSemId ); (char *)poolRessourceSemId);
return -1; return -1;
} }
// on met un semaphore sur le pool // on met un semaphore sur le pool
ressourceSemFd = sem_open(poolRessourceSemId, O_CREAT|O_EXCL,SEM_DEFAULT_MODE, buffNb); ressourceSemFd =
if (ressourceSemFd == SEM_FAILED){ sem_open(poolRessourceSemId, O_CREAT | O_EXCL, SEM_DEFAULT_MODE,
NZG_ERROR("sem_open : creation de la ressource",poolRessourceSemId); buffNb);
goto ERROR; if (ressourceSemFd == SEM_FAILED) {
} else { NZG_ERROR("sem_open : creation de la ressource",
printf("[ Created %s with %d ressources ]\n",poolRessourceSemId,buffNb); poolRessourceSemId);
} goto ERROR;
} else {
printf("[ Created %s with %d ressources ]\n",
poolRessourceSemId, buffNb);
}
//TODO: verrifier les erreurs sur l'ouverture de la sem //TODO: verrifier les erreurs sur l'ouverture de la sem
@ -55,7 +57,6 @@ int msgPoolCreate(
close(poolFd); close(poolFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -1,13 +1,14 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgPoolDataTabClose(msgSpace * space,void * addr){ int msgPoolDataTabClose(msgSpace * space, void *addr)
/* unmapper le msgPoolDataTab */ {
/* unmapper le msgPoolDataTab */
if (munmap(addr,(space->poolNb) * sizeof( msgPoolData )) < 0){ if (munmap(addr, (space->poolNb) * sizeof(msgPoolData)) < 0) {
NZG_ERROR("unmap",space->poolDataTabId); NZG_ERROR("unmap", space->poolDataTabId);
goto ERROR; goto ERROR;
} }
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -1,42 +1,39 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
void * msgPoolDataTabCreate(msgSpace * space){ void *msgPoolDataTabCreate(msgSpace * space)
int poolDataFd; // shm file descriptor {
msgPoolData * poolDataTabAddr; int poolDataFd; // shm file descriptor
poolDataFd=shm_open( msgPoolData *poolDataTabAddr;
space->poolDataTabId, poolDataFd = shm_open(space->poolDataTabId,
O_RDWR|O_CREAT|O_EXCL|O_TRUNC, O_RDWR | O_CREAT | O_EXCL | O_TRUNC,
MSGSPACE_DEFAULT_MODE MSGSPACE_DEFAULT_MODE);
); if (poolDataFd == -1) {
if (poolDataFd == -1 ) { NZG_ERROR("shm_open :create", space->poolDataTabId);
NZG_ERROR("shm_open :create",space->poolDataTabId); goto ERROR;
goto ERROR; }
}
/* allocation de la bonne zone mémoire pour le poolData */ /* allocation de la bonne zone mémoire pour le poolData */
if (ftruncate(poolDataFd, (space->poolNb)*sizeof(msgPoolData)) == -1){ if (ftruncate(poolDataFd, (space->poolNb) * sizeof(msgPoolData)) == -1) {
NZG_ERROR("ftruncate",space->poolDataTabId); NZG_ERROR("ftruncate", space->poolDataTabId);
goto ERROR; goto ERROR;
} }
close(poolDataFd); close(poolDataFd);
poolDataTabAddr = msgPoolDataTabOpen(space); poolDataTabAddr = msgPoolDataTabOpen(space);
if (poolDataTabAddr == NULL){ if (poolDataTabAddr == NULL) {
NZG_ERROR("msgPoolDataTabOpen : create",space->poolDataTabId); NZG_ERROR("msgPoolDataTabOpen : create", space->poolDataTabId);
goto ERROR; goto ERROR;
} }
/*mmap( 0, sizeof( *mSAddr ), /*mmap( 0, sizeof( *mSAddr ),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED, mSFd, 0 ); MAP_SHARED, mSFd, 0 );
if( poolDataAddr == MAP_FAILED ) { if( poolDataAddr == MAP_FAILED ) {
NZG_ERROR("mmap",poolDataTabId); NZG_ERROR("mmap",poolDataTabId);
goto ERROR; goto ERROR;
}*/ } */
return poolDataTabAddr; return poolDataTabAddr;
ERROR: ERROR:
return NULL; return NULL;
} }

View file

@ -3,34 +3,35 @@
#define DEBUG 0 #define DEBUG 0
int msgPoolDataTabLock(msgSpace * space){ int msgPoolDataTabLock(msgSpace * space)
int semval; {
sem_t * poolDataTabSemFd; int semval;
semval=0; sem_t *poolDataTabSemFd;
if (DEBUG) {printf("Locking %s\n",space->poolDataTabSemId);} semval = 0;
if (DEBUG) {
printf("Locking %s\n", space->poolDataTabSemId);
}
poolDataTabSemFd=sem_open( poolDataTabSemFd = sem_open(space->poolDataTabSemId, O_CREAT,
space->poolDataTabSemId SEM_DEFAULT_MODE, 1);
,O_CREAT, if (poolDataTabSemFd == SEM_FAILED) {
SEM_DEFAULT_MODE, NZG_ERROR("sem_open", space->poolDataTabSemId);
1); goto ERROR;
if(poolDataTabSemFd==SEM_FAILED){ }
NZG_ERROR("sem_open",space->poolDataTabSemId);
goto ERROR;
}
if(sem_wait(poolDataTabSemFd)==-1){ if (sem_wait(poolDataTabSemFd) == -1) {
NZG_ERROR("sem_wait",space->poolDataTabSemId); NZG_ERROR("sem_wait", space->poolDataTabSemId);
goto ERROR; goto ERROR;
} }
sem_getvalue(poolDataTabSemFd,&semval); sem_getvalue(poolDataTabSemFd, &semval);
if (DEBUG) { printf("(AfternValue:%d)\n",semval);} if (DEBUG) {
printf("(AfternValue:%d)\n", semval);
}
sem_close(poolDataTabSemFd); sem_close(poolDataTabSemFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -1,31 +1,30 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
void * msgPoolDataTabOpen(msgSpace * space){ void *msgPoolDataTabOpen(msgSpace * space)
int poolDataTabFd; {
void * poolDataTabAddr; int poolDataTabFd;
void *poolDataTabAddr;
poolDataTabFd=shm_open(space->poolDataTabId, poolDataTabFd = shm_open(space->poolDataTabId,
O_RDWR, O_RDWR, MSGSPACE_DEFAULT_MODE);
MSGSPACE_DEFAULT_MODE); if (poolDataTabFd == -1) {
if (poolDataTabFd == -1 ) { NZG_ERROR("shm_open", space->poolDataTabId);
NZG_ERROR("shm_open",space->poolDataTabId); goto ERROR;
goto ERROR; }
}
poolDataTabAddr = mmap(NULL, (space->poolNb) * sizeof(msgPoolData),
PROT_READ | PROT_WRITE,
MAP_SHARED, poolDataTabFd, 0);
poolDataTabAddr = mmap( NULL, (space->poolNb) * sizeof( msgPoolData ), if (poolDataTabAddr == MAP_FAILED) {
PROT_READ | PROT_WRITE, NZG_ERROR("mmap", space->poolDataTabId);
MAP_SHARED, poolDataTabFd, 0 ); goto ERROR;
}
if( poolDataTabAddr == MAP_FAILED) { close(poolDataTabFd);
NZG_ERROR("mmap",space->poolDataTabId);
goto ERROR;
}
close(poolDataTabFd); return poolDataTabAddr;
ERROR:
return poolDataTabAddr; return NULL;
ERROR:
return NULL;
} }

View file

@ -3,33 +3,34 @@
#define DEBUG 0 #define DEBUG 0
int msgPoolDataTabUnlock(msgSpace * space){ int msgPoolDataTabUnlock(msgSpace * space)
int semval; {
sem_t * poolDataTabSemFd; int semval;
if (DEBUG) { printf("Unlocking %s\n",space->poolDataTabSemId); } sem_t *poolDataTabSemFd;
poolDataTabSemFd=sem_open( if (DEBUG) {
space->poolDataTabSemId printf("Unlocking %s\n", space->poolDataTabSemId);
,O_CREAT, }
SEM_DEFAULT_MODE, poolDataTabSemFd = sem_open(space->poolDataTabSemId, O_CREAT,
1); SEM_DEFAULT_MODE, 1);
if(poolDataTabSemFd==SEM_FAILED){ if (poolDataTabSemFd == SEM_FAILED) {
NZG_ERROR("sem_open",space->poolDataTabSemId); NZG_ERROR("sem_open", space->poolDataTabSemId);
goto ERROR; goto ERROR;
} }
if(sem_post(poolDataTabSemFd)==-1){ if (sem_post(poolDataTabSemFd) == -1) {
NZG_ERROR("sem_post",space->poolDataTabSemId); NZG_ERROR("sem_post", space->poolDataTabSemId);
goto ERROR; goto ERROR;
} }
semval=0;
sem_getvalue(poolDataTabSemFd,&semval);
if (DEBUG) { printf("(AfternValue:%d)\n",semval); }
sem_close(poolDataTabSemFd); semval = 0;
sem_getvalue(poolDataTabSemFd, &semval);
if (DEBUG) {
printf("(AfternValue:%d)\n", semval);
}
return 0; sem_close(poolDataTabSemFd);
ERROR:
return -1; return 0;
ERROR:
return -1;
} }

View file

@ -1,50 +1,49 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
int msgPoolDelete( int msgPoolDelete(msgSpaceId spaceId, int poolIdx)
msgSpaceId spaceId, {
int poolIdx
) {
msgPoolId poolId; msgPoolId poolId;
msgBufferInfoTabId bufferInfoTabId; msgBufferInfoTabId bufferInfoTabId;
msgPoolSemId poolRessourceSemId; msgPoolSemId poolRessourceSemId;
/* suppression des infos sur buffers DEBUT */ /* suppression des infos sur buffers DEBUT */
if (msgBufferInfoTabIdIntern(bufferInfoTabId,spaceId,poolIdx) == -1){ if (msgBufferInfoTabIdIntern(bufferInfoTabId, spaceId, poolIdx) == -1) {
fprintf( stderr, "msgPoolId creation failed for id %s\n", (char*)poolId ); fprintf(stderr, "msgPoolId creation failed for id %s\n",
return -1; (char *)poolId);
return -1;
} }
if (shm_unlink(bufferInfoTabId) <0 ) { if (shm_unlink(bufferInfoTabId) < 0) {
fprintf( stderr, "msgInfoTab : %s deletion failed: %s\n",bufferInfoTabId, fprintf(stderr, "msgInfoTab : %s deletion failed: %s\n",
strerror( errno ) ); bufferInfoTabId, strerror(errno));
return -1; return -1;
} }
/* suppression des infos sur buffers FIN */ /* suppression des infos sur buffers FIN */
/* suppression des buffers DEBUT */ /* suppression des buffers DEBUT */
if (msgPoolIdIntern(poolId,spaceId,poolIdx) == -1){ if (msgPoolIdIntern(poolId, spaceId, poolIdx) == -1) {
fprintf( stderr, "msgPoolId creation failed for id %s\n", (char*)poolId ); fprintf(stderr, "msgPoolId creation failed for id %s\n",
return -1; (char *)poolId);
return -1;
} }
if (shm_unlink(poolId) <0 ) { if (shm_unlink(poolId) < 0) {
fprintf( stderr, "msgPool : %s deletion failed: %s\n",poolId, fprintf(stderr, "msgPool : %s deletion failed: %s\n", poolId,
strerror( errno ) ); strerror(errno));
return -1; return -1;
} }
/* suppression des buffers FIN */ /* suppression des buffers FIN */
/* suppression de la sémaphore */ /* suppression de la sémaphore */
if (msgPoolSemIdIntern(poolRessourceSemId,spaceId,poolIdx) == -1){ if (msgPoolSemIdIntern(poolRessourceSemId, spaceId, poolIdx) == -1) {
fprintf( stderr, "msgPoolId creation failed for id %s\n", fprintf(stderr, "msgPoolId creation failed for id %s\n",
(char*)poolRessourceSemId ); (char *)poolRessourceSemId);
return -1; return -1;
} }
// on met un semaphore sur le pool // on met un semaphore sur le pool
sem_unlink(poolRessourceSemId); sem_unlink(poolRessourceSemId);
//TODO: verrifier les erreurs sur l'ouverture de la sem //TODO: verrifier les erreurs sur l'ouverture de la sem
return 0; return 0;
} }

View file

@ -2,66 +2,77 @@
#define _NZG_PROTO 1 #define _NZG_PROTO 1
/* allocate.c */ /* allocate.c */
void *msgAllocate(msgSpace *space, int pool, int taille, int option); void *msgAllocate(msgSpace * space, int pool, int taille, int option);
/* bufferAttachProc.c */ /* bufferAttachProc.c */
int msgBufferAttachProc(msgPoolData *poolDataTabAddr, int poolIndex, int bufferIndex, void *addr); int msgBufferAttachProc(msgPoolData * poolDataTabAddr, int poolIndex,
int bufferIndex, void *addr);
/* buffer.c */ /* buffer.c */
int msgBufferGetAttachedProcIndex(msgPoolData *poolDataTabAddr, int poolIndex, void *addr); int msgBufferGetAttachedProcIndex(msgPoolData * poolDataTabAddr, int poolIndex,
void *addr);
/* bufferDetachProc.c */ /* bufferDetachProc.c */
int msgBufferDetachProc(msgPoolData *poolDataTabAddr, int poolIndex, int bufferIndex, void *addr); int msgBufferDetachProc(msgPoolData * poolDataTabAddr, int poolIndex,
int bufferIndex, void *addr);
/* bufferGetFreeIndex.c */ /* bufferGetFreeIndex.c */
int msgBufferGetFreeIndex(msgPoolData *poolDataTabAddr, int poolIndex); int msgBufferGetFreeIndex(msgPoolData * poolDataTabAddr, int poolIndex);
/* bufferGetProcAttach.c */ /* bufferGetProcAttach.c */
int msgBufferGetProcAttach(msgPoolData *poolDataTabAddr, int poolNb, int *poolIndex, int *bufferIndex, void *addr); int msgBufferGetProcAttach(msgPoolData * poolDataTabAddr, int poolNb,
int *poolIndex, int *bufferIndex, void *addr);
/* bufferInfoTabCreate.c */ /* bufferInfoTabCreate.c */
int msgBufferInfoTabCreate(msgSpaceId externId, msgPoolData *poolDataTabAddr, int poolIdx, int bufferNb); int msgBufferInfoTabCreate(msgSpaceId externId, msgPoolData * poolDataTabAddr,
int poolIdx, int bufferNb);
/* bufferInfoTabInit.c */ /* bufferInfoTabInit.c */
int msgBufferInfoTabInit(msgPoolData *poolDataTabAddr, int poolIndex); int msgBufferInfoTabInit(msgPoolData * poolDataTabAddr, int poolIndex);
/* bufferMap.c */ /* bufferMap.c */
void *msgBufferMap(msgPoolData *poolDataTab, int poolIndex, int bufferIndex); void *msgBufferMap(msgPoolData * poolDataTab, int poolIndex, int bufferIndex);
/* free.c */ /* free.c */
int msgFree(msgSpace *space, void *addr); int msgFree(msgSpace * space, void *addr);
/* get.c */ /* get.c */
void *msgGet(msgSpace *space, int queueIndex, int option); void *msgGet(msgSpace * space, int queueIndex, int option);
/* ids.c */ /* ids.c */
int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src); int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolDataTabSemIdIntern(msgPoolSemId destSemId, const msgSpaceId externId); int msgPoolDataTabSemIdIntern(msgPoolSemId destSemId,
int msgPoolSemIdIntern(msgPoolSemId destSemId, const msgSpaceId srcPoolId, int poolIdx); const msgSpaceId externId);
int msgPoolSemIdIntern(msgPoolSemId destSemId, const msgSpaceId srcPoolId,
int poolIdx);
int msgPoolDataIdIntern(msgPoolDataTabId dest, const msgSpaceId src); int msgPoolDataIdIntern(msgPoolDataTabId dest, const msgSpaceId src);
int msgPoolIdIntern(msgPoolId dest, msgPoolId src, int num); int msgPoolIdIntern(msgPoolId dest, msgPoolId src, int num);
int msgBufferInfoTabIdIntern(msgBufferInfoTabId dest, msgSpaceId src, int num); int msgBufferInfoTabIdIntern(msgBufferInfoTabId dest, msgSpaceId src, int num);
int msgQueueProtSemIdIntern(msgQueueSemId dest, msgSpaceId externId, int queueIdx); int msgQueueProtSemIdIntern(msgQueueSemId dest, msgSpaceId externId,
int msgQueueReadSemIdIntern(msgQueueSemId dest, msgSpaceId externId, int queueIdx); int queueIdx);
int msgQueueReadSemIdIntern(msgQueueSemId dest, msgSpaceId externId,
int queueIdx);
int msgQueueIdIntern(msgQueueId dest, msgSpaceId externId, int queueIdx); int msgQueueIdIntern(msgQueueId dest, msgSpaceId externId, int queueIdx);
int msgQueueElemIdIntern(msgQueueElemId dest, msgQueueId src, int counter); int msgQueueElemIdIntern(msgQueueElemId dest, msgQueueId src, int counter);
int msgSpaceListElemIdIntern(msgSpaceListElemId elemListId, msgSpaceId externId); int msgSpaceListElemIdIntern(msgSpaceListElemId elemListId,
msgSpaceId externId);
/* list.c */ /* list.c */
/* poolCreate.c */ /* poolCreate.c */
int msgPoolCreate(msgSpaceId externId, int poolIdx, int buffNb, int buffSize); int msgPoolCreate(msgSpaceId externId, int poolIdx, int buffNb, int buffSize);
/* poolDataTabClose.c */ /* poolDataTabClose.c */
int msgPoolDataTabClose(msgSpace *space, void *addr); int msgPoolDataTabClose(msgSpace * space, void *addr);
/* poolDataTabCreate.c */ /* poolDataTabCreate.c */
void *msgPoolDataTabCreate(msgSpace *space); void *msgPoolDataTabCreate(msgSpace * space);
/* poolDataTabLock.c */ /* poolDataTabLock.c */
int msgPoolDataTabLock(msgSpace *space); int msgPoolDataTabLock(msgSpace * space);
/* poolDataTabOpen.c */ /* poolDataTabOpen.c */
void *msgPoolDataTabOpen(msgSpace *space); void *msgPoolDataTabOpen(msgSpace * space);
/* poolDataTabUnlock.c */ /* poolDataTabUnlock.c */
int msgPoolDataTabUnlock(msgSpace *space); int msgPoolDataTabUnlock(msgSpace * space);
/* poolDelete.c */ /* poolDelete.c */
int msgPoolDelete(msgSpaceId spaceId, int poolIdx); int msgPoolDelete(msgSpaceId spaceId, int poolIdx);
/* put.c */ /* put.c */
int msgPut(msgSpace *space, int queueIndex, void *addr); int msgPut(msgSpace * space, int queueIndex, void *addr);
/* queueAdd.c */ /* queueAdd.c */
int msgQueueAdd(msgQueue *queue, msgQueueElemId newElemId); int msgQueueAdd(msgQueue * queue, msgQueueElemId newElemId);
/* queueClose.c */ /* queueClose.c */
int msgQueueClose(msgQueue *queue); int msgQueueClose(msgQueue * queue);
/* queueDelete.c */ /* queueDelete.c */
int msgQueueDelete(msgQueueId externId, int queueIdx); int msgQueueDelete(msgQueueId externId, int queueIdx);
/* queueElemClose.c */ /* queueElemClose.c */
int msgQueueElemClose(msgQueueElem *queueElem); int msgQueueElemClose(msgQueueElem * queueElem);
/* queueElemCreate.c */ /* queueElemCreate.c */
int msgQueueElemCreate(msgQueueElemId finalQueueElemId, msgQueueId queueId, int counter); int msgQueueElemCreate(msgQueueElemId finalQueueElemId, msgQueueId queueId,
int counter);
/* queueElemDelete.c */ /* queueElemDelete.c */
int msgQueueElemDelete(msgQueueElemId queueElemId); int msgQueueElemDelete(msgQueueElemId queueElemId);
/* queueElemOpen.c */ /* queueElemOpen.c */
@ -81,21 +92,22 @@ int msgQueueReadTryLock(msgSpaceId externId, int queueIdx);
/* queueReadUnlock.c */ /* queueReadUnlock.c */
int msgQueueReadUnlock(msgSpaceId externId, int queueIdx); int msgQueueReadUnlock(msgSpaceId externId, int queueIdx);
/* queueRem.c */ /* queueRem.c */
int msgQueueRem(msgQueue *queue, msgQueueElemId oldElemId); int msgQueueRem(msgQueue * queue, msgQueueElemId oldElemId);
/* spaceClose.c */ /* spaceClose.c */
int msgSpaceClose(msgSpace *space); int msgSpaceClose(msgSpace * space);
/* spaceCreate.c */ /* spaceCreate.c */
msgSpace *msgSpaceCreate(msgSpaceId externId, int queueNb, int poolNb, msgPool *poolInfos); msgSpace *msgSpaceCreate(msgSpaceId externId, int queueNb, int poolNb,
msgPool * poolInfos);
/* spaceDelete.c */ /* spaceDelete.c */
int msgSpaceDelete(msgSpaceId externId); int msgSpaceDelete(msgSpaceId externId);
/* spaceListAdd.c */ /* spaceListAdd.c */
int msgSpaceListAdd(msgSpaceListElemId newElemId); int msgSpaceListAdd(msgSpaceListElemId newElemId);
/* spaceListClose.c */ /* spaceListClose.c */
int msgSpaceListClose(msgSpaceList *list); int msgSpaceListClose(msgSpaceList * list);
/* spaceListElemClose.c */ /* spaceListElemClose.c */
int msgSpaceListElemClose(msgSpaceListElem *listElem); int msgSpaceListElemClose(msgSpaceListElem * listElem);
/* spaceListElemCreate.c */ /* spaceListElemCreate.c */
int msgSpaceListElemCreate(msgSpaceListElemId listElemId, msgSpace *space); int msgSpaceListElemCreate(msgSpaceListElemId listElemId, msgSpace * space);
/* spaceListElemLink.c */ /* spaceListElemLink.c */
/* spaceListElemOpen.c */ /* spaceListElemOpen.c */
void *msgSpaceListElemOpen(msgSpaceListElemId listElemId); void *msgSpaceListElemOpen(msgSpaceListElemId listElemId);

118
src/proto.h~ Normal file
View file

@ -0,0 +1,118 @@
#ifndef _NZG_PROTO
#define _NZG_PROTO 1
/* allocate.c */
void *msgAllocate(msgSpace *space, int pool, int taille, int option);
/* bufferAttachProc.c */
int msgBufferAttachProc(msgPoolData *poolDataTabAddr, int poolIndex, int bufferIndex, void *addr);
/* buffer.c */
int msgBufferGetAttachedProcIndex(msgPoolData *poolDataTabAddr, int poolIndex, void *addr);
/* bufferDetachProc.c */
int msgBufferDetachProc(msgPoolData *poolDataTabAddr, int poolIndex, int bufferIndex, void *addr);
/* bufferGetFreeIndex.c */
int msgBufferGetFreeIndex(msgPoolData *poolDataTabAddr, int poolIndex);
/* bufferGetProcAttach.c */
int msgBufferGetProcAttach(msgPoolData *poolDataTabAddr, int poolNb, int *poolIndex, int *bufferIndex, void *addr);
/* bufferInfoTabCreate.c */
int msgBufferInfoTabCreate(msgSpaceId externId, msgPoolData *poolDataTabAddr, int poolIdx, int bufferNb);
/* bufferInfoTabInit.c */
int msgBufferInfoTabInit(msgPoolData *poolDataTabAddr, int poolIndex);
/* bufferMap.c */
void *msgBufferMap(msgPoolData *poolDataTab, int poolIndex, int bufferIndex);
/* free.c */
int msgFree(msgSpace *space, void *addr);
/* get.c */
void *msgGet(msgSpace *space, int queueIndex, int option);
/* ids.c */
int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolDataTabSemIdIntern(msgPoolSemId destSemId, const msgSpaceId externId);
int msgPoolSemIdIntern(msgPoolSemId destSemId, const msgSpaceId srcPoolId, int poolIdx);
int msgPoolDataIdIntern(msgPoolDataTabId dest, const msgSpaceId src);
int msgPoolIdIntern(msgPoolId dest, msgPoolId src, int num);
int msgBufferInfoTabIdIntern(msgBufferInfoTabId dest, msgSpaceId src, int num);
int msgQueueProtSemIdIntern(msgQueueSemId dest, msgSpaceId externId, int queueIdx);
int msgQueueReadSemIdIntern(msgQueueSemId dest, msgSpaceId externId, int queueIdx);
int msgQueueIdIntern(msgQueueId dest, msgSpaceId externId, int queueIdx);
int msgQueueElemIdIntern(msgQueueElemId dest, msgQueueId src, int counter);
int msgSpaceListElemIdIntern(msgSpaceListElemId elemListId, msgSpaceId externId);
/* list.c */
/* poolCreate.c */
int msgPoolCreate(msgSpaceId externId, int poolIdx, int buffNb, int buffSize);
/* poolDataTabClose.c */
int msgPoolDataTabClose(msgSpace *space, void *addr);
/* poolDataTabCreate.c */
void *msgPoolDataTabCreate(msgSpace *space);
/* poolDataTabLock.c */
int msgPoolDataTabLock(msgSpace *space);
/* poolDataTabOpen.c */
void *msgPoolDataTabOpen(msgSpace *space);
/* poolDataTabUnlock.c */
int msgPoolDataTabUnlock(msgSpace *space);
/* poolDelete.c */
int msgPoolDelete(msgSpaceId spaceId, int poolIdx);
/* put.c */
int msgPut(msgSpace *space, int queueIndex, void *addr);
/* queueAdd.c */
int msgQueueAdd(msgQueue *queue, msgQueueElemId newElemId);
/* queueClose.c */
int msgQueueClose(msgQueue *queue);
/* queueDelete.c */
int msgQueueDelete(msgQueueId externId, int queueIdx);
/* queueElemClose.c */
int msgQueueElemClose(msgQueueElem *queueElem);
/* queueElemCreate.c */
int msgQueueElemCreate(msgQueueElemId finalQueueElemId, msgQueueId queueId, int counter);
/* queueElemDelete.c */
int msgQueueElemDelete(msgQueueElemId queueElemId);
/* queueElemOpen.c */
void *msgQueueElemOpen(msgQueueElemId queueElemId);
/* queueInit.c */
msgQueue *msgQueueInit(msgSpaceId externId, int queueIdx);
/* queueOpen.c */
void *msgQueueOpen(msgQueueId queueId);
/* queueProtLock.c */
int msgQueueProtLock(msgSpaceId externId, int queueIdx);
/* queueProtUnlock.c */
int msgQueueProtUnlock(msgSpaceId externId, int queueIdx);
/* queueReadLock.c */
int msgQueueReadLock(msgSpaceId externId, int queueIdx);
/* queueReadTryLock.c */
int msgQueueReadTryLock(msgSpaceId externId, int queueIdx);
/* queueReadUnlock.c */
int msgQueueReadUnlock(msgSpaceId externId, int queueIdx);
/* queueRem.c */
int msgQueueRem(msgQueue *queue, msgQueueElemId oldElemId);
/* spaceClose.c */
int msgSpaceClose(msgSpace *space);
/* spaceCreate.c */
msgSpace *msgSpaceCreate(msgSpaceId externId, int queueNb, int poolNb, msgPool *poolInfos);
/* spaceDelete.c */
int msgSpaceDelete(msgSpaceId externId);
/* spaceListAdd.c */
int msgSpaceListAdd(msgSpaceListElemId newElemId);
/* spaceListClose.c */
int msgSpaceListClose(msgSpaceList *list);
/* spaceListElemClose.c */
int msgSpaceListElemClose(msgSpaceListElem *listElem);
/* spaceListElemCreate.c */
int msgSpaceListElemCreate(msgSpaceListElemId listElemId, msgSpace *space);
/* spaceListElemLink.c */
/* spaceListElemOpen.c */
void *msgSpaceListElemOpen(msgSpaceListElemId listElemId);
/* spaceListFindId.c */
int msgSpaceListFindId(msgSpaceId externId);
int msgSpaceListElemFindId(msgSpaceListElemId elemId, msgSpaceId spaceId);
/* spaceListInit.c */
int msgSpaceListInit(void);
/* spaceListLocking.c */
int msgSpaceListLock(void);
int msgSpaceListUnlock(void);
/* spaceListOpen.c */
void *msgSpaceListOpen(void);
/* spaceListRem.c */
int msgSpaceListRem(msgSpaceId spaceId);
/* spaceOpen.c */
msgSpace *msgSpaceOpen(msgSpaceId externId);
/* spaceState.c */
#endif

116
src/put.c
View file

@ -1,74 +1,66 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgPut(msgSpace * space,int queueIndex, void * addr){ int msgPut(msgSpace * space, int queueIndex, void *addr)
// retrouver le pool, buffer qui correspondent à l'addresse... {
msgPoolData * poolDataTabAddr; // retrouver le pool, buffer qui correspondent à l'addresse...
int poolIndex; msgPoolData *poolDataTabAddr;
int bufferIndex; int poolIndex;
int err; int bufferIndex;
msgQueueElemId newElemId; int err;
msgQueueElem * queueElem; msgQueueElemId newElemId;
msgQueueId queueId; msgQueueElem *queueElem;
msgQueue * queue; msgQueueId queueId;
msgQueue *queue;
msgPoolDataTabLock(space); msgPoolDataTabLock(space);
poolDataTabAddr=msgPoolDataTabOpen(space); poolDataTabAddr = msgPoolDataTabOpen(space);
if (poolDataTabAddr == NULL){ if (poolDataTabAddr == NULL) {
NZG_ERROR("msgPoolDataTabOpen",space->poolDataTabId); NZG_ERROR("msgPoolDataTabOpen", space->poolDataTabId);
goto ERROR; goto ERROR;
} }
err = msgBufferGetProcAttach( err = msgBufferGetProcAttach(poolDataTabAddr,
poolDataTabAddr, space->poolNb,
space->poolNb, &poolIndex, &bufferIndex, addr);
&poolIndex, if (err) {
&bufferIndex, //FIXME
addr }
); // ouvrir la queue avec le bon index
if (err) { msgQueueIdIntern(queueId, space->externId, queueIndex);
//FIXME
}
// ouvrir la queue avec le bon index msgQueueProtLock(space->externId, queueIndex);
msgQueueIdIntern(queueId,space->externId,queueIndex); queue = msgQueueOpen(queueId);
msgQueueProtLock(space->externId,queueIndex); // creer un element vide
queue = msgQueueOpen(queueId);
// creer un element vide msgQueueElemCreate(newElemId, queueId, queue->elemCounter);
// ouvrir l'element
queueElem = msgQueueElemOpen(newElemId);
msgQueueElemCreate(newElemId,queueId,queue->elemCounter); // modifier les index pour retrouver le buffer
// ouvrir l'element queueElem->poolIndex = poolIndex;
queueElem=msgQueueElemOpen(newElemId); queueElem->bufferIndex = bufferIndex;
// modifier les index pour retrouver le buffer // fermer l'element
queueElem->poolIndex=poolIndex; if (msgQueueElemClose(queueElem) < 0) {
queueElem->bufferIndex=bufferIndex; NZG_ERROR("msgQueueElemClose", "");
goto ERROR;
}
//ajouter le message a la bonne file...
if (msgQueueAdd(queue, newElemId) < 0) {
NZG_ERROR("msgQueueAdd", newElemId);
goto ERROR;
}
// fermer la file
msgQueueClose(queue);
msgQueueProtUnlock(space->externId, queueIndex);
// fermer l'element msgPoolDataTabClose(space, poolDataTabAddr);
if (msgQueueElemClose(queueElem) <0 ){ msgPoolDataTabUnlock(space);
NZG_ERROR("msgQueueElemClose","");
goto ERROR;
}
//ajouter le message a la bonne file... // on laisse une nouvelle ressource de la liste au get
if (msgQueueAdd(queue,newElemId) < 0){ msgQueueReadUnlock(space->externId, queueIndex);
NZG_ERROR("msgQueueAdd",newElemId); return 0;
goto ERROR;
}
// fermer la file ERROR:
msgQueueClose(queue); return -1;
msgQueueProtUnlock(space->externId,queueIndex);
msgPoolDataTabClose(space,poolDataTabAddr);
msgPoolDataTabUnlock(space);
// on laisse une nouvelle ressource de la liste au get
msgQueueReadUnlock(space->externId,queueIndex);
return 0;
ERROR:
return -1;
} }

View file

@ -2,47 +2,48 @@
// on ajoute en fin de queue... // on ajoute en fin de queue...
int msgQueueAdd(msgQueue * queue, msgQueueElemId newElemId){ int msgQueueAdd(msgQueue * queue, msgQueueElemId newElemId)
msgQueueElem * queueOldTailElem; {
msgQueueElem * queueNewTailElem; msgQueueElem *queueOldTailElem;
msgQueueElem *queueNewTailElem;
// on informe l'element qu'il est le dernier // on informe l'element qu'il est le dernier
queueNewTailElem=msgQueueElemOpen(newElemId); queueNewTailElem = msgQueueElemOpen(newElemId);
if (queueNewTailElem == NULL){ if (queueNewTailElem == NULL) {
NZG_ERROR("msgQueueElemOpen",newElemId); NZG_ERROR("msgQueueElemOpen", newElemId);
goto ERROR; goto ERROR;
}
strcpy(queueNewTailElem->next, newElemId);
if (msgQueueElemClose(queueNewTailElem) < 0) {
NZG_ERROR("msgQueueElemClose", newElemId);
goto ERROR;
} }
strcpy(queueNewTailElem->next,newElemId);
if (msgQueueElemClose(queueNewTailElem) <0 ){
NZG_ERROR("msgQueueElemClose",newElemId);
goto ERROR;
}
/* TODO: verifier si la liste n'est pas vide... */ /* TODO: verifier si la liste n'est pas vide... */
if((strcmp(queue->headId,queue->id)==0) if ((strcmp(queue->headId, queue->id) == 0)
&& (strcmp(queue->tailId,queue->id)==0)) { && (strcmp(queue->tailId, queue->id) == 0)) {
printf("- premier elem de queue -\n"); printf("- premier elem de queue -\n");
// on donne a la queue l'id de l'element // on donne a la queue l'id de l'element
strcpy(queue->headId,newElemId); strcpy(queue->headId, newElemId);
strcpy(queue->tailId,newElemId); strcpy(queue->tailId, newElemId);
} else { } else {
/* on informe son prédecesseur qu'il a un suivant */ /* on informe son prédecesseur qu'il a un suivant */
queueOldTailElem=msgQueueElemOpen(queue->tailId); queueOldTailElem = msgQueueElemOpen(queue->tailId);
if (queueOldTailElem == NULL){ if (queueOldTailElem == NULL) {
NZG_ERROR("msgQueueElemOpen",queue->tailId); NZG_ERROR("msgQueueElemOpen", queue->tailId);
goto ERROR; goto ERROR;
} }
strcpy(queueOldTailElem->next,newElemId); strcpy(queueOldTailElem->next, newElemId);
if (msgQueueElemClose(queueOldTailElem) < 0){ if (msgQueueElemClose(queueOldTailElem) < 0) {
NZG_ERROR("msgQueueElemClose",queue->tailId); NZG_ERROR("msgQueueElemClose", queue->tailId);
goto ERROR; goto ERROR;
} }
// on donne a la queue l'id du dernier element // on donne a la queue l'id du dernier element
strcpy(queue->tailId,newElemId); strcpy(queue->tailId, newElemId);
} }
// on incremente elemCounter // on incremente elemCounter
queue->elemCounter++; queue->elemCounter++;
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -1,16 +1,17 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
int msgQueueClose(msgQueue * queue){ int msgQueueClose(msgQueue * queue)
msgQueueId qId; {
strcpy(qId,queue->id); msgQueueId qId;
strcpy(qId, queue->id);
if (munmap(queue,sizeof(msgQueue)) < 0){
NZG_ERROR("unmap",qId);
goto ERROR;
}
return 0; if (munmap(queue, sizeof(msgQueue)) < 0) {
ERROR: NZG_ERROR("unmap", qId);
return -1; goto ERROR;
}
return 0;
ERROR:
return -1;
} }

View file

@ -1,35 +1,34 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgQueueDelete(msgQueueId externId,int queueIdx){ int msgQueueDelete(msgQueueId externId, int queueIdx)
msgQueue * queue; {
msgQueueId queueId; msgQueue *queue;
msgQueueSemId queueProtLockSemId; msgQueueId queueId;
msgQueueSemId queueReadLockSemId; msgQueueSemId queueProtLockSemId;
msgQueueSemId queueReadLockSemId;
msgQueueIdIntern(queueId,externId,queueIdx);
queue = msgQueueOpen(queueId);
if (strcmp(queue->headId,queue->id)!=0){ msgQueueIdIntern(queueId, externId, queueIdx);
// liste non-vide queue = msgQueueOpen(queueId);
if (msgQueueElemDelete(queue->headId) <0){
NZG_ERROR("msgQueueElemDelete",queue->headId); if (strcmp(queue->headId, queue->id) != 0) {
goto ERROR; // liste non-vide
if (msgQueueElemDelete(queue->headId) < 0) {
NZG_ERROR("msgQueueElemDelete", queue->headId);
goto ERROR;
}
} }
}
msgQueueProtSemIdIntern(queueProtLockSemId,externId,queueIdx);
msgQueueReadSemIdIntern(queueReadLockSemId,externId,queueIdx);
sem_unlink(queueProtLockSemId);
sem_unlink(queueReadLockSemId);
if (shm_unlink(queueId) < 0){ msgQueueProtSemIdIntern(queueProtLockSemId, externId, queueIdx);
NZG_ERROR("shm_unlink msgQueueElem",queueId); msgQueueReadSemIdIntern(queueReadLockSemId, externId, queueIdx);
goto ERROR; sem_unlink(queueProtLockSemId);
} sem_unlink(queueReadLockSemId);
return 0; if (shm_unlink(queueId) < 0) {
ERROR: NZG_ERROR("shm_unlink msgQueueElem", queueId);
return -1; goto ERROR;
}
return 0;
ERROR:
return -1;
} }

View file

@ -1,15 +1,16 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgQueueElemClose(msgQueueElem * queueElem){ int msgQueueElemClose(msgQueueElem * queueElem)
msgQueueElemId qId; {
strcpy(qId,queueElem->id); msgQueueElemId qId;
strcpy(qId, queueElem->id);
if (munmap(queueElem,sizeof(msgQueueElem)) < 0){
NZG_ERROR("unmap",qId);
goto ERROR;
}
return 0; if (munmap(queueElem, sizeof(msgQueueElem)) < 0) {
ERROR: NZG_ERROR("unmap", qId);
return -1; goto ERROR;
}
return 0;
ERROR:
return -1;
} }

View file

@ -1,31 +1,30 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
int msgQueueElemCreate( int msgQueueElemCreate(msgQueueElemId finalQueueElemId,
msgQueueElemId finalQueueElemId , msgQueueId queueId, int counter)
msgQueueId queueId, {
int counter){ msgQueueElemId queueElemId;
msgQueueElemId queueElemId; int queueElemFd;
int queueElemFd;
msgQueueElemIdIntern(queueElemId,queueId,counter); msgQueueElemIdIntern(queueElemId, queueId, counter);
strcpy(finalQueueElemId,queueElemId); strcpy(finalQueueElemId, queueElemId);
queueElemFd=shm_open(queueElemId, queueElemFd = shm_open(queueElemId,
O_RDWR|O_CREAT|O_EXCL|O_TRUNC, O_RDWR | O_CREAT | O_EXCL | O_TRUNC,
SHM_DEFAULT_MODE); SHM_DEFAULT_MODE);
if (queueElemFd == -1 ) { if (queueElemFd == -1) {
NZG_ERROR("shm_open : msgQueueElem creation",queueElemId); NZG_ERROR("shm_open : msgQueueElem creation", queueElemId);
goto ERROR; goto ERROR;
} }
if (ftruncate(queueElemFd, sizeof(msgQueueElem)) < 0){ if (ftruncate(queueElemFd, sizeof(msgQueueElem)) < 0) {
NZG_ERROR("ftruncate", queueElemId); NZG_ERROR("ftruncate", queueElemId);
goto ERROR; goto ERROR;
} }
close(queueElemFd); close(queueElemFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -1,33 +1,33 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgQueueElemDelete(msgQueueElemId queueElemId){ int msgQueueElemDelete(msgQueueElemId queueElemId)
msgQueueElem * queueElem; {
msgQueueElemId nextIdToDie; msgQueueElem *queueElem;
msgQueueElemId nextIdToDie;
queueElem = msgQueueElemOpen(queueElemId);
if (queueElem==NULL){
NZG_ERROR("msgQueueElemOpen",queueElemId);
goto ERROR;
}
strcpy(nextIdToDie, queueElem->next); queueElem = msgQueueElemOpen(queueElemId);
if (queueElem == NULL) {
if (msgQueueElemClose(queueElem) < 0){ NZG_ERROR("msgQueueElemOpen", queueElemId);
NZG_ERROR("msgQueueElemClose",queueElemId); goto ERROR;
goto ERROR; }
}
// on détruit l'élément strcpy(nextIdToDie, queueElem->next);
shm_unlink(queueElemId);
if (nextIdToDie != queueElemId){ if (msgQueueElemClose(queueElem) < 0) {
return msgQueueElemDelete(nextIdToDie); NZG_ERROR("msgQueueElemClose", queueElemId);
} else { goto ERROR;
printf("msgQueueElemDelete: EOL reached \n"); }
// element unique... // on détruit l'élément
return 0; shm_unlink(queueElemId);
}
ERROR: if (nextIdToDie != queueElemId) {
return -1; return msgQueueElemDelete(nextIdToDie);
} else {
printf("msgQueueElemDelete: EOL reached \n");
// element unique...
return 0;
}
ERROR:
return -1;
} }

View file

@ -1,29 +1,28 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
void * msgQueueElemOpen(msgQueueElemId queueElemId){ void *msgQueueElemOpen(msgQueueElemId queueElemId)
int queueElemFd; {
void * queueElemAddr; int queueElemFd;
void *queueElemAddr;
queueElemFd=shm_open(queueElemId,O_RDWR,SHM_DEFAULT_MODE); queueElemFd = shm_open(queueElemId, O_RDWR, SHM_DEFAULT_MODE);
if (queueElemFd == -1 ) { if (queueElemFd == -1) {
NZG_ERROR("shm_open : msgQueue open",queueElemId); NZG_ERROR("shm_open : msgQueue open", queueElemId);
goto ERROR; goto ERROR;
} }
queueElemAddr=mmap(NULL, queueElemAddr = mmap(NULL,
sizeof(msgQueueElem), sizeof(msgQueueElem),
PROT_READ|PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED, MAP_SHARED, queueElemFd, 0);
queueElemFd, if (queueElemAddr == MAP_FAILED) {
0); NZG_ERROR("mmap", queueElemId);
if( queueElemAddr == MAP_FAILED ) { goto ERROR;
NZG_ERROR("mmap",queueElemId); }
goto ERROR;
}
close(queueElemFd); close(queueElemFd);
return queueElemAddr; return queueElemAddr;
ERROR: ERROR:
return NULL; return NULL;
} }

View file

@ -1,35 +1,34 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
msgQueue * msgQueueInit(msgSpaceId externId, int queueIdx) { msgQueue *msgQueueInit(msgSpaceId externId, int queueIdx)
{
int queueFd; int queueFd;
msgQueue * queue; msgQueue *queue;
sem_t * semProtectFd; sem_t *semProtectFd;
msgQueueSemId queueSemProtectId; msgQueueSemId queueSemProtectId;
sem_t * semReadableFd; sem_t *semReadableFd;
msgQueueSemId queueSemReadableId; msgQueueSemId queueSemReadableId;
msgQueueId queueId; msgQueueId queueId;
queue = NULL; queue = NULL;
msgQueueProtSemIdIntern(queueSemProtectId, externId, queueIdx);
msgQueueProtSemIdIntern(queueSemProtectId,externId,queueIdx); msgQueueReadSemIdIntern(queueSemReadableId, externId, queueIdx);
msgQueueReadSemIdIntern(queueSemReadableId,externId,queueIdx);
// creation du semaphore de lecture // creation du semaphore de lecture
semReadableFd = sem_open(queueSemReadableId, semReadableFd = sem_open(queueSemReadableId,
O_CREAT|O_EXCL, SEM_DEFAULT_MODE, 0); O_CREAT | O_EXCL, SEM_DEFAULT_MODE, 0);
if(semReadableFd == SEM_FAILED) { if (semReadableFd == SEM_FAILED) {
NZG_ERROR("sem_open", queueSemReadableId); NZG_ERROR("sem_open", queueSemReadableId);
goto ERROR; goto ERROR;
} }
// creation du semaphore de protection // creation du semaphore de protection
semProtectFd = sem_open(queueSemProtectId, semProtectFd = sem_open(queueSemProtectId,
O_CREAT|O_EXCL, SEM_DEFAULT_MODE, 0); O_CREAT | O_EXCL, SEM_DEFAULT_MODE, 0);
if(semProtectFd == SEM_FAILED) { if (semProtectFd == SEM_FAILED) {
NZG_ERROR("sem_open", queueSemProtectId); NZG_ERROR("sem_open", queueSemProtectId);
goto ERROR; goto ERROR;
} }
/* if(sem_wait(semProtectFd)==-1){ /* if(sem_wait(semProtectFd)==-1){
@ -37,35 +36,36 @@ msgQueue * msgQueueInit(msgSpaceId externId, int queueIdx) {
goto ERROR; goto ERROR;
} */ } */
if(msgQueueIdIntern(queueId, externId, queueIdx) < 0) { if (msgQueueIdIntern(queueId, externId, queueIdx) < 0) {
return NULL; return NULL;
} }
queueFd = shm_open(queueId, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, MSGSPACE_DEFAULT_MODE); queueFd =
if(queueFd == -1) { shm_open(queueId, O_RDWR | O_CREAT | O_EXCL | O_TRUNC,
NZG_ERROR("shm_open : queueInit",queueId); MSGSPACE_DEFAULT_MODE);
return NULL; if (queueFd == -1) {
NZG_ERROR("shm_open : queueInit", queueId);
return NULL;
} }
if(ftruncate(queueFd, sizeof(msgQueue)) == -1) { if (ftruncate(queueFd, sizeof(msgQueue)) == -1) {
fprintf( stderr, "Queue resizing failed: %s\n", fprintf(stderr, "Queue resizing failed: %s\n", strerror(errno));
strerror( errno ) ); return NULL;
return NULL;
} }
queue=msgQueueOpen(queueId); queue = msgQueueOpen(queueId);
/* on remplit la structure msgQueue */ /* on remplit la structure msgQueue */
queue->elemCounter = 0; queue->elemCounter = 0;
strcpy(queue->id,queueId); strcpy(queue->id, queueId);
strcpy(queue->headId,queue->id); strcpy(queue->headId, queue->id);
strcpy(queue->tailId,queue->id); strcpy(queue->tailId, queue->id);
/* on ferme tout ce qu'il faut */ /* on ferme tout ce qu'il faut */
close(queueFd); close(queueFd);
msgQueueProtUnlock(externId,queueIdx); msgQueueProtUnlock(externId, queueIdx);
return queue; return queue;
ERROR: ERROR:
return NULL; return NULL;
} }

View file

@ -1,29 +1,27 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
void * msgQueueOpen(msgQueueId queueId){ void *msgQueueOpen(msgQueueId queueId)
int queueFd; {
void * queueAddr; int queueFd;
void *queueAddr;
queueFd=shm_open(queueId,O_RDWR,SHM_DEFAULT_MODE); queueFd = shm_open(queueId, O_RDWR, SHM_DEFAULT_MODE);
if (queueFd == -1 ) { if (queueFd == -1) {
NZG_ERROR("shm_open : msgQueue open",queueId); NZG_ERROR("shm_open : msgQueue open", queueId);
goto ERROR; goto ERROR;
} }
queueAddr=mmap(NULL, queueAddr = mmap(NULL,
sizeof(msgQueue), sizeof(msgQueue),
PROT_READ|PROT_WRITE, PROT_READ | PROT_WRITE, MAP_SHARED, queueFd, 0);
MAP_SHARED, if (queueAddr == MAP_FAILED) {
queueFd, NZG_ERROR("mmap", queueId);
0); goto ERROR;
if( queueAddr == MAP_FAILED ) { }
NZG_ERROR("mmap",queueId);
goto ERROR;
}
close(queueFd); close(queueFd);
return queueAddr; return queueAddr;
ERROR: ERROR:
return NULL; return NULL;
} }

View file

@ -2,32 +2,36 @@
#include "ids.h" #include "ids.h"
#define DEBUG 0 #define DEBUG 0
int msgQueueProtLock(msgSpaceId externId,int queueIdx){ int msgQueueProtLock(msgSpaceId externId, int queueIdx)
int semval; {
sem_t * queueSemFd; int semval;
msgQueueSemId queueSemId; sem_t *queueSemFd;
msgQueueSemId queueSemId;
msgQueueProtSemIdIntern(queueSemId,externId,queueIdx); msgQueueProtSemIdIntern(queueSemId, externId, queueIdx);
if (DEBUG) { printf("Locking %s\n",queueSemId);} if (DEBUG) {
queueSemFd=sem_open(queueSemId,O_CREAT,SEM_DEFAULT_MODE,1); printf("Locking %s\n", queueSemId);
if(queueSemFd==SEM_FAILED){ }
NZG_ERROR("sem_open",queueSemId); queueSemFd = sem_open(queueSemId, O_CREAT, SEM_DEFAULT_MODE, 1);
goto ERROR; if (queueSemFd == SEM_FAILED) {
} NZG_ERROR("sem_open", queueSemId);
goto ERROR;
}
if(sem_wait(queueSemFd)==-1){ if (sem_wait(queueSemFd) == -1) {
NZG_ERROR("sem_wait",queueSemId); NZG_ERROR("sem_wait", queueSemId);
goto ERROR; goto ERROR;
} }
semval=0; semval = 0;
sem_getvalue(queueSemFd,&semval); sem_getvalue(queueSemFd, &semval);
if (DEBUG) { printf("(AfterValue:%d)\n",semval); } if (DEBUG) {
printf("(AfterValue:%d)\n", semval);
}
sem_close(queueSemFd); sem_close(queueSemFd);
sem_close(queueSemFd); sem_close(queueSemFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -3,32 +3,36 @@
#define DEBUG 0 #define DEBUG 0
int msgQueueProtUnlock(msgSpaceId externId,int queueIdx){ int msgQueueProtUnlock(msgSpaceId externId, int queueIdx)
int semval; {
sem_t * queueSemFd; int semval;
msgQueueSemId queueSemId; sem_t *queueSemFd;
msgQueueSemId queueSemId;
msgQueueProtSemIdIntern(queueSemId,externId,queueIdx); msgQueueProtSemIdIntern(queueSemId, externId, queueIdx);
if (DEBUG) { printf("Unlocking %s\n",queueSemId); } if (DEBUG) {
printf("Unlocking %s\n", queueSemId);
}
queueSemFd=sem_open(queueSemId,O_CREAT,SEM_DEFAULT_MODE,1); queueSemFd = sem_open(queueSemId, O_CREAT, SEM_DEFAULT_MODE, 1);
if(queueSemFd==SEM_FAILED){ if (queueSemFd == SEM_FAILED) {
NZG_ERROR("sem_open",queueSemId); NZG_ERROR("sem_open", queueSemId);
goto ERROR; goto ERROR;
} }
if(sem_post(queueSemFd)==-1){ if (sem_post(queueSemFd) == -1) {
NZG_ERROR("sem_post",queueSemId); NZG_ERROR("sem_post", queueSemId);
goto ERROR; goto ERROR;
} }
semval=0; semval = 0;
sem_getvalue(queueSemFd,&semval); sem_getvalue(queueSemFd, &semval);
if (DEBUG) { printf("(AfterValue:%d)\n",semval); } if (DEBUG) {
printf("(AfterValue:%d)\n", semval);
}
sem_close(queueSemFd); sem_close(queueSemFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -3,31 +3,35 @@
#define DEBUG 0 #define DEBUG 0
int msgQueueReadLock(msgSpaceId externId,int queueIdx){ int msgQueueReadLock(msgSpaceId externId, int queueIdx)
int semval; {
sem_t * queueSemFd; int semval;
msgQueueSemId queueSemId; sem_t *queueSemFd;
msgQueueSemId queueSemId;
msgQueueReadSemIdIntern(queueSemId,externId,queueIdx); msgQueueReadSemIdIntern(queueSemId, externId, queueIdx);
if (DEBUG) { printf("Locking %s\n",queueSemId); } if (DEBUG) {
queueSemFd=sem_open(queueSemId,O_CREAT,SEM_DEFAULT_MODE,1); printf("Locking %s\n", queueSemId);
if(queueSemFd==SEM_FAILED){ }
NZG_ERROR("sem_open",queueSemId); queueSemFd = sem_open(queueSemId, O_CREAT, SEM_DEFAULT_MODE, 1);
goto ERROR; if (queueSemFd == SEM_FAILED) {
} NZG_ERROR("sem_open", queueSemId);
goto ERROR;
}
if(sem_wait(queueSemFd)==-1){ if (sem_wait(queueSemFd) == -1) {
NZG_ERROR("sem_wait",queueSemId); NZG_ERROR("sem_wait", queueSemId);
goto ERROR; goto ERROR;
} }
semval=0; semval = 0;
sem_getvalue(queueSemFd,&semval); sem_getvalue(queueSemFd, &semval);
if (DEBUG) { printf("(AfterValue:%d)\n",semval); } if (DEBUG) {
printf("(AfterValue:%d)\n", semval);
}
sem_close(queueSemFd); sem_close(queueSemFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -3,31 +3,35 @@
#define DEBUG 0 #define DEBUG 0
int msgQueueReadTryLock(msgSpaceId externId,int queueIdx){ int msgQueueReadTryLock(msgSpaceId externId, int queueIdx)
int semval; {
sem_t * queueSemFd; int semval;
msgQueueSemId queueSemId; sem_t *queueSemFd;
msgQueueSemId queueSemId;
msgQueueReadSemIdIntern(queueSemId,externId,queueIdx); msgQueueReadSemIdIntern(queueSemId, externId, queueIdx);
queueSemFd=sem_open(queueSemId,O_CREAT,SEM_DEFAULT_MODE,1); queueSemFd = sem_open(queueSemId, O_CREAT, SEM_DEFAULT_MODE, 1);
if(queueSemFd==SEM_FAILED){ if (queueSemFd == SEM_FAILED) {
NZG_ERROR("sem_open",queueSemId); NZG_ERROR("sem_open", queueSemId);
goto ERROR; goto ERROR;
} }
if(sem_trywait(queueSemFd)==-1){ if (sem_trywait(queueSemFd) == -1) {
NZG_ERROR("sem_wait",queueSemId); NZG_ERROR("sem_wait", queueSemId);
goto ERROR; goto ERROR;
} }
if (DEBUG) { printf("Locking %s\n",queueSemId); } if (DEBUG) {
semval=0; printf("Locking %s\n", queueSemId);
sem_getvalue(queueSemFd,&semval); }
if (DEBUG) { printf("(AfterValue:%d)\n",semval); } semval = 0;
sem_getvalue(queueSemFd, &semval);
if (DEBUG) {
printf("(AfterValue:%d)\n", semval);
}
sem_close(queueSemFd); sem_close(queueSemFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -3,32 +3,35 @@
#define DEBUG 0 #define DEBUG 0
int msgQueueReadUnlock(msgSpaceId externId,int queueIdx){ int msgQueueReadUnlock(msgSpaceId externId, int queueIdx)
int semval; {
sem_t * queueSemFd; int semval;
msgQueueSemId queueSemId; sem_t *queueSemFd;
msgQueueSemId queueSemId;
msgQueueReadSemIdIntern(queueSemId,externId,queueIdx); msgQueueReadSemIdIntern(queueSemId, externId, queueIdx);
if (DEBUG) { printf("Unlocking %s\n",queueSemId); } if (DEBUG) {
queueSemFd=sem_open(queueSemId,O_CREAT,SEM_DEFAULT_MODE,1); printf("Unlocking %s\n", queueSemId);
if(queueSemFd==SEM_FAILED){ }
NZG_ERROR("sem_open",queueSemId); queueSemFd = sem_open(queueSemId, O_CREAT, SEM_DEFAULT_MODE, 1);
goto ERROR; if (queueSemFd == SEM_FAILED) {
} NZG_ERROR("sem_open", queueSemId);
goto ERROR;
}
if(sem_post(queueSemFd)==-1){ if (sem_post(queueSemFd) == -1) {
NZG_ERROR("sem_post",queueSemId); NZG_ERROR("sem_post", queueSemId);
goto ERROR; goto ERROR;
} }
semval=0; semval = 0;
sem_getvalue(queueSemFd,&semval); sem_getvalue(queueSemFd, &semval);
if (DEBUG) { printf("(AfterValue:%d)\n",semval); } if (DEBUG) {
printf("(AfterValue:%d)\n", semval);
}
sem_close(queueSemFd);
sem_close(queueSemFd); return 0;
ERROR:
return 0; return -1;
ERROR:
return -1;
} }

View file

@ -2,37 +2,36 @@
// l'id de l'element enlevé vas dans oldElemId // l'id de l'element enlevé vas dans oldElemId
int msgQueueRem(msgQueue * queue, msgQueueElemId oldElemId){ int msgQueueRem(msgQueue * queue, msgQueueElemId oldElemId)
msgQueueElem * queueOldHeadElem; {
msgQueueElem *queueOldHeadElem;
if (strcmp(queue->headId, queue->id) == 0) {
if (strcmp(queue->headId,queue->id)==0){ NZG_ERROR("msgQueueElemRem : Empty queue", queue->id);
NZG_ERROR("msgQueueElemRem : Empty queue",queue->id); goto ERROR;
goto ERROR; }
} strcpy(oldElemId, queue->headId);
strcpy(oldElemId,queue->headId);
queueOldHeadElem=msgQueueElemOpen(oldElemId); queueOldHeadElem = msgQueueElemOpen(oldElemId);
if (queueOldHeadElem == NULL){ if (queueOldHeadElem == NULL) {
NZG_ERROR("msgQueueElemOpen",oldElemId); NZG_ERROR("msgQueueElemOpen", oldElemId);
goto ERROR; goto ERROR;
} }
// on indique à la queue le nouveau premier element
strcpy(queue->headId, queueOldHeadElem->next);
if (strcmp(queueOldHeadElem->next, oldElemId) == 0) {
strcpy(queue->headId, queue->id);
}
// on fait en sorte que l'element ne connaisse plus
// ses voisins (utile pour le Delete)
strcpy(queueOldHeadElem->next, queueOldHeadElem->id);
// on indique à la queue le nouveau premier element if (msgQueueElemClose(queueOldHeadElem) < 0) {
strcpy(queue->headId,queueOldHeadElem->next); NZG_ERROR("msgQueueElemClose", oldElemId);
if (strcmp(queueOldHeadElem->next,oldElemId)==0){ goto ERROR;
strcpy(queue->headId,queue->id);
}
// on fait en sorte que l'element ne connaisse plus
// ses voisins (utile pour le Delete)
strcpy(queueOldHeadElem->next,queueOldHeadElem->id);
if (msgQueueElemClose(queueOldHeadElem)<0 ){ }
NZG_ERROR("msgQueueElemClose",oldElemId); return 0;
goto ERROR; ERROR:
return -1;
}
return 0;
ERROR:
return -1;
} }

View file

@ -1,15 +1,16 @@
#include "libnazgul.h" #include "libnazgul.h"
int msgSpaceClose(msgSpace * space){ int msgSpaceClose(msgSpace * space)
msgSpaceId spaceId; {
strcpy(spaceId,space->id); msgSpaceId spaceId;
strcpy(spaceId, space->id);
if (munmap(space,sizeof(msgSpace)) < 0){ if (munmap(space, sizeof(msgSpace)) < 0) {
NZG_ERROR("unmap",spaceId); NZG_ERROR("unmap", spaceId);
goto ERROR; goto ERROR;
} }
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -2,10 +2,10 @@
#include "ids.h" #include "ids.h"
/* prototypes des fonctions annexes à ne pas exporter */ /* prototypes des fonctions annexes à ne pas exporter */
int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgPoolDataIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolDataTabSemIdIntern( int msgPoolDataTabSemIdIntern(msgPoolSemId destSemId,
msgPoolSemId destSemId,const msgSpaceId externId); const msgSpaceId externId);
/* /*
* spaceId : identifiant externe de l'espace de msg * spaceId : identifiant externe de l'espace de msg
* queueNb : nombre de files de messages * queueNb : nombre de files de messages
@ -13,185 +13,185 @@ int msgPoolDataTabSemIdIntern(
* queueNbCar : tableau de caracteristiques des different pool * queueNbCar : tableau de caracteristiques des different pool
*/ */
msgSpace * msgSpaceCreate( msgSpace *msgSpaceCreate(msgSpaceId externId,
msgSpaceId externId, int queueNb, int poolNb, msgPool * poolInfos)
int queueNb, {
int poolNb, msgSpaceId nzgId;
msgPool * poolInfos ){ /* msgSpaceList mSList; */
msgSpaceId nzgId; int mSFd; // shm file descriptor
/* msgSpaceList mSList; */ int i;
int mSFd; // shm file descriptor int err;
int i; static int mSIdNum = -1;
int err; msgSpace *space;
static int mSIdNum=-1; msgSpaceListElemId listElemId;
msgSpace * space;
msgSpaceListElemId listElemId;
msgPoolDataTabId poolDataTabId; msgPoolDataTabId poolDataTabId;
msgPoolData * poolDataTabAddr; msgPoolData *poolDataTabAddr;
msgBufferInfoTabId bufferInfoTabId; msgBufferInfoTabId bufferInfoTabId;
msgPoolId poolId; msgPoolId poolId;
sem_t * mSDataTabSemFd; sem_t *mSDataTabSemFd;
mSIdNum++; mSIdNum++;
space=NULL; space = NULL;
/** recuperation de la liste des msgSpace **/ /** recuperation de la liste des msgSpace **/
/* (creation si elle n'existe pas */ /* (creation si elle n'existe pas */
/** on créee le nouvel element **/ /** on créee le nouvel element **/
printf("PAGESIZE : %d\n",(int)PAGESIZE); printf("PAGESIZE : %d\n", (int)PAGESIZE);
if (msgSpaceIdIntern(nzgId,externId) < 0){ if (msgSpaceIdIntern(nzgId, externId) < 0) {
return NULL; return NULL;
} }
if (msgSpaceListInit() <0){ if (msgSpaceListInit() < 0) {
NZG_ERROR("msgSpaceListInit",nzgId); NZG_ERROR("msgSpaceListInit", nzgId);
goto ERROR; goto ERROR;
}; };
if (msgSpaceListLock() <0){ if (msgSpaceListLock() < 0) {
NZG_ERROR("msgSpaceListLock",""); NZG_ERROR("msgSpaceListLock", "");
goto ERROR; goto ERROR;
} }
if ((err=msgSpaceListFindId(externId)) < 1){ if ((err = msgSpaceListFindId(externId)) < 1) {
if (err==0){ if (err == 0) {
// soit le msgSpace existe deja // soit le msgSpace existe deja
NZG_ERROR("spaceListFindId : existing ",nzgId); NZG_ERROR("spaceListFindId : existing ", nzgId);
} else {
// zut, il y a soit une erreur
NZG_ERROR("spaceListFindId : error ", nzgId);
}
msgSpaceListUnlock();
// on quitte
goto ERROR;
}
printf("spaceListFind ok\n");
if (msgSpaceListUnlock() < 0) {
NZG_ERROR("msgSpaceListUnlock", "");
goto ERROR;
}
printf("spaceListUnlock ok\n");
fprintf(stderr, "Creating msgSpace with id : %s\n", nzgId);
mSFd = shm_open(nzgId,
O_RDWR | O_CREAT | O_EXCL | O_TRUNC,
MSGSPACE_DEFAULT_MODE);
if (mSFd == -1) {
NZG_ERROR("shm_open : msgSpace creation", nzgId);
goto ERROR;
}
/* on redimentionne l'element */
if (ftruncate(mSFd, sizeof(*space)) == -1) {
NZG_ERROR("ftruncate", nzgId);
goto ERROR;
}
/* Map the memory object */
space = mmap(0, sizeof(*space),
PROT_READ | PROT_WRITE, MAP_SHARED, mSFd, 0);
if (space == MAP_FAILED) {
NZG_ERROR("mmap", nzgId);
goto ERROR;
}
printf("CREAT: msgSpace mapped to 0x%08lx in %d\n", (long)space,
(int)getpid());
/* on ferme le descripteur du fichier */
close(mSFd);
/* on remplit la structure */
strcpy(space->id, nzgId);
space->poolNb = poolNb;
space->queueNb = queueNb;
space->pid = getpid();
msgPoolDataTabSemIdIntern(space->poolDataTabSemId, externId);
/* creation du poolData */
msgPoolDataIdIntern(poolDataTabId, externId);
strcpy(space->poolDataTabId, poolDataTabId);
strcpy(space->externId, externId);
mSDataTabSemFd = sem_open(space->poolDataTabSemId,
O_CREAT | O_EXCL, SEM_DEFAULT_MODE, 0);
if (mSDataTabSemFd == SEM_FAILED) {
NZG_ERROR("sem_open : creation de la ressource",
space->poolDataTabSemId);
goto ERROR;
} else { } else {
// zut, il y a soit une erreur /* NZG_ERROR("sem_open : creation oki",
NZG_ERROR("spaceListFindId : error ",nzgId); space->poolDataTabSemId); */
} }
sem_close(mSDataTabSemFd);
/* attacher le tableau des msgPoolData */
poolDataTabAddr = msgPoolDataTabCreate(space);
if (poolDataTabAddr == NULL) {
NZG_ERROR("msgPoolDataTabCreate", space->poolDataTabId);
goto ERROR;
}
for (i = 0; i < poolNb; i++) {
/* Pour chacun des poolData
* - y écrire les informations passées en param
* - creer les zones mémoires de chacune des pool
*/
// creation d'une pool
poolDataTabAddr[i].bufferNb = poolInfos[i].bufferNb;
poolDataTabAddr[i].bufferSize = poolInfos[i].bufferSize;
poolDataTabAddr[i].allocDispBuffer = 0;
if (msgBufferInfoTabIdIntern(bufferInfoTabId, externId, i) ==
-1) {
fprintf(stderr,
"msgBufferInfoTabId creation failed for id %s\n",
(char *)poolId);
return NULL;
}
strcpy(poolDataTabAddr[i].bufferInfoTabId, bufferInfoTabId);
printf("buffIfoTab %d name : %s\n", i,
poolDataTabAddr[i].bufferInfoTabId);
msgBufferInfoTabCreate(externId, poolDataTabAddr, i,
poolInfos[i].bufferNb);
if (msgPoolIdIntern(poolId, externId, i) == -1) {
fprintf(stderr, "msgPoolId creation failed for id %s\n",
(char *)poolId);
return NULL;
}
strcpy(poolDataTabAddr[i].poolId, poolId);
msgPoolCreate(externId, i, poolInfos[i].bufferNb,
poolInfos[i].bufferSize);
}
/* on crée queueNb files de messages */
printf("Creating queues:\n");
for (i = 0; i < queueNb; i++) {
printf("- queue %i...", i);
fflush(stdout);
if (msgQueueInit(space->externId, i) < 0) {
printf("fail.\n");
NZG_ERROR("msgQueueInit : queue ?? for",
space->externId);
goto ERROR;
}
printf("ok.\n");
}
msgPoolDataTabClose(space, poolDataTabAddr);
/* on ajoute spaceId a la liste des msgSpace connus */
printf("spaceListInit...\n");
printf("ok\n");
msgSpaceListLock();
printf("spaceListLock...ok\n");
msgSpaceListElemCreate(listElemId, space);
printf("spaceListElemCreate...ok\n");
msgSpaceListAdd(listElemId);
printf("spaceListAdd...ok\n");
msgSpaceListUnlock(); msgSpaceListUnlock();
// on quitte printf("spaceListUnlock...ok\n");
goto ERROR;
}
printf("spaceListFind ok\n");
if (msgSpaceListUnlock() < 0){
NZG_ERROR("msgSpaceListUnlock","");
goto ERROR;
}
printf("spaceListUnlock ok\n");
fprintf(stderr,"Creating msgSpace with id : %s\n",nzgId); /* on renvoie un pointeur sur le bon spaceId */
mSFd=shm_open( msgPoolDataTabUnlock(space);
nzgId, return space;
O_RDWR|O_CREAT|O_EXCL|O_TRUNC, ERROR:
MSGSPACE_DEFAULT_MODE return NULL;
);
if (mSFd == -1 ) {
NZG_ERROR("shm_open : msgSpace creation",nzgId);
goto ERROR;
}
/* on redimentionne l'element */
if (ftruncate(mSFd, sizeof(* space)) == -1){
NZG_ERROR("ftruncate",nzgId);
goto ERROR;
}
/* Map the memory object */
space = mmap( 0, sizeof( *space ),
PROT_READ | PROT_WRITE,
MAP_SHARED, mSFd, 0 );
if( space == MAP_FAILED ) {
NZG_ERROR("mmap",nzgId);
goto ERROR;
}
printf( "CREAT: msgSpace mapped to 0x%08lx in %d\n", (long)space,(int)getpid());
/* on ferme le descripteur du fichier */
close(mSFd);
/* on remplit la structure */
strcpy(space->id,nzgId);
space->poolNb=poolNb;
space->queueNb=queueNb;
space->pid=getpid();
msgPoolDataTabSemIdIntern(space->poolDataTabSemId,externId);
/* creation du poolData */
msgPoolDataIdIntern(poolDataTabId,externId);
strcpy(space->poolDataTabId,poolDataTabId);
strcpy(space->externId,externId);
mSDataTabSemFd=sem_open(space->poolDataTabSemId,
O_CREAT|O_EXCL,SEM_DEFAULT_MODE,0);
if (mSDataTabSemFd == SEM_FAILED){
NZG_ERROR("sem_open : creation de la ressource",
space->poolDataTabSemId);
goto ERROR;
} else {
/* NZG_ERROR("sem_open : creation oki",
space->poolDataTabSemId); */
}
sem_close(mSDataTabSemFd);
/* attacher le tableau des msgPoolData */
poolDataTabAddr=msgPoolDataTabCreate(space);
if (poolDataTabAddr == NULL){
NZG_ERROR("msgPoolDataTabCreate",space->poolDataTabId);
goto ERROR;
}
for (i=0;i<poolNb;i++){
/* Pour chacun des poolData
* - y écrire les informations passées en param
* - creer les zones mémoires de chacune des pool
*/
// creation d'une pool
poolDataTabAddr[i].bufferNb=poolInfos[i].bufferNb;
poolDataTabAddr[i].bufferSize=poolInfos[i].bufferSize;
poolDataTabAddr[i].allocDispBuffer=0;
if (msgBufferInfoTabIdIntern(bufferInfoTabId,externId,i) == -1){
fprintf( stderr, "msgBufferInfoTabId creation failed for id %s\n",
(char*)poolId );
return NULL;
}
strcpy(poolDataTabAddr[i].bufferInfoTabId,bufferInfoTabId);
printf("buffIfoTab %d name : %s\n",i,poolDataTabAddr[i].bufferInfoTabId);
msgBufferInfoTabCreate(externId,poolDataTabAddr,i,poolInfos[i].bufferNb);
if (msgPoolIdIntern(poolId,externId,i) == -1){
fprintf( stderr, "msgPoolId creation failed for id %s\n",
(char*)poolId );
return NULL;
}
strcpy(poolDataTabAddr[i].poolId,poolId);
msgPoolCreate(externId,i,poolInfos[i].bufferNb,poolInfos[i].bufferSize);
}
/* on crée queueNb files de messages */
printf("Creating queues:\n");
for (i=0; i<queueNb;i++){
printf("- queue %i...",i); fflush(stdout);
if (msgQueueInit(space->externId,i) < 0){
printf("fail.\n");
NZG_ERROR("msgQueueInit : queue ?? for",space->externId);
goto ERROR;
}
printf("ok.\n");
}
msgPoolDataTabClose(space,poolDataTabAddr);
/* on ajoute spaceId a la liste des msgSpace connus */
printf("spaceListInit...\n");
printf("ok\n");
msgSpaceListLock();
printf("spaceListLock...ok\n");
msgSpaceListElemCreate(listElemId,space);
printf("spaceListElemCreate...ok\n");
msgSpaceListAdd(listElemId);
printf("spaceListAdd...ok\n");
msgSpaceListUnlock();
printf("spaceListUnlock...ok\n");
/* on renvoie un pointeur sur le bon spaceId */
msgPoolDataTabUnlock(space);
return space;
ERROR:
return NULL;
} }

View file

@ -2,66 +2,66 @@
#include "ids.h" #include "ids.h"
/* prototypes des fonctions annexes à ne pas exporter */ /* prototypes des fonctions annexes à ne pas exporter */
int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgPoolDataIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolDataTabSemIdIntern( int msgPoolDataTabSemIdIntern(msgPoolSemId destSemId,
msgPoolSemId destSemId,const msgSpaceId externId); const msgSpaceId externId);
int msgSpaceDelete(msgSpaceId externId){ int msgSpaceDelete(msgSpaceId externId)
//int shmId; {
msgSpaceId nzgId; //int shmId;
msgSpace * space; msgSpaceId nzgId;
int i; msgSpace *space;
fprintf(stderr,"Deleting msgSpace with id : %s\n",externId); int i;
if (msgSpaceIdIntern(nzgId,externId) == -1){ fprintf(stderr, "Deleting msgSpace with id : %s\n", externId);
NZG_ERROR("msgSpaceIdIntern",externId); if (msgSpaceIdIntern(nzgId, externId) == -1) {
goto ERROR; NZG_ERROR("msgSpaceIdIntern", externId);
} goto ERROR;
}
space = msgSpaceOpen(externId); space = msgSpaceOpen(externId);
/* supprimer chaque pool */ /* supprimer chaque pool */
for (i=0;i<space->poolNb;i++){ for (i = 0; i < space->poolNb; i++) {
msgPoolDelete(externId,i); msgPoolDelete(externId, i);
} }
/* supprimer chaque queue */ /* supprimer chaque queue */
for (i=0;i<space->queueNb;i++){ for (i = 0; i < space->queueNb; i++) {
msgQueueDelete(externId,i); msgQueueDelete(externId, i);
} }
printf("openned successfully !\n"); printf("openned successfully !\n");
printf("Unlinking DataTab... "); printf("Unlinking DataTab... ");
if (shm_unlink(space->poolDataTabId) < 0){ if (shm_unlink(space->poolDataTabId) < 0) {
perror("shm_unlink"); perror("shm_unlink");
return -1;
}
printf("ok\n");
printf("Unlinking DataTabSem... ");
if (sem_unlink(space->poolDataTabSemId) < 0) {
NZG_ERROR("sem_unlink", space->poolDataTabSemId);
return -1;
}
printf("ok\n");
if (shm_unlink(nzgId) < 0) {
NZG_ERROR("shm_unlink", nzgId);
goto ERROR;
};
if (msgSpaceListLock() < 0) {
NZG_ERROR("msgSpaceListLock", "");
goto ERROR;
}
if (msgSpaceListRem(nzgId) < 0) {
NZG_ERROR("msgSpaceListRem", nzgId);
goto ERROR;
}
if (msgSpaceListUnlock() < 0) {
NZG_ERROR("msgSpaceListUnlock", "");
goto ERROR;
}
return 0;
ERROR:
return -1; return -1;
}
printf("ok\n");
printf("Unlinking DataTabSem... ");
if (sem_unlink(space->poolDataTabSemId) < 0){
NZG_ERROR("sem_unlink",space->poolDataTabSemId);
return -1;
}
printf("ok\n");
if (shm_unlink(nzgId)<0){
NZG_ERROR("shm_unlink",nzgId);
goto ERROR;
};
if (msgSpaceListLock()<0){
NZG_ERROR("msgSpaceListLock","");
goto ERROR;
}
if (msgSpaceListRem(nzgId) < 0){
NZG_ERROR("msgSpaceListRem",nzgId);
goto ERROR;
}
if (msgSpaceListUnlock()< 0){
NZG_ERROR("msgSpaceListUnlock","");
goto ERROR;
}
return 0;
ERROR:
return -1;
} }

View file

@ -3,54 +3,55 @@
// ajoute un element en début de liste. // ajoute un element en début de liste.
// fait pointer l'ancien dernier vers lui // fait pointer l'ancien dernier vers lui
// il s'enregistre en tant que dernier chez la liste // il s'enregistre en tant que dernier chez la liste
int msgSpaceListAdd(msgSpaceListElemId newElemId){ int msgSpaceListAdd(msgSpaceListElemId newElemId)
msgSpaceListElem * listOldTailElem; {
msgSpaceListElem * listNewTailElem; msgSpaceListElem *listOldTailElem;
msgSpaceList * list; msgSpaceListElem *listNewTailElem;
msgSpaceList *list;
list=msgSpaceListOpen(); list = msgSpaceListOpen();
if (list == NULL){ if (list == NULL) {
NZG_ERROR("msgSpaceListOpen",""); NZG_ERROR("msgSpaceListOpen", "");
goto ERROR; goto ERROR;
} }
// on informe l'element qui est le dernier // on informe l'element qui est le dernier
listNewTailElem=msgSpaceListElemOpen(newElemId); listNewTailElem = msgSpaceListElemOpen(newElemId);
if (listNewTailElem==NULL){ if (listNewTailElem == NULL) {
NZG_ERROR("msgSpaceListElemOpen",newElemId); NZG_ERROR("msgSpaceListElemOpen", newElemId);
goto ERROR; goto ERROR;
}
strcpy(listNewTailElem->next, newElemId);
if (msgSpaceListElemClose(listNewTailElem) < 0) {
NZG_ERROR("msgSpaceListElemClose", newElemId);
goto ERROR;
} }
strcpy(listNewTailElem->next,newElemId);
if (msgSpaceListElemClose(listNewTailElem) <0 ){
NZG_ERROR("msgSpaceListElemClose",newElemId);
goto ERROR;
}
/* verifier si la liste n'est pas vide... */ /* verifier si la liste n'est pas vide... */
if((strcmp(list->headId,list->id)==0) if ((strcmp(list->headId, list->id) == 0)
&& (strcmp(list->tailId,list->id)==0)) { && (strcmp(list->tailId, list->id) == 0)) {
printf("- premier elem de spaceList -\n"); printf("- premier elem de spaceList -\n");
// on donne a la queue l'id de l'element // on donne a la queue l'id de l'element
strcpy(list->headId,newElemId); strcpy(list->headId, newElemId);
strcpy(list->tailId,newElemId); strcpy(list->tailId, newElemId);
} else { } else {
/* on informe son prédecesseur qu'il a un suivant */ /* on informe son prédecesseur qu'il a un suivant */
listOldTailElem=msgSpaceListElemOpen(list->tailId); listOldTailElem = msgSpaceListElemOpen(list->tailId);
if (listOldTailElem == NULL){ if (listOldTailElem == NULL) {
NZG_ERROR("msgSpaceListElemOpen",list->tailId); NZG_ERROR("msgSpaceListElemOpen", list->tailId);
goto ERROR; goto ERROR;
} }
strcpy(listOldTailElem->next,newElemId); strcpy(listOldTailElem->next, newElemId);
if (msgSpaceListElemClose(listOldTailElem) < 0){ if (msgSpaceListElemClose(listOldTailElem) < 0) {
NZG_ERROR("msgSpaceListElemClose",list->tailId); NZG_ERROR("msgSpaceListElemClose", list->tailId);
goto ERROR; goto ERROR;
} }
// on donne a la queue l'id du dernier element // on donne a la queue l'id du dernier element
strcpy(list->tailId,newElemId); strcpy(list->tailId, newElemId);
} }
// on incremente elemCounter // on incremente elemCounter
msgSpaceListClose(list); msgSpaceListClose(list);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }

View file

@ -1,17 +1,18 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
int msgSpaceListClose(msgSpaceList * list){ int msgSpaceListClose(msgSpaceList * list)
{
msgSpaceListId spaceListId;
strcpy(spaceListId,DEFAULT_MSGSPACELISTID);
if (munmap(list,sizeof(msgSpaceList)) < 0){ msgSpaceListId spaceListId;
NZG_ERROR("unmap",spaceListId); strcpy(spaceListId, DEFAULT_MSGSPACELISTID);
goto ERROR;
}
return 0; if (munmap(list, sizeof(msgSpaceList)) < 0) {
ERROR: NZG_ERROR("unmap", spaceListId);
return -1; goto ERROR;
}
return 0;
ERROR:
return -1;
} }

View file

@ -4,15 +4,15 @@
int msgSpaceListElemClose(msgSpaceListElem * listElem) int msgSpaceListElemClose(msgSpaceListElem * listElem)
{ {
msgSpaceListElemId eId; msgSpaceListElemId eId;
strcpy(eId,listElem->id); strcpy(eId, listElem->id);
if (munmap(listElem,sizeof(msgSpaceListElem)) < 0){ if (munmap(listElem, sizeof(msgSpaceListElem)) < 0) {
NZG_ERROR("unmap",eId); NZG_ERROR("unmap", eId);
goto ERROR; goto ERROR;
} }
return 0; return 0;
ERROR: ERROR:
return-1; return -1;
}; };

View file

@ -2,47 +2,43 @@
#include "ids.h" #include "ids.h"
// cree un nouvel elemen // cree un nouvel elemen
int msgSpaceListElemCreate( int msgSpaceListElemCreate(msgSpaceListElemId listElemId, msgSpace * space)
msgSpaceListElemId listElemId,
msgSpace * space)
{ {
int listElemFd; int listElemFd;
msgSpaceListElem * listElem; msgSpaceListElem *listElem;
msgSpaceListElemIdIntern(listElemId,space->externId); msgSpaceListElemIdIntern(listElemId, space->externId);
listElemFd=shm_open(listElemId, listElemFd = shm_open(listElemId,
O_RDWR|O_CREAT|O_EXCL|O_TRUNC, O_RDWR | O_CREAT | O_EXCL | O_TRUNC,
SHM_DEFAULT_MODE); SHM_DEFAULT_MODE);
if (listElemFd == -1 ) { if (listElemFd == -1) {
NZG_ERROR("shm_open : msgSpaceListElem creation",listElemId); NZG_ERROR("shm_open : msgSpaceListElem creation", listElemId);
goto ERROR; goto ERROR;
} }
if (ftruncate(listElemFd, sizeof(msgSpaceListElem)) < 0){ if (ftruncate(listElemFd, sizeof(msgSpaceListElem)) < 0) {
NZG_ERROR("ftruncate", listElemId); NZG_ERROR("ftruncate", listElemId);
goto ERROR; goto ERROR;
} }
close(listElemFd); close(listElemFd);
listElem=msgSpaceListElemOpen(listElemId); listElem = msgSpaceListElemOpen(listElemId);
if (listElem ==NULL){ if (listElem == NULL) {
NZG_ERROR("msgSpaceListElemOpen",listElemId); NZG_ERROR("msgSpaceListElemOpen", listElemId);
goto ERROR; goto ERROR;
} }
strcpy(listElem->id,listElemId); strcpy(listElem->id, listElemId);
strcpy(listElem->next,listElemId); strcpy(listElem->next, listElemId);
strcpy(listElem->spaceId,space->id); strcpy(listElem->spaceId, space->id);
printf("[ ListElem : id %s,next: %s, Sid: %s ]\n", printf("[ ListElem : id %s,next: %s, Sid: %s ]\n",
listElem->id, listElem->id, listElem->next, listElem->spaceId);
listElem->next, if (msgSpaceListElemClose(listElem) < 0) {
listElem->spaceId); NZG_ERROR("msgSpaceListElemClose", listElemId);
if (msgSpaceListElemClose(listElem) <0){ }
NZG_ERROR("msgSpaceListElemClose",listElemId); return 0;
} ERROR:
return 0; return -1;
ERROR:
return -1;
}; };

View file

@ -2,6 +2,5 @@
// modifie l'element suivant de l'element choisi // modifie l'element suivant de l'element choisi
// et le fait pointer sur un nouvel ID // et le fait pointer sur un nouvel ID
int msgSpaceListElemLinkNext(msgSpaceListElemId current,msgSpaceListElemId next); int msgSpaceListElemLinkNext(msgSpaceListElemId current,
msgSpaceListElemId next);

View file

@ -2,32 +2,29 @@
#include "ids.h" #include "ids.h"
// cree un nouvel elemen // cree un nouvel elemen
void * msgSpaceListElemOpen(msgSpaceListElemId listElemId){ void *msgSpaceListElemOpen(msgSpaceListElemId listElemId)
int listElemFd; {
void * listElemAddr; int listElemFd;
void *listElemAddr;
listElemFd=shm_open(listElemId,O_RDWR,SHM_DEFAULT_MODE); listElemFd = shm_open(listElemId, O_RDWR, SHM_DEFAULT_MODE);
if (listElemFd == -1 ) { if (listElemFd == -1) {
NZG_ERROR("shm_open : msgSpaceListElem open",listElemId); NZG_ERROR("shm_open : msgSpaceListElem open", listElemId);
goto ERROR; goto ERROR;
} }
listElemAddr=mmap(NULL, listElemAddr = mmap(NULL,
sizeof(msgQueueElem), sizeof(msgQueueElem),
PROT_READ|PROT_WRITE, PROT_READ | PROT_WRITE, MAP_SHARED, listElemFd, 0);
MAP_SHARED,
listElemFd,
0);
if( listElemAddr == MAP_FAILED ) { if (listElemAddr == MAP_FAILED) {
NZG_ERROR("mmap",listElemId); NZG_ERROR("mmap", listElemId);
goto ERROR; goto ERROR;
} }
close(listElemFd);
return listElemAddr;
ERROR:
return NULL;
close(listElemFd);
return listElemAddr;
ERROR:
return NULL;
}; };

View file

@ -2,78 +2,84 @@
#define DEBUG 1 #define DEBUG 1
int msgSpaceListFindId(msgSpaceId externId){ int msgSpaceListFindId(msgSpaceId externId)
msgSpaceList * list; {
msgSpaceId spaceId; msgSpaceList *list;
msgSpaceListElemId listHeadElemId; msgSpaceId spaceId;
msgSpaceListElemId listTailElemId; msgSpaceListElemId listHeadElemId;
msgSpaceListId listId; msgSpaceListElemId listTailElemId;
msgSpaceListId listId;
msgSpaceIdIntern(spaceId,externId); msgSpaceIdIntern(spaceId, externId);
list=msgSpaceListOpen(); list = msgSpaceListOpen();
if (list==NULL){ if (list == NULL) {
NZG_ERROR("msgSpaceListOpen",""); NZG_ERROR("msgSpaceListOpen", "");
goto ERROR; goto ERROR;
}
if (DEBUG) { printf("Before ListStrCpy\n"); }
strcpy(listHeadElemId,list->headId);
strcpy(listTailElemId,list->tailId);
strcpy(listId,list->id);
if (DEBUG) { printf("After ListStrCpy\n"); }
if ((strcmp(listHeadElemId,listId)==0)
&& strcmp(listTailElemId,listId)==0){
// si la liste est vide
if (DEBUG) { printf("SpaceList : vide\n"); }
return 1;
} else {
return msgSpaceListElemFindId(listHeadElemId,spaceId);
}
if (msgSpaceListClose(list) < 0){
NZG_ERROR("msgSpaceListClose","");
goto ERROR;
}
return 0;
ERROR:
return -1;
}
int msgSpaceListElemFindId(msgSpaceListElemId elemId,msgSpaceId spaceId){
msgSpaceListElem * listElem;
msgSpaceListElemId listElemIdNext;
msgSpaceId currentElemSpaceId;
listElem=msgSpaceListElemOpen(elemId);
if (listElem==NULL){
NZG_ERROR("msgSpaceListElemOpen",elemId);
goto ERROR;
}
strcpy(listElemIdNext,listElem->next);
strcpy(currentElemSpaceId,listElem->spaceId);
if (msgSpaceListElemClose(listElem) < 0){
NZG_ERROR("msgSpaceListElemClose",elemId);
goto ERROR;
}
printf("Current ListElem: Next:%s, Sid:%s\n",
listElemIdNext,currentElemSpaceId);
printf("comparisons...\n");
if (strcmp(currentElemSpaceId,spaceId)==0) {
printf("Found %s in spaceList !\n",spaceId);
return 0;
} else {
if (strcmp(listElemIdNext,elemId)==0){
printf("End of spaceList reached.\n");
/* fin de liste ?? */
return 1;
} else {
/* continuer sur l'element suivant */
return msgSpaceListElemFindId(listElemIdNext,spaceId);
} }
} if (DEBUG) {
printf("Before ListStrCpy\n");
return 0; }
ERROR: strcpy(listHeadElemId, list->headId);
return -1; strcpy(listTailElemId, list->tailId);
strcpy(listId, list->id);
if (DEBUG) {
printf("After ListStrCpy\n");
}
if ((strcmp(listHeadElemId, listId) == 0)
&& strcmp(listTailElemId, listId) == 0) {
// si la liste est vide
if (DEBUG) {
printf("SpaceList : vide\n");
}
return 1;
} else {
return msgSpaceListElemFindId(listHeadElemId, spaceId);
}
if (msgSpaceListClose(list) < 0) {
NZG_ERROR("msgSpaceListClose", "");
goto ERROR;
}
return 0;
ERROR:
return -1;
} }
int msgSpaceListElemFindId(msgSpaceListElemId elemId, msgSpaceId spaceId)
{
msgSpaceListElem *listElem;
msgSpaceListElemId listElemIdNext;
msgSpaceId currentElemSpaceId;
listElem = msgSpaceListElemOpen(elemId);
if (listElem == NULL) {
NZG_ERROR("msgSpaceListElemOpen", elemId);
goto ERROR;
}
strcpy(listElemIdNext, listElem->next);
strcpy(currentElemSpaceId, listElem->spaceId);
if (msgSpaceListElemClose(listElem) < 0) {
NZG_ERROR("msgSpaceListElemClose", elemId);
goto ERROR;
}
printf("Current ListElem: Next:%s, Sid:%s\n",
listElemIdNext, currentElemSpaceId);
printf("comparisons...\n");
if (strcmp(currentElemSpaceId, spaceId) == 0) {
printf("Found %s in spaceList !\n", spaceId);
return 0;
} else {
if (strcmp(listElemIdNext, elemId) == 0) {
printf("End of spaceList reached.\n");
/* fin de liste ?? */
return 1;
} else {
/* continuer sur l'element suivant */
return msgSpaceListElemFindId(listElemIdNext, spaceId);
}
}
return 0;
ERROR:
return -1;
}

View file

@ -1,76 +1,73 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
int msgSpaceListInit(){ int msgSpaceListInit()
int spaceListFd; {
sem_t * spaceListSemFd; int spaceListFd;
msgSpaceList *list; sem_t *spaceListSemFd;
msgSpaceList *list;
msgSpaceListSemId spaceListSemId; msgSpaceListSemId spaceListSemId;
msgSpaceListId spaceListId; msgSpaceListId spaceListId;
strcpy(spaceListSemId,DEFAULT_MSGSPACELISTSEMID); strcpy(spaceListSemId, DEFAULT_MSGSPACELISTSEMID);
strcpy(spaceListId,DEFAULT_MSGSPACELISTID); strcpy(spaceListId, DEFAULT_MSGSPACELISTID);
/* Creation de semaphore */ /* Creation de semaphore */
spaceListSemFd=sem_open(spaceListSemId,O_CREAT|O_EXCL,0666,0); spaceListSemFd = sem_open(spaceListSemId, O_CREAT | O_EXCL, 0666, 0);
if(spaceListSemFd==SEM_FAILED){ if (spaceListSemFd == SEM_FAILED) {
spaceListSemFd=sem_open(spaceListSemId,O_CREAT,0666,0); spaceListSemFd = sem_open(spaceListSemId, O_CREAT, 0666, 0);
if(spaceListSemFd==SEM_FAILED){ if (spaceListSemFd == SEM_FAILED) {
NZG_ERROR("sem_open",spaceListSemId); NZG_ERROR("sem_open", spaceListSemId);
goto ERROR; goto ERROR;
}
} }
}
/*Fait un segment de memoir partager sur espace de listelement */
spaceListFd = shm_open(spaceListId,
/*Fait un segment de memoir partager sur espace de listelement*/ O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0666);
spaceListFd=shm_open(spaceListId, if (spaceListFd == -1) {
O_RDWR|O_CREAT|O_EXCL|O_TRUNC, spaceListFd = shm_open(spaceListId, O_RDWR, 0666);
0666); if (spaceListFd == -1) {
if (spaceListFd == -1){ NZG_ERROR("shm_open", spaceListId);
spaceListFd=shm_open(spaceListId,O_RDWR,0666); goto ERROR;
if (spaceListFd == -1){ } else {
NZG_ERROR("shm_open",spaceListId); close(spaceListFd);
goto ERROR; goto EXISTING;
} else { }
close(spaceListFd);
goto EXISTING;
} }
} if (ftruncate(spaceListFd, sizeof(msgSpaceList)) < 0) {
if (ftruncate(spaceListFd, sizeof(msgSpaceList)) < 0){ NZG_ERROR("ftruncate", spaceListId);
NZG_ERROR("ftruncate",spaceListId); goto ERROR;
goto ERROR; }
} close(spaceListFd);
close(spaceListFd);
list=msgSpaceListOpen(); list = msgSpaceListOpen();
if (list == NULL){ if (list == NULL) {
NZG_ERROR("msgSpaceListOpen",""); NZG_ERROR("msgSpaceListOpen", "");
goto ERROR; goto ERROR;
} }
strcpy(list->id,spaceListId); strcpy(list->id, spaceListId);
strcpy(list->headId,spaceListId); strcpy(list->headId, spaceListId);
strcpy(list->tailId,spaceListId); strcpy(list->tailId, spaceListId);
if (msgSpaceListClose(list) <0){ if (msgSpaceListClose(list) < 0) {
NZG_ERROR("msgSpaceListClose",""); NZG_ERROR("msgSpaceListClose", "");
goto ERROR; goto ERROR;
} }
if (msgSpaceListUnlock() < 0){ if (msgSpaceListUnlock() < 0) {
NZG_ERROR("msgSpaceListUnlock",""); NZG_ERROR("msgSpaceListUnlock", "");
goto ERROR; goto ERROR;
} }
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
EXISTING: EXISTING:
if (msgSpaceListUnlock() < 0){ if (msgSpaceListUnlock() < 0) {
NZG_ERROR("msgSpaceListUnlock",""); NZG_ERROR("msgSpaceListUnlock", "");
goto ERROR; goto ERROR;
} }
return 0; return 0;
}; };

View file

@ -1,69 +1,65 @@
#include "libnazgul.h" #include "libnazgul.h"
// verouille le semaphore de la liste // verouille le semaphore de la liste
int msgSpaceListLock(){ int msgSpaceListLock()
sem_t * msgSpaceListSemFd; {
msgSpaceListSemId spaceListSemId; sem_t *msgSpaceListSemFd;
strcpy(spaceListSemId,DEFAULT_MSGSPACELISTSEMID); msgSpaceListSemId spaceListSemId;
strcpy(spaceListSemId, DEFAULT_MSGSPACELISTSEMID);
/* Ouverture d'un semafore */ /* Ouverture d'un semafore */
msgSpaceListSemFd=sem_open(spaceListSemId,O_CREAT,0666,1); msgSpaceListSemFd = sem_open(spaceListSemId, O_CREAT, 0666, 1);
if(msgSpaceListSemFd==SEM_FAILED){ if (msgSpaceListSemFd == SEM_FAILED) {
NZG_ERROR("sem_open",spaceListSemId); NZG_ERROR("sem_open", spaceListSemId);
goto ERROR; goto ERROR;
} }
/* Pose d'un semaphore et le verrouille */ /* Pose d'un semaphore et le verrouille */
if(sem_wait(msgSpaceListSemFd)==-1){ if (sem_wait(msgSpaceListSemFd) == -1) {
NZG_ERROR("sem_wait",spaceListSemId); NZG_ERROR("sem_wait", spaceListSemId);
goto ERROR; goto ERROR;
} }
/* Ferme le semaphore*/ /* Ferme le semaphore */
/* if(sem_close(msgSpaceListSemFd)==-1){ /* if(sem_close(msgSpaceListSemFd)==-1){
NZG_ERROR("sem_close",spaceListSemId); NZG_ERROR("sem_close",spaceListSemId);
return -1; return -1;
}*/ } */
sem_close(msgSpaceListSemFd); sem_close(msgSpaceListSemFd);
return 0; return 0;
ERROR: ERROR:
return -1; return -1;
} }
// deverouille le semaphore de la liste // deverouille le semaphore de la liste
int msgSpaceListUnlock(){ int msgSpaceListUnlock()
sem_t * msgSpaceListSemFd; {
msgSpaceListSemId spaceListSemId; sem_t *msgSpaceListSemFd;
strcpy(spaceListSemId,DEFAULT_MSGSPACELISTSEMID); msgSpaceListSemId spaceListSemId;
strcpy(spaceListSemId, DEFAULT_MSGSPACELISTSEMID);
/*Ouverture dun semaphore */
msgSpaceListSemFd = sem_open(spaceListSemId, O_CREAT, 0666, 0);
if (msgSpaceListSemFd == SEM_FAILED) {
NZG_ERROR("sem_open", spaceListSemId);
goto ERROR;
}
/*Ouverture dun semaphore*/ /*Relachement du semaphore */
msgSpaceListSemFd=sem_open(spaceListSemId,O_CREAT,0666,0); if (sem_post(msgSpaceListSemFd) == -1) {
if(msgSpaceListSemFd==SEM_FAILED){ NZG_ERROR("sem_relache", spaceListSemId);
NZG_ERROR("sem_open",spaceListSemId); goto ERROR;
goto ERROR; }
}
/* Ferme le semaphore */
if (sem_close(msgSpaceListSemFd) == -1) {
NZG_ERROR("sem_close", spaceListSemId);
goto ERROR;
}
/*Relachement du semaphore*/ return 0;
if(sem_post(msgSpaceListSemFd)==-1){ ERROR:
NZG_ERROR("sem_relache",spaceListSemId); return -1;
goto ERROR;
}
/* Ferme le semaphore */
if(sem_close(msgSpaceListSemFd)==-1){
NZG_ERROR("sem_close",spaceListSemId);
goto ERROR;
}
return 0;
ERROR:
return -1;
}; };

View file

@ -1,32 +1,30 @@
#include "libnazgul.h" #include "libnazgul.h"
#include "ids.h" #include "ids.h"
void * msgSpaceListOpen(){ void *msgSpaceListOpen()
int listFd; {
void * listAddr; int listFd;
void *listAddr;
msgSpaceListId spaceListId; msgSpaceListId spaceListId;
strcpy(spaceListId,DEFAULT_MSGSPACELISTID); strcpy(spaceListId, DEFAULT_MSGSPACELISTID);
listFd=shm_open(spaceListId,O_RDWR,SHM_DEFAULT_MODE); listFd = shm_open(spaceListId, O_RDWR, SHM_DEFAULT_MODE);
if (listFd == -1 ) { if (listFd == -1) {
NZG_ERROR("shm_open : msgSpaceList open",spaceListId); NZG_ERROR("shm_open : msgSpaceList open", spaceListId);
goto ERROR; goto ERROR;
} }
listAddr=mmap(NULL, listAddr = mmap(NULL,
sizeof(msgSpaceList), sizeof(msgSpaceList),
PROT_READ|PROT_WRITE, PROT_READ | PROT_WRITE, MAP_SHARED, listFd, 0);
MAP_SHARED, if (listAddr == MAP_FAILED) {
listFd, NZG_ERROR("mmap", spaceListId);
0); goto ERROR;
if( listAddr == MAP_FAILED ) { }
NZG_ERROR("mmap",spaceListId);
goto ERROR;
}
close(listFd); close(listFd);
return listAddr; return listAddr;
ERROR: ERROR:
return NULL; return NULL;
} }

View file

@ -2,109 +2,127 @@
#define DEBUG 1 #define DEBUG 1
int msgSpaceListRem(msgSpaceId spaceId){ int msgSpaceListRem(msgSpaceId spaceId)
msgSpaceList * list; {
msgSpaceListElemId listHeadElemId; msgSpaceList *list;
msgSpaceListElemId listTailElemId; msgSpaceListElemId listHeadElemId;
msgSpaceListId listId; msgSpaceListElemId listTailElemId;
msgSpaceListId listId;
msgSpaceListElemId prevElemId; msgSpaceListElemId prevElemId;
msgSpaceListElemId currElemId; msgSpaceListElemId currElemId;
msgSpaceListElemId nextElemId; msgSpaceListElemId nextElemId;
msgSpaceListElem * prevElem; msgSpaceListElem *prevElem;
msgSpaceListElem * currElem; msgSpaceListElem *currElem;
msgSpaceId currSpaceId; msgSpaceId currSpaceId;
list=msgSpaceListOpen(); list = msgSpaceListOpen();
if (list==NULL){ if (list == NULL) {
NZG_ERROR("msgSpaceListOpen",""); NZG_ERROR("msgSpaceListOpen", "");
goto ERROR;
}
if (DEBUG) { printf("Before ListStrCpy\n"); }
strcpy(listHeadElemId,list->headId);
strcpy(listTailElemId,list->tailId);
strcpy(listId,list->id);
if (DEBUG) { printf("After ListStrCpy\n"); }
if ((strcmp(listHeadElemId,listId)==0)
&& strcmp(listTailElemId,listId)==0){
if (DEBUG) { printf("SpaceList : vide\n"); }
return 1;
} else {
bool found=false;
strcpy(prevElemId,list->headId);
strcpy(currElemId,list->headId);
while(!found){
printf("Recherche dans l'element %s\n",listHeadElemId);
currElem=msgSpaceListElemOpen(currElemId);
if (currElem==NULL){
NZG_ERROR("msgSpaceListElemOpen",currElemId);
goto ERROR; goto ERROR;
} }
strcpy(nextElemId,currElem->next); if (DEBUG) {
strcpy(currSpaceId,currElem->spaceId); printf("Before ListStrCpy\n");
if (msgSpaceListElemClose(currElem) <0){ }
NZG_ERROR("msgSpaceListElemClose",currElemId); strcpy(listHeadElemId, list->headId);
goto ERROR; strcpy(listTailElemId, list->tailId);
} strcpy(listId, list->id);
if (strcmp(prevElemId,nextElemId)==0){ if (DEBUG) {
// list à 1 seul élement printf("After ListStrCpy\n");
if (strcmp(currSpaceId,spaceId)==0){ }
// on a trouvé l'elem if ((strcmp(listHeadElemId, listId) == 0)
strcpy(list->headId,list->id); && strcmp(listTailElemId, listId) == 0) {
strcpy(list->tailId,list->id); if (DEBUG) {
// on efface printf("SpaceList : vide\n");
shm_unlink(currElemId);
found=true;
} else {
break;
} }
} else { return 1;
// liste à plusieurs élements... } else {
if (strcmp(currSpaceId,spaceId)==0){ bool found = false;
// ca correspond
if (strcmp(prevElemId,currElemId)==0){ strcpy(prevElemId, list->headId);
// si on est en début de liste (prev=current) strcpy(currElemId, list->headId);
// - la tete de liste pointe sur le suivant while (!found) {
strcpy(list->headId,nextElemId); printf("Recherche dans l'element %s\n", listHeadElemId);
} else { currElem = msgSpaceListElemOpen(currElemId);
if (strcmp(currElemId,nextElemId)==0){ if (currElem == NULL) {
// si on est en find de liste (current=next) NZG_ERROR("msgSpaceListElemOpen", currElemId);
// - on fait pointer la queue de liste sur le précédent goto ERROR;
strcpy(list->tailId,prevElemId);
// - on fait pointer le précédent sur lui-meme
prevElem=msgSpaceListElemOpen(prevElemId);
strcpy(prevElem->next,prevElemId);
msgSpaceListElemClose(prevElem);
}else {
// on est en milieu de liste
// - on fait pointer le précédent sur le suivant
prevElem=msgSpaceListElemOpen(prevElemId);
strcpy(prevElem->next,nextElemId);
msgSpaceListElemClose(prevElem);
} }
} strcpy(nextElemId, currElem->next);
// - on détruit l'actuel strcpy(currSpaceId, currElem->spaceId);
shm_unlink(currElemId); if (msgSpaceListElemClose(currElem) < 0) {
found=true; NZG_ERROR("msgSpaceListElemClose", currElemId);
break; goto ERROR;
} else { }
//cela ne correspond pas if (strcmp(prevElemId, nextElemId) == 0) {
//on recopie // list à 1 seul élement
strcpy(prevElemId,currElemId); if (strcmp(currSpaceId, spaceId) == 0) {
strcpy(currElemId,nextElemId); // on a trouvé l'elem
strcpy(list->headId, list->id);
strcpy(list->tailId, list->id);
// on efface
shm_unlink(currElemId);
found = true;
} else {
break;
}
} else {
// liste à plusieurs élements...
if (strcmp(currSpaceId, spaceId) == 0) {
// ca correspond
if (strcmp(prevElemId, currElemId) == 0) {
// si on est en début de liste (prev=current)
// - la tete de liste pointe sur le suivant
strcpy(list->headId,
nextElemId);
} else {
if (strcmp
(currElemId,
nextElemId) == 0) {
// si on est en find de liste (current=next)
// - on fait pointer la queue de liste sur le précédent
strcpy(list->tailId,
prevElemId);
// - on fait pointer le précédent sur lui-meme
prevElem =
msgSpaceListElemOpen
(prevElemId);
strcpy(prevElem->next,
prevElemId);
msgSpaceListElemClose
(prevElem);
} else {
// on est en milieu de liste
// - on fait pointer le précédent sur le suivant
prevElem =
msgSpaceListElemOpen
(prevElemId);
strcpy(prevElem->next,
nextElemId);
msgSpaceListElemClose
(prevElem);
}
}
// - on détruit l'actuel
shm_unlink(currElemId);
found = true;
break;
} else {
//cela ne correspond pas
//on recopie
strcpy(prevElemId, currElemId);
strcpy(currElemId, nextElemId);
}
}
} }
} if (msgSpaceListClose(list) < 0) {
NZG_ERROR("msgSpaceListClose", "");
goto ERROR;
}
return 0;
} }
if (msgSpaceListClose(list) < 0){ ERROR:
NZG_ERROR("msgSpaceListClose",""); return -1;
goto ERROR;
}
return 0;
}
ERROR:
return -1;
} }

View file

@ -2,25 +2,26 @@
#include "ids.h" #include "ids.h"
/* prototypes des fonctions annexes à ne pas exporter */ /* prototypes des fonctions annexes à ne pas exporter */
int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgSpaceIdIntern(msgSpaceId dest, const msgSpaceId src);
int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src ); int msgPoolDataIdIntern(msgSpaceId dest, const msgSpaceId src);
msgSpace *msgSpaceOpen(msgSpaceId externId)
{
int shmFd;
msgSpaceId nzgId;
msgSpace *mSAddr;
if (msgSpaceIdIntern(nzgId, externId) < 0) {
return NULL;
}
if ((shmFd = shm_open(nzgId, O_RDWR, MSGSPACE_DEFAULT_MODE)) < 0) {
perror("shm_open");
return NULL;
}
mSAddr =
mmap(NULL, sizeof(msgSpace), PROT_READ | PROT_WRITE, MAP_SHARED,
shmFd, (off_t) 0);
msgSpace * msgSpaceOpen(msgSpaceId externId){ printf("OPEN: msgSpace mapped to %p in %d\n", (void *)mSAddr,
int shmFd; (int)getpid());
msgSpaceId nzgId; return mSAddr;
msgSpace * mSAddr;
if (msgSpaceIdIntern(nzgId,externId) < 0){
return NULL;
}
if ((shmFd=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){
perror("shm_open");
return NULL;
}
mSAddr=mmap(NULL,sizeof(msgSpace),PROT_READ|PROT_WRITE,MAP_SHARED,shmFd,(off_t)0);
printf( "OPEN: msgSpace mapped to %p in %d\n", (void *)mSAddr,(int)getpid());
return mSAddr;
} }

View file

@ -1,2 +1 @@
#include "libnazgul.h" #include "libnazgul.h"

View file

@ -6,62 +6,61 @@
#include "libnazgul.h" #include "libnazgul.h"
int main(void) { int main(void)
// msgSpace mS=0; {
// msgSpace mS=0;
pid_t pid; pid_t pid;
msgSpaceId testId; msgSpaceId testId;
msgSpace * mSPAC; msgSpace *mSPAC;
msgPool poolInfos[3]; msgPool poolInfos[3];
char * montext; char *montext;
poolInfos[0].bufferNb=4; poolInfos[0].bufferNb = 4;
poolInfos[0].bufferSize=200; poolInfos[0].bufferSize = 200;
poolInfos[1].bufferNb=5;
poolInfos[1].bufferSize=250;
poolInfos[2].bufferNb=5; poolInfos[1].bufferNb = 5;
poolInfos[2].bufferSize=280; poolInfos[1].bufferSize = 250;
sprintf(testId,"test%d",(int)getuid());
printf("RequestedId: %s\n",testId);
printf("Void size: %d\n",(int)sizeof(void));
mSPAC=msgSpaceCreate(testId,0,3,poolInfos);
if (mSPAC ==NULL) {
NZG_ERROR("msgSpaceCreate",testId);
exit(0);
}
pid=fork(); poolInfos[2].bufferNb = 5;
poolInfos[2].bufferSize = 280;
sprintf(testId, "test%d", (int)getuid());
if (pid ==0){ printf("RequestedId: %s\n", testId);
mSPAC=msgSpaceOpen(testId); printf("Void size: %d\n", (int)sizeof(void));
sleep(5); mSPAC = msgSpaceCreate(testId, 0, 3, poolInfos);
montext=msgAllocate(mSPAC,2,180,0); if (mSPAC == NULL) {
sleep(5); NZG_ERROR("msgSpaceCreate", testId);
montext=msgAllocate(mSPAC,2,170,0); exit(0);
sleep(5); }
montext=msgAllocate(mSPAC,2,270,0);
pid = fork();
} else {
mSPAC=msgSpaceOpen(testId); if (pid == 0) {
montext=msgAllocate(mSPAC,2,280,0); mSPAC = msgSpaceOpen(testId);
*montext=42; sleep(5);
printf("### test Valeur0 %d ###\n",(int)*montext); montext = msgAllocate(mSPAC, 2, 180, 0);
//sleep(2); sleep(5);
montext=msgAllocate(mSPAC,2,270,0); montext = msgAllocate(mSPAC, 2, 170, 0);
*montext=17; sleep(5);
printf("### test Valeur1 %d ###\n",(int)*montext); montext = msgAllocate(mSPAC, 2, 270, 0);
sleep(5);
montext=msgAllocate(mSPAC,2,270,0); } else {
msgFree(mSPAC,(void *)montext); mSPAC = msgSpaceOpen(testId);
//msgFree(mSPAC,(void *)montext); montext = msgAllocate(mSPAC, 2, 280, 0);
//msgFree(mSPAC,(void *)montext); *montext = 42;
wait(NULL); printf("### test Valeur0 %d ###\n", (int)*montext);
msgSpaceDelete(testId); //sleep(2);
} montext = msgAllocate(mSPAC, 2, 270, 0);
return 0; *montext = 17;
printf("### test Valeur1 %d ###\n", (int)*montext);
sleep(5);
montext = msgAllocate(mSPAC, 2, 270, 0);
msgFree(mSPAC, (void *)montext);
//msgFree(mSPAC,(void *)montext);
//msgFree(mSPAC,(void *)montext);
wait(NULL);
msgSpaceDelete(testId);
}
return 0;
} }

View file

@ -6,95 +6,106 @@
#include "libnazgul.h" #include "libnazgul.h"
void usage(char * myname){ void usage(char *myname)
printf("Usage: %s [--all | msgSpaceId1 msgSpaceId2 ... ]\n",myname); {
printf("Usage: %s [--all | msgSpaceId1 msgSpaceId2 ... ]\n", myname);
} }
int about(char * spaceId){ int about(char *spaceId)
msgPoolDataTabSemId ressourceSemId; {
sem_t * ressourceSemFd; msgPoolDataTabSemId ressourceSemId;
int i; sem_t *ressourceSemFd;
msgPoolData * poolDataTab; int i;
msgSpace * space; msgPoolData *poolDataTab;
int ressourceSemVal; msgSpace *space;
if (strlen(spaceId)> MSGSPACE_ID_LEN){ int ressourceSemVal;
//verif de la longueur if (strlen(spaceId) > MSGSPACE_ID_LEN) {
fprintf(stderr,"Too long ident : %s\n",spaceId); //verif de la longueur
goto ERROR; fprintf(stderr, "Too long ident : %s\n", spaceId);
} else { goto ERROR;
int err; } else {
msgSpaceListLock(); int err;
err=msgSpaceListFindId(spaceId); msgSpaceListLock();
msgSpaceListUnlock(); err = msgSpaceListFindId(spaceId);
if (err < 1){ msgSpaceListUnlock();
if (err==0){ if (err < 1) {
// le msgSpace existe if (err == 0) {
space=msgSpaceOpen(spaceId); // le msgSpace existe
// on lit les informations sur le msgSpace space = msgSpaceOpen(spaceId);
// les pool // on lit les informations sur le msgSpace
for (i=0;i<space->poolNb;i++){ // les pool
msgPoolSemIdIntern(ressourceSemId,space->externId,i); for (i = 0; i < space->poolNb; i++) {
ressourceSemFd = sem_open(ressourceSemId,O_CREAT,SEM_DEFAULT_MODE,0); msgPoolSemIdIntern(ressourceSemId,
if (ressourceSemFd == SEM_FAILED){ space->externId, i);
NZG_ERROR("sem_open",ressourceSemId); ressourceSemFd =
goto ERROR; sem_open(ressourceSemId, O_CREAT,
} SEM_DEFAULT_MODE, 0);
if (sem_getvalue(ressourceSemFd, &ressourceSemVal) < 0){ if (ressourceSemFd == SEM_FAILED) {
NZG_ERROR("sem_getvalue",ressourceSemId); NZG_ERROR("sem_open",
goto ERROR; ressourceSemId);
} goto ERROR;
msgPoolDataTabLock(space); }
poolDataTab= msgPoolDataTabOpen(space); if (sem_getvalue
// donner le nombre de buffer disponibles (ressourceSemFd,
printf("- %d/%d free buffers in pool #%d\n", &ressourceSemVal) < 0) {
ressourceSemVal, NZG_ERROR("sem_getvalue",
poolDataTab[i].bufferNb, ressourceSemId);
i); goto ERROR;
msgPoolDataTabClose(space,poolDataTab); }
msgPoolDataTabUnlock(space); msgPoolDataTabLock(space);
if(sem_close(ressourceSemFd) <0){ poolDataTab = msgPoolDataTabOpen(space);
NZG_ERROR("sem_getvalue",ressourceSemId); // donner le nombre de buffer disponibles
goto ERROR; printf
} ("- %d/%d free buffers in pool #%d\n",
// pour chaque buffer, dire s'il est libre ou pas ressourceSemVal,
// et le processus/addresse associé poolDataTab[i].bufferNb, i);
msgPoolDataTabClose(space, poolDataTab);
msgPoolDataTabUnlock(space);
if (sem_close(ressourceSemFd) < 0) {
NZG_ERROR("sem_getvalue",
ressourceSemId);
goto ERROR;
}
// pour chaque buffer, dire s'il est libre ou pas
// et le processus/addresse associé
}
msgSpaceClose(space);
} else {
// zut, il y a soit une erreur
NZG_ERROR("spaceListFindId : error ", spaceId);
}
} else {
// on quitte : l'element n'existe
fprintf(stderr, "Ident %s does not exist.\n", spaceId);
} }
msgSpaceClose(space);
} else {
// zut, il y a soit une erreur
NZG_ERROR("spaceListFindId : error ",spaceId);
}
} else {
// on quitte : l'element n'existe
fprintf(stderr,"Ident %s does not exist.\n",spaceId);
} }
} return 0;
return 0; ERROR:
ERROR: return -1;
return -1;
} }
int main(int argc,char **argv){ int main(int argc, char **argv)
printf("Args %d\n",argc); {
if (argc<2){ printf("Args %d\n", argc);
usage(argv[0]); if (argc < 2) {
}else { usage(argv[0]);
if (argc==2){
if (strcmp("--all",argv[1])==0){
// listing
printf("[ Listing of msgSpaces ]\n");
} else {
// only one msgSpaceId
printf("[ About %s ]\n",argv[1]);
about(argv[1]);
}
} else { } else {
int i; if (argc == 2) {
for (i=1;i<argc;i++){ if (strcmp("--all", argv[1]) == 0) {
printf("[ About %s ]\n",argv[i]); // listing
about(argv[i]); printf("[ Listing of msgSpaces ]\n");
} } else {
// only one msgSpaceId
printf("[ About %s ]\n", argv[1]);
about(argv[1]);
}
} else {
int i;
for (i = 1; i < argc; i++) {
printf("[ About %s ]\n", argv[i]);
about(argv[i]);
}
}
} }
} return 0;
return 0;
} }

View file

@ -6,49 +6,51 @@
#include "libnazgul.h" #include "libnazgul.h"
int main(void) { int main(void)
// msgSpace mS=0; {
// msgSpace mS=0;
msgSpaceId testId; msgSpaceId testId;
msgSpace * mSPAC; msgSpace *mSPAC;
msgPool poolInfos[3]; msgPool poolInfos[3];
char * montext; char *montext;
poolInfos[0].bufferNb=4; poolInfos[0].bufferNb = 4;
poolInfos[0].bufferSize=200; poolInfos[0].bufferSize = 200;
poolInfos[1].bufferNb=5;
poolInfos[1].bufferSize=250;
sprintf(testId,"test%d",(int)getuid());
printf("RequestedId: %s\n",testId);
printf("Void size: %d\n",(int)sizeof(void));
//creation de l'espace de messages
mSPAC=msgSpaceCreate(testId,1,2,poolInfos);
if (mSPAC ==NULL) {
NZG_ERROR("msgSpaceCreate",testId);
exit(0);
}
printf("CREATION ------------------ ok\n"); poolInfos[1].bufferNb = 5;
poolInfos[1].bufferSize = 250;
mSPAC=msgSpaceOpen(testId); sprintf(testId, "test%d", (int)getuid());
printf("RequestedId: %s\n", testId);
printf("Void size: %d\n", (int)sizeof(void));
//creation de l'espace de messages
mSPAC = msgSpaceCreate(testId, 1, 2, poolInfos);
if (mSPAC == NULL) {
NZG_ERROR("msgSpaceCreate", testId);
exit(0);
}
printf("CREATION ------------------ ok\n");
mSPAC = msgSpaceOpen(testId);
/***** TEST 1 *****/ /***** TEST 1 *****/
montext=msgAllocate(mSPAC,1,280,0); montext = msgAllocate(mSPAC, 1, 280, 0);
*montext=42; *montext = 42;
printf("### test Valeur0 %d ###\n",(int)*montext); printf("### test Valeur0 %d ###\n", (int)*montext);
printf("Put..."); fflush(stdout); printf("Put...");
msgPut(mSPAC,0,montext); fflush(stdout);
printf("put-ok\n"); msgPut(mSPAC, 0, montext);
printf("put-ok\n");
printf("Get...");
printf("Get..."); fflush(stdout); fflush(stdout);
montext=msgGet(mSPAC,0,0); montext = msgGet(mSPAC, 0, 0);
printf("get-ok\n"); printf("get-ok\n");
printf("### test Reception %d ###\n",(int)*montext); printf("### test Reception %d ###\n", (int)*montext);
msgFree(mSPAC,montext); msgFree(mSPAC, montext);
/***** TEST 1 *****/ /***** TEST 1 *****/
msgSpaceDelete(testId); msgSpaceDelete(testId);
return 0; return 0;
} }

View file

@ -6,50 +6,53 @@
#include "libnazgul.h" #include "libnazgul.h"
int main(void) { int main(void)
// msgSpace mS=0; {
// msgSpace mS=0;
msgSpaceId testId; msgSpaceId testId;
msgSpace * mSPAC; msgSpace *mSPAC;
msgPool poolInfos[3]; msgPool poolInfos[3];
char * montext; char *montext;
poolInfos[0].bufferNb=4; poolInfos[0].bufferNb = 4;
poolInfos[0].bufferSize=200; poolInfos[0].bufferSize = 200;
poolInfos[1].bufferNb=5;
poolInfos[1].bufferSize=250;
sprintf(testId,"test%d",(int)getuid());
printf("RequestedId: %s\n",testId);
printf("Void size: %d\n",(int)sizeof(void));
//creation de l'espace de messages
mSPAC=msgSpaceCreate(testId,1,2,poolInfos);
if (mSPAC ==NULL) {
NZG_ERROR("msgSpaceCreate",testId);
exit(0);
}
printf("CREATION ------------------ ok\n"); poolInfos[1].bufferNb = 5;
pid_t pid=fork(); poolInfos[1].bufferSize = 250;
if (pid==0){
sleep(2); sprintf(testId, "test%d", (int)getuid());
mSPAC=msgSpaceOpen(testId); printf("RequestedId: %s\n", testId);
montext=msgAllocate(mSPAC,1,280,0); printf("Void size: %d\n", (int)sizeof(void));
*montext=42; //creation de l'espace de messages
printf("### test Valeur0 %d ###\n",(int)*montext); mSPAC = msgSpaceCreate(testId, 1, 2, poolInfos);
printf("Put1..."); fflush(stdout); if (mSPAC == NULL) {
msgPut(mSPAC,0,montext); NZG_ERROR("msgSpaceCreate", testId);
printf("put1-ok\n"); exit(0);
} else { }
mSPAC=msgSpaceOpen(testId);
printf("Get..."); fflush(stdout); printf("CREATION ------------------ ok\n");
montext=msgGet(mSPAC,0,0); pid_t pid = fork();
printf("get-ok\n"); if (pid == 0) {
printf("### test Reception %d ###\n",(int)*montext); sleep(2);
msgFree(mSPAC,montext); mSPAC = msgSpaceOpen(testId);
wait(NULL); montext = msgAllocate(mSPAC, 1, 280, 0);
msgSpaceDelete(testId); *montext = 42;
} printf("### test Valeur0 %d ###\n", (int)*montext);
return 0; printf("Put1...");
fflush(stdout);
msgPut(mSPAC, 0, montext);
printf("put1-ok\n");
} else {
mSPAC = msgSpaceOpen(testId);
printf("Get...");
fflush(stdout);
montext = msgGet(mSPAC, 0, 0);
printf("get-ok\n");
printf("### test Reception %d ###\n", (int)*montext);
msgFree(mSPAC, montext);
wait(NULL);
msgSpaceDelete(testId);
}
return 0;
} }