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