#include #include "decoder.h" #include "ff_register.h" #include "filelib.h" #include "verbose.h" #include "viterbi.h" #include #include #include "nn.hpp" using namespace std; struct TheObserver : public DecoderObserver { string translation; virtual void NotifyTranslationForest(const SentenceMetadata& smeta, Hypergraph* hg) { translation.clear(); vector trans; ViterbiESentence(*hg, &trans); translation = TD::GetString(trans); } }; int send(nn::socket& sock, const string trans) { cout << "sending translation '" << trans << "'" << endl << endl; sock.send(trans.c_str(), trans.size()+1, 0); } bool recv(nn::socket& sock, string& source) { char *buf = NULL; size_t sz = sock.recv(&buf, NN_MSG, 0); if (buf) { string s(buf, buf+sz); source = s; nn::freemsg(buf); return true; } return false; } void loop(Decoder& decoder, nn::socket& sock) { int to = 100; sock.setsockopt(NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof (to)); TheObserver o; while(true) { string source; bool r = recv(sock, source); if (r) { cout << "received source '" << source << "'" << endl; decoder.Decode(source, &o); send(sock, o.translation); } else { // Oh no! } } } int main(int argc, char** argv) { register_feature_functions(); ReadFile f(argv[1]); Decoder decoder(f.stream()); SetSilent(true); nn::socket sock(AF_SP, NN_PAIR); //string url = "ipc:///tmp/network_decoder.ipc"; string url = "tcp://127.0.0.1:60666"; sock.bind(url.c_str()); loop(decoder, sock); return 0; }