Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

QueryResponseUnpacker.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2004 University of Massachusetts.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010 */
00011 
00012 
00013 //
00014 // QueryResponseUnpacker
00015 //
00016 // 23 March 2004 -- tds
00017 //
00018 
00019 #ifndef INDRI_QUERYRESPONSEUNPACKER_HPP
00020 #define INDRI_QUERYRESPONSEUNPACKER_HPP
00021 
00022 #include "indri/NetworkMessageStream.hpp"
00023 #include "indri/InferenceNetwork.hpp"
00024 #include "Exception.hpp"
00025 
00026 class QueryResponseUnpacker : public MessageStreamHandler {
00027 private:
00028   NetworkMessageStream* _stream;
00029   InferenceNetwork::MAllResults _results;
00030   std::string _exception;
00031   bool _done;
00032 
00033 public:
00034   QueryResponseUnpacker( NetworkMessageStream* stream ) :
00035     _stream(stream),
00036     _done(false)
00037   {
00038   }
00039 
00040   InferenceNetwork::MAllResults& getResults() {
00041     while( !_done && _stream->alive() && !_exception.length() )
00042       _stream->read(*this);
00043 
00044     if( _exception.length() )
00045       LEMUR_THROW( LEMUR_RUNTIME_ERROR, _exception );
00046     
00047     return _results;
00048   }
00049 
00050   void reply( XMLNode* node ) {
00051     assert( false && "Query responses are binary only for now" );
00052   }
00053 
00054   void reply( const std::string& name, const void* buffer, unsigned int length ) {
00055     std::string nodeName;
00056     std::string listName;
00057     
00058     nodeName = name.substr( 0, name.find(':') );
00059     listName = name.substr( name.find(':')+1 );
00060     
00061     ScoredExtentResult aligned;
00062     int count = length / sizeof(ScoredExtentResult);
00063     std::vector<ScoredExtentResult>& resultVector = _results[nodeName][listName];
00064 
00065     for( int i=0; i<count; i++ ) {
00066       // copy for alignment
00067       memcpy( &aligned, (const char*)buffer + i*sizeof(ScoredExtentResult), sizeof(ScoredExtentResult) );
00068 
00069       aligned.begin = ntohl(aligned.begin);
00070       aligned.end = ntohl(aligned.end);
00071       aligned.document = ntohl(aligned.document);
00072       aligned.score = lemur_compat::ntohd(aligned.score);
00073 
00074       resultVector.push_back(aligned);
00075     }
00076   }
00077 
00078   void replyDone() {
00079     _done = true;
00080   }
00081 
00082   void request( XMLNode* node ) {
00083     assert( false && "No requests expected from the query server" );
00084   }
00085 
00086   void error( const std::string& err ) {
00087     _exception = err;
00088   }
00089 
00090 };
00091 
00092 #endif // INDRI_QUERYRESPONSEUNPACKER_HPP
00093 
00094 

Generated on Wed Nov 3 12:59:02 2004 for Lemur Toolkit by doxygen1.2.18