Cambridge SMT System
main-run.hifst-client.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
15 #ifndef MAIN_RUN_HIFST_CLIENT_HPP
16 #define MAIN_RUN_HIFST_CLIENT_HPP
17 
25 namespace ucam {
26 namespace hifst {
27 
28 using boost::asio::ip::tcp;
29 const int max_length = 1024;
30 typedef boost::shared_ptr<tcp::socket> socket_ptr;
31 
36 template <class Data = HifstClientTaskData>
38  private:
41 
45  std::string textoutput_;
46 
48  const ucam::util::RegistryPO& rg_;
49 
51  const std::string host_;
53  const std::string port_;
54 
55  public:
61  fastforwardread_ ( new iszfstream ( rg.get<std::string>
62  ( HifstConstants::kSourceLoad ) ) ),
63  textoutput_ ( rg.get<std::string> ( HifstConstants::kTargetStore ) ),
64  host_ ( rg.get<std::string> ( HifstConstants::kHifstHost ) ),
65  port_ ( rg.get<std::string> ( HifstConstants::kHifstPort ) ),
66  rg_ ( rg ) {
67  };
68 
70  inline bool operator() () {
71  Data d;
72  bool finished = false;
73  oszfstream *fileoutput = NULL;
74  if ( textoutput_ != "" ) {
75  fileoutput = new oszfstream ( textoutput_ );
76  }
78  !ir->done ();
79  ir->next () ) {
80  d.sidx = ir->get ();
81  boost::scoped_ptr<std::string> aux ( new std::string ( "" ) );
82  d.translation = aux.get();
83  finished = fastforwardread_ ( d.sidx ,
84  &d.sentence ); //Move to whichever next sentence and read
85  boost::algorithm::trim (d.sentence);
86  if (finished && d.sentence == "" ) break;
87  FORCELINFO ( "Translating sentence " << d.sidx << ":" << d.sentence );
89  run ( d );
90  FORCELINFO ( *d.translation );
91  if ( fileoutput != NULL )
92  *fileoutput << *d.translation << endl;
93  if ( finished ) break;
94  }
95  delete fileoutput;
96  return false;
97  }
98 
99  private:
100 
106  bool run ( Data& d ) {
107  try {
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 );
115  {
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 ) );
122  }
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,
129  reply_length ) );
130  LINFO ("Reply Length=" << reply_length2);
131  reply[reply_length2] = 0;
132  *d.translation = reply;
133  delete reply;
134  s.close();
135  } catch ( std::exception& e ) {
136  std::cerr << "Exception: " << e.what() << "\n";
137  }
138  };
139 
140  DISALLOW_COPY_AND_ASSIGN ( SingleThreadedHifstClientTask );
141 
142 };
143 
144 }
145 } // end namespaces
146 
147 #endif
Wrapper stream class that writes to pipes, text files or gzipped files.
Definition: szfstream.hpp:200
Convenience class that reads "quickly" until a queried line.
Definition: szfstream.hpp:381
#define LINFO(msg)
const std::string kHifstHost
Full single-threaded Translation system.
const std::string kTargetStore
#define FORCELINFO(msg)
boost::scoped_ptr< NumberRangeInterface< unsigned > > IntRangePtr
Definition: range.hpp:214
#define LDEBUG(msg)
const std::string kHifstPort
boost::shared_ptr< tcp::socket > socket_ptr
#define IntRangeFactory
Definition: range.hpp:213
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.
Definition: szfstream.hpp:34
Definition: bleu.hpp:14