mirror of
https://github.com/RetroShare/RetroShare.git
synced 2024-10-01 02:35:48 -04:00
added regexp search to turtle router
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@1564 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
bb7e3684f0
commit
3c09b4f2d2
@ -65,6 +65,22 @@ int FileIndexMonitor::SearchKeywords(std::list<std::string> keywords, std::list<
|
||||
std::list<FileEntry *> firesults;
|
||||
|
||||
fi.searchTerms(keywords, firesults);
|
||||
|
||||
return filterResults(firesults,results,flags) ;
|
||||
}
|
||||
|
||||
int FileIndexMonitor::SearchBoolExp(Expression *exp, std::list<FileDetail>& results,uint32_t flags) const
|
||||
{
|
||||
results.clear();
|
||||
std::list<FileEntry *> firesults;
|
||||
|
||||
fi.searchBoolExp(exp, firesults);
|
||||
|
||||
return filterResults(firesults,results,flags) ;
|
||||
}
|
||||
|
||||
int FileIndexMonitor::filterResults(std::list<FileEntry*>& firesults,std::list<FileDetail>& results,uint32_t flags) const
|
||||
{
|
||||
time_t now = time(NULL) ;
|
||||
|
||||
/* translate/filter results */
|
||||
|
@ -76,6 +76,9 @@ class FileIndexMonitor: public CacheSource, public RsThread
|
||||
/* external interface for filetransfer */
|
||||
bool findLocalFile(std::string hash,uint32_t f, std::string &fullpath, uint64_t &size) const;
|
||||
int SearchKeywords(std::list<std::string> keywords, std::list<FileDetail> &results,uint32_t flags) ;
|
||||
int SearchBoolExp(Expression *exp, std::list<FileDetail> &results,uint32_t flags) const ;
|
||||
int filterResults(std::list<FileEntry*>& firesults,std::list<FileDetail>& results,uint32_t flags) const ;
|
||||
|
||||
|
||||
/* external interface for local access to files */
|
||||
bool convertSharedFilePath(std::string path, std::string &fullpath);
|
||||
|
@ -29,12 +29,20 @@
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
||||
|
||||
/******************************************************************************************
|
||||
eval functions of relational expressions.
|
||||
|
||||
******************************************************************************************/
|
||||
|
||||
template<>
|
||||
void RelExpression<int>::linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._ints.push_back(Op) ;
|
||||
e._ints.push_back(LowerValue) ;
|
||||
e._ints.push_back(HigherValue) ;
|
||||
}
|
||||
|
||||
|
||||
bool DateExpression::eval(FileEntry *file)
|
||||
{
|
||||
return evalRel(file->modtime);
|
||||
@ -148,3 +156,125 @@ bool StringExpression :: evalStr ( std::string &str ){
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* linearization code
|
||||
*************************************************************************/
|
||||
|
||||
void CompoundExpression::linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_COMP) ;
|
||||
e._ints.push_back(Op) ;
|
||||
|
||||
Lexp->linearize(e) ;
|
||||
Rexp->linearize(e) ;
|
||||
}
|
||||
|
||||
void StringExpression::linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._ints.push_back(Op) ;
|
||||
e._ints.push_back(IgnoreCase) ;
|
||||
e._ints.push_back(terms.size()) ;
|
||||
|
||||
for(std::list<std::string>::const_iterator it(terms.begin());it!=terms.end();++it)
|
||||
e._strings.push_back(*it) ;
|
||||
}
|
||||
|
||||
Expression *LinearizedExpression::toExpr(const LinearizedExpression& e)
|
||||
{
|
||||
int i=0,j=0,k=0 ;
|
||||
return toExpr(e,i,j,k) ;
|
||||
}
|
||||
|
||||
void LinearizedExpression::readStringExpr(const LinearizedExpression& e,int& n_ints,int& n_strings,std::list<std::string>& strings,bool& b,StringOperator& op)
|
||||
{
|
||||
op = static_cast<StringOperator>(e._ints[n_ints++]) ;
|
||||
b = e._ints[n_ints++] ;
|
||||
int n = e._ints[n_ints++] ;
|
||||
|
||||
strings.clear() ;
|
||||
for(int i=0;i<n;++i)
|
||||
strings.push_back(e._strings[n_strings++]) ;
|
||||
}
|
||||
|
||||
Expression *LinearizedExpression::toExpr(const LinearizedExpression& e,int& n_tok,int& n_ints,int& n_strings)
|
||||
{
|
||||
LinearizedExpression::token tok = static_cast<LinearizedExpression::token>(e._tokens[n_tok++]) ;
|
||||
|
||||
switch(tok)
|
||||
{
|
||||
case EXPR_COMP: {
|
||||
LogicalOperator op = static_cast<LogicalOperator>(e._ints[n_ints++]) ;
|
||||
|
||||
Expression *e1 = toExpr(e,n_tok,n_ints,n_strings) ;
|
||||
Expression *e2 = toExpr(e,n_tok,n_ints,n_strings) ;
|
||||
|
||||
return new CompoundExpression(op,e1,e2) ;
|
||||
}
|
||||
|
||||
case EXPR_POP: {
|
||||
RelOperator op = static_cast<RelOperator>(e._ints[n_ints++]) ;
|
||||
int lv = e._ints[n_ints++] ;
|
||||
int hv = e._ints[n_ints++] ;
|
||||
|
||||
return new PopExpression(op,lv,hv) ;
|
||||
}
|
||||
case EXPR_SIZE: {
|
||||
RelOperator op = static_cast<RelOperator>(e._ints[n_ints++]) ;
|
||||
int lv = e._ints[n_ints++] ;
|
||||
int hv = e._ints[n_ints++] ;
|
||||
|
||||
return new SizeExpression(op,lv,hv) ;
|
||||
}
|
||||
case EXPR_DATE: {
|
||||
RelOperator op = static_cast<RelOperator>(e._ints[n_ints++]) ;
|
||||
int lv = e._ints[n_ints++] ;
|
||||
int hv = e._ints[n_ints++] ;
|
||||
|
||||
return new DateExpression(op,lv,hv) ;
|
||||
}
|
||||
case EXPR_HASH: {
|
||||
std::list<std::string> strings ;
|
||||
StringOperator op ;
|
||||
bool b ;
|
||||
|
||||
readStringExpr(e,n_ints,n_strings,strings,b,op) ;
|
||||
return new HashExpression(op,strings) ;
|
||||
}
|
||||
case EXPR_EXT:
|
||||
{
|
||||
std::list<std::string> strings ;
|
||||
StringOperator op ;
|
||||
bool b ;
|
||||
|
||||
readStringExpr(e,n_ints,n_strings,strings,b,op) ;
|
||||
|
||||
return new ExtExpression(op,strings,b) ;
|
||||
}
|
||||
case EXPR_PATH:
|
||||
{
|
||||
std::list<std::string> strings ;
|
||||
StringOperator op ;
|
||||
bool b ;
|
||||
|
||||
readStringExpr(e,n_ints,n_strings,strings,b,op) ;
|
||||
|
||||
return new ExtExpression(op,strings,b) ;
|
||||
}
|
||||
case EXPR_NAME:
|
||||
{
|
||||
std::list<std::string> strings ;
|
||||
StringOperator op ;
|
||||
bool b ;
|
||||
|
||||
readStringExpr(e,n_ints,n_strings,strings,b,op) ;
|
||||
|
||||
return new NameExpression(op,strings,b) ;
|
||||
}
|
||||
default:
|
||||
std::cerr << "No expression match the current value " << tok << std::endl ;
|
||||
return NULL ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -457,8 +457,11 @@ int ftServer::SearchKeywords(std::list<std::string> keywords, std::list<FileDeta
|
||||
return mFiStore->SearchKeywords(keywords, results,flags);
|
||||
}
|
||||
|
||||
int ftServer::SearchBoolExp(Expression * exp, std::list<FileDetail> &results)
|
||||
int ftServer::SearchBoolExp(Expression * exp, std::list<FileDetail> &results,uint32_t flags)
|
||||
{
|
||||
if(flags & DIR_FLAGS_LOCAL)
|
||||
return mFiMon->SearchBoolExp(exp,results,flags) ;
|
||||
else
|
||||
return mFiStore->searchBoolExp(exp, results);
|
||||
}
|
||||
|
||||
|
@ -158,7 +158,7 @@ virtual int RequestDirDetails(std::string uid, std::string path, DirDetails &det
|
||||
virtual int RequestDirDetails(void *ref, DirDetails &details, uint32_t flags);
|
||||
|
||||
virtual int SearchKeywords(std::list<std::string> keywords, std::list<FileDetail> &results,uint32_t flags);
|
||||
virtual int SearchBoolExp(Expression * exp, std::list<FileDetail> &results);
|
||||
virtual int SearchBoolExp(Expression * exp, std::list<FileDetail> &results,uint32_t flags);
|
||||
|
||||
/***
|
||||
* Utility Functions
|
||||
|
@ -37,28 +37,59 @@ Enumerations defining the Operators usable in the Boolean search expressions
|
||||
|
||||
enum LogicalOperator{
|
||||
AndOp=0, /* exp AND exp */
|
||||
OrOp, /* exp OR exp */
|
||||
XorOp /* exp XOR exp */
|
||||
OrOp=1, /* exp OR exp */
|
||||
XorOp=2 /* exp XOR exp */
|
||||
};
|
||||
|
||||
|
||||
/*Operators for String Queries*/
|
||||
enum StringOperator{
|
||||
ContainsAnyStrings = 0, /* e.g. name contains any of 'conference' 'meeting' 'presentation' */
|
||||
ContainsAllStrings, /* same as above except that it contains ALL of the strings */
|
||||
EqualsString /* exactly equal*/
|
||||
ContainsAllStrings = 1, /* same as above except that it contains ALL of the strings */
|
||||
EqualsString = 2 /* exactly equal*/
|
||||
};
|
||||
|
||||
/*Relational operators ( >, <, >=, <=, == and InRange )*/
|
||||
enum RelOperator{
|
||||
Equals = 0,
|
||||
GreaterEquals,
|
||||
Greater,
|
||||
SmallerEquals,
|
||||
Smaller,
|
||||
InRange /* lower limit <= value <= upper limit*/
|
||||
GreaterEquals = 1,
|
||||
Greater = 2,
|
||||
SmallerEquals = 3,
|
||||
Smaller = 4,
|
||||
InRange = 5 /* lower limit <= value <= upper limit*/
|
||||
};
|
||||
|
||||
/********************************************************************************************
|
||||
* Helper class for further serialisation
|
||||
********************************************************************************************/
|
||||
|
||||
class StringExpression ;
|
||||
class Expression ;
|
||||
|
||||
class LinearizedExpression
|
||||
{
|
||||
public:
|
||||
std::vector<uint8_t> _tokens ;
|
||||
std::vector<uint32_t> _ints ;
|
||||
std::vector<std::string> _strings ;
|
||||
|
||||
typedef enum { EXPR_DATE= 0,
|
||||
EXPR_POP = 1,
|
||||
EXPR_SIZE= 2,
|
||||
EXPR_HASH= 3,
|
||||
EXPR_NAME= 4,
|
||||
EXPR_PATH= 5,
|
||||
EXPR_EXT = 6,
|
||||
EXPR_COMP= 7 } token ;
|
||||
|
||||
static Expression *toExpr(const LinearizedExpression& e) ;
|
||||
|
||||
private:
|
||||
static Expression *toExpr(const LinearizedExpression& e,int&,int&,int&) ;
|
||||
static void readStringExpr(const LinearizedExpression& e,int& n_ints,int& n_strings,std::list<std::string>& strings,bool& b,StringOperator& op) ;
|
||||
};
|
||||
|
||||
|
||||
/******************************************************************************************
|
||||
Boolean Search Expression
|
||||
classes:
|
||||
@ -74,14 +105,18 @@ classes:
|
||||
|
||||
class FileEntry;
|
||||
|
||||
class Expression{
|
||||
class Expression
|
||||
{
|
||||
public:
|
||||
virtual bool eval (FileEntry *file) = 0;
|
||||
virtual ~Expression() {};
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const = 0 ;
|
||||
};
|
||||
|
||||
|
||||
class CompoundExpression : public Expression {
|
||||
class CompoundExpression : public Expression
|
||||
{
|
||||
public:
|
||||
CompoundExpression( enum LogicalOperator op, Expression * exp1, Expression *exp2)
|
||||
: Lexp(exp1), Rexp(exp2), Op(op){ }
|
||||
@ -105,6 +140,8 @@ public:
|
||||
delete Lexp;
|
||||
delete Rexp;
|
||||
}
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const ;
|
||||
private:
|
||||
Expression *Lexp;
|
||||
Expression *Rexp;
|
||||
@ -112,31 +149,37 @@ private:
|
||||
|
||||
};
|
||||
|
||||
class StringExpression: public Expression {
|
||||
class StringExpression: public Expression
|
||||
{
|
||||
public:
|
||||
StringExpression(enum StringOperator op, std::list<std::string> &t,
|
||||
bool ic): Op(op),terms(t), IgnoreCase(ic){}
|
||||
StringExpression(enum StringOperator op, std::list<std::string> &t, bool ic): Op(op),terms(t), IgnoreCase(ic){}
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const ;
|
||||
protected:
|
||||
bool evalStr(std::string &str);
|
||||
private:
|
||||
|
||||
enum StringOperator Op;
|
||||
std::list<std::string> terms;
|
||||
bool IgnoreCase;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class RelExpression: public Expression {
|
||||
class RelExpression: public Expression
|
||||
{
|
||||
public:
|
||||
RelExpression(enum RelOperator op, T lv, T hv):
|
||||
Op(op), LowerValue(lv), HigherValue(hv) {}
|
||||
RelExpression(enum RelOperator op, T lv, T hv): Op(op), LowerValue(lv), HigherValue(hv) {}
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const ;
|
||||
protected:
|
||||
bool evalRel(T val);
|
||||
private:
|
||||
|
||||
enum RelOperator Op;
|
||||
T LowerValue;
|
||||
T HigherValue;
|
||||
};
|
||||
|
||||
template<> void RelExpression<int>::linearize(LinearizedExpression& e) const ;
|
||||
|
||||
template <class T>
|
||||
bool RelExpression<T>::evalRel(T val) {
|
||||
switch (Op) {
|
||||
@ -181,11 +224,18 @@ Some implementations of StringExpressions.
|
||||
|
||||
******************************************************************************************/
|
||||
|
||||
class NameExpression: public StringExpression {
|
||||
class NameExpression: public StringExpression
|
||||
{
|
||||
public:
|
||||
NameExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
|
||||
StringExpression(op,t,ic) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_NAME) ;
|
||||
StringExpression::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class PathExpression: public StringExpression {
|
||||
@ -193,6 +243,12 @@ public:
|
||||
PathExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
|
||||
StringExpression(op,t,ic) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_PATH) ;
|
||||
StringExpression::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class ExtExpression: public StringExpression {
|
||||
@ -200,6 +256,12 @@ public:
|
||||
ExtExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
|
||||
StringExpression(op,t,ic) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_EXT) ;
|
||||
StringExpression::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class HashExpression: public StringExpression {
|
||||
@ -207,6 +269,12 @@ public:
|
||||
HashExpression(enum StringOperator op, std::list<std::string> &t):
|
||||
StringExpression(op,t, true) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_HASH) ;
|
||||
StringExpression::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
/******************************************************************************************
|
||||
@ -214,28 +282,50 @@ Some implementations of Relational Expressions.
|
||||
|
||||
******************************************************************************************/
|
||||
|
||||
class DateExpression: public RelExpression<int> {
|
||||
class DateExpression: public RelExpression<int>
|
||||
{
|
||||
public:
|
||||
DateExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
|
||||
DateExpression(enum RelOperator op, int lv, int hv):
|
||||
RelExpression<int>(op,lv,hv) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_DATE) ;
|
||||
RelExpression<int>::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class SizeExpression: public RelExpression<int> {
|
||||
class SizeExpression: public RelExpression<int>
|
||||
{
|
||||
public:
|
||||
SizeExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
|
||||
SizeExpression(enum RelOperator op, int lv, int hv):
|
||||
RelExpression<int>(op,lv,hv) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_SIZE) ;
|
||||
RelExpression<int>::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class PopExpression: public RelExpression<int> {
|
||||
class PopExpression: public RelExpression<int>
|
||||
{
|
||||
public:
|
||||
PopExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
|
||||
PopExpression(enum RelOperator op, int lv, int hv):
|
||||
RelExpression<int>(op,lv,hv) {}
|
||||
PopExpression(enum RelOperator op, int lv, int hv): RelExpression<int>(op,lv,hv) {}
|
||||
PopExpression(const LinearizedExpression& e) ;
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_POP) ;
|
||||
RelExpression<int>::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* RS_EXPRESSIONS_H */
|
||||
|
||||
|
@ -150,7 +150,7 @@ virtual int RequestDirDetails(std::string uid, std::string path, DirDetails &det
|
||||
virtual int RequestDirDetails(void *ref, DirDetails &details, uint32_t flags) = 0;
|
||||
|
||||
virtual int SearchKeywords(std::list<std::string> keywords, std::list<FileDetail> &results,uint32_t flags) = 0;
|
||||
virtual int SearchBoolExp(Expression * exp, std::list<FileDetail> &results) = 0;
|
||||
virtual int SearchBoolExp(Expression * exp, std::list<FileDetail> &results,uint32_t flags) = 0;
|
||||
|
||||
/***
|
||||
* Utility Functions.
|
||||
|
@ -44,6 +44,7 @@ const uint32_t RS_POPUP_MSG = 0x0001;
|
||||
const uint32_t RS_POPUP_CHAT = 0x0002;
|
||||
const uint32_t RS_POPUP_CALL = 0x0004;
|
||||
const uint32_t RS_POPUP_CONNECT = 0x0008;
|
||||
const uint32_t RS_SYSTRAY_GROUP_MSG = 0x0010;
|
||||
|
||||
/* CHAT flags are here - so they are in the same place as
|
||||
* other Notify flags... not used by libretroshare though
|
||||
|
@ -31,6 +31,8 @@
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
class LinearizedExpression ;
|
||||
|
||||
class RsTurtle;
|
||||
extern RsTurtle *rsTurtle ;
|
||||
|
||||
@ -67,6 +69,7 @@ class RsTurtle
|
||||
// as they come back.
|
||||
//
|
||||
virtual TurtleRequestId turtleSearch(const std::string& match_string) = 0 ;
|
||||
virtual TurtleRequestId turtleSearch(const LinearizedExpression& expr) = 0 ;
|
||||
|
||||
// Initiates tunnel handling for the given file hash. tunnels. Launches
|
||||
// an exception if an error occurs during the initialization process. The
|
||||
|
@ -32,6 +32,40 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/* UInt8 get/set */
|
||||
|
||||
bool getRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t *out)
|
||||
{
|
||||
/* first check there is space */
|
||||
if (size < *offset + 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
void *buf = (void *) &(((uint8_t *) data)[*offset]);
|
||||
|
||||
/* extract the data */
|
||||
memcpy(out, buf, sizeof(uint8_t));
|
||||
(*offset) += 1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool setRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t in)
|
||||
{
|
||||
/* first check there is space */
|
||||
if (size < *offset + 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void *buf = (void *) &(((uint8_t *) data)[*offset]);
|
||||
|
||||
/* pack it in */
|
||||
memcpy(buf, &in, sizeof(uint8_t));
|
||||
|
||||
(*offset) += 1;
|
||||
return true;
|
||||
}
|
||||
/* UInt16 get/set */
|
||||
|
||||
bool getRawUInt16(void *data, uint32_t size, uint32_t *offset, uint16_t *out)
|
||||
|
@ -48,6 +48,9 @@
|
||||
******************************************************************/
|
||||
|
||||
|
||||
bool getRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t *out);
|
||||
bool setRawUInt8(void *data, uint32_t size, uint32_t *offset, uint8_t in);
|
||||
|
||||
bool getRawUInt16(void *data, uint32_t size, uint32_t *offset, uint16_t *out);
|
||||
bool setRawUInt16(void *data, uint32_t size, uint32_t *offset, uint16_t in);
|
||||
|
||||
|
@ -526,7 +526,8 @@ int p3turtle::handleIncoming()
|
||||
|
||||
switch(item->PacketSubType())
|
||||
{
|
||||
case RS_TURTLE_SUBTYPE_SEARCH_REQUEST: handleSearchRequest(dynamic_cast<RsTurtleSearchRequestItem *>(item)) ;
|
||||
case RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST:
|
||||
case RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST: handleSearchRequest(dynamic_cast<RsTurtleSearchRequestItem *>(item)) ;
|
||||
break ;
|
||||
|
||||
case RS_TURTLE_SUBTYPE_SEARCH_RESULT : handleSearchResult(dynamic_cast<RsTurtleSearchResultItem *>(item)) ;
|
||||
@ -596,7 +597,8 @@ void p3turtle::handleSearchRequest(RsTurtleSearchRequestItem *item)
|
||||
if(_sharing_strategy != SHARE_FRIENDS_ONLY || item->depth < 2)
|
||||
{
|
||||
std::list<TurtleFileInfo> result ;
|
||||
performLocalSearch(item->match_string,result) ;
|
||||
|
||||
item->performLocalSearch(result) ;
|
||||
|
||||
RsTurtleSearchResultItem *res_item = NULL ;
|
||||
uint32_t item_size = 0 ;
|
||||
@ -658,7 +660,7 @@ void p3turtle::handleSearchRequest(RsTurtleSearchRequestItem *item)
|
||||
std::cerr << " Forwarding request to peer = " << *it << std::endl ;
|
||||
#endif
|
||||
// Copy current item and modify it.
|
||||
RsTurtleSearchRequestItem *fwd_item = new RsTurtleSearchRequestItem(*item) ;
|
||||
RsTurtleSearchRequestItem *fwd_item = item->clone() ;
|
||||
|
||||
++(fwd_item->depth) ; // increase search depth
|
||||
fwd_item->PeerId(*it) ;
|
||||
@ -767,9 +769,8 @@ void p3turtle::handleRecvFileRequest(RsTurtleFileRequestItem *item)
|
||||
}
|
||||
else // No, it's a request we should forward down the pipe.
|
||||
{
|
||||
RsTurtleFileRequestItem *res_item = new RsTurtleFileRequestItem ;
|
||||
RsTurtleFileRequestItem *res_item = new RsTurtleFileRequestItem(*item) ;
|
||||
|
||||
*res_item = *item ;
|
||||
res_item->PeerId(tunnel.local_dst) ;
|
||||
|
||||
sendItem(res_item) ;
|
||||
@ -812,6 +813,8 @@ void p3turtle::handleRecvFileData(RsTurtleFileDataItem *item)
|
||||
|
||||
// Only file data transfer updates tunnels time_stamp field, to avoid maintaining tunnel that are incomplete.
|
||||
tunnel.time_stamp = time(NULL) ;
|
||||
// also update the hash time stamp to show that it's actually being downloaded.
|
||||
_incoming_file_hashes[tunnel.hash].time_stamp = time(NULL) ;
|
||||
|
||||
// Let's figure out whether this reuqest is for us or not.
|
||||
|
||||
@ -845,9 +848,7 @@ void p3turtle::handleRecvFileData(RsTurtleFileDataItem *item)
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << " Forwarding data chunk to peer " << tunnel.local_src << std::endl ;
|
||||
#endif
|
||||
RsTurtleFileDataItem *res_item = new RsTurtleFileDataItem ;
|
||||
|
||||
*res_item = *item ;
|
||||
RsTurtleFileDataItem *res_item = new RsTurtleFileDataItem(*item) ;
|
||||
|
||||
res_item->chunk_data = malloc(res_item->chunk_size) ;
|
||||
|
||||
@ -1298,14 +1299,14 @@ void p3turtle::handleTunnelResult(RsTurtleTunnelOkItem *item)
|
||||
// ------------------------------ IO with libretroshare ----------------------------//
|
||||
// -----------------------------------------------------------------------------------//
|
||||
//
|
||||
void p3turtle::performLocalSearch(const std::string& s,std::list<TurtleFileInfo>& result)
|
||||
void RsTurtleStringSearchRequestItem::performLocalSearch(std::list<TurtleFileInfo>& result) const
|
||||
{
|
||||
/* call to core */
|
||||
std::list<FileDetail> initialResults;
|
||||
std::list<std::string> words ;
|
||||
|
||||
// to do: split search string into words.
|
||||
words.push_back(s) ;
|
||||
words.push_back(match_string) ;
|
||||
|
||||
// now, search!
|
||||
rsFiles->SearchKeywords(words, initialResults,DIR_FLAGS_LOCAL | DIR_FLAGS_NETWORK_WIDE);
|
||||
@ -1322,6 +1323,30 @@ void p3turtle::performLocalSearch(const std::string& s,std::list<TurtleFileInfo>
|
||||
result.push_back(i) ;
|
||||
}
|
||||
}
|
||||
void RsTurtleRegExpSearchRequestItem::performLocalSearch(std::list<TurtleFileInfo>& result) const
|
||||
{
|
||||
/* call to core */
|
||||
std::list<FileDetail> initialResults;
|
||||
|
||||
// to do: split search string into words.
|
||||
Expression *exp = LinearizedExpression::toExpr(expr) ;
|
||||
|
||||
// now, search!
|
||||
rsFiles->SearchBoolExp(exp,initialResults,DIR_FLAGS_LOCAL | DIR_FLAGS_NETWORK_WIDE);
|
||||
|
||||
result.clear() ;
|
||||
|
||||
for(std::list<FileDetail>::const_iterator it(initialResults.begin());it!=initialResults.end();++it)
|
||||
{
|
||||
TurtleFileInfo i ;
|
||||
i.hash = it->hash ;
|
||||
i.size = it->size ;
|
||||
i.name = it->name ;
|
||||
|
||||
result.push_back(i) ;
|
||||
}
|
||||
delete exp ;
|
||||
}
|
||||
|
||||
TurtleRequestId p3turtle::turtleSearch(const std::string& string_to_match)
|
||||
{
|
||||
@ -1331,7 +1356,7 @@ TurtleRequestId p3turtle::turtleSearch(const std::string& string_to_match)
|
||||
|
||||
// Form a request packet that simulates a request from us.
|
||||
//
|
||||
RsTurtleSearchRequestItem *item = new RsTurtleSearchRequestItem ;
|
||||
RsTurtleStringSearchRequestItem *item = new RsTurtleStringSearchRequestItem ;
|
||||
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << "performing search. OwnId = " << mConnMgr->getOwnId() << std::endl ;
|
||||
@ -1359,6 +1384,42 @@ TurtleRequestId p3turtle::turtleSearch(const std::string& string_to_match)
|
||||
|
||||
return id ;
|
||||
}
|
||||
TurtleRequestId p3turtle::turtleSearch(const LinearizedExpression& expr)
|
||||
{
|
||||
// generate a new search id.
|
||||
|
||||
TurtleRequestId id = generateRandomRequestId() ;
|
||||
|
||||
// Form a request packet that simulates a request from us.
|
||||
//
|
||||
RsTurtleRegExpSearchRequestItem *item = new RsTurtleRegExpSearchRequestItem ;
|
||||
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << "performing search. OwnId = " << mConnMgr->getOwnId() << std::endl ;
|
||||
#endif
|
||||
while(mConnMgr->getOwnId() == "")
|
||||
{
|
||||
std::cerr << "... waitting for connect manager to form own id." << std::endl ;
|
||||
#ifdef WIN32
|
||||
Sleep(1000) ;
|
||||
#else
|
||||
sleep(1) ;
|
||||
#endif
|
||||
}
|
||||
|
||||
item->PeerId(mConnMgr->getOwnId()) ;
|
||||
item->expr = expr ;
|
||||
item->request_id = id ;
|
||||
item->depth = 0 ;
|
||||
|
||||
// send it
|
||||
|
||||
handleSearchRequest(item) ;
|
||||
|
||||
delete item ;
|
||||
|
||||
return id ;
|
||||
}
|
||||
|
||||
void p3turtle::monitorFileTunnels(const std::string& name,const std::string& file_hash,uint64_t size)
|
||||
{
|
||||
@ -1395,7 +1456,7 @@ void p3turtle::returnSearchResult(RsTurtleSearchResultItem *item)
|
||||
// just cout for now, but it should be notified to the gui
|
||||
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << " Returning result for search request " << item->request_id << " upwards." << std::endl ;
|
||||
std::cerr << " Returning result for search request " << (void*)item->request_id << " upwards." << std::endl ;
|
||||
#endif
|
||||
|
||||
rsicontrol->getNotify().notifyTurtleSearchResult(item->request_id,item->result) ;
|
||||
@ -1459,7 +1520,7 @@ void p3turtle::getInfo( std::vector<std::vector<std::string> >& hashes_info,
|
||||
tunnel.push_back(rsPeers->getPeerName(it->second.local_src)) ;
|
||||
tunnel.push_back(rsPeers->getPeerName(it->second.local_dst)) ;
|
||||
tunnel.push_back(it->second.hash) ;
|
||||
tunnel.push_back(printNumber(now-it->second.time_stamp)) ;
|
||||
tunnel.push_back(printNumber(now-it->second.time_stamp) + " secs ago") ;
|
||||
}
|
||||
|
||||
search_reqs_info.clear();
|
||||
@ -1471,7 +1532,7 @@ void p3turtle::getInfo( std::vector<std::vector<std::string> >& hashes_info,
|
||||
|
||||
search_req.push_back(printNumber(it->first,true)) ;
|
||||
search_req.push_back(rsPeers->getPeerName(it->second.origin)) ;
|
||||
search_req.push_back(printNumber(now - it->second.time_stamp)) ;
|
||||
search_req.push_back(printNumber(now - it->second.time_stamp) + " secs ago") ;
|
||||
}
|
||||
|
||||
tunnel_reqs_info.clear();
|
||||
@ -1483,7 +1544,7 @@ void p3turtle::getInfo( std::vector<std::vector<std::string> >& hashes_info,
|
||||
|
||||
tunnel_req.push_back(printNumber(it->first,true)) ;
|
||||
tunnel_req.push_back(rsPeers->getPeerName(it->second.origin)) ;
|
||||
tunnel_req.push_back(printNumber(now - it->second.time_stamp)) ;
|
||||
tunnel_req.push_back(printNumber(now - it->second.time_stamp) + " secs ago") ;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -207,6 +207,7 @@ class p3turtle: public p3Service, public pqiMonitor, public RsTurtle, public ftS
|
||||
// as they come back.
|
||||
//
|
||||
virtual TurtleSearchRequestId turtleSearch(const std::string& string_to_match) ;
|
||||
virtual TurtleSearchRequestId turtleSearch(const LinearizedExpression& expr) ;
|
||||
|
||||
// Initiates tunnel handling for the given file hash. tunnels. Launches
|
||||
// an exception if an error occurs during the initialization process. The
|
||||
|
@ -13,14 +13,35 @@
|
||||
//
|
||||
// ---------------------------------- Packet sizes -----------------------------------//
|
||||
//
|
||||
uint32_t RsTurtleSearchRequestItem::serial_size()
|
||||
|
||||
uint32_t RsTurtleStringSearchRequestItem::serial_size()
|
||||
{
|
||||
uint32_t s = 0 ;
|
||||
|
||||
s += 8 ; // header
|
||||
s += GetTlvStringSize(match_string) ;
|
||||
s += 4 ; // request_id
|
||||
s += 2 ; // depth
|
||||
s += GetTlvStringSize(match_string) ; // match_string
|
||||
|
||||
return s ;
|
||||
}
|
||||
uint32_t RsTurtleRegExpSearchRequestItem::serial_size()
|
||||
{
|
||||
uint32_t s = 0 ;
|
||||
|
||||
s += 8 ; // header
|
||||
s += 4 ; // request_id
|
||||
s += 2 ; // depth
|
||||
|
||||
s += 4 ; // number of strings
|
||||
|
||||
for(uint i=0;i<expr._strings.size();++i)
|
||||
s += GetTlvStringSize(expr._strings[i]) ;
|
||||
|
||||
s += 4 ; // number of ints
|
||||
s += 4 * expr._ints.size() ;
|
||||
s += 4 ; // number of tokens
|
||||
s += expr._tokens.size() ; // uint8_t has size 1
|
||||
|
||||
return s ;
|
||||
}
|
||||
@ -118,7 +139,8 @@ RsItem *RsTurtleSerialiser::deserialise(void *data, uint32_t *size)
|
||||
#endif
|
||||
switch(getRsItemSubType(rstype))
|
||||
{
|
||||
case RS_TURTLE_SUBTYPE_SEARCH_REQUEST: return new RsTurtleSearchRequestItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST : return new RsTurtleStringSearchRequestItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST : return new RsTurtleRegExpSearchRequestItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_SEARCH_RESULT : return new RsTurtleSearchResultItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_OPEN_TUNNEL : return new RsTurtleOpenTunnelItem(data,*size) ;
|
||||
case RS_TURTLE_SUBTYPE_TUNNEL_OK : return new RsTurtleTunnelOkItem(data,*size) ;
|
||||
@ -140,7 +162,7 @@ RsItem *RsTurtleSerialiser::deserialise(void *data, uint32_t *size)
|
||||
|
||||
}
|
||||
|
||||
bool RsTurtleSearchRequestItem::serialize(void *data,uint32_t& pktsize)
|
||||
bool RsTurtleStringSearchRequestItem::serialize(void *data,uint32_t& pktsize)
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
uint32_t offset = 0;
|
||||
@ -174,11 +196,61 @@ bool RsTurtleSearchRequestItem::serialize(void *data,uint32_t& pktsize)
|
||||
return ok;
|
||||
}
|
||||
|
||||
RsTurtleSearchRequestItem::RsTurtleSearchRequestItem(void *data,uint32_t pktsize)
|
||||
: RsTurtleItem(RS_TURTLE_SUBTYPE_SEARCH_REQUEST)
|
||||
bool RsTurtleRegExpSearchRequestItem::serialize(void *data,uint32_t& pktsize)
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
uint32_t offset = 0;
|
||||
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << "RsTurtleSerialiser::serialising RegExp search packet (size=" << tlvsize << ")" << std::endl;
|
||||
#endif
|
||||
if (pktsize < tlvsize)
|
||||
return false; /* not enough space */
|
||||
|
||||
pktsize = tlvsize;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
ok &= setRsItemHeader(data,tlvsize,PacketId(), tlvsize);
|
||||
|
||||
/* skip the header */
|
||||
offset += 8;
|
||||
|
||||
/* add mandatory parts first */
|
||||
|
||||
ok &= setRawUInt32(data, tlvsize, &offset, request_id);
|
||||
ok &= setRawUInt16(data, tlvsize, &offset, depth);
|
||||
|
||||
// now serialize the regexp
|
||||
ok &= setRawUInt32(data,tlvsize,&offset,expr._tokens.size()) ;
|
||||
|
||||
for(uint i=0;i<expr._tokens.size();++i) ok &= setRawUInt8(data,tlvsize,&offset,expr._tokens[i]) ;
|
||||
|
||||
ok &= setRawUInt32(data,tlvsize,&offset,expr._ints.size()) ;
|
||||
|
||||
for(uint i=0;i<expr._ints.size();++i) ok &= setRawUInt32(data,tlvsize,&offset,expr._ints[i]) ;
|
||||
|
||||
ok &= setRawUInt32(data,tlvsize,&offset,expr._strings.size()) ;
|
||||
|
||||
for(uint i=0;i<expr._strings.size();++i) ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VALUE, expr._strings[i]);
|
||||
|
||||
|
||||
if (offset != tlvsize)
|
||||
{
|
||||
ok = false;
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << "RsTurtleSerialiser::serialiseTransfer() Size Error! (offset=" << offset << ", tlvsize=" << tlvsize << ")" << std::endl;
|
||||
#endif
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
RsTurtleStringSearchRequestItem::RsTurtleStringSearchRequestItem(void *data,uint32_t pktsize)
|
||||
: RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST)
|
||||
{
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << " type = search request" << std::endl ;
|
||||
std::cerr << " deserializibg packet. type = search request (string)" << std::endl ;
|
||||
#endif
|
||||
uint32_t offset = 8; // skip the header
|
||||
uint32_t rssize = getRsItemSize(data);
|
||||
@ -197,6 +269,46 @@ RsTurtleSearchRequestItem::RsTurtleSearchRequestItem(void *data,uint32_t pktsize
|
||||
#endif
|
||||
}
|
||||
|
||||
RsTurtleRegExpSearchRequestItem::RsTurtleRegExpSearchRequestItem(void *data,uint32_t pktsize)
|
||||
: RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST)
|
||||
{
|
||||
#ifdef P3TURTLE_DEBUG
|
||||
std::cerr << " deserializibg packet. type = search request (regexp)" << std::endl ;
|
||||
#endif
|
||||
uint32_t offset = 8; // skip the header
|
||||
uint32_t rssize = getRsItemSize(data);
|
||||
bool ok = true ;
|
||||
|
||||
ok &= getRawUInt32(data, pktsize, &offset, &request_id);
|
||||
ok &= getRawUInt16(data, pktsize, &offset, &depth);
|
||||
|
||||
// now serialize the regexp
|
||||
uint32_t n =0 ;
|
||||
ok &= getRawUInt32(data,pktsize,&offset,&n) ;
|
||||
|
||||
expr._tokens.resize(n) ;
|
||||
|
||||
for(uint i=0;i<n;++i) ok &= getRawUInt8(data,pktsize,&offset,&expr._tokens[i]) ;
|
||||
|
||||
ok &= getRawUInt32(data,pktsize,&offset,&n) ;
|
||||
expr._ints.resize(n) ;
|
||||
|
||||
for(uint i=0;i<n;++i) ok &= getRawUInt32(data,pktsize,&offset,&expr._ints[i]) ;
|
||||
|
||||
ok &= getRawUInt32(data,pktsize,&offset,&n) ;
|
||||
expr._strings.resize(n) ;
|
||||
|
||||
for(uint i=0;i<n;++i) ok &= GetTlvString(data, pktsize, &offset, TLV_TYPE_STR_VALUE, expr._strings[i]);
|
||||
|
||||
#ifdef WINDOWS_SYS // No Exceptions in Windows compile. (drbobs).
|
||||
#else
|
||||
if (offset != rssize)
|
||||
throw std::runtime_error("Size error while deserializing.") ;
|
||||
if (!ok)
|
||||
throw std::runtime_error("Unknown error while deserializing.") ;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool RsTurtleSearchResultItem::serialize(void *data,uint32_t& pktsize)
|
||||
{
|
||||
uint32_t tlvsize = serial_size();
|
||||
@ -544,9 +656,9 @@ bool RsTurtleFileDataItem::serialize(void *data,uint32_t& pktsize)
|
||||
// ------------------------------------- IO --------------------------------------- //
|
||||
// -----------------------------------------------------------------------------------//
|
||||
//
|
||||
std::ostream& RsTurtleSearchRequestItem::print(std::ostream& o, uint16_t)
|
||||
std::ostream& RsTurtleStringSearchRequestItem::print(std::ostream& o, uint16_t)
|
||||
{
|
||||
o << "Search request:" << std::endl ;
|
||||
o << "Search request (string):" << std::endl ;
|
||||
o << " direct origin: \"" << PeerId() << "\"" << std::endl ;
|
||||
o << " match string: \"" << match_string << "\"" << std::endl ;
|
||||
o << " Req. Id: " << (void *)request_id << std::endl ;
|
||||
@ -554,6 +666,19 @@ std::ostream& RsTurtleSearchRequestItem::print(std::ostream& o, uint16_t)
|
||||
|
||||
return o ;
|
||||
}
|
||||
std::ostream& RsTurtleRegExpSearchRequestItem::print(std::ostream& o, uint16_t)
|
||||
{
|
||||
o << "Search request (regexp):" << std::endl ;
|
||||
o << " direct origin: \"" << PeerId() << "\"" << std::endl ;
|
||||
o << " Req. Id: " << (void *)request_id << std::endl ;
|
||||
o << " Depth : " << depth << std::endl ;
|
||||
o << " RegExp: " << std::endl ;
|
||||
o << " Toks: " ; for(uint i=0;i<expr._tokens.size();++i) std::cout << (int)expr._tokens[i] << " " ; std::cout << std::endl ;
|
||||
o << " Ints: " ; for(uint i=0;i<expr._ints.size();++i) std::cout << (int)expr._ints[i] << " " ; std::cout << std::endl ;
|
||||
o << " Strs: " ; for(uint i=0;i<expr._strings.size();++i) std::cout << expr._strings[i] << " " ; std::cout << std::endl ;
|
||||
|
||||
return o ;
|
||||
}
|
||||
|
||||
std::ostream& RsTurtleSearchResultItem::print(std::ostream& o, uint16_t)
|
||||
{
|
||||
|
@ -4,10 +4,11 @@
|
||||
#include "serialiser/rstlvbase.h"
|
||||
#include "serialiser/rsbaseserial.h"
|
||||
#include "rsiface/rsturtle.h"
|
||||
#include "rsiface/rsexpr.h"
|
||||
#include "serialiser/rsserviceids.h"
|
||||
#include "turtle/turtletypes.h"
|
||||
|
||||
const uint8_t RS_TURTLE_SUBTYPE_SEARCH_REQUEST = 0x01 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST = 0x01 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_SEARCH_RESULT = 0x02 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_OPEN_TUNNEL = 0x03 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_TUNNEL_OK = 0x04 ;
|
||||
@ -15,6 +16,11 @@ const uint8_t RS_TURTLE_SUBTYPE_CLOSE_TUNNEL = 0x05 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_TUNNEL_CLOSED = 0x06 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_REQUEST = 0x07 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_FILE_DATA = 0x08 ;
|
||||
const uint8_t RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST = 0x09 ;
|
||||
|
||||
/***********************************************************************************/
|
||||
/* Basic Turtle Item Class */
|
||||
/***********************************************************************************/
|
||||
|
||||
class RsTurtleItem: public RsItem
|
||||
{
|
||||
@ -27,6 +33,10 @@ class RsTurtleItem: public RsItem
|
||||
virtual void clear() {}
|
||||
};
|
||||
|
||||
/***********************************************************************************/
|
||||
/* Turtle Search Item classes */
|
||||
/***********************************************************************************/
|
||||
|
||||
class RsTurtleSearchResultItem: public RsTurtleItem
|
||||
{
|
||||
public:
|
||||
@ -51,20 +61,53 @@ class RsTurtleSearchResultItem: public RsTurtleItem
|
||||
class RsTurtleSearchRequestItem: public RsTurtleItem
|
||||
{
|
||||
public:
|
||||
RsTurtleSearchRequestItem() : RsTurtleItem(RS_TURTLE_SUBTYPE_SEARCH_REQUEST) {}
|
||||
RsTurtleSearchRequestItem(void *data,uint32_t size) ; // deserialization
|
||||
RsTurtleSearchRequestItem(uint32_t subtype) : RsTurtleItem(subtype) {}
|
||||
|
||||
virtual RsTurtleSearchRequestItem *clone() const = 0 ; // used for cloning in routing methods
|
||||
virtual void performLocalSearch(std::list<TurtleFileInfo>&) const = 0 ; // abstracts the search method
|
||||
|
||||
std::string match_string ; // string to match
|
||||
uint32_t request_id ; // randomly generated request id.
|
||||
uint16_t depth ; // Used for limiting search depth.
|
||||
};
|
||||
|
||||
class RsTurtleStringSearchRequestItem: public RsTurtleSearchRequestItem
|
||||
{
|
||||
public:
|
||||
RsTurtleStringSearchRequestItem() : RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_STRING_SEARCH_REQUEST) {}
|
||||
RsTurtleStringSearchRequestItem(void *data,uint32_t size) ;
|
||||
|
||||
std::string match_string ; // string to match
|
||||
|
||||
virtual RsTurtleSearchRequestItem *clone() const { return new RsTurtleStringSearchRequestItem(*this) ; }
|
||||
virtual void performLocalSearch(std::list<TurtleFileInfo>&) const ;
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) ;
|
||||
virtual uint32_t serial_size() ;
|
||||
};
|
||||
|
||||
class RsTurtleRegExpSearchRequestItem: public RsTurtleSearchRequestItem
|
||||
{
|
||||
public:
|
||||
RsTurtleRegExpSearchRequestItem() : RsTurtleSearchRequestItem(RS_TURTLE_SUBTYPE_REGEXP_SEARCH_REQUEST) {}
|
||||
RsTurtleRegExpSearchRequestItem(void *data,uint32_t size) ;
|
||||
|
||||
LinearizedExpression expr ; // Reg Exp in linearised mode
|
||||
|
||||
virtual RsTurtleSearchRequestItem *clone() const { return new RsTurtleRegExpSearchRequestItem(*this) ; }
|
||||
virtual void performLocalSearch(std::list<TurtleFileInfo>&) const ;
|
||||
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) ;
|
||||
protected:
|
||||
virtual bool serialize(void *data,uint32_t& size) ;
|
||||
virtual uint32_t serial_size() ;
|
||||
};
|
||||
|
||||
/***********************************************************************************/
|
||||
/* Turtle Tunnel Item classes */
|
||||
/***********************************************************************************/
|
||||
|
||||
class RsTurtleOpenTunnelItem: public RsTurtleItem
|
||||
{
|
||||
public:
|
||||
@ -99,6 +142,7 @@ class RsTurtleTunnelOkItem: public RsTurtleItem
|
||||
virtual uint32_t serial_size() ;
|
||||
};
|
||||
|
||||
#ifdef A_VIRER
|
||||
class RsTurtleCloseTunnelItem: public RsTurtleItem
|
||||
{
|
||||
public:
|
||||
@ -126,6 +170,11 @@ class RsTurtleTunnelClosedItem: public RsTurtleItem
|
||||
virtual bool serialize(void *data,uint32_t& size) ;
|
||||
virtual uint32_t serial_size() ;
|
||||
};
|
||||
#endif
|
||||
|
||||
/***********************************************************************************/
|
||||
/* Turtle File Transfer item classes */
|
||||
/***********************************************************************************/
|
||||
|
||||
class RsTurtleFileRequestItem: public RsTurtleItem
|
||||
{
|
||||
@ -161,8 +210,10 @@ class RsTurtleFileDataItem: public RsTurtleItem
|
||||
virtual uint32_t serial_size() ;
|
||||
};
|
||||
|
||||
// Class responsible for serializing/deserializing all turtle items.
|
||||
//
|
||||
/***********************************************************************************/
|
||||
/* Turtle Serialiser class */
|
||||
/***********************************************************************************/
|
||||
|
||||
class RsTurtleSerialiser: public RsSerialType
|
||||
{
|
||||
public:
|
||||
|
@ -412,11 +412,23 @@ void SearchDialog::advancedSearch(Expression* expression)
|
||||
|
||||
/* call to core */
|
||||
std::list<FileDetail> results;
|
||||
rsFiles -> SearchBoolExp(expression, results);
|
||||
qulonglong searchId = rand() ; // for now, because we need to call the turtle search to get a proper id.
|
||||
|
||||
// send a turtle search request
|
||||
LinearizedExpression e ;
|
||||
expression->linearize(e) ;
|
||||
|
||||
TurtleRequestId req_id = rsTurtle->turtleSearch(e) ;
|
||||
|
||||
rsFiles -> SearchBoolExp(expression, results, DIR_FLAGS_REMOTE | DIR_FLAGS_NETWORK_WIDE | DIR_FLAGS_BROWSABLE);
|
||||
|
||||
/* abstraction to allow reusee of tree rendering code */
|
||||
resultsToTree((advSearchDialog->getSearchAsString()).toStdString(),searchId, results);
|
||||
resultsToTree((advSearchDialog->getSearchAsString()).toStdString(),req_id, results);
|
||||
|
||||
// debug stuff
|
||||
Expression *expression2 = LinearizedExpression::toExpr(e) ;
|
||||
results.clear() ;
|
||||
rsFiles -> SearchBoolExp(expression2, results, DIR_FLAGS_REMOTE | DIR_FLAGS_NETWORK_WIDE | DIR_FLAGS_BROWSABLE);
|
||||
resultsToTree((advSearchDialog->getSearchAsString()).toStdString(),req_id+1, results);
|
||||
}
|
||||
|
||||
|
||||
|
@ -37,28 +37,59 @@ Enumerations defining the Operators usable in the Boolean search expressions
|
||||
|
||||
enum LogicalOperator{
|
||||
AndOp=0, /* exp AND exp */
|
||||
OrOp, /* exp OR exp */
|
||||
XorOp /* exp XOR exp */
|
||||
OrOp=1, /* exp OR exp */
|
||||
XorOp=2 /* exp XOR exp */
|
||||
};
|
||||
|
||||
|
||||
/*Operators for String Queries*/
|
||||
enum StringOperator{
|
||||
ContainsAnyStrings = 0, /* e.g. name contains any of 'conference' 'meeting' 'presentation' */
|
||||
ContainsAllStrings, /* same as above except that it contains ALL of the strings */
|
||||
EqualsString /* exactly equal*/
|
||||
ContainsAllStrings = 1, /* same as above except that it contains ALL of the strings */
|
||||
EqualsString = 2 /* exactly equal*/
|
||||
};
|
||||
|
||||
/*Relational operators ( >, <, >=, <=, == and InRange )*/
|
||||
enum RelOperator{
|
||||
Equals = 0,
|
||||
GreaterEquals,
|
||||
Greater,
|
||||
SmallerEquals,
|
||||
Smaller,
|
||||
InRange /* lower limit <= value <= upper limit*/
|
||||
GreaterEquals = 1,
|
||||
Greater = 2,
|
||||
SmallerEquals = 3,
|
||||
Smaller = 4,
|
||||
InRange = 5 /* lower limit <= value <= upper limit*/
|
||||
};
|
||||
|
||||
/********************************************************************************************
|
||||
* Helper class for further serialisation
|
||||
********************************************************************************************/
|
||||
|
||||
class StringExpression ;
|
||||
class Expression ;
|
||||
|
||||
class LinearizedExpression
|
||||
{
|
||||
public:
|
||||
std::vector<uint8_t> _tokens ;
|
||||
std::vector<uint32_t> _ints ;
|
||||
std::vector<std::string> _strings ;
|
||||
|
||||
typedef enum { EXPR_DATE= 0,
|
||||
EXPR_POP = 1,
|
||||
EXPR_SIZE= 2,
|
||||
EXPR_HASH= 3,
|
||||
EXPR_NAME= 4,
|
||||
EXPR_PATH= 5,
|
||||
EXPR_EXT = 6,
|
||||
EXPR_COMP= 7 } token ;
|
||||
|
||||
static Expression *toExpr(const LinearizedExpression& e) ;
|
||||
|
||||
private:
|
||||
static Expression *toExpr(const LinearizedExpression& e,int&,int&,int&) ;
|
||||
static void readStringExpr(const LinearizedExpression& e,int& n_ints,int& n_strings,std::list<std::string>& strings,bool& b,StringOperator& op) ;
|
||||
};
|
||||
|
||||
|
||||
/******************************************************************************************
|
||||
Boolean Search Expression
|
||||
classes:
|
||||
@ -74,14 +105,18 @@ classes:
|
||||
|
||||
class FileEntry;
|
||||
|
||||
class Expression{
|
||||
class Expression
|
||||
{
|
||||
public:
|
||||
virtual bool eval (FileEntry *file) = 0;
|
||||
virtual ~Expression() {};
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const = 0 ;
|
||||
};
|
||||
|
||||
|
||||
class CompoundExpression : public Expression {
|
||||
class CompoundExpression : public Expression
|
||||
{
|
||||
public:
|
||||
CompoundExpression( enum LogicalOperator op, Expression * exp1, Expression *exp2)
|
||||
: Lexp(exp1), Rexp(exp2), Op(op){ }
|
||||
@ -105,6 +140,8 @@ public:
|
||||
delete Lexp;
|
||||
delete Rexp;
|
||||
}
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const ;
|
||||
private:
|
||||
Expression *Lexp;
|
||||
Expression *Rexp;
|
||||
@ -112,31 +149,37 @@ private:
|
||||
|
||||
};
|
||||
|
||||
class StringExpression: public Expression {
|
||||
class StringExpression: public Expression
|
||||
{
|
||||
public:
|
||||
StringExpression(enum StringOperator op, std::list<std::string> &t,
|
||||
bool ic): Op(op),terms(t), IgnoreCase(ic){}
|
||||
StringExpression(enum StringOperator op, std::list<std::string> &t, bool ic): Op(op),terms(t), IgnoreCase(ic){}
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const ;
|
||||
protected:
|
||||
bool evalStr(std::string &str);
|
||||
private:
|
||||
|
||||
enum StringOperator Op;
|
||||
std::list<std::string> terms;
|
||||
bool IgnoreCase;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class RelExpression: public Expression {
|
||||
class RelExpression: public Expression
|
||||
{
|
||||
public:
|
||||
RelExpression(enum RelOperator op, T lv, T hv):
|
||||
Op(op), LowerValue(lv), HigherValue(hv) {}
|
||||
RelExpression(enum RelOperator op, T lv, T hv): Op(op), LowerValue(lv), HigherValue(hv) {}
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const ;
|
||||
protected:
|
||||
bool evalRel(T val);
|
||||
private:
|
||||
|
||||
enum RelOperator Op;
|
||||
T LowerValue;
|
||||
T HigherValue;
|
||||
};
|
||||
|
||||
template<> void RelExpression<int>::linearize(LinearizedExpression& e) const ;
|
||||
|
||||
template <class T>
|
||||
bool RelExpression<T>::evalRel(T val) {
|
||||
switch (Op) {
|
||||
@ -181,11 +224,18 @@ Some implementations of StringExpressions.
|
||||
|
||||
******************************************************************************************/
|
||||
|
||||
class NameExpression: public StringExpression {
|
||||
class NameExpression: public StringExpression
|
||||
{
|
||||
public:
|
||||
NameExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
|
||||
StringExpression(op,t,ic) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_NAME) ;
|
||||
StringExpression::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class PathExpression: public StringExpression {
|
||||
@ -193,6 +243,12 @@ public:
|
||||
PathExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
|
||||
StringExpression(op,t,ic) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_PATH) ;
|
||||
StringExpression::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class ExtExpression: public StringExpression {
|
||||
@ -200,6 +256,12 @@ public:
|
||||
ExtExpression(enum StringOperator op, std::list<std::string> &t, bool ic):
|
||||
StringExpression(op,t,ic) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_EXT) ;
|
||||
StringExpression::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class HashExpression: public StringExpression {
|
||||
@ -207,6 +269,12 @@ public:
|
||||
HashExpression(enum StringOperator op, std::list<std::string> &t):
|
||||
StringExpression(op,t, true) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_HASH) ;
|
||||
StringExpression::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
/******************************************************************************************
|
||||
@ -214,28 +282,50 @@ Some implementations of Relational Expressions.
|
||||
|
||||
******************************************************************************************/
|
||||
|
||||
class DateExpression: public RelExpression<int> {
|
||||
class DateExpression: public RelExpression<int>
|
||||
{
|
||||
public:
|
||||
DateExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
|
||||
DateExpression(enum RelOperator op, int lv, int hv):
|
||||
RelExpression<int>(op,lv,hv) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_DATE) ;
|
||||
RelExpression<int>::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class SizeExpression: public RelExpression<int> {
|
||||
class SizeExpression: public RelExpression<int>
|
||||
{
|
||||
public:
|
||||
SizeExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
|
||||
SizeExpression(enum RelOperator op, int lv, int hv):
|
||||
RelExpression<int>(op,lv,hv) {}
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_SIZE) ;
|
||||
RelExpression<int>::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
class PopExpression: public RelExpression<int> {
|
||||
class PopExpression: public RelExpression<int>
|
||||
{
|
||||
public:
|
||||
PopExpression(enum RelOperator op, int v): RelExpression<int>(op,v,v){}
|
||||
PopExpression(enum RelOperator op, int lv, int hv):
|
||||
RelExpression<int>(op,lv,hv) {}
|
||||
PopExpression(enum RelOperator op, int lv, int hv): RelExpression<int>(op,lv,hv) {}
|
||||
PopExpression(const LinearizedExpression& e) ;
|
||||
bool eval(FileEntry *file);
|
||||
|
||||
virtual void linearize(LinearizedExpression& e) const
|
||||
{
|
||||
e._tokens.push_back(LinearizedExpression::EXPR_POP) ;
|
||||
RelExpression<int>::linearize(e) ;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* RS_EXPRESSIONS_H */
|
||||
|
||||
|
@ -150,7 +150,7 @@ virtual int RequestDirDetails(std::string uid, std::string path, DirDetails &det
|
||||
virtual int RequestDirDetails(void *ref, DirDetails &details, uint32_t flags) = 0;
|
||||
|
||||
virtual int SearchKeywords(std::list<std::string> keywords, std::list<FileDetail> &results,uint32_t flags) = 0;
|
||||
virtual int SearchBoolExp(Expression * exp, std::list<FileDetail> &results) = 0;
|
||||
virtual int SearchBoolExp(Expression * exp, std::list<FileDetail> &results,uint32_t flags) = 0;
|
||||
|
||||
/***
|
||||
* Utility Functions.
|
||||
|
@ -31,6 +31,8 @@
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
class LinearizedExpression ;
|
||||
|
||||
class RsTurtle;
|
||||
extern RsTurtle *rsTurtle ;
|
||||
|
||||
@ -67,6 +69,7 @@ class RsTurtle
|
||||
// as they come back.
|
||||
//
|
||||
virtual TurtleRequestId turtleSearch(const std::string& match_string) = 0 ;
|
||||
virtual TurtleRequestId turtleSearch(const LinearizedExpression& expr) = 0 ;
|
||||
|
||||
// Initiates tunnel handling for the given file hash. tunnels. Launches
|
||||
// an exception if an error occurs during the initialization process. The
|
||||
|
Loading…
Reference in New Issue
Block a user