summaryrefslogtreecommitdiff
path: root/server.rb
blob: def401ebd34a6a0a1292ffeffd49d0f695c9b469 (plain)
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
#!/usr/bin/env ruby

require 'sinatra'
require 'sinatra/cross_origin'
require 'nanomsg'
require 'zipf'

set :bind, '147.142.207.52'
set :port, 60666

set :allow_origin, :any
set :allow_methods, [:get, :post, :options]
set :allow_credentials, true
set :max_age, "1728000"
set :expose_headers, ['Content-Type']

sock = NanoMsg::PairSocket.new
addr = "ipc:///tmp/dtrain.ipc"
sock.bind addr

input = ReadFile.readlines_strip "model/src.gz"
input_ = Array.new input

get '/' do
  cross_origin
  "Nothing to see here."
end

get '/next' do
  cross_origin
  if params[:example]
    sock.send params[:example].strip
    puts params.to_s
    sock.recv # dummy
  end
  src = input.shift
  if !src
    puts "end of input, sending 'fi'"
    "fi"
  else
    puts "sending source '#{src}' ..."
    sock.send "act:translate ||| #{src}"
    puts "done"
    sleep 1
    puts "waiting for translation ..."
    t = sock.recv
    puts "got translation '#{t}'"
    "#{src}\t#{t}"
  end
end

get '/reset' do
  cross_origin
  input = Array.new input_
  "done"
end