Ejemplo n.º 1
0
int main()
{	
	int fd = socket(AF_INET, SOCK_STREAM, 0);
	struct sockaddr_in svraddr;
	bzero(&svraddr, sizeof svraddr);
	svraddr.sin_family = AF_INET;
	svraddr.sin_port = htons(7000);
	svraddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	if(connect(fd, (struct sockaddr*)&svraddr, sizeof(svraddr))<0)
	{
		cout<<strerror(errno)<<endl;
		exit(1);
	}
	char buf[1024] = "give me some info";
	int nw = write(fd,buf,strlen(buf) );
	int n =0;
	int nr = read(fd,buf,1024 );
	while(nr > 0 )
	{
		n += nr;
		nr = read(fd, buf ,1024- n );
	}
	SearchRequest r;
	cout<<r.ParseFromArray(buf, 1024)<<" "<<n<<" "<<nw<<endl;
	cout<<r.query()<<" "<<r.page_number()<<" "<<r.result_per_page()<<endl;
	return 0;
}
Ejemplo n.º 2
0
string JabberClient::search(const char *jid, const char *condition)
{
    SearchRequest *req = new SearchRequest(this, jid);
    req->start_element("query");
    req->add_attribute("xmlns", "jabber:iq:search");
    req->add_condition(condition);
    req->send();
    m_requests.push_back(req);
    return req->m_id;
}
Ejemplo n.º 3
0
string JabberClient::search(const char *jid, const char *condition)
{
    SearchRequest *req = new SearchRequest(this, jid);
    req->start_element("query");
    req->add_attribute("xmlns", "jabber:iq:search");
    QString cond = QString::fromUtf8(condition);
    while (cond.length()){
        QString item = getToken(cond, ';');
        QString key = getToken(item, '=');
        req->text_tag(key.utf8(), item.utf8());
    }
    req->send();
    m_requests.push_back(req);
    return req->m_id;
}
Ejemplo n.º 4
0
void FindDialog::search(SearchRequest & request)
{
    ui->searchFor->setText(request.getQuery());
    ui->replaceWith->setText(request.getReplacement());
    ui->backward->setChecked(request.getOptions() & SearchRequest::Backwards);
    ui->cs->setChecked(request.getOptions() & SearchRequest::CaseSensitive);
    ui->re->setChecked(request.getOptions() & SearchRequest::RegExp);
    ui->wrapSearch->setChecked(request.getOptions() & SearchRequest::Wrap);
    ui->wo->setChecked(request.getOptions() & SearchRequest::WholeWords);
    ui->wo->setChecked(request.getOptions() & SearchRequest::WholeWords);
    ui->ungreedy->setChecked(request.getOptions() & SearchRequest::Ungreedy);
    ui->ungreedy->setEnabled(ui->re->isChecked());
    ui->currentFile->setChecked(true);
    ui->openFiles->setChecked(false);
    searchClicked(request.getOptions() & SearchRequest::All, false, request.getOptions() & SearchRequest::Replace);
}
  SearchResultListing searchFoods(const SearchRequest& req)
  {
    BackEnd::SearchRequest be_req;
    SearchResultListing resultListing;

    be_req.searchTerms = QString::fromStdString(req.searchterms());

    for (int i = 0; i < req.excludegroupids_size(); ++i) {
      be_req.excludeGroups.append
        (Group::getGroup(QString::fromStdString(req.excludegroupids(i))));
    }

    be_req.searchSingleFoods = req.searchsinglefoods();
    be_req.searchComposites = req.searchcomposites();
    be_req.searchTemplates = req.searchtemplates();

    be_req.sourceUSDA = req.sourceusda();
    be_req.sourceImport = req.sourceimport();
    be_req.sourceSelf = req.sourceself();
    be_req.sourceOthers = req.sourceothers();
    be_req.includeExpired = req.includeexpired();

    be_req.maxResults = req.maxresults();

    be_req.userId = User::getLoggedInUserId();

    resultListing.addObjects(BackEnd::getBackEnd()->searchFoods(be_req));

    return resultListing;
  }
bool
NavSearchHandler::handleSearch( UserItem* userItem, 
                                NavRequestPacket* req, 
                                NavReplyPacket* reply )
{
   if ( !checkExpectations( req->getParamBlock(), reply ) ) {
      return false;
   }
   bool ok = true;

   uint32 startTime = TimeUtility::getCurrentTime();
   uint32 now = TimeUtility::getRealTime();

   // The params
   const NParamBlock& params = req->getParamBlock();
   NParamBlock& rparams = reply->getParamBlock();
   // The user
   UserUser* user = userItem->getUser();

   const ClientSetting* clientSetting = 
      m_userHelp->getClientSetting( params, user );
   UserEnums::URType urmask = m_userHelp->getUrMask( clientSetting );

   // Start parameter printing
   mc2log << info << "handleSearch:";

   // Language
   StringTable::languageCode language = user->getLanguage();
   if ( params.getParam( 6 ) ) {
      language = NavUtil::mc2LanguageCode( 
         params.getParam( 6 )->getUint16() );
      mc2log << " Lang " << StringTable::getString( 
         StringTable::getLanguageAsStringCode( language ), 
         StringTable::ENGLISH );
   }

   SearchRequestParameters searchParams;
   searchParams.setTryHarder( true );
   searchParams.setLookupCoordinates( true  );
   searchParams.setAddStreetNamesToCompanies( true );
   searchParams.setSearchForTypes( user->getSearchForTypes() );
   searchParams.setSearchForLocationTypes( 
      user->getSearchForLocationTypes() );
   searchParams.setMatchType( SearchTypes::StringMatching( 
                                 user->getSearch_type() ) );
   searchParams.setStringPart( SearchTypes::StringPart( 
                                  user->getSearch_substring() ) );
   searchParams.setSortingType( SearchTypes::DistanceSort );
   searchParams.setRequestedLang( language );
   SearchRequestParameters areaSearchParams( searchParams );
   
   // Start index
   uint16 areaStartIndex = 0;
   uint16 itemStartIndex = 0;
   if ( params.getParam( 1200 ) ) {
      areaStartIndex = params.getParam( 1200 )->getUint16Array( 0 );
      itemStartIndex = params.getParam( 1200 )->getUint16Array( 1 );
      mc2log << " IN " << areaStartIndex << "," << itemStartIndex;
   }

   // Search area string
   MC2String areaStrStr;
   const char* areaStr = NULL;
   if ( params.getParam( 1201 ) ) {
      areaStrStr = params.getParam( 1201 )->getString(
         m_thread->clientUsesLatin1() );
      areaStr = areaStrStr.c_str();
      mc2log << " A " << areaStr;
   }

   // Search AreaID
   MC2String areaIDStr;
   const char* areaID = NULL;
   if ( params.getParam( 1202 ) ) {
      areaIDStr = params.getParam( 1202 )->getString(
         m_thread->clientUsesLatin1());
      areaID = areaIDStr.c_str();
      mc2log << " SA " << areaID;
   }

   // Search item string
   MC2String itemStr;
   if ( params.getParam( 1203 ) ) {
      itemStr = StringUtility::trimStartEnd( 
         params.getParam( 1203 )->getString(
            m_thread->clientUsesLatin1() ) );
      mc2log << " I " << itemStr;
   }

   // Search category
   MC2String categoryStr;
   if ( params.getParam( 1204 ) ) {
      categoryStr = params.getParam( 1204 )->getString(
         m_thread->clientUsesLatin1() );
      mc2log << " C " << categoryStr;
      SearchParserHandler::Category cat = 
         m_thread->getSearchHandler().
         findCategoryFromListLowerCase( categoryStr );
      if ( cat == SearchParserHandler::INVALID_CATEGORY ) {
         // do old style search
         searchParams.setMatchType( SearchTypes::FullMatch );
      } else {
         // set category id and do a faster new category search.
         set<SearchParserHandler::Category::CategoryID> categories;
         categories.insert( cat.getCategoryID() );
         searchParams.setCategories( categories );
         // must clear it to search all categories.
         categoryStr.clear();
         itemStr.clear();
      }

   }

   // TopRegionID
   uint32 topRegionID = MAX_UINT32;
   if ( params.getParam( 1205 ) ) {
      topRegionID = params.getParam( 1205 )->getUint32();
      mc2log << " TID " << MC2HEX( topRegionID );
   }

   // Search pos
   MC2Coordinate searchPos;
   if ( params.getParam( 1206 ) ) {
      searchPos = MC2Coordinate( 
         Nav2Coordinate( params.getParam( 1206 )->getInt32Array( 0 ),
                         params.getParam( 1206 )->getInt32Array( 1 ) ) );
      mc2log << " Pos " << searchPos;//.lat << ", " << searchPos.lon;
   }

   // Max Nbr Search Matches
   uint32 maxNbrMatches = 10;
   if ( params.getParam( 1207 ) ) {
      maxNbrMatches = params.getParam( 1207 )->getUint16();
      mc2log << " MAX " << maxNbrMatches;
   }

   // Sorting
   if ( params.getParam( 1208 ) ) {
      areaSearchParams.setSortingType( 
         mc2SortType( params.getParam( 1208 )->getByteArray()[ 0 ] ) );
      searchParams.setSortingType( 
         mc2SortType( params.getParam( 1208 )->getByteArray()[ 1 ] ) );
      mc2log << " AS " << SearchTypes::getSortingString( 
                areaSearchParams.getSortingType() ) << " IS " 
             << SearchTypes::getSortingString( 
                searchParams.getSortingType() );
   }

   // End parameter printing
   mc2log << endl;

   // AURA
   set< uint32 >* allowedMaps = NULL;
   if ( ok && !m_thread->getMapIdsForUserRegionAccess( user, allowedMaps,
                                                       now, urmask ) )
   {
      mc2log << warn << "handleSearch: getMapIdsForUserRegionAccess"
             << " failed. ";
      if ( TimeUtility::getCurrentTime() - startTime > 3000 ) {
         reply->setStatusCode( 
            NavReplyPacket::NAV_STATUS_REQUEST_TIMEOUT );
         mc2log << "Timeout";
      } else {
         reply->setStatusCode( NavReplyPacket::NAV_STATUS_NOT_OK );
         mc2log << "Error";
      }
      mc2log << endl;
      ok = false;
   }

   // Store user position
   if ( ok && searchPos.isValid() ) {
      m_userHelp->storeUserPosition( user->getUIN(), searchPos, "Search" );
   }


   // Do we have selected location?
   SearchMatch* selectedArea = NULL;
   if ( areaID != NULL ) {
      selectedArea = SearchMatch::createMatch( areaID );
      if ( selectedArea == NULL ) {
         mc2log << warn << "handleSearch: Bad areaID " 
                << MC2CITE( areaID ) << endl;
         ok = false;
         reply->setStatusCode( 
            NavReplyPacket::NAV_STATUS_PARAMETER_INVALID );
         reply->setStatusMessage( "Search AreaID" );
      } else {
         // Check if area is in allowed maps
         if ( allowedMaps != NULL && allowedMaps->find( 
                 selectedArea->getMapID() ) == allowedMaps->end() )
         {
            mc2log << warn << "handleSearch: selected area unallowed map "
                   << MC2CITE( areaID ) << endl;
            ok = false;
            reply->setStatusCode( 
               NavReplyPacket::NAV_STATUS_OUTSIDE_ALLOWED_AREA );
         }
      }
   }

   // Which item search string
   MC2String searchString;
   if ( categoryStr.length() > 0 ) {
      searchString = categoryStr;
   } else if ( itemStr.length() > 0 ) {
      searchString = itemStr;
   }
   bool searchForArea = true;

   // Search!
   if ( ok ) {
      SearchResultRequest* sr = NULL;
      RequestData reqID = m_thread->getNextRequestID();

      if ( selectedArea != NULL ) {
         vector < IDPair_t > selectedAreas;
         selectedAreas.push_back( selectedArea->getID() );

         SearchRequest* s = new SearchRequest( 
            reqID, searchParams, selectedAreas, searchString.c_str(),
            m_thread->getTopRegionRequest(),
            searchPos, &areaSearchParams );
         s->setAllowedMaps( allowedMaps );
         sr = s;
         searchForArea = false;
      } else {
         // Get topRegionID from position and check position if proximity
         if ( topRegionID == MAX_UINT32 || areaStr == NULL ) {
            if ( searchPos.isValid() ) {
               CoordinateRequest* navReq = new 
                  CoordinateRequest( m_thread->getNextRequestID(),
                                     m_thread->getTopRegionRequest());
               const byte itemType = ItemTypes::streetSegmentItem;
               navReq->addCoordinate( 
                  searchPos.lat, searchPos.lon,
                  ( COORDINATE_PACKET_RESULT_ANGLE |
                    COORDINATE_PACKET_RESULT_STRINGS |
                    COORDINATE_PACKET_RESULT_IDS ), 1, &itemType);
               m_thread->putRequest( navReq );
               CoordinateReplyPacket* crp = navReq->getCoordinateReply();
               if ( crp == NULL || crp->getStatus() != StringTable::OK ) {
                  // We have an error report it to the user!
                  mc2log << warn << "handleSearch: Lookup of pos falied ";
                  uint8 errorCode = NavReplyPacket::NAV_STATUS_NOT_OK;
                  if ( crp != NULL ) {
                     if ( crp->getStatus() == StringTable::MAPNOTFOUND ) {
                        mc2log << "OUTSIDE_MAP";
                        errorCode = NavReplyPacket::NAV_STATUS_OUTSIDE_MAP;
                     } else {
                        mc2log << "NOT_OK";
                        errorCode = NavReplyPacket::NAV_STATUS_NOT_OK;
                     }
                  } else {
                     if ( TimeUtility::getCurrentTime() - startTime > 3000 ) {
                        mc2log << "REQUEST_TIMEOUT";
                        errorCode = 
                           NavReplyPacket::NAV_STATUS_REQUEST_TIMEOUT;
                     } else {
                        mc2log << "Reply null NOT_OK";
                        errorCode = NavReplyPacket::NAV_STATUS_NOT_OK;
                     }
                  }
                  mc2log << endl;
                  reply->setStatusCode( errorCode );
                  ok = false;
               } else { // Reply is ok
                  const TopRegionRequest* topRegReq = 
                     m_group->getTopRegionRequest( &*m_thread );

                  // Note that the topRegionID is a topRegionID - not CC.
                  if ( topRegionID == MAX_UINT32 ) {
                     // Find the topRegionMatch of type country.
                     const TopRegionMatch* topReg =
                        topRegReq->getCountryForMapID( crp->getMapID() );
                     if ( topReg != NULL ) {
                        topRegionID = topReg->getID();
                     }
                  }
               }
               delete navReq;
            } else if ( topRegionID != MAX_UINT32 && areaStr == NULL ) {
               // Have topregion and proximity search but no pos is ok now
            } else {
               mc2log << warn << "handleSearch: not valid pos";
               if ( topRegionID == MAX_UINT32 ) {
                  mc2log << " for unknown topRegionID ";
               }
               if ( areaStr == NULL ) {
                  mc2log << " for proximity ";
               }
               mc2log << endl;
               ok = false;
               reply->setStatusCode( 
                  NavReplyPacket::NAV_STATUS_MISSING_PARAMETER );
               reply->setStatusMessage( "Search Position" );
            }
         } // End if needs to lookup pos


         if ( ok ) {
            if ( areaStr != NULL ) {
               // Check if topRegionID is in allowed regions
               if ( m_thread->checkUserRegionAccess( topRegionID, user,
                                                     urmask ) )
               {
                  sr = new SearchRequest( 
                     reqID, searchParams, topRegionID, areaStr,
                     searchString.c_str(), m_thread->getTopRegionRequest(),
                     searchPos, &areaSearchParams );
               } else {
                  mc2log << warn << "handleSearch: selected topRegionID "
                         << hex << topRegionID << dec << " not allowed."
                         << endl;
                  ok = false;
                  reply->setStatusCode( 
                     NavReplyPacket::NAV_STATUS_OUTSIDE_ALLOWED_AREA );
               }
            } else { // Search around position
               // Will search the whole country.
               if ( searchPos.isValid() ) {
                  sr = new ClosestSearchRequest( 
                     reqID, searchParams, searchPos, searchString,
                     m_thread->getTopRegionRequest(),
                     allowedMaps ? *allowedMaps : set<uint32>() );
               } else if ( topRegionID != MAX_UINT32 ) {
                  if ( m_thread->checkUserRegionAccess( topRegionID, 
                                                        user, urmask ) )
                  {
                     sr = new ClosestSearchRequest( 
                        reqID, searchParams, topRegionID, searchString,
                        m_thread->getTopRegionRequest(),
                        allowedMaps ? *allowedMaps : set<uint32>() );
                  } else {
                     mc2log << warn << "handleSearch: proximity: selected "
                            << "topRegionID " << hex << topRegionID << dec
                            << " not allowed." << endl;
                     ok = false;
                     reply->setStatusCode( 
                        NavReplyPacket::NAV_STATUS_OUTSIDE_ALLOWED_AREA );
                  }
               } else {
                  mc2log << warn << "handleSearch: not valid pos nor "
                         << "topRegion ";
                  mc2log << endl;
                  ok = false;
                  reply->setStatusCode( 
                     NavReplyPacket::NAV_STATUS_MISSING_PARAMETER );
                  reply->setStatusMessage( 
                     "Search Position or TopRegion" );
               }
               searchForArea = false;
            }
         }
         
      } // End else if have selected search area
      
      if ( ok ) {
         m_thread->putRequest( sr );

         // Check sr
         mc2log << info << "handleSearch: Reply:";
         if ( sr->getStatus() != StringTable::OK ) {
            if ( sr->getStatus() == StringTable::TIMEOUT_ERROR ) {
               mc2log << " REQUEST_TIMEOUT";
               reply->setStatusCode( 
                  NavReplyPacket::NAV_STATUS_REQUEST_TIMEOUT );
            } else if ( sr->getStatus() == 
                        StringTable::OUTSIDE_ALLOWED_AREA )
            {
               mc2log << " OUTSIDE_ALLOWED_AREA";
               reply->setStatusCode( 
                  NavReplyPacket::NAV_STATUS_OUTSIDE_ALLOWED_AREA );
            } else { 
               mc2log << " NOT_OK";
               reply->setStatusCode( NavReplyPacket::NAV_STATUS_NOT_OK );
            }
         } else {
            // Add the result
            addMatches( rparams, sr, searchForArea, maxNbrMatches, 
                        areaStartIndex, itemStartIndex, 1302, 
                        1303, 1300, 1304, 1301 );
         } // End else sr is ok
         mc2log << endl;
         
         PacketContainer* searchAnswer = sr->getAnswer();
         
         // Debit search
         MC2String extraInfo = m_userHelp->makeExtraUserInfoStr( params );
         if ( !m_thread->getDebitHandler()->makeSearchDebit( 
                 userItem, sr, searchAnswer, areaStr,
                 searchString.c_str(), extraInfo.c_str() ) )
         {
            mc2log << warn << "handleSearch: debit failed." << endl;
         }


         delete searchAnswer;
         delete sr->getAnswer();
         delete sr;
      } // End if ok
   } // End if ok


   delete allowedMaps;
   delete selectedArea;

   return ok;
}