Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

TextSearchConnector.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Trec_TextSearchConnector_h
00002 #define Impala_Core_Trec_TextSearchConnector_h
00003 
00004 #include <iostream>
00005 #include <queue>
00006 
00007 // these two need to be moved somewhere:
00008 #include "Application/videolympics/VideolympicsClient.h"
00009 //#ifndef PRACTICALSOCKETS_LOADED
00010 //#include "Application/videolympics/Link/PracticalSocket.h"
00011 //#include "Application/videolympics/Link/PracticalSocket.cpp"
00012 //#define PRACTICALSOCKETS_LOADED
00013 
00014 #include "Util/StringParser.h"
00015 #include "Basis/CmdOptions.h"
00016 #include "Core/VideoSet/Segmentation.h"
00017 #include "Core/Trec/ShotResult.h"
00018 
00019 namespace Impala {
00020 namespace Core {
00021 namespace Trec {
00022 
00023 class TextSearchConnector
00024 {
00025 public:
00026     TextSearchConnector(VideoSet::Segmentation *seg)
00027     {
00028         CmdOptions& options = CmdOptions::GetInstance();
00029         mTextServer = options.GetString("textServer");
00030         mTextPort = options.GetInt("textPort");
00031         mConnected = false;
00032         mHasResults = false;
00033         mSegmentation = seg;
00034     }
00035 
00036     void Connect()
00037     {
00038         if (mConnected)
00039             return;
00040         ILOG_DEBUG("establishing connection... ");
00041         try {
00042             mSocket = new TCPSocket(mTextServer, mTextPort);
00043         } catch (SocketException e) {
00044             ILOG_DEBUG("Could not connect to server.");
00045             mConnected = false;
00046             return;
00047         }
00048         mConnected = true;
00049         ILOG_DEBUG("connection established.");
00050     }
00051 
00052     void Query(std::string q)
00053     {
00054         ILOG_DEBUG("Query: " << q);
00055         mHasResults = false;
00056         if (!mConnected)
00057             Connect();
00058 
00059         if (!mConnected)
00060             return;
00061 
00062         q = q + '\n';
00063 
00064         ILOG_DEBUG("Sending query...");
00065         try {
00066             mSocket->send(q.c_str(), strlen(q.c_str()));
00067         } catch (SocketException e) {
00068             ILOG_ERROR("Server connection failed during SEND.");
00069             mConnected = false;
00070             return;
00071         }
00072         ILOG_DEBUG("query sent.");
00073 
00074         int rem = 0;
00075         char buffer[20000];
00076         for (int i=0; i < 20000; i++) buffer[i]=0;
00077         char *pos = buffer;
00078 
00079         std::vector<std::string> results;
00080         try {
00081             while (true) {
00082                 int beetje = mSocket->recv(pos, 20000 - rem);
00083                 rem += beetje;
00084                 pos = &buffer[rem];
00085                 if (rem >= 20000)
00086                 {
00087                     ILOG_ERROR("Out of buffer memory while retrieving results.");
00088                     return;
00089                 }
00090                 results = tokenize(std::string(buffer));
00091                 // temp bugfix: in Windows the tokenization adds more than just
00092                 // 'DONE'. Since DONE is the only statement ever sent from the
00093                 // text server starting with a D we can also just look for that
00094                 // - Ork
00095                 if (results.size() > 0 && results[results.size()-1][0] == 'D')
00096                 {
00097                     break;
00098                 } 
00099                 if (results.size() > 0 && results[results.size()-1] == "DONE")
00100                 {
00101                     break;
00102                 }
00103             }
00104         } catch (SocketException e) {
00105             ILOG_ERROR("Server connection failed during RECV.");
00106             mConnected = false;
00107             return;
00108         }
00109         results = tokenize(std::string(buffer));
00110         if (results.size() == 1 && results[0] == "DONE")
00111         {
00112             ILOG_INFO("No text results found.");
00113             mHasResults = false;
00114             return;
00115         }
00116 
00117         int found = 0, i=0;
00118         int number = results.size() / 6;
00119         ILOG_DEBUG("Number of results: " << number);
00120 
00121         mResults.clear();
00122         int rank = 1;
00123         while (found < number) {
00124             if (results[i] == "DONE")
00125                 break;
00126             Core::Trec::ShotResult r;
00127             r.shotid = mSegmentation->GetShotId(results[i+2]);
00128             ILOG_DEBUG("Found text result " << rank << " : " << results[i+2] << " score " << results[i+4]);
00129             r.rank = rank++;
00130             r.score = atof(results[i+4]);
00131             mResults.push_back(r);
00132             i+=6;
00133             found++;
00134         }
00135         mHasResults = true;
00136         return;
00137     }
00138 
00139     bool
00140     HasResults()
00141     {
00142         return mHasResults;
00143     }
00144 
00145     std::list<Core::Trec::ShotResult> GetResults()
00146     {
00147         return mResults;
00148     }
00149 
00150 private:
00151 
00152     std::vector<std::string> tokenize(const std::string& input, const std::string& delims = " \n")
00153     {
00154         std::string::size_type lPos = input.find_first_not_of(delims, 0);
00155         std::string::size_type pos = input.find_first_of(delims, lPos);
00156 
00157         std::vector<std::string> tokens;
00158 
00159         while (std::string::npos != pos || std::string::npos != lPos)
00160         {
00161             tokens.push_back(input.substr(lPos, pos-lPos));
00162             lPos = input.find_first_not_of(delims, pos);
00163             pos = input.find_first_of(delims, lPos);
00164         }
00165 
00166         return tokens;
00167     }
00168 
00169     std::string mTextServer;
00170     int         mTextPort;
00171 
00172     TCPSocket   *mSocket;
00173 
00174     bool        mConnected;
00175     bool        mHasResults;
00176 
00177     VideoSet::Segmentation *mSegmentation;
00178 
00179     std::list<Core::Trec::ShotResult> mResults;
00180 
00181     ILOG_VAR_DEC;
00182 };
00183 
00184 ILOG_VAR_INIT(TextSearchConnector, Core.Trec);
00185 
00186 
00187 }
00188 }
00189 }
00190 
00191 #endif

Generated on Fri Mar 19 09:31:26 2010 for ImpalaSrc by  doxygen 1.5.1