summaryrefslogtreecommitdiff
path: root/decoder
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2015-03-25 15:23:41 +0100
committerPatrick Simianer <p@simianer.de>2015-03-25 15:23:41 +0100
commit9aa075c79122709d8d5b8adf8c91ebd348eac658 (patch)
tree90d59e442881801fbf51de0ba5a5582c2049d9b3 /decoder
parent4ce8dd5d091d63fcafddb447475ae36f769fd8c5 (diff)
updated network_decoder
Diffstat (limited to 'decoder')
-rw-r--r--decoder/Makefile.am6
-rw-r--r--decoder/feed.cc (renamed from decoder/local.cc)12
-rwxr-xr-xdecoder/feed.rb21
-rw-r--r--decoder/network_decoder.cc12
4 files changed, 39 insertions, 12 deletions
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/local.cc b/decoder/feed.cc
index b03fd17e..477d6599 100644
--- a/decoder/local.cc
+++ b/decoder/feed.cc
@@ -1,4 +1,5 @@
#include <iostream>
+#include <string>
#include <sstream>
#include <stdio.h>
#include <unistd.h>
@@ -13,9 +14,9 @@ void
recv(nn::socket& sock)
{
char *buf = NULL;
- sock.recv(&buf, NN_MSG, 0);
+ size_t sz = sock.recv(&buf, NN_MSG, 0);
if (buf) {
- string translation(buf);
+ string translation(buf, buf+sz);
cout << "received translation '" << translation << "'" << endl;
}
}
@@ -32,8 +33,8 @@ 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 .");
+ for (string line; getline(cin, line);) {
+ send(sock, line);
sleep(1);
recv(sock);
}
@@ -42,7 +43,8 @@ loop(nn::socket& sock)
int main(int argc, char const* argv[])
{
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.connect(url.c_str());
loop(sock);
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/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);