inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/wall/data_client
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_wall_data_client
00005 #define inlib_wall_data_client
00006 
00007 #include "../net/inet_socket"
00008 #include "protocol"
00009 #include "../smanip" //words
00010 
00011 namespace inlib {
00012 namespace wall {
00013 
00014 class data_client {
00015 public:
00016   data_client(std::ostream& a_out,
00017               bool a_verbose,
00018               bool a_warn) //warn if received an unknow prot.
00019   :m_out(a_out)
00020   ,m_socket(a_out,a_verbose)
00021   ,m_warn(a_warn)
00022   {}
00023   virtual ~data_client(){}
00024 protected:
00025   data_client(const data_client& a_from)
00026   :m_out(a_from.m_out)
00027   ,m_socket(a_from.m_out,false)
00028   ,m_warn(a_from.m_warn)
00029   {}
00030 private:
00031   data_client& operator=(const data_client&){return *this;}
00032 public:
00033   inlib::net::inet_socket& socket() {return m_socket;}
00034   std::ostream& out() {return m_out;}
00035   bool initialize(const std::string& a_server,unsigned int a_port) {
00036     if(!m_socket.connect(a_server,a_port,20,1)) return false;
00037     if(m_socket.verbose()) {
00038       m_out << "inlib::data_client::initialize :"
00039             << " connected."
00040             << std::endl;
00041     }
00042     return true;
00043   }
00044 
00045   bool steer() {
00046     while(true){
00047       std::string prot;
00048       if(!m_socket.fetch_string(prot)) {
00049         m_out << "inlib::data_client::steer :"
00050               << " fetch failed"
00051               << std::endl;       
00052         m_socket.disconnect();
00053         return false;
00054       }
00055   
00056       if(m_socket.verbose()) {
00057         m_out << "inlib::data_client::steer :"
00058               << " received prot " << inlib::sout(prot)
00059               << std::endl;       
00060       }
00061   
00062       bool handled;
00063       if(!dispatch(prot,handled)) {
00064         m_out << "inlib::data_client::steer :"
00065               << " distpatch failed"
00066               << std::endl;       
00067         m_socket.disconnect();
00068         return false;
00069       }
00070   
00071     }
00072   
00073     m_socket.disconnect();
00074   
00075     return true;
00076   }
00077 
00078   bool poll() {
00079     while(true){
00080       bool have_input;
00081       if(!m_socket.is_there_input(have_input)) {
00082         m_out << "inlib::data_client::poll :"
00083               << " is_there_input failed"
00084               << std::endl;       
00085         m_socket.disconnect();
00086         return false;
00087       }
00088       if(!have_input) return true;
00089 
00090       std::string prot;
00091       if(!m_socket.fetch_string(prot)) {
00092         m_out << "inlib::data_client::poll :"
00093               << " fetch failed"
00094               << std::endl;       
00095         m_socket.disconnect();
00096         return false;
00097       }
00098   
00099       if(m_socket.verbose()) {
00100         m_out << "inlib::data_client::poll :"
00101               << " received prot " << prot
00102               << std::endl;       
00103       }  
00104 
00105       bool handled;
00106       if(!dispatch(prot,handled)) {
00107         m_out << "inlib::data_client::poll :"
00108               << " distpatch failed"
00109               << std::endl;       
00110         m_socket.disconnect();
00111         return false;
00112       }
00113       //could have received a inlib::wall::protocol::disconnect()
00114       if(!m_socket.is_connected()) return true;
00115   
00116     }  
00117   }
00118 
00119 /*
00120   bool send_doc(const std::string& a_file,
00121                        const std::string& a_options = "") {
00122     if(!m_socket.send_string(inlib::wall::protocol::doc())) {
00123       m_out << "inlib::data_client::send_doc :"
00124             << " send_string() failed."
00125             << std::endl;
00126       return false;
00127     }
00128 
00129     if(!m_socket.send_string(a_options)) {
00130       m_out << "inlib::data_client::send_doc :"
00131             << " send_string() failed."
00132             << std::endl;
00133       return false;
00134     }
00135 
00136     if(!m_socket.send_file(a_file)) {
00137       m_out << "inlib::data_client::send_doc :"
00138             << " send_file() failed."
00139             << std::endl;
00140       return false;
00141     }
00142 
00143     return true;
00144   }
00145 */
00146 
00147   bool send_buffer_doc(inlib::uint64 a_length,
00148                               const char* a_buffer,
00149                               const std::string& a_options = "") {
00150     if(!m_socket.send_string(inlib::wall::protocol::doc())) {
00151       m_out << "inlib::data_client::send_doc :"
00152             << " send_string() failed."
00153             << std::endl;
00154       return false;
00155     }
00156 
00157     if(!m_socket.send_string(a_options)) {
00158       m_out << "inlib::data_client::send_doc :"
00159             << " send_string() failed."
00160             << std::endl;
00161       return false;
00162     }
00163 
00164     if(!m_socket.send<inlib::uint64>(a_length)) {
00165       m_out << "inlib::data_client::send_doc :"
00166             << " send lengtj failed."
00167             << std::endl;
00168       return false;
00169     }
00170     if(!m_socket.send_buffer(a_buffer,a_length)) {
00171       m_out << "inlib::data_client::send_doc :"
00172             << " send_buffer() failed."
00173             << std::endl;
00174       return false;
00175     }
00176 
00177     return true;
00178   }
00179 
00180 protected:
00181   virtual bool dispatch(const std::string& a_prot,bool& a_handled) {
00182     if(a_prot==inlib::wall::protocol::disconnect()) {
00183       m_socket.disconnect();
00184       a_handled = true;
00185 
00186     } else if(a_prot==inlib::wall::protocol::start()) {
00187   
00188       a_handled = true;
00189 
00190     } else {
00191       if(m_warn) {
00192         m_out << "inlib::data_client::dispatch :"
00193               << " unknown prot " << inlib::sout(a_prot) << "."
00194               << std::endl;
00195       }
00196       a_handled = false;
00197     }
00198 
00199     return true;
00200   }
00201 protected:
00202   std::ostream& m_out;
00203   inlib::net::inet_socket m_socket;
00204 private:
00205   bool m_warn;
00206 };
00207 
00208 }}
00209 
00210 #endif
00211 
00212 
00213 /* example to override :
00214   class data_client : public inlib::wall::data_client {
00215   public:
00216     data_client(std::ostream& a_out,bool a_verbose = false)
00217     : inlib::wall::data_client(a_out,a_verbose){}
00218     virtual ~data_client(){}
00219   private:
00220     data_client(const data_client& a_from)
00221     : inlib::wall::data_client(a_from){}
00222     data_client& operator=(const data_client&){return *this;}
00223   protected:
00224     virtual bool dispatch(const std::string& a_prot,bool& a_handled) {
00225       if(!inlib::wall::data_client::dispatch(a_prot,a_handled)) return false;
00226       if(a_handled) return true;
00227   
00228       //if(a_prot==inlib_net_protocol_cbk) {
00229       //} else {
00230         m_out << "exlib::geant4::session::data_client::dispatch :"
00231               << " unknown prot " << inlib::sout(a_prot) << "."
00232               << std::endl;
00233         a_handled = false;
00234       //}
00235       return true;
00236     }
00237   };
00238 */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines