From 9aa075c79122709d8d5b8adf8c91ebd348eac658 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Wed, 25 Mar 2015 15:23:41 +0100 Subject: updated network_decoder --- .gitignore | 2 +- decoder/Makefile.am | 6 +++--- decoder/feed.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++++ decoder/feed.rb | 21 ++++++++++++++++++ decoder/local.cc | 51 -------------------------------------------- decoder/network_decoder.cc | 12 +++++++---- training/dtrain/feed.rb | 12 ++++++----- 7 files changed, 93 insertions(+), 64 deletions(-) create mode 100644 decoder/feed.cc create mode 100755 decoder/feed.rb delete mode 100644 decoder/local.cc diff --git a/.gitignore b/.gitignore index dde591c7..ba07047a 100644 --- a/.gitignore +++ b/.gitignore @@ -53,7 +53,7 @@ decoder/t2s_test decoder/trule_test decoder/weights_test decoder/network_decoder -decoder/local +decoder/feed depcomp dist dpmert/Makefile diff --git a/decoder/Makefile.am b/decoder/Makefile.am index 563ca0b9..dcc518bc 100644 --- a/decoder/Makefile.am +++ b/decoder/Makefile.am @@ -1,4 +1,4 @@ -bin_PROGRAMS = cdec minimal_decoder network_decoder local +bin_PROGRAMS = cdec minimal_decoder network_decoder feed noinst_PROGRAMS = \ trule_test \ @@ -29,8 +29,8 @@ minimal_decoder_LDADD = libcdec.a ../utils/libutils.a network_decoder_SOURCES = network_decoder.cc nn.hpp network_decoder_LDADD = libcdec.a ../klm/search/libksearch.a ../mteval/libmteval.a ../utils/libutils.a ../klm/lm/libklm.a ../klm/util/libklm_util.a ../klm/util/double-conversion/libklm_util_double.a /usr/lib64/libnanomsg.so -local_SOURCES = local.cc nn.hpp -local_LDADD = /usr/lib64/libnanomsg.so +feed_SOURCES = feed.cc nn.hpp +feed_LDADD = /usr/lib64/libnanomsg.so AM_CPPFLAGS = -DTEST_DATA=\"$(top_srcdir)/decoder/test_data\" -DBOOST_TEST_DYN_LINK -W -Wno-sign-compare -I$(top_srcdir) -I$(top_srcdir)/mteval -I$(top_srcdir)/utils -I$(top_srcdir)/klm diff --git a/decoder/feed.cc b/decoder/feed.cc new file mode 100644 index 00000000..477d6599 --- /dev/null +++ b/decoder/feed.cc @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +#include +#include +#include "nn.hpp" + +using namespace std; + +void +recv(nn::socket& sock) +{ + char *buf = NULL; + size_t sz = sock.recv(&buf, NN_MSG, 0); + if (buf) { + string translation(buf, buf+sz); + cout << "received translation '" << translation << "'" << endl; + } +} + +void +send(nn::socket& sock, const string& msg) +{ + cout << "sending source '" << msg << "'" << endl; + sock.send(msg.c_str(), msg.size()+1, 0); +} + +void +loop(nn::socket& sock) +{ + int to = 100; + sock.setsockopt(NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof(to)); + for (string line; getline(cin, line);) { + send(sock, line); + sleep(1); + recv(sock); + } +} + +int main(int argc, char const* argv[]) +{ + nn::socket sock(AF_SP, NN_PAIR); + //string url = "ipc:///tmp/network_decoder.ipc"; + string url = "tcp://127.0.0.1:60666"; + sock.connect(url.c_str()); + loop(sock); + + return 0; +} + diff --git a/decoder/feed.rb b/decoder/feed.rb new file mode 100755 index 00000000..c835536c --- /dev/null +++ b/decoder/feed.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby + +require 'nanomsg' + +port = 60666 +sock = NanoMsg::PairSocket.new +addr = "tcp://127.0.0.1:#{port}" +#addr = "ipc:///tmp/network_decoder.ipc" +sock.connect addr + +while true + line = STDIN.gets + if !line + sock.send 'shutdown' + break + end + sock.send line.strip + sleep 1 + puts "got translation: #{sock.recv}\n\n" +end + diff --git a/decoder/local.cc b/decoder/local.cc deleted file mode 100644 index b03fd17e..00000000 --- a/decoder/local.cc +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include "nn.hpp" - -using namespace std; - -void -recv(nn::socket& sock) -{ - char *buf = NULL; - sock.recv(&buf, NN_MSG, 0); - if (buf) { - string translation(buf); - cout << "received translation '" << translation << "'" << endl; - } -} - -void -send(nn::socket& sock, const string& msg) -{ - cout << "sending source '" << msg << "'" << endl; - sock.send(msg.c_str(), msg.size()+1, 0); -} - -void -loop(nn::socket& sock) -{ - int to = 100; - sock.setsockopt(NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof(to)); - while(1) { - send(sock, "das ist ein test ."); - sleep(1); - recv(sock); - } -} - -int main(int argc, char const* argv[]) -{ - nn::socket sock(AF_SP, NN_PAIR); - string url = "ipc:///tmp/network_decoder.ipc"; - sock.connect(url.c_str()); - loop(sock); - - return 0; -} - diff --git a/decoder/network_decoder.cc b/decoder/network_decoder.cc index aaa1842d..ebbb91b5 100644 --- a/decoder/network_decoder.cc +++ b/decoder/network_decoder.cc @@ -28,7 +28,7 @@ struct TheObserver : public DecoderObserver int send(nn::socket& sock, const string trans) { - cout << "sending translation '" << trans << "'" << endl; + cout << "sending translation '" << trans << "'" << endl << endl; sock.send(trans.c_str(), trans.size()+1, 0); } @@ -36,10 +36,11 @@ bool recv(nn::socket& sock, string& source) { char *buf = NULL; - sock.recv(&buf, NN_MSG, 0); + size_t sz = sock.recv(&buf, NN_MSG, 0); if (buf) { - string s(buf); + string s(buf, buf+sz); source = s; + nn::freemsg(buf); return true; } @@ -62,6 +63,8 @@ loop(Decoder& decoder, nn::socket& sock) cout << "received source '" << source << "'" << endl; decoder.Decode(source, &o); send(sock, o.translation); + } else { + // Oh no! } } } @@ -75,7 +78,8 @@ main(int argc, char** argv) SetSilent(true); nn::socket sock(AF_SP, NN_PAIR); - string url = "ipc:///tmp/network_decoder.ipc"; + //string url = "ipc:///tmp/network_decoder.ipc"; + string url = "tcp://127.0.0.1:60666"; sock.bind(url.c_str()); loop(decoder, sock); diff --git a/training/dtrain/feed.rb b/training/dtrain/feed.rb index fe8dd509..5f45edfb 100755 --- a/training/dtrain/feed.rb +++ b/training/dtrain/feed.rb @@ -2,12 +2,14 @@ require 'nanomsg' -port = ARGV[0] +#port = ARGV[0] +port = 60666 sock = NanoMsg::PairSocket.new addr = "tcp://127.0.0.1:#{port}" -sock.bind addr +#addr = "ipc:///tmp/network_decoder.ipc" +sock.connect addr -puts sock.recv +#puts sock.recv while true line = STDIN.gets if !line @@ -16,7 +18,7 @@ while true end sock.send line.strip sleep 1 - sock.recv - sock.send "a=1 b=2" + puts "got translation: #{sock.recv}\n\n" + #sock.send "a=1 b=2" end -- cgit v1.2.3