<- Dynamic Updates using a Shared Object Callback Dynamic Updates using the GUI ->

Dynamic Updates Using Shared Memory

In the previous two sections we described how to effect dynamic updates to the EphemerisModel class by either programming it directly in the application, or using a shared object callback. There is yet a simpler method for doing updates provided by osgEphemeris through a shared memory interface.

First a little background on shared memory. The implementation for shared memory uses the memory mapping of a file. This allows multiple processing entities to identify the same piece of shared memory by mapping the same file. At the core of the implementation is a Shmem class, which uses a parameter in the new() operator, namely the name of a file which is then created, if it does not exist, and mapped into the processes memory space.

Classes derived from Shmem then can use the new() operator to allocate their memory in file mapped shared memory space. Interestingly, the values contained in the memory mapped file survive restarts of the program and even reboots of the computer.

osgEphemeris::EphemerisData is derived from Shmem. This all values of the EphemerisData class are accessible by external programs for either reading or writing. osgEphemeris::EphemerisData has a static method getDefaultShmemFileName(), which returns the name of a commonly accessible file on the system, which is used to map the contents of EphemerisData.

An example is provided in src/ShmemExample in the distribution, which demonstrates how to update EphemerisData using shared memory.

    #include <time.h>
    #include <unistd.h>
    #include <osgEphemeris/EphemerisData.h>
    
    int main( int argc, char **argv )
    {
        time_t seconds = 0;
    
        // Attached to the default shared memory segment
        osgEphemeris::EphemerisData *data = new(osgEphemeris::EphemerisData::getDefaultShmemFileName())osgEphemeris::EphemerisData;
    
        for( ;; )
        {
             seconds += 60;
             struct tm *_tm = localtime(&seconds);
    
             data->dateTime.setYear( _tm->tm_year + 1900 );
             data->dateTime.setMonth( _tm->tm_mon + 1 );
             data->dateTime.setDayOfMonth( _tm->tm_mday + 1 );
             data->dateTime.setHour( _tm->tm_hour );
             data->dateTime.setMinute( _tm->tm_min );
             data->dateTime.setSecond( _tm->tm_sec );
    
             // Sleep 16 milliseconds
             usleep(16667);
        }
    
        return 0;
    }
    
  


<- Dynamic Updates using a Shared Object Callback Dynamic Updates using the GUI ->