Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

ShmemRoadSource.cc

Go to the documentation of this file.
00001 
00005 #include <stdio.h>
00006 #include <ipt/ipt.h>
00007 #include <ipt/sharedmem.h>
00008 
00009 #include <utils/SymbolTable.h>
00010 #include <utils/ConfigFile.h>
00011 
00012 #include <RoadDest/RoadStructs.h>
00013 #include "RoadSource.h"
00014 
00017 class ShmemRoadSource : public RoadSource {
00018  public:
00019   ShmemRoadSource();
00020 
00022   virtual bool getPoints(utils::Time& time,
00023                          std::vector<utils::Vec3d>& points,
00024                          bool blocking = true);
00025 
00027   bool init(utils::ConfigFile& params, utils::SymbolTable* globals);
00028 
00029  private:
00030   IPSharedMemory* _shm;  
00031   int _last_tag;  
00032 };
00033 
00035 UTILS_INTF_CREATOR(RoadSource, shmem, gen, params, globals)
00036 {
00037   UTILS_INTF_REPORT(RoadSource, shmem);
00038   ShmemRoadSource* intf = new ShmemRoadSource();
00039   if (!intf->init(*params, globals)) {
00040     delete intf;
00041     return NULL;
00042   }
00043   return intf;
00044 }
00045 
00046 ShmemRoadSource::ShmemRoadSource()
00047 {
00048   _last_tag = -1;
00049 }
00050 
00051 bool ShmemRoadSource::init(utils::ConfigFile& params,
00052                            utils::SymbolTable* globals)
00053 {
00054   // get or create the IPT communicator
00055   // If it is created, it is cached in the global symbol table
00056   IPCommunicator* com =
00057     IPCommunicator::Communicator(globals, 
00058                                  params.getString("ipt_spec",
00059                                                   "unix: int port=0;"));
00060   if (!com)
00061     return false;
00062 
00063   // setup the shared memory specification
00064   // first set up the default, which is based on the memory name
00065   // the host machine, and the port for the memory manager
00066   const char* mem_name = params.getString("name", ROAD_SHMEM_NAME);
00067   const char* machine = params.getString("machine");
00068   int port = params.getInt("port", 1389);
00069   char buffer[200];
00070   if (!*machine) {
00071     sprintf(buffer, "managed: name=%s;", mem_name);
00072   } else {
00073     sprintf(buffer, "managed: name='%s@%s|%d';", mem_name, machine, port);
00074   }
00075   const char* mem_spec = params.getString("mem", buffer);
00076   // get the maximum expected number of points
00077   int max_points = params.getInt("max_points", 20);
00078   // create the shared memory region
00079   _shm =
00080     com->OpenSharedMemory(mem_spec, ROAD_SHMEM_FMT,
00081                           sizeof(RoadShmemStruct) +
00082                           max_points*sizeof(RoadDataPoint));
00083   if (!_shm) {
00084     printf("Problem opening shared memory %s\n", mem_spec);
00085     return false;
00086   }
00087 
00088   return true;
00089 }
00090 
00091 bool ShmemRoadSource::getPoints(utils::Time& time,
00092                                 std::vector<utils::Vec3d>& points,
00093                                 bool blocking)
00094 {
00095   if (blocking) {
00096     // wait for new data in the shared memory region
00097     if (!_shm->Wait()) {
00098       // if waiting failed, mark result with a bad time and return false
00099       time = utils::Time();
00100       return false;
00101     }
00102   }
00103   // Get the data
00104   RoadShmemStruct input_area;  
00105   if (!_shm->FormattedData(&input_area)) {
00106     // if there was a formatting failure, mark as bad and return
00107     time = utils::Time();
00108     return false;
00109   }
00110 
00111   // package the results
00112   time.setValue(input_area.secs, input_area.usecs);
00113   points.clear();
00114   for (int i=0;i<input_area.data.num_points;i++) {
00115     RoadDataPoint& pt = input_area.data.points[i];
00116     points.push_back(utils::Vec3d(pt.x, pt.y, pt.z));
00117   }
00118 
00119   // and release memory back to IPT
00120   _shm->DeleteContents(&input_area);
00121 
00122   // determine if this is "new" data or not by the shared memory tags
00123   if (_shm->Tag() != _last_tag) {
00124     _last_tag = _shm->Tag();
00125     return true;
00126   } else 
00127     return false;
00128 }
00129 

Generated on Fri Jun 16 13:21:19 2006 for ModUtils by  doxygen 1.4.4