15 #ifndef MAIN_RUN_HIFST_CLIENT_HPP 16 #define MAIN_RUN_HIFST_CLIENT_HPP 28 using boost::asio::ip::tcp;
30 typedef boost::shared_ptr<tcp::socket>
socket_ptr;
36 template <
class Data = HifstClientTaskData>
45 std::string textoutput_;
51 const std::string host_;
53 const std::string port_;
61 fastforwardread_ ( new iszfstream ( rg.get<std::string>
72 bool finished =
false;
73 oszfstream *fileoutput = NULL;
74 if ( textoutput_ !=
"" ) {
75 fileoutput =
new oszfstream ( textoutput_ );
81 boost::scoped_ptr<std::string> aux (
new std::string (
"" ) );
82 d.translation = aux.get();
83 finished = fastforwardread_ ( d.sidx ,
85 boost::algorithm::trim (d.sentence);
86 if (finished && d.sentence ==
"" )
break;
87 FORCELINFO (
"Translating sentence " << d.sidx <<
":" << d.sentence );
91 if ( fileoutput != NULL )
92 *fileoutput << *d.translation << endl;
93 if ( finished )
break;
106 bool run ( Data& d ) {
108 LINFO (
"Attempting connection to host=" << host_ <<
",port=" << port_ );
109 boost::asio::io_service io_service;
110 tcp::resolver resolver ( io_service );
111 tcp::resolver::query query ( tcp::v4(), host_.c_str(), port_.c_str() );
112 tcp::resolver::iterator iterator = resolver.resolve ( query );
113 tcp::socket s ( io_service );
114 boost::asio::connect ( s, iterator );
116 char request[max_length + 1];
117 strcpy ( request, (
char * ) d.sentence.c_str() );
118 std::size_t request_length = strlen ( request );
119 boost::asio::write ( s, boost::asio::buffer ( &request_length,
120 sizeof ( std::size_t ) ) );
121 boost::asio::write ( s, boost::asio::buffer ( request, request_length ) );
123 char *reply =
new char[max_length + 1];
124 std::size_t reply_length = 0;
125 std::size_t reply_length1 = boost::asio::read ( s,
126 boost::asio::buffer ( &reply_length,
sizeof ( std::size_t ) ) );
127 LDEBUG (
"Expected reply Length=" << reply_length);
128 std::size_t reply_length2 = boost::asio::read ( s, boost::asio::buffer ( reply,
130 LINFO (
"Reply Length=" << reply_length2);
131 reply[reply_length2] = 0;
132 *d.translation = reply;
135 }
catch ( std::exception& e ) {
136 std::cerr <<
"Exception: " << e.what() <<
"\n";
Wrapper stream class that writes to pipes, text files or gzipped files.
Convenience class that reads "quickly" until a queried line.
const std::string kHifstHost
Full single-threaded Translation system.
const std::string kTargetStore
boost::scoped_ptr< NumberRangeInterface< unsigned > > IntRangePtr
const std::string kHifstPort
boost::shared_ptr< tcp::socket > socket_ptr
SingleThreadedHifstClientTask(const ucam::util::RegistryPO &rg)
Constructor.
Templated (hybrid) Interface for Task classes.
const std::string kSourceLoad
bool operator()()
Sends request, retrieves server reply, prints translation to file.
Wrapper stream class that reads pipes, text files or gzipped files.