int
DisturbanceStorage::
applyAllConnectionChangesToMultiConnection(const fromToNode_t& multi)
{
   int nbrChanges = 0;
   
   pair<nodesInMultiMap_t::iterator, nodesInMultiMap_t::iterator>
      range = m_nodesInMulti.equal_range(multi);

   // Copy the old connectiondata.
   RoutingNode* fromNode   = m_map->getNodeFromTrueNodeNumber(multi.first);
   RoutingNode* toNode     = m_map->getNodeFromTrueNodeNumber(multi.second);
   RoutingConnection* conn = fromNode->getConnection(toNode, true);
   
   RoutingConnectionData oldData( *conn->getData() );
   
   for( nodesInMultiMap_t::iterator it = range.first;
        it != range.second;
        ++it ) {
      // Find change
      changedNodes_t::const_iterator change = m_changedNodes.find(it->second);
      
      // Apply change
      change->second.applyDiff(m_map, fromNode, conn, &(it->second));
      
      ++nbrChanges;
   }

   if ( nbrChanges != 0 ) {
      // Save the old connection data
      RoutingMapConnChanges diff(fromNode, toNode, oldData,
                                 *conn->getData(), true);
      m_changedNodes.insert(
         make_pair(
            fromToNode_t(fromNode->getItemID(),
                         toNode->getItemID()),
            diff
            ));
   }

   mc2dbg << "[DS]: " << nbrChanges << " applied" << endl;
   
   return nbrChanges;
}
bool SQLiteDatabaseConnection::saveNode(const RoutingNode &node)
{
    int rc;
    if(_saveNodeStatement == NULL)
    {
        rc = sqlite3_prepare_v2(_db, "INSERT INTO NODES VALUES(@ID, @LAT, @LON, @BUCKETID);", -1, &_saveNodeStatement, NULL);
        if (rc != SQLITE_OK)
        {	
            std::cerr << "Failed to create saveNodeStatement." << " Resultcode: " << rc << std::endl;
            return false;
        }
    }

    // Parameter an das Statement binden
    sqlite3_bind_int64(_saveNodeStatement, 1, node.getID());
    sqlite3_bind_double(_saveNodeStatement, 2, node.getLat());
    sqlite3_bind_double(_saveNodeStatement, 3, node.getLon());
    sqlite3_bind_int64(_saveNodeStatement, 4, spc->getBucketID(node.getLat(), node.getLon()));

    // Statement ausfuehren
    rc = sqlite3_step(_saveNodeStatement);
    
    if (rc != SQLITE_DONE)
    {	
        std::cerr << "Failed to execute saveNodeStatement." << " Resultcode: " << rc << std::endl;
        return false;
    }


    rc = sqlite3_reset(_saveNodeStatement);
    if(rc != SQLITE_OK)
    {
        std::cerr << "Failed to reset saveNodeStatement." << " Resultcode: " << rc << std::endl;
    }
    return true;
}
inline UpdateTrafficCostReplyPacket*
RouteProcessor::processUpdateTrafficCostRequestPacket(
   const UpdateTrafficCostRequestPacket* updateCostsPacket)
{
   mc2dbg2 << "RouteProcessor::processUpdateTrafficCostRequestPacket"
           << endl;
   
   // Temporary trick
   UpdateTrafficCostReplyPacket* reply = 
         new UpdateTrafficCostReplyPacket( updateCostsPacket );
   reply->setStatus(StringTable::OK);
   return reply;
   
   UpdateTrafficCostReplyPacket* replyPacket = NULL;

   if (updateCostsPacket != NULL) {
      
      uint32 mapID = updateCostsPacket->getMapID();
      
      DEBUG8(updateCostsPacket->dump());         
      mc2dbg << "Updating map " << mapID << endl;

      // Get the CalcRoute for the map.
      CalcRoute* calcRoute = getCalcRoute(mapID);
      StringTable::stringCode status = StringTable::OK;
      
      if (calcRoute != NULL) {
         RoutingMap* mapToUpdate = calcRoute->getMap();
            
         uint32 nbrNodesToUpdate = updateCostsPacket->getNbrCosts();
         for (uint32 j = 0; j < nbrNodesToUpdate; j++) {
            
            uint32 nodeID = MAX_UINT32;
            uint32 cost   = MAX_UINT32;
            
            if (updateCostsPacket->getCost(nodeID, cost, j)) {
               
               RoutingNode* node =
                  mapToUpdate->getNodeFromTrueNodeNumber(nodeID);
               
               if (node != NULL) {
                  RoutingConnection* connection =
                     node->getFirstConnection();
                  
                  while (connection != NULL) {
                     RoutingConnectionData* connData =
                        connection->getData();
//                       RoutingNode* toNode =
//                          mapToUpdate->getNode(connection->getIndex());
                     RoutingNode* toNode = connection->getNode();
                     if ( toNode == NULL ) {
                        mc2dbg << "Coulnd't find connection from "
                               << hex << nodeID << " to node with index "
                               << connection << endl;
                        continue;
                     }
                     uint32 costC = connData->getCostB(0) + cost;
                     mapToUpdate->changeConnectionCosts(
                        node,
                        connection,
                        false,
                        connData->getCostA(0),
                        connData->getCostB(0),
                        costC,
                        connData->getVehicleRestriction(false));
                     connection = connection->getNext();
                  }
               }
               else {
                  MC2WARNING("Node not found");
                  cerr << "map " << mapID << "   nodeID: " << hex << nodeID
                       << dec << " (" << nodeID << ")" << endl;
                  status = StringTable::NOTFOUND;
               }
            }
            else {
               cerr << "Could not get cost " << j << endl;
            }
         }
      }
      else {
         handleMapNotFound(mapID, updateCostsPacket);
         status = StringTable::MAPNOTFOUND;
      }
 
      replyPacket = new UpdateTrafficCostReplyPacket(updateCostsPacket);
      replyPacket->setStatus(status);
   } // end if (calcRoute != NULL)
      
   return replyPacket;
} // updateTrafficCostRequestPacket
inline void
RouteProcessor::copySubroutesToOrigin(SubRouteList* subList,
                                      Head* origin,
                                      RoutingMap* routingMap)
{
   // Add all origins from subList
   if (subList->getNbrSubRoutes() == 0) {
      mc2dbg << "subList->getNbrSubRoutes(1) == 0" << endl;
   }

   for (uint32 i = 0; i < subList->getNbrSubRoutes(); i++) {
      RMSubRoute* sub = subList->getSubRoute(i);
      for (uint32 j = 0; j < sub->getNbrConnections(); j++) {
         
         uint32 mapID;
         uint32 nodeID;
         uint32 cost;
         uint32 estimatedCost;
         uint16 offset;
         int32 lat,lon;
         uint32 costASum;
         uint32 costBSum;
         uint32 costCSum;

         sub->getExternal(j, mapID, nodeID, cost, estimatedCost,
                          lat, lon, costASum, costBSum, costCSum);

         mc2dbg8 << "RP: copySubRoutesToOrigin: From packet costs "
                 << costASum << ','
                 << costBSum << ','
                 << costCSum << endl;

         
         if (routingMap->getMapID() == mapID) {
            RoutingNode* routingNode =
               routingMap->getNodeFromTrueNodeNumber(nodeID);
            if ( routingNode != NULL ) {
               // XXX Check this
               if (IS_NODE0(nodeID)) {
                  offset = 0;
               }
               else {
                  offset = 0; // Should always be zero
               }

               mc2dbg8 << "[RP] - new OrigDestNode with lat = "
                       << routingNode->getLat() << " and lon = "
                       << routingNode->getLong() << endl;
               mc2dbg8 << "[RP] - server thinks         lat = "
                       << lat << " and lon = " << lon << endl;

               
               
               OrigDestNode* node 
                  = routingMap->newOrigDestNode(routingNode->getIndex(),
                                                mapID,
                                                offset,
                                                routingNode->getLat(),
                                                routingNode->getLong(),
                                                cost,
                                                estimatedCost,
                                                costASum,
                                                costBSum,
                                                costCSum);
               node->setItemID(nodeID); 
               node->into(origin);
            } else {
               mc2log << error
                      << "RP: Strange node " << hex
                      << nodeID << dec << " not fould in map "
                      << mapID << endl;
            }
         }
      }
   }
} // copySubRoutesToOrigin
Example #5
0
uint qHash(RoutingNode& node)
{
	return node.getID();
}
int
DisturbanceStorage::addDisturbances( const DisturbanceVector* distVect )
{
   int nbrDistAdded = 0;
   int nbrDist = distVect->size();
   const bool isOverviewMap = MapBits::isOverviewMap( m_map->getMapID() );
   for(int i=0; i < nbrDist; ++i ) {
      DisturbanceListElement* el = (*distVect)[i];
      uint32 mapID = el->getMapID();
      uint32 nodeID = el->getNodeID();

      mc2dbg4 << "[DStor]: Element has ID " << IDPair_t(mapID, nodeID)
              << endl;
      
      if ( isOverviewMap &&
           !MapBits::isOverviewMap( mapID ) ) {
         // We will have to translate the id

         uint32 higherID = m_map->translateToHigher( mapID,
                                                     nodeID );

         if ( higherID != MAX_UINT32 ) {
            mapID  = m_map->getMapID();
            nodeID = higherID;
         } else {
            // Not found - go another round
            continue;
         }
      }

      mc2dbg4 << "[DStor]: Dist-node has id = " << IDPair_t(mapID, nodeID)
              << endl;
      
      if ( mapID == m_map->getMapID() ) {
         RoutingNode* node = m_map->getNodeFromTrueNodeNumber(nodeID);
         if ( node == NULL ) {
            mc2dbg << "[DStor]: Coulnd't find node "
                   << hex << nodeID << dec << endl;
            continue;
         }
         if ( el->getUseFactor() || el->getRawFactor() == MAX_UINT32 ) {
            mc2dbg8 << "[DStor]: Using factor when adding disturbance on node "
                   << MC2HEX( nodeID )
                   << endl;
            mc2dbg8 << "RAW factor is " << el->getRawFactor() << endl;
            
            if ( el->getRawFactor() == MAX_UINT32 ) {
               mc2dbg8 << "[DStor]: Node " << MC2HEX( nodeID )
                       << " is blocked " << endl;
               avoidNode( node );
               ++nbrDistAdded;
            } else {
               const float factor = el->getFactor();
               mc2dbg8 << "[DStor]: Factor is " << factor << endl;
               bool added = false;
               for ( RoutingConnection* curConn = node->getFirstConnection();
                     curConn != NULL;
                     curConn = curConn->getNext() ) {
                  added = true;
                  const RoutingConnectionData* connData = curConn->getData();
                  uint32 newCostC = uint32(connData->getCostB(0) * factor );
                  if(newCostC == connData->getCostB(0)){
                     mc2dbg8 << "newCostC same adding 1 **" << endl;
                     newCostC++;
                  }
                  
                  changeConnectionCosts(
                     node,
                     curConn,
                     connData->getCostA(0),
                     connData->getCostB(0),
                     newCostC,
                     connData->getVehicleRestriction(false));
               }
               nbrDistAdded += added;
            }
         } else {
            mc2dbg4 << "Using time instead of factor" << endl;

            ++nbrDistAdded;
            uint32 cost = uint32(SEC_TO_TIME_COST(el->getTime()));
            
            RoutingConnection* curConn = node->getFirstConnection();
            while ( curConn != NULL ) {
               RoutingConnectionData* connData =
                  curConn->getData();

               uint32 costC = connData->getCostB(0) + cost;
               changeConnectionCosts( node,
                                      curConn,
                                      connData->getCostA(0),
                                      connData->getCostB(0),
                                      costC,
                                      connData->getVehicleRestriction(false));
               curConn = curConn->getNext();
            }
         }
      }
   }
   return nbrDistAdded;
}
boost::shared_ptr<RoutingNode> SQLiteDatabaseConnection::getNodeByID(boost::uint64_t id)
{
    boost::shared_ptr<RoutingNode> retVal;
    
	int rc;
	if(_getNodeByIDStatement == NULL)
	{		
		rc = sqlite3_prepare_v2(_db, "SELECT ID, LAT, LON FROM NODES WHERE ID=?;",
			-1, &_getNodeByIDStatement, NULL);
		if (rc != SQLITE_OK)
		{	
			std::cerr << "Failed to create getNodeByIDStatement." << " Resultcode: " << rc << std::endl;
			return boost::shared_ptr<RoutingNode>();
		}
	}
	
	// Parameter an das Statement binden
    RoutingNode node;
    node.setID(id);
    if (node.isIDInLongFormat())
    {
        sqlite3_bind_int64(_getNodeByIDStatement, 1, RoutingNode::convertIDToShortFormat(id));
    }
    else
    {
        sqlite3_bind_int64(_getNodeByIDStatement, 1, id);
    }
	
	// Statement ausfuehren, in einer Schleife immer neue Zeilen holen
	while ((rc = sqlite3_step(_getNodeByIDStatement)) != SQLITE_DONE)
    {
        //Es können verschiedene Fehler aufgetreten sein.
        if (!sqlite_functions::handleSQLiteResultcode(rc))
            break;
        
        //Erstelle einen neuen Knoten auf dem Heap.
        //Verwirrend: Hier ist der erste Parameter mit Index 0 und nicht 1 (!!).
        RoutingNode* newNode = new RoutingNode(sqlite3_column_int64(_getNodeByIDStatement, 0),
                        sqlite3_column_double(_getNodeByIDStatement, 1),
                        sqlite3_column_double(_getNodeByIDStatement, 2));
        //Gib ihn an einen boost::shared_ptr weiter. newNode jetzt nicht mehr verwenden oder delete drauf anwenden!
        boost::shared_ptr<RoutingNode> ptr(newNode);
        //den boost::shared_ptr zur Liste hinzufügen
        retVal = ptr;
    }
	
    if (rc != SQLITE_DONE)
	{	
		std::cerr << "Failed to execute getNodeByIDStatement." << " Resultcode: " << rc << std::endl;
		return boost::shared_ptr<RoutingNode>();
	}
	
	
	rc = sqlite3_reset(_getNodeByIDStatement);
	if(rc != SQLITE_OK)
	{
		std::cerr << "Failed to reset getNodeByIDStatement." << " Resultcode: " << rc << std::endl;
	}
	
    return retVal;
}