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 // NetworkMessageStream 00015 // 00016 // 23 March 2004 -- tds 00017 // 00018 00019 #ifndef INDRI_NETWORKMESSAGESTREAM_HPP 00020 #define INDRI_NETWORKMESSAGESTREAM_HPP 00021 00022 #include "indri/NetworkStream.hpp" 00023 #include "indri/Buffer.hpp" 00024 #include "indri/Mutex.hpp" 00025 #include "indri/XMLNode.hpp" 00026 namespace indri 00027 { 00029 namespace net 00030 { 00031 00032 class MessageStreamHandler { 00033 public: 00034 virtual void request( indri::xml::XMLNode* node ) = 0; 00035 virtual void reply( indri::xml::XMLNode* node ) = 0; 00036 virtual void reply( const std::string& name, const void* buffer, unsigned int length ) = 0; 00037 virtual void replyDone() = 0; 00038 virtual void error( const std::string& e ) = 0; 00039 }; 00040 00041 class XMLReplyReceiver : public MessageStreamHandler { 00042 private: 00043 bool _done; 00044 std::string _exception; 00045 indri::xml::XMLNode* _reply; 00046 00047 public: 00048 XMLReplyReceiver() : _done(false), _reply(0) {} 00049 ~XMLReplyReceiver(); 00050 00051 void request( indri::xml::XMLNode* node ) { 00052 _exception = "XMLReplyReceiver: doesn't accept requests"; 00053 _done = true; 00054 } 00055 00056 void reply( indri::xml::XMLNode* reply ) { 00057 _reply = reply; 00058 } 00059 00060 void reply( const std::string& name, const void* buffer, unsigned int length ) { 00061 _exception = "XMLReplyReceiver: should only get xml replies"; 00062 _done = true; 00063 } 00064 00065 void replyDone() { 00066 _done = true; 00067 } 00068 00069 void error( const std::string& e ) { 00070 _exception = e; 00071 _done = true; 00072 } 00073 00074 indri::xml::XMLNode* getReply() { 00075 return _reply; 00076 } 00077 00078 bool done() { 00079 return _done; 00080 } 00081 00082 void wait( class NetworkMessageStream* stream ); 00083 }; 00084 00085 class NetworkMessageStream { 00086 private: 00087 indri::thread::Mutex _lock; 00088 indri::utility::Buffer _buffer; 00089 NetworkStream* _stream; 00090 int _readPosition; 00091 int _writePosition; 00092 00093 int _findEOL(); 00094 void _cleanBuffer(); 00095 int _bufferLength(); 00096 00097 public: 00098 NetworkMessageStream( NetworkStream* stream ); 00099 void read( MessageStreamHandler& handler ); 00100 void request( indri::xml::XMLNode* messageNode ); 00101 void reply( indri::xml::XMLNode* replyNode ); 00102 void reply( const std::string& name, const void* buffer, unsigned int size ); 00103 void replyDone(); 00104 void error( const std::string& errorMessage ); 00105 bool alive(); 00106 indri::thread::Lockable& mutex(); 00107 }; 00108 } 00109 } 00110 00111 #endif // INDRI_NETWORKMESSAGESTREAM_HPP