My Project
SerialNeutronExchanger.hh
00001 /*
00002  * $Id:$
00003  */
00004 
00005 #if !defined(SERIALNEUTRONEXCHANGER)
00006 #define SERIALNEUTRONEXCHANGER
00007 
00008 
00009 #include <iostream>
00010 #include <string>
00011 #include <vector>
00012 
00013 // boost asio
00014 #include <boost/asio.hpp>
00015 
00016 // boost serialization
00017 #include <boost/serialization/serialization.hpp>
00018 #include <boost/serialization/vector.hpp>
00019 //  boost archive
00020 #include <boost/archive/binary_iarchive.hpp>
00021 #include <boost/archive/binary_oarchive.hpp>
00022 
00023 #include "Header.hh"
00024 #include "ElementContainer.hh"
00025 #include "ElementContainerArray.hh"
00026 #include "ElementContainerMatrix.hh"
00027 #include "UInt4Container.hh"
00028 #include "UInt4ContainerArray.hh"
00029 #include "UInt4ContainerMatrix.hh"
00030 
00035 class ASIO_local {
00036 private:
00037   boost::asio::ip::tcp::iostream  *h;
00039 
00040 public:
00041   /*
00042    * @param host hostname string to send(transmit)
00043    * @param port port number as a string of the remote host to send(transmit)
00044    */
00045   ASIO_local(const std::string &host, const std::string &port) {
00046         h  = new boost::asio::ip::tcp::iostream (host, port);
00047   }
00048   ~ASIO_local() {
00049         delete h;
00050   }
00051 
00056   bool stat(void) const { return *h; };
00057 
00058   friend class SerialNeutronExchanger;
00059   friend class SerialPythonServerClient;
00060 };
00061 
00062 
00067 class ASIO_remote {
00068 private:
00069   boost::asio::io_service io;
00070   boost::asio::ip::tcp::acceptor *acc;
00071 
00072   boost::asio::ip::tcp::iostream  *h;
00074 
00075 public:
00076   /*
00077    * @param port port number to listen to
00078    */
00079   ASIO_remote(const UInt4 port) {
00080         h  = new boost::asio::ip::tcp::iostream;
00081 
00082         acc = new boost::asio::ip::tcp::acceptor
00083           (io, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
00084 
00085         acc->accept( *h->rdbuf() );
00086 
00087   }
00088   ~ASIO_remote() {
00089         delete h;
00090         delete acc;
00091   }
00092   std::string from_host(void) const { /*
00093         boost::asio::ip::tcp::endpoint T  = h->rdbuf()->remote_endpoint();
00094         boost::asio::ip::address A = T.address();
00095         return A.to_string(); */
00096         return h->rdbuf()->remote_endpoint().address().to_string();
00097   }
00098 
00099   friend class SerialNeutronExchanger;
00100 };
00101 
00107 class SerialNeutronExchanger {
00108 private:
00109   boost::asio::ip::tcp::iostream  *h;
00111 
00112 public:
00113   SerialNeutronExchanger(ASIO_local &Al) {
00114         h = Al.h;
00115   }
00116   SerialNeutronExchanger(ASIO_remote &Ar) {
00117         h = Ar.h;
00118   }
00119   /*
00120    * @param *_h pointer to the boost::asio::ip::tcp::iostream in ASIO_local or ASIO_remote
00121    */
00122   SerialNeutronExchanger(boost::asio::ip::tcp::iostream  *_h) : h(_h) {
00123   }
00124 
00125 
00126 
00133   template <typename T>
00134   void Transmit(const T &val) {
00135         boost::archive::binary_oarchive *oa = new boost::archive::binary_oarchive (*h);
00136         *oa << val;
00137         delete oa;
00138   }
00139 
00146   template <typename T>
00147   void Receive(T &val) {
00148         try {
00149           boost::archive::binary_iarchive *ia = new boost::archive::binary_iarchive (*h);
00150           *ia >> val;
00151           delete ia;
00152         }
00153         catch (boost::archive::archive_exception &N) {
00154           std::cerr << "SerialNeutronExchanger CAUGHT Exception!: Receive Failed" << std::endl;
00155           T nulval;
00156           val = nulval;
00157         }
00158   }
00159 
00160 
00161   UInt4 ReceiveUInt4(void) {
00162         UInt4 data;
00163         Receive(data);
00164         return data;
00165   }
00166   Int4  ReceiveInt4(void) {
00167         Int4 data;
00168         Receive(data);
00169         return data;
00170   }
00171   std::string  ReceiveString(void) {
00172         std::string data;
00173         Receive(data);
00174         return data;
00175   }
00176   Double ReceiveDouble(void) {
00177         Double data;
00178         Receive(data);
00179         return data;
00180   }
00181 
00182 };
00183 
00184 
00185 #endif /* SERIALNEUTRONEXCHANGER */
 All Classes Functions Variables Friends