Exemplo n.º 1
0
void MainWindow::reloadFile(){
    int input_fd;
    ssize_t ret_in;
    char buffer[BUF_SIZE];
    struct stat st;
    off_t oldSize, newSize;
    input_fd = open (curFile.toLatin1(), O_RDONLY);
    if (input_fd == -1) {
            perror ("open");
            return;
    }
    errno = 0;
    if(fstat(input_fd, &st))
    {
        printf("\nfstat error: [%s]\n",strerror(errno));
        ::close(input_fd);
        return;
    }
    oldSize = st.st_size;

    forever {
        QCoreApplication::processEvents();
        if(fstat(input_fd, &st))
        {
            printf("\nfstat error: [%s]\n",strerror(errno));
            ::close(input_fd);
            return;
        }
        newSize = st.st_size;

        if (oldSize == newSize)
            continue;

        lseek(input_fd, oldSize, SEEK_SET);
        while((ret_in = read (input_fd, &buffer, newSize-oldSize)) > 0){
            QString qBuf(buffer);
            QCoreApplication::processEvents();
            textEdit->appendPlainText(qBuf.trimmed());
        }
        memset(buffer,0,sizeof(buffer));
        oldSize = newSize;
        sleep(1);
    }
    ::close (input_fd);

}
bool getDisturbanceNodes( uint32 mapID,
                          vector<DisturbanceElement*>& disturbances ) {
   // nothing to read
   if ( disturbances.empty() ) {
      return true;
   }

   char handshake[1024];
   uint32 IP = NetUtility::getLocalIP();
   sprintf(handshake, "InfoModule ready to roll out! (%u.%u.%u.%u)", 
           uint32((IP & 0xff000000) >> 24),
           uint32((IP & 0x00ff0000) >> 16),
           uint32((IP & 0x0000ff00) >>  8),
           uint32(IP & 0x000000ff));
   
   //set up a socket to the MapModule so that we can send a MAPREQUEST_INFO
   auto_ptr<TCPSocket>
      mapSocket( ModuleMap::
                 getMapLoadingSocket( mapID,
                                      MapRequestPacket::MAPREQUEST_INFO,
                                      handshake,
                                      0,
                                      NULL ) );

   if ( mapSocket.get() == NULL ) {
      mc2log << error
             << "[getDisturbanceNodes] ModuleMap::getMapLoadingSocket( "
             << MC2HEX( mapID )
             << ") failed. Aborting loadmap " << endl;
      return false;
   }

   uint32 nbrCoordinates = 0;
   // TODO: use std::accumulate
   for ( vector<DisturbanceElement*>::const_iterator 
            it = disturbances.begin(), itEnd = disturbances.end(); 
          it != itEnd; it++) {
      nbrCoordinates += (*it)->getNbrCoordinates();
   }

   // New scope
   // Send the lat/lon/angles to the mapmodule and receive the
   // itemID:s
   // Question contains 4 bytes lat
   //                   4 bytes lon
   //                   4 bytes angle 0-359
   
   const int qSize = nbrCoordinates * 12;
   DataBuffer qBuf( qSize );
   
   const int ansSize = nbrCoordinates * 20;
   DataBuffer ansBuf( ansSize );
   
   DataBuffer sizeBuf( 4 );
   sizeBuf.writeNextLong( nbrCoordinates );
   int keept = 0;
   int removed = 0;

   if ( mapSocket->writeAll( sizeBuf.getBufferAddress(), 4 ) != 4 ) {
      // Error
      mc2log << warn << "[getDisturbanceNodes] Error writing on mapsocket."
             << endl;
      return false;
   }

   sizeBuf.reset();

   typedef DisturbanceElement::RouteIndex2CoordMap RouteIndex2CoordMap;
   typedef DisturbanceElement::RouteIndex2AngleMap RouteIndex2AngleMap;
   typedef DisturbanceElement::RouteIndex RouteIndex;

   for ( vector<DisturbanceElement*>::iterator 
            it = disturbances.begin(), itEnd = disturbances.end();
         it != itEnd; ++it ) {
      DisturbanceElement* dist = *it;
      const RouteIndex& indexVector = dist->getRouteIndex();
      const RouteIndex2CoordMap& latVector = dist->getLatMap();
      const RouteIndex2CoordMap& lonVector = dist->getLonMap();
      const RouteIndex2AngleMap& angleVector = dist->getAngle();
      
      RouteIndex::const_iterator indexIt;
      RouteIndex2CoordMap::const_iterator latlonIt;
      RouteIndex2AngleMap::const_iterator angleIt;
      
      for ( indexIt = indexVector.begin(); indexIt != indexVector.end();
            ++indexIt ) {
         uint32 index = *indexIt;
         latlonIt = latVector.find( index );
         int32 lat = latlonIt->second;
         latlonIt = lonVector.find( index );
         int32 lon = latlonIt->second;
         angleIt = angleVector.find( index );
         uint32 angle = angleIt->second;
         qBuf.writeNextLong( lat );
         qBuf.writeNextLong( lon );
         qBuf.writeNextLong( angle );
      }
   }

   if ( mapSocket->writeAll( qBuf.getBufferAddress(),
                             qSize) != qSize ) {
      // Error
      mc2log << warn
             << "[getDisturbanceNodes] Error writing on mapsocket" << endl;
      return false;
   }

   qBuf.reset();
   ansBuf.reset();

   if ( mapSocket->readExactly( ansBuf.getBufferAddress(),
                                ansSize ) != ansSize ) {
      // Error
      mc2log << warn << "[getDisturbanceNodes] Error reading on mapsocket"
             << endl;
      return false;
   }

   // FIXME: Set the values and remove the ones with too large
   //        distances.
   vector<DisturbanceElement*>::iterator it = disturbances.begin();
   while ( it != disturbances.end() ) {
      DisturbanceElement* dist = *it;
      const RouteIndex& indexVector = dist->getRouteIndex();
      const RouteIndex2CoordMap& latVector = dist->getLatMap();
      const RouteIndex2CoordMap& lonVector = dist->getLonMap();
      const RouteIndex2AngleMap& angleVector = dist->getAngle();

      RouteIndex2CoordMap::const_iterator latlonIt;
      RouteIndex2AngleMap::const_iterator angleIt;
      const uint32 nbrCoordinates = dist->getNbrCoordinates();
      for ( uint32 i = 0; i < nbrCoordinates; ++i ) {
         const uint32 index    = indexVector[ i ];
         const uint32 mapID    = ansBuf.readNextLong();
         const uint32 nodeID_0 = ansBuf.readNextLong();
         const uint32 nodeID_1 = ansBuf.readNextLong();
         const uint32 distance = ansBuf.readNextLong();
         ansBuf.readNextLong(); // offset

         latlonIt = latVector.find( index );
         const int32 lat = latlonIt->second;
         latlonIt = lonVector.find( index );
         const int32 lon = latlonIt->second;
         angleIt = angleVector.find( index );
         const uint32 angle = angleIt->second;

         // less than 20 meters?, for some unknown reason.
         if ( distance < 20 ) {
            dist->addNodeID( nodeID_0, index );
            dist->setMapID( mapID );
            if( angle == 32767 ) { // 7FFF
               dist->addCoordinate( nodeID_1,
                                    lat, lon,
                                    MAX_UINT32,
                                    dist->getNbrCoordinates() );
            }
            keept++;
         } else {
            dist->removeIndex( index );
            removed++;
         }
      }

      if ( dist->getNbrCoordinates() > 0 ) {
         ++it;
      } else {
         // no coordinates, we dont need an empty disturbance
         delete *it;
         it = disturbances.erase( it );
      }
   }

   return true;
}