l3.libnazgul/src/nzg_spaceCreate.c
2020-03-03 23:46:32 +01:00

167 lines
4.6 KiB
C

#include "libnazgul.h"
#include "nzg_ids.h"
/* prototypes des fonctions annexes à ne pas exporter */
int msgSpaceIdIntern(msgSpaceId dest,const msgSpaceId src );
int msgPoolDataIdIntern(msgSpaceId dest,const msgSpaceId src );
int msgPoolDataTabSemIdIntern(
msgPoolSemId destSemId,const msgSpaceId externId);
/*
* spaceId : identifiant externe de l'espace de msg
* queueNb : nombre de files de messages
* poolNb : nombre de pool de buffers
* queueNbCar : tableau de caracteristiques des different pool
*/
msgSpace * msgSpaceCreate(
msgSpaceId externId,
int queueNb,
int poolNb,
msgPool * poolInfos ){
msgSpaceId nzgId;
/* msgSpaceList mSList; */
int mSFd; // shm file descriptor
int mSPoolDataFd; // shm file descriptor
int i;
static int mSIdNum=-1;
msgSpace * mSAddr;
msgPoolDataTabId poolDataTabId;
msgPoolData * mSPoolDataAddr;
msgBufferInfoTabId bufferInfoTabId;
msgPoolId poolId;
sem_t * mSDataTabSemFd;
mSIdNum++;
mSAddr=NULL;
/** recuperation de la liste des msgSpace **/
/* (creation si elle n'existe pas */
/** on créee le nouvel element **/
printf("PAGESIZE : %d\n",(int)PAGESIZE);
if (msgSpaceIdIntern(nzgId,externId) < 0){
return NULL;
}
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);
return NULL;
}
/* on redimentionne l'element */
if (ftruncate(mSFd, sizeof(* mSAddr)) == -1){
fprintf( stderr, "msgSpace resizing failed: %s\n",
strerror( errno ) );
return NULL;
}
/* Map the memory object */
mSAddr = mmap( 0, sizeof( *mSAddr ),
PROT_READ | PROT_WRITE,
MAP_SHARED, mSFd, 0 );
if( mSAddr == MAP_FAILED ) {
fprintf( stderr, "mmap failed: %s\n",
strerror( errno ) );
return NULL;
}
printf( "CREAT: msgSpace mapped to 0x%08x in %d\n", (int)mSAddr,(int)getpid());
/* on ferme le descripteur du fichier */
close(mSFd);
/* on remplit la structure */
strncpy(mSAddr->id,externId,MSGSPACE_ID_LEN);
mSAddr->poolNb=poolNb;
mSAddr->queueNb=queueNb;
mSAddr->pid=getpid();
msgPoolDataTabSemIdIntern(mSAddr->poolDataTabSemId,externId);
/* creation du poolData */
msgPoolDataIdIntern(poolDataTabId,externId);
strcpy(mSAddr->poolDataTabId,poolDataTabId);
mSDataTabSemFd=sem_open(mSAddr->poolDataTabSemId,
O_CREAT|O_EXCL,SEM_DEFAULT_MODE,1);
if (mSDataTabSemFd == SEM_FAILED){
NZG_ERROR("sem_open : creation de la ressource",
mSAddr->poolDataTabSemId);
return NULL;
} else {
NZG_ERROR("sem_open : creation oki",
mSAddr->poolDataTabSemId);
}
sem_wait(mSDataTabSemFd);
mSPoolDataFd=shm_open(
poolDataTabId,
O_RDWR|O_CREAT|O_EXCL|O_TRUNC,
MSGSPACE_DEFAULT_MODE
);
if (mSPoolDataFd == -1 ) {
fprintf( stderr, "poolData %s creation failed: %s\n",
(char*)poolDataTabId,
strerror( errno ) );
return NULL;
}
/* allocation de la bonne zone mémoire pour le poolData */
if (ftruncate(mSPoolDataFd, poolNb*sizeof(msgPoolData)) == -1){
fprintf( stderr, "msgSpace resizing failed: %s\n",
strerror( errno ) );
return NULL;
}
/* TODO: attacher chacun des msgPoolData :
* - y écrire les informations passées en param
* - creer les zones mémoires de chacune des pool
*/
mSPoolDataAddr = mmap( 0, sizeof( *mSAddr ),
PROT_READ | PROT_WRITE,
MAP_SHARED, mSFd, 0 );
if( mSPoolDataAddr == MAP_FAILED ) {
fprintf( stderr, "mmap failed: %s\n",
strerror( errno ) );
return NULL;
}
close(mSPoolDataFd);
printf( "msgPoolData map addr is 0x%08x\n", (int)mSPoolDataAddr );
for (i=0;i<poolNb;i++){
// creation d'une pool
mSPoolDataAddr[i].bufferNb=poolInfos[i].bufferNb;
mSPoolDataAddr[i].bufferSize=poolInfos[i].bufferSize;
mSPoolDataAddr[i].allocDispBuffer=0;
if (msgBufferInfoTabIdIntern(bufferInfoTabId,externId,i) == -1){
fprintf( stderr, "msgBufferInfoTabId creation failed for id %s\n",
(char*)poolId );
return NULL;
}
strcpy(mSPoolDataAddr[i].bufferInfoTabId,bufferInfoTabId);
printf("buffIfoTab %d name : %s\n",i,mSPoolDataAddr[i].bufferInfoTabId);
msgBufferInfoTabCreate(externId,mSPoolDataAddr,i,poolInfos[i].bufferNb);
if (msgPoolIdIntern(poolId,externId,i) == -1){
fprintf( stderr, "msgPoolId creation failed for id %s\n",
(char*)poolId );
return NULL;
}
strcpy(mSPoolDataAddr[i].poolId,poolId);
msgPoolCreate(externId,i,poolInfos[i].bufferNb,poolInfos[i].bufferSize);
}
/* TODO: on ajoute spaceId a la liste des msgSpace connus */
/* TODO: on crée queueNb files de messages */
/* on renvoie un pointeur sur le bon spaceId */
sem_post(mSDataTabSemFd);
return mSAddr;
}