BaseConnectionManager.h

00001 #ifndef BASECONNECTIONMANAGER_H_
00002 #define BASECONNECTIONMANAGER_H_
00003 
00004 #include "NicEntry.h"
00005 
00006 class ChannelAccess;
00007 
00024 class BaseConnectionManager : public cSimpleModule
00025 {
00026 private:
00034   class GridCoord
00035   {
00036   public:
00038     static const int UNDEFINED = 0;
00041     int x;
00042     int y;
00043     int z;
00046     bool use2D;
00047 
00048   public:
00053     GridCoord()
00054       :x(0), y(0), z(0), use2D(false) {};
00055 
00059     GridCoord(int x, int y)
00060       :x(x), y(y), z(UNDEFINED), use2D(true) {};
00061 
00065     GridCoord(int x, int y, int z)
00066       :x(x), y(y), z(z), use2D(false) {};
00067 
00071     GridCoord(const GridCoord& o) {
00072       x = o.x;
00073       y = o.y;
00074       z = o.z;
00075       use2D = o.use2D;
00076         }
00077 
00083         GridCoord(const Coord& c, const Coord& gridCellSize = Coord(1.0,1.0,1.0)) {
00084             x = static_cast<int>(c.getX() / gridCellSize.getX());
00085             y = static_cast<int>(c.getY() / gridCellSize.getY());
00086             z = static_cast<int>(c.getZ() / gridCellSize.getZ());
00087             use2D = c.is2D();
00088         }
00089 
00091         std::string info() const {
00092       std::stringstream os;
00093       if (use2D) {
00094         os << "(" << x << "," << y << ")";
00095       } else {
00096         os << "(" << x << "," << y << "," << z << ")";
00097       }
00098       return os.str();
00099     }
00100 
00102     friend bool operator==(const GridCoord& a, const GridCoord& b) {
00103       return a.x == b.x && a.y == b.y && a.z == b.z;
00104     }
00105 
00107     friend bool operator!=(const GridCoord& a, const GridCoord& b) {
00108       return !(a==b);
00109     }
00110   };
00111 
00117   class CoordSet {
00118   protected:
00120     std::vector<GridCoord*> data;
00122     unsigned maxSize;
00124     unsigned size;
00126     unsigned current;
00127 
00128   protected:
00129 
00138     void insert(const GridCoord& c, unsigned pos) {
00139       if(data[pos] == 0) {
00140         data[pos] = new GridCoord(c);
00141         size++;
00142       } else {
00143         if(*data[pos] != c) {
00144           insert(c, (pos + 2) % maxSize);
00145         }
00146       }
00147     }
00148 
00149   public:
00153     CoordSet(unsigned sz)
00154       :maxSize(sz), size(0), current(0)
00155     {
00156       data.resize(maxSize);
00157     }
00158 
00162     ~CoordSet() {
00163       for(unsigned i = 0; i < maxSize; i++) {
00164         if(data[i] != 0) {
00165           delete data[i];
00166         }
00167       }
00168     }
00169 
00175     void add(const GridCoord& c) {
00176       unsigned hash = (c.x * 10000 + c.y * 100 + c.z) % maxSize;
00177       insert(c, hash);
00178     }
00179 
00184     GridCoord* next() {
00185       for(;current < maxSize; current++) {
00186         if(data[current] != 0) {
00187           return data[current++];
00188         }
00189       }
00190       return 0;
00191     }
00192 
00196     unsigned getSize() { return size; }
00197 
00203     unsigned getmaxSize() { return maxSize; }
00204   };
00205 
00206 protected:
00208   typedef std::map<int, NicEntry*> NicEntries;
00209 
00211   NicEntries nics;
00212 
00214   bool coreDebug;
00215 
00217   bool sendDirect;
00218 
00220   const Coord* playgroundSize;
00221 
00223   double maxInterferenceDistance;
00224 
00227   double maxDistSquared;
00228 
00230   bool useTorus;
00231 
00234   bool drawMIR;
00235 
00237   typedef std::vector<NicEntries> RowVector;
00239   typedef std::vector<RowVector> NicMatrix;
00241     typedef std::vector<NicMatrix> NicCube;
00242 
00249     NicCube nicGrid;
00250 
00259     Coord findDistance;
00260 
00262     GridCoord gridDim;
00263 
00264 private:
00266     void updateNicConnections(NicEntries& nmap, NicEntry* nic);
00267 
00271     void checkGrid(GridCoord& oldCell,
00272                    GridCoord& newCell,
00273                    int id);
00274 
00278     GridCoord getCellForCoordinate(const Coord& c);
00279 
00284     NicEntries& getCellEntries(GridCoord& cell);
00285 
00291     int wrapIfTorus(int value, int max);
00292 
00296     void fillUnionWithNeighbors(CoordSet& gridUnion, GridCoord cell);
00297 protected:
00298 
00309   virtual double calcInterfDist() = 0;
00310 
00323   virtual void registerNicExt(int nicID);
00324 
00337   virtual void updateConnections(int nicID, const Coord* oldPos, const Coord* newPos);
00338 
00339 public:
00340 
00341   virtual ~BaseConnectionManager();
00342 
00344   virtual int numInitStages() const {
00345     return 2;
00346   }
00347 
00352   virtual void initialize(int stage);
00353 
00360   bool registerNic(cModule* nic, ChannelAccess* chAccess, const Coord* nicPos);
00361 
00374   bool unregisterNic(cModule* nic);
00375 
00377   void updateNicPos(int nicID, const Coord* newPos);
00378 
00380   const NicEntry::GateList& getGateList( int nicID);
00381 
00383   const cGate* getOutGateTo(const NicEntry* nic, const NicEntry* targetNic);
00384 };
00385 
00386 #endif /*BASECONNECTIONMANAGER_H_*/