positiondb.cc

00001 /*
00002  * Copyright (c) 2008 Regents of the SIGNET lab, University of Padova.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University of Padova (SIGNET lab) nor the 
00014  *    names of its contributors may be used to endorse or promote products 
00015  *    derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
00019  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00020  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00021  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00022  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00023  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00024  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00025  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00026  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00027  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 
00031 #include"positiondb.h"
00032 #include"position-clmsg.h"
00033 #include<node-core.h>
00034 #include<iostream>
00035 
00036 static class PositionDBClass : public TclClass {
00037 public:
00038   PositionDBClass() : TclClass("PlugIn/PositionDB") {}
00039         TclObject* create(int, const char*const*) {
00040                 return (new PositionDB());
00041         }
00042 } class_positiondb;
00043 
00044 
00045 
00046 std::map<nsaddr_t, Position*> PositionDB::posmap;
00047 
00048 PositionDB::PositionDB()
00049 {
00050   bind("debug_",&debug_);
00051 }
00052 
00053 
00054 int PositionDB::command(int argc, const char*const* argv)
00055 {
00056   Tcl& tcl = Tcl::instance();
00057   if(argc == 2)
00058     {
00059         
00060         
00061     }
00062   else if(argc == 4)
00063     {
00064       if(strcasecmp(argv[1], "addpos") == 0)
00065         {
00066           int index = atoi(argv[2]);
00067           Position* pp = dynamic_cast<Position*>(tcl.lookup(argv[3]));
00068           assert(pp == getPosition());
00069           posmap[index] = pp;
00070           return TCL_OK;
00071         }
00072     }
00073   return PlugIn::command(argc, argv);
00074 }
00075 
00076 double PositionDB::getDistance(nsaddr_t srcid, nsaddr_t dstid)
00077 {
00078   std::map<int,Position*>::iterator srcit, dstit;
00079   
00080   srcit = posmap.find(srcid);
00081   dstit = posmap.find(dstid);
00082 
00083   if ( ( srcit == posmap.end()) || ( dstit == posmap.end()) )
00084     {
00085       // some entry is missing
00086       return -1;
00087     }
00088   else
00089     {
00090       return (srcit->second)->getDist(dstit->second);
00091     }
00092 }
00093 
00094 
00095 int PositionDB::recvSyncClMsg(ClMessage* m)
00096 {
00097   //std::cerr << __PRETTY_FUNCTION__ << " received msg of type " << m->type() << std::endl; 
00098   if (m->type() == CLMSG_POSITION_GET_DIST)
00099     {
00100 
00101       ClMsgPositionGetDist* mgd;
00102       mgd = dynamic_cast<ClMsgPositionGetDist*>(m);
00103       assert(mgd);
00104       //std::cerr << __PRETTY_FUNCTION__ << " received CLMSG_POSITION_GET_DIST" << std::endl; 
00105       mgd->returnDist(getDistance(mgd->getId1(),mgd->getId2()));
00106     }
00107 
00108   
00109 return 0;
00110 }

Generated on Wed Nov 26 15:47:28 2008 for NS-MIRACLE library by  doxygen 1.5.2