summaryrefslogtreecommitdiff
path: root/decoder
diff options
context:
space:
mode:
Diffstat (limited to 'decoder')
-rw-r--r--decoder/local.cc51
-rw-r--r--decoder/network_decoder.cc85
-rw-r--r--decoder/nn.hpp204
3 files changed, 340 insertions, 0 deletions
diff --git a/decoder/local.cc b/decoder/local.cc
new file mode 100644
index 00000000..b03fd17e
--- /dev/null
+++ b/decoder/local.cc
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <sstream>
+#include <stdio.h>
+#include <unistd.h>
+
+#include <nanomsg/nn.h>
+#include <nanomsg/pair.h>
+#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
new file mode 100644
index 00000000..aaa1842d
--- /dev/null
+++ b/decoder/network_decoder.cc
@@ -0,0 +1,85 @@
+#include <string>
+
+#include "decoder.h"
+#include "ff_register.h"
+#include "filelib.h"
+#include "verbose.h"
+#include "viterbi.h"
+
+#include <nanomsg/nn.h>
+#include <nanomsg/pair.h>
+#include "nn.hpp"
+
+using namespace std;
+
+struct TheObserver : public DecoderObserver
+{
+ string translation;
+
+ virtual void
+ NotifyTranslationForest(const SentenceMetadata& smeta, Hypergraph* hg)
+ {
+ translation.clear();
+ vector<WordID> trans;
+ ViterbiESentence(*hg, &trans);
+ translation = TD::GetString(trans);
+ }
+};
+
+int send(nn::socket& sock, const string trans)
+{
+ cout << "sending translation '" << trans << "'" << endl;
+ sock.send(trans.c_str(), trans.size()+1, 0);
+}
+
+bool
+recv(nn::socket& sock, string& source)
+{
+ char *buf = NULL;
+ sock.recv(&buf, NN_MSG, 0);
+ if (buf) {
+ string s(buf);
+ source = s;
+
+ 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);
+ }
+ }
+}
+
+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";
+ sock.bind(url.c_str());
+
+ loop(decoder, sock);
+
+ return 0;
+}
+
diff --git a/decoder/nn.hpp b/decoder/nn.hpp
new file mode 100644
index 00000000..50b8304c
--- /dev/null
+++ b/decoder/nn.hpp
@@ -0,0 +1,204 @@
+/*
+ Copyright (c) 2013 250bpm s.r.o.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"),
+ to deal in the Software without restriction, including without limitation
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ and/or sell copies of the Software, and to permit persons to whom
+ the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included
+ in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ IN THE SOFTWARE.
+*/
+
+#ifndef NN_HPP_INCLUDED
+#define NN_HPP_INCLUDED
+
+#include <nanomsg/nn.h>
+
+#include <cassert>
+#include <cstring>
+#include <algorithm>
+#include <exception>
+
+#if defined __GNUC__
+#define nn_slow(x) __builtin_expect ((x), 0)
+#else
+#define nn_slow(x) (x)
+#endif
+
+namespace nn
+{
+
+ class exception : public std::exception
+ {
+ public:
+
+ exception () : err (nn_errno ()) {}
+
+ virtual const char *what () const throw ()
+ {
+ return nn_strerror (err);
+ }
+
+ int num () const
+ {
+ return err;
+ }
+
+ private:
+
+ int err;
+ };
+
+ inline const char *symbol (int i, int *value)
+ {
+ return nn_symbol (i, value);
+ }
+
+ inline void *allocmsg (size_t size, int type)
+ {
+ void *msg = nn_allocmsg (size, type);
+ if (nn_slow (!msg))
+ throw nn::exception ();
+ return msg;
+ }
+
+ inline int freemsg (void *msg)
+ {
+ int rc = nn_freemsg (msg);
+ if (nn_slow (rc != 0))
+ throw nn::exception ();
+ return rc;
+ }
+
+ class socket
+ {
+ public:
+
+ inline socket (int domain, int protocol)
+ {
+ s = nn_socket (domain, protocol);
+ if (nn_slow (s < 0))
+ throw nn::exception ();
+ }
+
+ inline ~socket ()
+ {
+ int rc = nn_close (s);
+ assert (rc == 0);
+ }
+
+ inline void setsockopt (int level, int option, const void *optval,
+ size_t optvallen)
+ {
+ int rc = nn_setsockopt (s, level, option, optval, optvallen);
+ if (nn_slow (rc != 0))
+ throw nn::exception ();
+ }
+
+ inline void getsockopt (int level, int option, void *optval,
+ size_t *optvallen)
+ {
+ int rc = nn_getsockopt (s, level, option, optval, optvallen);
+ if (nn_slow (rc != 0))
+ throw nn::exception ();
+ }
+
+ inline int bind (const char *addr)
+ {
+ int rc = nn_bind (s, addr);
+ if (nn_slow (rc < 0))
+ throw nn::exception ();
+ return rc;
+ }
+
+ inline int connect (const char *addr)
+ {
+ int rc = nn_connect (s, addr);
+ if (nn_slow (rc < 0))
+ throw nn::exception ();
+ return rc;
+ }
+
+ inline void shutdown (int how)
+ {
+ int rc = nn_shutdown (s, how);
+ if (nn_slow (rc != 0))
+ throw nn::exception ();
+ }
+
+ inline int send (const void *buf, size_t len, int flags)
+ {
+ int rc = nn_send (s, buf, len, flags);
+ if (nn_slow (rc < 0)) {
+ if (nn_slow (nn_errno () != EAGAIN))
+ throw nn::exception ();
+ return -1;
+ }
+ return rc;
+ }
+
+ inline int recv (void *buf, size_t len, int flags)
+ {
+ int rc = nn_recv (s, buf, len, flags);
+ if (nn_slow (rc < 0)) {
+ if (nn_slow (nn_errno () != EAGAIN))
+ throw nn::exception ();
+ return -1;
+ }
+ return rc;
+ }
+
+ inline int sendmsg (const struct nn_msghdr *msghdr, int flags)
+ {
+ int rc = nn_sendmsg (s, msghdr, flags);
+ if (nn_slow (rc < 0)) {
+ if (nn_slow (nn_errno () != EAGAIN))
+ throw nn::exception ();
+ return -1;
+ }
+ return rc;
+ }
+
+ inline int recvmsg (struct nn_msghdr *msghdr, int flags)
+ {
+ int rc = nn_recvmsg (s, msghdr, flags);
+ if (nn_slow (rc < 0)) {
+ if (nn_slow (nn_errno () != EAGAIN))
+ throw nn::exception ();
+ return -1;
+ }
+ return rc;
+ }
+
+ private:
+
+ int s;
+
+ /* Prevent making copies of the socket by accident. */
+ socket (const socket&);
+ void operator = (const socket&);
+ };
+
+ inline void term ()
+ {
+ nn_term ();
+ }
+
+}
+
+#undef nn_slow
+
+#endif
+
+