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

ShmemRoadDest.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.h"
00013 #include "RoadStructs.h"
00014 
00016 class ShmemRoadDest : public RoadDest {
00017  public:
00018   ShmemRoadDest();
00019   virtual ~ShmemRoadDest();
00020 
00022   virtual bool outputPoints(utils::Time time,
00023                             const std::vector<utils::Vec3d>& points);
00024 
00026   bool init(utils::ConfigFile& params, utils::SymbolTable* globals);
00027 
00028  private:
00029   IPSharedMemory* _shm;  
00030   RoadShmemStruct _output_area;  
00031   unsigned int _max_points;      
00032 };
00033 
00035 UTILS_INTF_CREATOR(RoadDest, shmem, gen, params, globals)
00036 {
00037   UTILS_INTF_REPORT(RoadDest, shmem);
00038   ShmemRoadDest* intf = new ShmemRoadDest();
00039   if (!intf->init(*params, globals)) {
00040     delete intf;
00041     return NULL;
00042   }
00043   return intf;
00044 }
00045 
00046 ShmemRoadDest::ShmemRoadDest()
00047 {
00048   _max_points = 5;
00049   _output_area.data.points = new RoadDataPoint[_max_points];
00050 }
00051 
00052 ShmemRoadDest::~ShmemRoadDest()
00053 {
00054   delete [] _output_area.data.points;
00055 }
00056 
00057 bool ShmemRoadDest::init(utils::ConfigFile& params,
00058                          utils::SymbolTable* globals)
00059 {
00060   // get or create the IPT communicator
00061   // If it is created, it is cached in the global symbol table
00062   IPCommunicator* com =
00063     IPCommunicator::Communicator(globals, 
00064                                  params.getString("ipt_spec",
00065                                                   "unix: int port=0;"));
00066   if (!com)
00067     return false;
00068 
00069   // setup the shared memory specification
00070   const char* mem_name = params.getString("name", ROAD_SHMEM_NAME);
00071   char buffer[200];
00072   // first set up the default, which is based on the memory name
00073   sprintf(buffer, "managed: name=%s; owner=true;", mem_name);
00074   // and then get the spec given the default (i.e., it can be arbitrarily
00075   // overridden
00076   const char* mem_spec = params.getString("mem", buffer);
00077   // get the maximum expected number of points
00078   int max_points = params.getInt("max_points", 20);
00079   // create the shared memory region
00080   _shm =
00081     com->OpenSharedMemory(mem_spec, ROAD_SHMEM_FMT,
00082                           sizeof(RoadShmemStruct) +
00083                           max_points*sizeof(RoadDataPoint));
00084   if (!_shm) {
00085     printf("Problem opening shared memory %s\n", mem_spec);
00086     return false;
00087   }
00088 
00089   return true;
00090 }
00091 
00092 bool ShmemRoadDest::outputPoints(utils::Time time,
00093                                  const std::vector<utils::Vec3d>& points)
00094 {
00095   // make sure there is enough room in the cached points for the data
00096   if (points.size() > _max_points) {
00097     _max_points = points.size();
00098     delete [] _output_area.data.points;
00099     _output_area.data.points = new RoadDataPoint[_max_points];
00100   }
00101 
00102   // setup the output area
00103   _output_area.data.num_points = (int) points.size();
00104   for (int i=0;i<_output_area.data.num_points;i++) {
00105     const utils::Vec3d& src = points[i];
00106     RoadDataPoint& dest = _output_area.data.points[i];
00107     dest.x = src.x;
00108     dest.y = src.y;
00109     dest.z = src.z;
00110   }
00111   time.getValue(_output_area.secs, _output_area.usecs);
00112 
00113   // and output to shared memory
00114   _shm->PutFormattedData((void*) &_output_area);
00115 
00116   return true;
00117 }
00118 
00119 
00120 
00121   

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