server.ml 2.16 KB
(*
 *  ENIAM: Categorial Syntactic-Semantic Parser for Polish
 *  Copyright (C) 2016 Wojciech Jaworski <wjaworski atSPAMfree mimuw dot edu dot pl>
 *  Copyright (C) 2016 Institute of Computer Science Polish Academy of Sciences
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *)
 
open ExecTypes

let logfile = open_out_gen [Open_wronly; Open_append; Open_creat] ((6*8+4)*8+4) "results/queries.log"

let get_sock_addr host_name port =
  let he = Unix.gethostbyname host_name in
  let addr = he.Unix.h_addr_list in
  Unix.ADDR_INET(addr.(0),port)

let parse query =
  let max_n = 10 in
  let ic,oc = Unix.open_connection (get_sock_addr Paths.pre_host Paths.pre_port) in
  let result = Exec.process_query ic oc 100. false "x" query max_n in
  Printf.fprintf oc "\n%!";
  let _ = Unix.shutdown_connection ic in
  {result with 
     graph=[| |];
     term=[| |];
     disamb=[| |];
     sem=[| |];
     sem2=[| |];
     sem3=LCGtypes.Dot;
     paths=[| |]}

let rec main_loop in_chan out_chan =
  let query = input_line in_chan in
  if query = "" then Marshal.to_channel out_chan {Exec.empty_result with msg="Empty query"} [] else 
  (try 
    let result : ExecTypes.result = parse query in
    Exec.print_result logfile result;
    Marshal.to_channel out_chan result []
  with e -> 
    Printf.fprintf logfile "query: %s\nerror_other: %s\n%!" query (Printexc.to_string e);
    Marshal.to_channel out_chan {Exec.empty_result with msg=Printexc.to_string e} []);
  flush out_chan


let sockaddr = Unix.ADDR_INET(Unix.inet_addr_any,Paths.server_port)

let _ = 
  Unix.establish_server main_loop sockaddr