Add forgotten files 1.4.70b
[polyglot.git] / engine.c
1 // engine.c
2
3 // includes
4
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <string.h>
8 #include <errno.h>
9
10 #include "engine.h"
11 #include "option.h"
12 #include "pipex.h"
13 #include "util.h"
14
15 // variables
16
17 engine_t Engine[1];
18
19 // functions
20
21 // set_affinity()
22
23 void set_affinity(engine_t *engine, int value){
24     pipex_set_affinity(engine->pipex,value);
25 }
26
27 // engine_set_nice_value()
28
29 void engine_set_nice_value(engine_t *engine, int value){
30     pipex_set_priority(engine->pipex,value);
31 }
32
33 // engine_send_queue()
34
35 void engine_send_queue(engine_t * engine, const char *format, ...) {
36     char buf[FormatBufferSize];
37     CONSTRUCT_ARG_STRING(format,buf);
38     pipex_write(engine->pipex,buf);
39 }
40
41 // engine_send()
42
43 void engine_send(engine_t * engine, const char *format, ...) {
44     char buf[FormatBufferSize];
45     CONSTRUCT_ARG_STRING(format,buf);
46     pipex_writeln(engine->pipex,buf);
47 }
48
49 // engine_close()
50
51 void engine_close(engine_t * engine){
52     char string[StringSize];
53     int elapsed_time;
54     int ret;
55     int close_timeout=500;
56     my_log("POLYGLOT Closing engine.\n");
57     pipex_send_eof(engine->pipex);
58         // TODO: Timeout
59
60     elapsed_time=0;
61     while (!engine_eof(engine) && (elapsed_time<close_timeout)) { 
62       ret=engine_get_non_blocking(engine,string);
63       if(!ret  && !engine_eof(engine)){
64         my_log("POLYGLOT Engine does not reply. Sleeping %dms.\n", WAIT_GRANULARITY);
65         my_sleep(WAIT_GRANULARITY);
66         elapsed_time+=WAIT_GRANULARITY;
67       }
68     }
69     if(elapsed_time>=close_timeout){
70       my_log("POLYGLOT Waited more than %dms. Moving on.\n",close_timeout); 
71     }
72     pipex_exit(engine->pipex,200);
73 }
74
75 // engine_open()
76
77 void engine_open(engine_t * engine){
78     int affinity;
79     pipex_open(engine->pipex,
80                "Engine",
81                option_get_string(Option,"EngineDir"),
82                option_get_string(Option,"EngineCommand"));
83     if(pipex_active(engine->pipex)){
84             //play with affinity (bad idea)
85         affinity=option_get_int(Option,"Affinity");
86         if(affinity!=-1) set_affinity(engine,affinity); //AAA
87             // set a low priority
88         if (option_get_bool(Option,"UseNice")){
89             my_log("POLYGLOT Adjust Engine Piority\n");
90             engine_set_nice_value(engine, option_get_int(Option,"NiceValue"));
91         }
92     }
93     
94 }
95
96 bool engine_active(engine_t *engine){
97     return pipex_active(engine->pipex);
98 }
99
100 bool engine_eof(engine_t *engine){
101     return pipex_eof(engine->pipex);
102 }
103
104 bool engine_get_non_blocking(engine_t * engine, char *string){
105     return pipex_readln_nb(engine->pipex,string);
106  }
107
108 void engine_get(engine_t * engine, char *string){
109     pipex_readln(engine->pipex,string);
110 }
111
112