1717import java .util .Map ;
1818import java .util .concurrent .ConcurrentHashMap ;
1919import java .util .concurrent .TimeUnit ;
20+ import java .util .logging .Level ;
21+ import java .util .logging .Logger ;
2022
2123/**
2224 * Created by Tomas Verhelst on 03/09/2018.
@@ -26,37 +28,41 @@ public class OnvifExecutor {
2628
2729 //Constants
2830 public static final String TAG = OnvifExecutor .class .getSimpleName ();
31+ private static final Logger LOGGER = Logger .getLogger (OnvifExecutor .class .getName ());
2932
3033 //Attributes
31- private final OkHttpClient client ;
3234 private final MediaType reqBodyType = MediaType .parse ("application/soap+xml; charset=utf-8;" );
33- private final Credentials credentials = new Credentials ( "" , "" );
35+ private final ConcurrentHashMap < String , OkHttpClient > clientPool = new ConcurrentHashMap <>( );
3436 private OnvifResponseListener onvifResponseListener ;
3537
3638 //Constructors
3739
3840 OnvifExecutor (OnvifResponseListener onvifResponseListener ) {
3941 this .onvifResponseListener = onvifResponseListener ;
42+ }
4043
41- DigestAuthenticator authenticator = new DigestAuthenticator (credentials );
42- Map <String , CachingAuthenticator > authCache = new ConcurrentHashMap <>();
43-
44- client = new OkHttpClient .Builder ()
45- .connectTimeout (3 , TimeUnit .SECONDS )
46- .writeTimeout (5 , TimeUnit .SECONDS )
47- .readTimeout (0 , TimeUnit .SECONDS ) // PullMessages can keep the connection open longer, timeout will be applied per request
48- .addInterceptor (new AuthenticationCacheInterceptor (authCache ))
49- .authenticator (new CachingAuthenticatorDecorator (authenticator , authCache ))
50- .build ();
44+ /**
45+ * Visszaadja vagy létrehozza az OkHttpClient példányt az adott eszközhöz
46+ */
47+ private OkHttpClient getClientForDevice (OnvifDevice device ) {
48+ String deviceKey = device .getHostName () + ":" + device .getUsername ();
49+ return clientPool .computeIfAbsent (deviceKey , key -> {
50+ Credentials deviceCredentials = new Credentials (device .getUsername (), device .getPassword ());
51+ DigestAuthenticator authenticator = new DigestAuthenticator (deviceCredentials );
52+ Map <String , CachingAuthenticator > authCache = new ConcurrentHashMap <>();
53+
54+ return new OkHttpClient .Builder ()
55+ .connectTimeout (3 , TimeUnit .SECONDS )
56+ .writeTimeout (5 , TimeUnit .SECONDS )
57+ .readTimeout (0 , TimeUnit .SECONDS ) // PullMessages can keep the connection open longer, timeout will be applied per request
58+ .addInterceptor (new AuthenticationCacheInterceptor (authCache ))
59+ .authenticator (new CachingAuthenticatorDecorator (authenticator , authCache ))
60+ .build ();
61+ });
5162 }
5263
5364 //Methods
5465
55- private synchronized void setCredentials (String username , String password ) {
56- credentials .setUserName (username );
57- credentials .setPassword (password );
58- }
59-
6066 <T > void sendRequest (OnvifDevice device , OnvifRequest <T > request ) {
6167 sendRequest (device , request , 5 );
6268 }
@@ -65,12 +71,9 @@ <T> void sendRequest(OnvifDevice device, OnvifRequest<T> request) {
6571 * Sends a request to the Onvif-compatible device.
6672 */
6773 <T > void sendRequest (OnvifDevice device , OnvifRequest <T > request , int timeoutSeconds ) {
68- String body ;
69- synchronized (credentials ) {
70- setCredentials (device .getUsername (), device .getPassword ());
71- body = OnvifXMLBuilder .getSoapHeader (credentials , request .getSoapHeader ()) + request .getXml () + OnvifXMLBuilder .getEnvelopeEnd ();
72- }
73-
74+ Credentials deviceCredentials = new Credentials (device .getUsername (), device .getPassword ());
75+ String body = OnvifXMLBuilder .getSoapHeader (deviceCredentials , request .getSoapHeader ()) + request .getXml () + OnvifXMLBuilder .getEnvelopeEnd ();
76+ LOGGER .log (Level .INFO , "Onvif Sending Request: {0}" , body );
7477 performXmlRequest (device , request , buildOnvifRequest (device , request , RequestBody .create (body , reqBodyType )), timeoutSeconds );
7578 }
7679
@@ -79,6 +82,12 @@ <T> void sendRequest(OnvifDevice device, OnvifRequest<T> request, int timeoutSec
7982 */
8083 void clear () {
8184 onvifResponseListener = null ;
85+ // Kliensek leállítása és erőforrások felszabadítása
86+ for (OkHttpClient client : clientPool .values ()) {
87+ client .dispatcher ().executorService ().shutdown ();
88+ client .connectionPool ().evictAll ();
89+ }
90+ clientPool .clear ();
8291 }
8392
8493 //Properties
@@ -94,14 +103,13 @@ public void setOnvifResponseListener(OnvifResponseListener onvifResponseListener
94103 * @param listener Válasz listener (ByteArray)
95104 */
96105 void downloadSnapshot (OnvifDevice device , String snapshotUri , int timeoutSeconds , OnvifRequest .Listener <byte []> listener ) {
97- setCredentials (device .getUsername (), device .getPassword ());
98-
99106 Request request = new Request .Builder ()
100107 .url (snapshotUri )
101108 .get ()
102109 .build ();
103110
104- final Call call = client .newCall (request );
111+ OkHttpClient deviceClient = getClientForDevice (device );
112+ final Call call = deviceClient .newCall (request );
105113 call .timeout ().timeout (timeoutSeconds , TimeUnit .SECONDS );
106114
107115 try (Response response = call .execute ()) {
@@ -127,7 +135,8 @@ void downloadSnapshot(OnvifDevice device, String snapshotUri, int timeoutSeconds
127135 private <T > void performXmlRequest (OnvifDevice device , OnvifRequest <T > request , Request xmlRequest , int timeoutSeconds ) {
128136 if (xmlRequest == null ) return ;
129137
130- final Call call = client .newCall (xmlRequest );
138+ OkHttpClient deviceClient = getClientForDevice (device );
139+ final Call call = deviceClient .newCall (xmlRequest );
131140 call .timeout ().timeout (timeoutSeconds , TimeUnit .SECONDS );
132141
133142 try (Response xmlResponse = call .execute ()) {
0 commit comments