11package ch.fhnw.pepper_realtime.network
22
3- import android.os.Looper
43import android.util.Log
54import kotlinx.coroutines.CoroutineDispatcher
65import kotlinx.coroutines.suspendCancellableCoroutine
@@ -10,7 +9,6 @@ import okhttp3.MediaType.Companion.toMediaType
109import okhttp3.RequestBody.Companion.toRequestBody
1110import java.io.IOException
1211import java.util.concurrent.TimeUnit
13- import java.util.concurrent.atomic.AtomicBoolean
1412import javax.inject.Inject
1513import javax.inject.Singleton
1614import kotlin.coroutines.resume
@@ -48,7 +46,6 @@ class HttpClientManager @Inject constructor() {
4846 private val webSocketClient: OkHttpClient
4947 private val apiClient: OkHttpClient
5048 private val quickApiClient: OkHttpClient
51- private val shutdownInitiated = AtomicBoolean (false )
5249
5350 init {
5451 Log .i(TAG , " Initializing optimized HTTP client manager" )
@@ -178,46 +175,6 @@ class HttpClientManager @Inject constructor() {
178175 )
179176 }
180177
181- /* *
182- * Cleanup resources - call on app shutdown
183- */
184- fun shutdown () {
185- if (! shutdownInitiated.compareAndSet(false , true )) {
186- Log .d(TAG , " Shutdown already in progress - ignoring duplicate request" )
187- return
188- }
189-
190- synchronized(HttpClientManager ::class .java) {
191- if (instance == this ) {
192- instance = null
193- }
194- }
195-
196- val task = Runnable { shutdownInternal() }
197- if (Looper .myLooper() == Looper .getMainLooper()) {
198- Log .d(TAG , " Shutdown requested on main thread - offloading to background thread" )
199- Thread (task, " http-client-shutdown" ).start()
200- } else {
201- task.run ()
202- }
203- }
204-
205- private fun shutdownInternal () {
206- try {
207- webSocketClient.dispatcher.executorService.shutdown()
208- apiClient.dispatcher.executorService.shutdown()
209- quickApiClient.dispatcher.executorService.shutdown()
210-
211- webSocketClient.connectionPool.evictAll()
212-
213- Log .i(TAG , " HTTP client manager shutdown completed" )
214- } catch (e: Exception ) {
215- Log .w(TAG , " Error during HTTP client shutdown" , e)
216- } finally {
217- Log .i(TAG , " HTTP client manager instance reset for clean restart" )
218- shutdownInitiated.set(false )
219- }
220- }
221178}
222179
223180
0 commit comments