11use std:: sync:: { mpsc, Arc , atomic:: { AtomicI32 , self } } ;
22
3- use log:: { warn, info} ;
3+ use log:: { warn, info, debug } ;
44use anyhow:: Result ;
55use serde_json as json;
66
@@ -64,36 +64,41 @@ fn process_client_reader(reader: impl std::io::Read,
6464
6565fn process_server_reader ( reader : impl std:: io:: Read ,
6666 channel_pub : mpsc:: Sender < String > ,
67- bytecode_options : BytecodeOptions ) -> Result < ( ) > {
67+ bytecode_options : Option < BytecodeOptions > ) -> Result < ( ) > {
6868 let mut bufreader = std:: io:: BufReader :: new ( reader) ;
6969 loop {
7070 let msg = rpcio:: rpc_read ( & mut bufreader) ?;
7171 if msg. is_empty ( ) {
7272 break
7373 }
74- let json_val = json :: from_str ( & msg ) ? ;
75- match bytecode :: generate_bytecode_repl ( & json_val , bytecode_options . clone ( ) ) {
76- Ok ( bytecode_str) => {
74+ if let Some ( ref bytecode_options ) = bytecode_options {
75+ let json_val = json :: from_str ( & msg ) ? ;
76+ if let Ok ( bytecode_str) = bytecode :: generate_bytecode_repl ( & json_val , bytecode_options . clone ( ) ) {
7777 channel_pub. send ( bytecode_str) ?;
78- } ,
79- Err ( e) => {
80- warn ! ( "Failed to generate bytecode: {}; fallback to original json" , e) ;
81- channel_pub. send ( msg) ?;
82- } ,
78+ continue
79+ }
8380 }
81+ channel_pub. send ( msg) ?;
8482 }
8583 Ok ( ( ) )
8684}
8785
8886pub struct AppOptions {
89- pub bytecode_options : bytecode:: BytecodeOptions ,
87+ // if bytecode_options is None, then don't generate bytecode!
88+ pub bytecode_options : Option < bytecode:: BytecodeOptions > ,
9089}
9190
9291pub fn run_app_forever ( client_reader : impl std:: io:: Read + Send + ' static ,
9392 client_writer : impl std:: io:: Write + Send + ' static ,
9493 mut server_cmd : std:: process:: Command ,
9594 options : AppOptions ) -> Result < std:: process:: ExitStatus > {
9695 info ! ( "Running server {:?}" , server_cmd) ;
96+ if let Some ( ref bytecode_options) = options. bytecode_options {
97+ info ! ( "Will convert server json to bytecode! bytecode options: {:?}" , bytecode_options) ;
98+ } else {
99+ info ! ( "Bytecode disabled! Will forward server json as-is." )
100+ }
101+
97102 let mut proc = server_cmd
98103 . stdin ( std:: process:: Stdio :: piped ( ) )
99104 . stdout ( std:: process:: Stdio :: piped ( ) )
@@ -108,30 +113,30 @@ pub fn run_app_forever(client_reader: impl std::io::Read + Send + 'static,
108113 let c2s_channel_counter = c2s_channel_counter. clone ( ) ;
109114 let proc_stdin = proc. stdin . take ( ) . unwrap ( ) ;
110115 std:: thread:: spawn ( move || {
111- info ! ( "Started client->server write thread" ) ;
116+ debug ! ( "Started client->server write thread" ) ;
112117 process_channel_to_writer ( c2s_channel_sub, Some ( c2s_channel_counter) , proc_stdin) . unwrap ( ) ;
113- info ! ( "Finished client->server write thread" ) ;
118+ debug ! ( "Finished client->server write thread" ) ;
114119 } ) ;
115120 }
116121 std:: thread:: spawn ( move || {
117- info ! ( "Started server->client write thread" ) ;
122+ debug ! ( "Started server->client write thread" ) ;
118123 process_channel_to_writer ( s2c_channel_sub, None , client_writer) . unwrap ( ) ;
119- info ! ( "Finished server->client write thread" ) ;
124+ debug ! ( "Finished server->client write thread" ) ;
120125 } ) ;
121126 {
122127 let s2c_channel_pub = s2c_channel_pub. clone ( ) ;
123128 let proc_stdout = proc. stdout . take ( ) . unwrap ( ) ;
124129 std:: thread:: spawn ( move || {
125- info ! ( "Started server->client read thread" ) ;
130+ debug ! ( "Started server->client read thread" ) ;
126131 process_server_reader ( proc_stdout, s2c_channel_pub, options. bytecode_options ) . unwrap ( ) ;
127- info ! ( "Finished server->client read thread" ) ;
132+ debug ! ( "Finished server->client read thread" ) ;
128133 } ) ;
129134 }
130135 std:: thread:: spawn ( move || {
131- info ! ( "Started client->server read thread" ) ;
136+ debug ! ( "Started client->server read thread" ) ;
132137 process_client_reader (
133138 client_reader, c2s_channel_pub, c2s_channel_counter, s2c_channel_pub) . unwrap ( ) ;
134- info ! ( "Finished client->server read thread" ) ;
139+ debug ! ( "Finished client->server read thread" ) ;
135140 } ) ;
136141
137142 Ok ( proc. wait ( ) ?)
0 commit comments