@@ -15,6 +15,7 @@ use std::error::Error;
1515use std:: sync:: { Arc , Mutex , Once } ;
1616use std:: thread;
1717use veilid_core:: veilid_core_setup_android;
18+ use std:: time:: Duration ;
1819
1920
2021trait IntoJObject {
@@ -104,8 +105,47 @@ pub extern "system" fn Java_net_opendasharchive_openarchive_services_snowbird_Sn
104105 _clazz : JClass ,
105106 ctx : JObject ,
106107) -> jstring {
108+ log_debug ! ( TAG , "Bridge: stopping server" ) ;
109+
110+ // Create a runtime to handle async operations
111+ let runtime = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
112+
113+ // Stop the backend server and clean up Veilid API
114+ let stop_result = runtime. block_on ( async {
115+ // First stop the backend
116+ match crate :: server:: server:: stop ( ) . await {
117+ Ok ( _) => {
118+ log_info ! ( TAG , "Backend stopped successfully" ) ;
119+
120+ // Get the backend to access Veilid API
121+ if let Ok ( mut backend) = crate :: server:: server:: get_backend ( ) . await {
122+ // Shutdown Veilid API
123+ if let Some ( veilid_api) = backend. get_veilid_api ( ) {
124+ veilid_api. shutdown ( ) . await ;
125+ log_info ! ( TAG , "Veilid API shut down successfully" ) ;
126+ }
127+ }
128+
129+ // Add a small delay to ensure tasks complete
130+ tokio:: time:: sleep ( Duration :: from_millis ( 500 ) ) . await ;
131+
132+ Ok ( ( ) )
133+ }
134+ Err ( e) => {
135+ log_error ! ( TAG , "Error stopping server: {:?}" , e) ;
136+ Err ( e)
137+ }
138+ }
139+ } ) ;
140+
141+ // Create response string based on result
142+ let response = match stop_result {
143+ Ok ( _) => "Server stopped successfully" ,
144+ Err ( _) => "Error stopping server" ,
145+ } ;
146+
107147 let output = env
108- . new_string ( "Server stopped" )
148+ . new_string ( response )
109149 . expect ( "Couldn't create java string!" ) ;
110150
111151 output. into_raw ( )
0 commit comments