File size: 1,766 Bytes
fd49381 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#!/usr/bin/perl -w
use FindBin qw($Bin);
use warnings;
use strict;
$|++;
# file: daemon.pl
# Herve Saint-Amand
# Universitaet des Saarlandes
# Tue May 13 19:45:31 2008
# This script starts Moses to run in the background, so that it can be used by
# the CGI script. It spawns the Moses process, then binds itself to listen on
# some port, and when it gets a connection, reads it line by line, feeds those
# to Moses, and sends back the translation.
# You can either run one instance of this on your Web server, or, if you have
# the hardware setup for it, run several instances of this, then configure
# translate.cgi to connect to these.
#------------------------------------------------------------------------------
# includes
use IO::Socket::INET;
use IPC::Open2;
#------------------------------------------------------------------------------
# constants, global vars, config
my $MOSES = "$Bin/../../../bin/moses";
die "usage: daemon.pl <hostname> <port> <ini>" unless (@ARGV == 3);
my $LISTEN_HOST = shift;
my $LISTEN_PORT = shift;
my $MOSES_INI = shift;
#------------------------------------------------------------------------------
# main
# spawn moses
my ($MOSES_IN, $MOSES_OUT);
my $pid = open2 ($MOSES_OUT, $MOSES_IN, $MOSES, '-f', $MOSES_INI, '-t');
# open server socket
my $server_sock = new IO::Socket::INET
(LocalAddr => $LISTEN_HOST, LocalPort => $LISTEN_PORT, Listen => 1)
|| die "Can't bind server socket";
while (my $client_sock = $server_sock->accept) {
while (my $line = <$client_sock>) {
print $MOSES_IN $line;
$MOSES_IN->flush ();
print $client_sock scalar <$MOSES_OUT>;
}
$client_sock->close ();
}
#------------------------------------------------------------------------------
|