Fusion:RPC Example

From DirectFBWiki

fusionee_rpc.c

This program will create a simple process that will share a procedure through an arena. To do so we'll have to :

  • Initialize a fusion world
  • Initialize a shared memory pool
  • Initialize an arena
  • Register a FusionCall structure in shared memory using fusion_call_init()
  • Add it to the arena
  • Initialize it with a trivial function
  • Publish a pointer to it in that arena

When shutdown we'll have to :

  • Get the pointer from the arena
  • Execute the procedure
  • Unregister the FusionCall structure from shared memory using fusion_call_destroy()
  • Free the shared memory used by the FusionCall
  • Leave the arena
  • Free the shared memory pool
  • Exit the fusion world
#include <stdio.h>                                                  
#include <stdlib.h>
#include <errno.h>
#include <directfb.h>
#include <fusion/fusion.h>
#include <fusion/shmalloc.h>
#include <fusion/call.h> 
#include <linux/fusion.h>
#include <unistd.h>  
#include <string.h>   
#include <sys/types.h>

int initialize(FusionArena *arena, void *ctx);                       
int shutdown(FusionArena *arena, void *ctx, bool emergency);

/*Our RPC*/
int rpc1_handler (int caller, int call_arg, void *call_ptr, void *ctx);
int main()
{
       int ret, ret_err;                                            
       FusionArena *arena;                                          
       char c;                                                      
        
       /*Initialising Fusion*/                                      
       fusion_init(2,0, &ret);                                      
       fprintf(stdout, "Fusion initialized\n");                     
                                                                     
       /*Initialising Arena with name TestArena*/
       fprintf(stdout, "Initializing Arena ..\n");
       fusion_arena_enter("TestArena",initialize, NULL, NULL, &arena, &ret_err );

       /*Waiting for another process to join arena and get the data*/
       fprintf(stdout, "Enter a key to exit ...\n");
       read(1, &c, 1);

       /*Exiting arena*/
       fprintf(stdout, "Exiting arena ...\n");
       fusion_arena_exit(arena, shutdown, NULL, NULL, 0, &ret_err);

       /*Exiting Fusion*/
       fusion_exit(0);
       fprintf(stdout, "Fusion exited\n");
 
       return(0);
}
 
int initialize(FusionArena *arena, void *ctx)
{
       FusionCall *rpc1;

       fprintf(stdout, "allocating shared memory chunk for FusionCall struct\n");
       rpc1=(FusionCall *)SHMALLOC(sizeof(FusionCall));

       /*Registering the RPC*/
       fusion_call_init(rpc1, rpc1_handler, NULL);

       fprintf(stdout, "* Publishing RPC1 in Arena ..\n");
       fusion_arena_add_shared_field (arena, "RPC1", (void *)rpc1 );
}

/*Arena shutdown callback*/
int shutdown(FusionArena *arena, void *ctx, bool emergency)
{
       FusionCall *rpc1;

       fprintf(stdout, "Shutting down arena\n");
       fusion_arena_get_shared_field(arena, "RPC1", (void **)&rpc1);

       fprintf(stdout, "freeing shared memory\n");

       /*Unregistering the RPC*/
       fusion_call_destroy(rpc1);
       SHFREE(rpc1);
}

int rpc1_handler (int caller, int call_arg, void *call_ptr, void *ctx)
{
       fprintf(stdout, "RPC1 handler called !! \n");
       return 0;
}


Compiling and running

For compiling use :

gcc -Wall -O2 `pkg-config --cflags --libs fusion` fusionee_rpc.c -o fusionee_rpc

The environment variable $PKG_CONFIG_PATH must be set to something like ${INSTALL_PATH}/lib/pkgconfig

Now run fusionee_rpc on a terminal, then press enter.



Return to Fusion:Examples