l3.libnazgul/src/nzg_spaces.c

122 lines
2.5 KiB
C
Raw Normal View History

#include "libnazgul.h"
2019-09-17 06:58:16 +00:00
2004-02-16 21:25:11 +00:00
/*
* 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 spaceId,
int queueNb,
int poolNb,
msgPool * queueNbCar ){
msgSpaceId nzgId;
//msgSpaceList mSList;
2019-09-17 06:58:16 +00:00
int mSFd; // shm file descriptor
int i;
2019-09-16 15:17:43 +00:00
static int mSIdNum=-1;
2019-09-17 06:58:16 +00:00
msgSpace * mSAddr;
2019-09-16 15:15:37 +00:00
2019-09-16 15:17:43 +00:00
mSIdNum++;
2019-09-17 06:58:16 +00:00
mSAddr=NULL;
2019-09-16 15:15:37 +00:00
/** recuperation de la liste des msgSpace **/
/* (creation si elle n'existe pas */
/** on cr<63>ee le nouvel element **/
2020-03-03 22:42:59 +00:00
printf("PAGESIZE : %d\n",(int)PAGESIZE);
msgSp2nzgId(spaceId,nzgId);
2019-09-17 06:58:16 +00:00
mSFd=shm_open(
2019-09-16 15:15:37 +00:00
nzgId,
2019-09-17 06:58:16 +00:00
O_RDWR|O_CREAT|O_EXCL|O_TRUNC,
2019-09-16 15:15:37 +00:00
MSGSPACE_DEFAULT_MODE
);
2019-09-17 06:58:16 +00:00
if (mSFd == -1 ) {
fprintf( stderr, "msgSpace %s creation failed: %s\n",
(char*)nzgId,
strerror( errno ) );
return NULL;
}
//on redimentionne l'element
if (ftruncate(mSFd, sizeof(PAGESIZE)) == -1){
fprintf( stderr, "msgSpace resizing failed: %s\n",
strerror( errno ) );
2020-03-03 22:42:59 +00:00
return NULL;
}
2019-09-17 06:58:16 +00:00
/* 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;
}
2004-02-16 21:25:11 +00:00
2019-09-17 06:58:16 +00:00
printf( "Map addr is 0x%08x\n", (int)mSAddr );
2004-02-16 21:25:11 +00:00
2019-09-17 06:58:16 +00:00
/* on ferme le descripteur du fichier */
close(mSFd);
2019-09-17 06:58:16 +00:00
/* TODO: on ajoute spaceId a la liste des msgSpace connus */
2019-09-17 06:58:16 +00:00
/* TODO: on cr<63>e queueNb files de messages */
/* on cr<63>er poolNb pool de buffers */
/* et on "attache" tout ce beau monde au spaceId */
for (i=0;i<poolNb;i++){
// creation d'une pool
2019-09-17 06:58:16 +00:00
// mSAddr->
}
2004-02-16 21:25:11 +00:00
2019-09-17 06:58:16 +00:00
/* on renvoie un pointeur sur le bon spaceId */
return mSAddr;
2004-02-16 21:25:11 +00:00
}
2019-09-16 15:15:37 +00:00
/*
* spaceId : blabla
*/
2020-03-03 22:42:59 +00:00
msgSpace * msgSpaceOpen(msgSpaceId spaceId){
int shmId;
msgSpaceId nzgId;
if (msgSp2nzgId(nzgId,spaceId));
2020-03-03 22:42:59 +00:00
if ((shmId=shm_open(nzgId,O_RDWR,MSGSPACE_DEFAULT_MODE)) < 0){
perror("shm_open");
return NULL;
}
2019-09-16 14:43:39 +00:00
return NULL;
}
int msgSpaceDelete(msgSpaceId spaceId){
2020-03-03 22:41:24 +00:00
//int shmId;
msgSpaceId nzgId;
if (msgSp2nzgId(nzgId,spaceId) == -1){
//TODO: message d'erreur
return -1;
}
if (shm_unlink(nzgId)<0){
2019-09-16 14:54:52 +00:00
perror("shm_unlink");
return -1;
2019-09-16 14:54:52 +00:00
};
return 0;
2019-09-16 14:43:39 +00:00
}
int msgSp2nzgId(msgSpaceId dest,const msgSpaceId src ){
if (strlen(src)>MSGSPACE_ID_LEN){
return -1;
}
2019-09-17 06:58:16 +00:00
/* sprintf(resNzgId,"/tmp/.nzg-%s",(char *)spaceId); */
sprintf(dest,"/nzgSpace%s",(char *)src);
return 0;
2019-09-16 14:54:52 +00:00
}