11use std:: error:: Error ;
22use std:: io:: Write ;
3- use std:: io:: { BufRead , BufReader , Read } ;
3+ use std:: io:: { BufRead , BufReader } ;
4+ use std:: os:: unix:: net:: UnixStream ;
45use std:: path:: { Path , PathBuf } ;
56use std:: process:: { Child , Command , Stdio } ;
67use std:: thread;
8+ use std:: time:: Duration ;
79
810use log:: info;
911
12+ use crate :: paths:: ct_rr_worker_socket_path;
1013use crate :: query:: CtRRQuery ;
1114
1215#[ derive( Debug ) ]
@@ -23,6 +26,8 @@ pub struct CtRRWorker {
2326 pub ct_rr_worker_exe : PathBuf ,
2427 pub rr_trace_folder : PathBuf ,
2528 process : Option < Child > ,
29+ sending_stream : Option < UnixStream > ,
30+ receiving_stream : Option < UnixStream > ,
2631}
2732
2833#[ derive( Default ) ]
@@ -39,6 +44,8 @@ impl CtRRWorker {
3944 ct_rr_worker_exe : PathBuf :: from ( ct_rr_worker_exe) ,
4045 rr_trace_folder : PathBuf :: from ( rr_trace_folder) ,
4146 process : None ,
47+ sending_stream : None ,
48+ receiving_stream : None ,
4249 }
4350 }
4451
@@ -59,47 +66,70 @@ impl CtRRWorker {
5966 . spawn ( ) ?;
6067
6168 self . process = Some ( ct_worker) ;
62- thread :: sleep_ms ( 1_000 ) ;
69+ self . setup_worker_sockets ( ) ? ;
6370 self . active = true ;
6471 Ok ( ( ) )
6572 }
6673
74+ fn setup_worker_sockets ( & mut self ) -> Result < ( ) , Box < dyn Error > > {
75+ // assuming that the ct rr worker creates the sockets!
76+ // code copied and adapted from `connect_socket_with_backend_and_loop` in ct-rr-worker
77+ // which is itself copied/adapted/written from/based on https://emmanuelbosquet.com/2022/whatsaunixsocket/
78+
79+ let run_id = std:: process:: id ( ) as usize ;
80+
81+ // sending socket:
82+ let sending_socket_path = ct_rr_worker_socket_path ( "backend" , & self . name , run_id) ?;
83+ info ! (
84+ "try to connect to worker with sending socket in {}" ,
85+ sending_socket_path. display( )
86+ ) ;
87+ loop {
88+ if let Ok ( sending_stream) = UnixStream :: connect ( & sending_socket_path) {
89+ self . sending_stream = Some ( sending_stream) ;
90+ break ;
91+ }
92+ thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
93+ // TODO: handle different kinds of errors
94+ }
95+ // receiving socket:
96+ let receiving_socket_path = ct_rr_worker_socket_path ( "worker" , & self . name , run_id) ?;
97+ info ! (
98+ "try to connect to worker with receiving socket in {}" ,
99+ receiving_socket_path. display( )
100+ ) ;
101+ loop {
102+ if let Ok ( receiving_stream) = UnixStream :: connect ( & receiving_socket_path) {
103+ self . receiving_stream = Some ( receiving_stream) ;
104+ break ;
105+ }
106+ thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
107+ // TODO: handle different kinds of errors
108+ }
109+
110+ Ok ( ( ) )
111+ }
112+
67113 // for now: don't return a typed value here, only ok or an error
68114 pub fn run_query ( & mut self , query : CtRRQuery ) -> Result < ( ) , Box < dyn Error > > {
69- let mut stdin = self
70- . process
71- . as_mut ( )
72- . expect ( "valid process" )
73- . stdin
74- . take ( )
75- . expect ( "stdin: TODO error" ) ;
76- let mut stdout = self
77- . process
78- . as_mut ( )
79- . expect ( "valid process" )
80- . stdout
81- . take ( )
82- . expect ( "stdout: TODO error" ) ;
83-
84115 let raw_json = serde_json:: to_string ( & query) ?;
85- let reader = BufReader :: new ( stdout) ;
86116
87117 info ! ( "send to worker {raw_json}\n " ) ;
88- write ! ( stdin, "{}\n " , raw_json) ?;
118+ self . sending_stream
119+ . as_mut ( )
120+ . expect ( "valid sending stream" )
121+ . write ( & format ! ( "{raw_json}\n " ) . into_bytes ( ) ) ?;
89122
90123 let mut res = "" . to_string ( ) ;
91124 info ! ( "wait to read" ) ;
92125
93- for line_result in reader. lines ( ) {
94- info ! ( "line_result {line_result:?}" ) ;
95- if let Ok ( line) = line_result {
96- res. push_str ( & line) ;
97- res. push_str ( "\n " ) ;
98- } else {
99- continue ;
100- }
101- }
126+ let mut reader = BufReader :: new ( self . receiving_stream . as_mut ( ) . expect ( "valid receiving stream" ) ) ;
127+ reader. read_line ( & mut res) ?;
128+
129+ res = String :: from ( res. trim ( ) ) ; // trim newlines/whitespace!
130+
102131 info ! ( "res {res}" ) ;
132+
103133 if res == "ok" {
104134 Ok ( ( ) )
105135 } else {
0 commit comments