Great! Thanks. Very useful.
Miguel.
Alain Bonnefoy wrote:
Hi Miguel,
The following example should help you. Just ‘cat /var/resmgr_0x’ to see
the result.
Be root to run the server.
$ >qcc -o server main.c
>./server&
$ >cat /var/resmgr_02
…
…
\
#include <stdio.h
#include <stdlib.h
#include <errno.h
#include <string.h
#include <sys/iofunc.h
#include <sys/dispatch.h
#include <sys/netmgr.h
#include <sys/neutrino.h
#include <sys/resmgr.h
dispatch_t *dpp;
dispatch_context_t *ctp;
resmgr_connect_funcs_t connect_funcs;
resmgr_io_funcs_t io_funcs;
resmgr_attr_t rattr;
iofunc_attr_t ioattr[10];
char replied_string[27] = {"You are reading resmgr "};
int io_my_read(resmgr_context_t *ctp, io_read_t *msg, iofunc_ocb_t *ocb) {
int nbytes, nleft, status;
char buffer[100] = {0};
if ((status = iofunc_read_verify(ctp, msg, ocb, NULL)) != EOK) {
return(status);
}
//figure out how many bytes are left
nleft = ocb->attr->nbytes - ocb->offset;
// and how many bytes we can return to the client
nbytes = min(nleft, msg->i.nbytes);
if (nbytes) {
// This method is very bad! We should find a better solution to create our file content
// as io_read() func maybe called several times depending on the client’s buffer length.
sprintf(buffer, “%s%02d\n”, replied_string, ocb->attr->inode);
if (MsgReply(ctp->rcvid, nbytes, buffer + ocb->offset, nbytes) == -1) {
printf(“MsgReply() : %s\n”, strerror(errno));
exit(EXIT_FAILURE);
}
ocb->offset+= nbytes;
if (msg->i.nbytes > 0)
ocb->attr->flags |= IOFUNC_ATTR_ATIME | IOFUNC_ATTR_DIRTY_TIME;
} else {
if (MsgReply(ctp->rcvid, EOK, NULL, 0) == -1) {
printf(“MsgReply() : %s\n”, strerror(errno));
exit(EXIT_FAILURE);
}
}
return(_RESMGR_NOREPLY);
}
int main(int argc, char *argv) {
char pathname[_POSIX_PATH_MAX];
int i;
if ((dpp = dispatch_create()) == NULL) {
printf(“dispatch_create() : %s\n”, strerror(errno));
exit(EXIT_FAILURE);
}
memset(&rattr, 0, sizeof(rattr)); / using the default for rattr */
/*
- initialize the connect function and I/O functions tables to their defaults and then
- override the defaults with the functions that we are providing.
*/
iofunc_func_init(_RESMGR_CONNECT_NFUNCS, &connect_funcs, _RESMGR_IO_NFUNCS, &io_funcs);
io_funcs.read = io_my_read;
for (i = 0; i < 9; i++) {
iofunc_attr_init(&ioattr> , S_IFREG | 0666, NULL, NULL);
sprintf(pathname, "/var/resmgr%02d", i+1);
printf(“register pathname %s\n”, pathname);
ioattr> .nbytes = 27; // our file length
ioattr> .inode = i+1; // 0 means 'not used"
if (resmgr_attach(dpp, &rattr, pathname, FTYPE_ANY, 0, &connect_funcs, &io_funcs, &ioattr> ) == -1) {
printf(“resmgr_attach() : %s\n”, strerror(errno));
exit(EXIT_FAILURE);
}
}
if ((ctp = dispatch_context_alloc(dpp)) == NULL){
printf(“dispatch_context_alloc() : %s\n”, strerror(errno));
exit(EXIT_FAILURE);
}
while (1) {
if ((ctp = dispatch_block(ctp)) == NULL) {
printf(“dispatch_block() : %s\n”, strerror(errno));
exit(EXIT_FAILURE);
}
if (dispatch_handler(ctp) == -1) {
printf(“dispatch_handler() : %s\n”, strerror(errno));
exit(EXIT_FAILURE);
}
}
}