1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.plugin.reconnectplugin;
import java.util.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.netaddr.*;
import net.java.sip.communicator.service.netaddr.event.*;
import net.java.sip.communicator.service.notification.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import org.jitsi.service.configuration.*;
import org.jitsi.service.resources.*;
import org.osgi.framework.*;
/**
* Activates the reconnect plug-in.
*
* @author Damian Minkov
*/
public class ReconnectPluginActivator
implements BundleActivator,
ServiceListener,
NetworkConfigurationChangeListener,
RegistrationStateChangeListener
{
/**
* Logger of this class
*/
private static final Logger logger
= Logger.getLogger(ReconnectPluginActivator.class);
/**
* The current BundleContext.
*/
private static BundleContext bundleContext = null;
/**
* The ui service.
*/
private static UIService uiService;
/**
* The resources service.
*/
private static ResourceManagementService resourcesService;
/**
* A reference to the ConfigurationService implementation instance that
* is currently registered with the bundle context.
*/
private static ConfigurationService configurationService = null;
/**
* Notification service.
*/
private static NotificationService notificationService;
/**
* Network address manager service will inform us for changes in
* network configuration.
*/
private NetworkAddressManagerService networkAddressManagerService = null;
/**
* Holds every protocol provider which is can be reconnected and
* a list of the available and up interfaces when the provider was
* registered. When a provider is unregistered it is removed
* from this collection.
*/
private final Map<ProtocolProviderService, List<String>> autoReconnEnabledProviders
= new HashMap<ProtocolProviderService, List<String>>();
/**
* Holds the currently reconnecting providers and their reconnect tasks.
* When they get connected they are removed from this collection.
*/
private final Map<ProtocolProviderService, ReconnectTask> currentlyReconnecting
= new HashMap<ProtocolProviderService, ReconnectTask>();
/**
* If network is down we save here the providers which need to be reconnected.
*/
private Set<ProtocolProviderService> needsReconnection =
new HashSet<ProtocolProviderService>();
/**
* A list of providers on which we have called unregister. This is a
* way to differ our unregister calls from calls coming from user, wanting
* to stop all reconnects.
*/
private Set<ProtocolProviderService> unregisteredProviders
= new HashSet<ProtocolProviderService>();
/**
* A list of currently connected interfaces. If empty network is down.
*/
private Set<String> connectedInterfaces = new HashSet<String>();
/**
* Timer for scheduling all reconnect operations.
*/
private Timer timer = null;
/**
* Start of the delay interval when starting a reconnect.
*/
private static final int RECONNECT_DELAY_MIN = 2; // sec
/**
* The end of the interval for the initial reconnect.
*/
private static final int RECONNECT_DELAY_MAX = 4; // sec
/**
* Max value for growing the reconnect delay, all subsequent reconnects
* use this maximum delay.
*/
private static final int MAX_RECONNECT_DELAY = 300; // sec
/**
* Network notifications event type.
*/
public static final String NETWORK_NOTIFICATIONS = "NetworkNotifications";
/**
*
*/
public static final String ATLEAST_ONE_CONNECTION_PROP =
"net.java.sip.communicator.plugin.reconnectplugin." +
"ATLEAST_ONE_SUCCESSFUL_CONNECTION";
/**
* Starts this bundle.
*
* @param bundleContext the <tt>BundleContext</tt> in which this bundle is
* to be started
* @throws Exception if anything goes wrong while starting this bundle
*/
public void start(BundleContext bundleContext)
throws Exception
{
try
{
logger.logEntry();
ReconnectPluginActivator.bundleContext = bundleContext;
}
finally
{
logger.logExit();
}
bundleContext.addServiceListener(this);
if(timer == null)
timer = new Timer("Reconnect timer", true);
this.networkAddressManagerService
= ServiceUtils.getService(
bundleContext,
NetworkAddressManagerService.class);
this.networkAddressManagerService
.addNetworkConfigurationChangeListener(this);
ServiceReference[] protocolProviderRefs = null;
try
{
protocolProviderRefs = bundleContext.getServiceReferences(
ProtocolProviderService.class.getName(), null);
}
catch (InvalidSyntaxException ex)
{
// this shouldn't happen since we're providing no parameter string
// but let's log just in case.
logger.error(
"Error while retrieving service refs", ex);
return;
}
// in case we found any
if (protocolProviderRefs != null)
{
if (logger.isDebugEnabled())
logger.debug("Found "
+ protocolProviderRefs.length
+ " already installed providers.");
for (int i = 0; i < protocolProviderRefs.length; i++)
{
ProtocolProviderService provider
= (ProtocolProviderService) bundleContext
.getService(protocolProviderRefs[i]);
this.handleProviderAdded(provider);
}
}
}
/**
* Stops this bundle.
*
* @param bundleContext the <tt>BundleContext</tt> in which this bundle is
* to be stopped
* @throws Exception if anything goes wrong while stopping this bundle
*/
public void stop(BundleContext bundleContext)
throws Exception
{
if(timer != null)
{
timer.cancel();
timer = null;
}
}
/**
* Returns the <tt>UIService</tt> obtained from the bundle context.
*
* @return the <tt>UIService</tt> obtained from the bundle context
*/
public static UIService getUIService()
{
if (uiService == null)
{
ServiceReference uiReference =
bundleContext.getServiceReference(UIService.class.getName());
uiService =
(UIService) bundleContext
.getService(uiReference);
}
return uiService;
}
/**
* Returns resource service.
* @return the resource service.
*/
public static ResourceManagementService getResources()
{
if (resourcesService == null)
{
ServiceReference serviceReference = bundleContext
.getServiceReference(ResourceManagementService.class.getName());
if(serviceReference == null)
return null;
resourcesService = (ResourceManagementService) bundleContext
.getService(serviceReference);
}
return resourcesService;
}
/**
* Returns a reference to a ConfigurationService implementation currently
* registered in the bundle context or null if no such implementation was
* found.
*
* @return a currently valid implementation of the ConfigurationService.
*/
public static ConfigurationService getConfigurationService()
{
if (configurationService == null)
{
ServiceReference confReference
= bundleContext.getServiceReference(
ConfigurationService.class.getName());
configurationService
= (ConfigurationService) bundleContext
.getService(confReference);
}
return configurationService;
}
/**
* Returns the <tt>NotificationService</tt> obtained from the bundle context.
*
* @return the <tt>NotificationService</tt> obtained from the bundle context
*/
public static NotificationService getNotificationService()
{
if (notificationService == null)
{
ServiceReference serviceReference = bundleContext
.getServiceReference(NotificationService.class.getName());
notificationService = (NotificationService) bundleContext
.getService(serviceReference);
notificationService.registerDefaultNotificationForEvent(
NETWORK_NOTIFICATIONS,
NotificationAction.ACTION_POPUP_MESSAGE,
null,
null);
}
return notificationService;
}
/**
* When new protocol provider is registered we add needed listeners.
*
* @param serviceEvent ServiceEvent
*/
public void serviceChanged(ServiceEvent serviceEvent)
{
ServiceReference serviceRef = serviceEvent.getServiceReference();
// if the event is caused by a bundle being stopped, we don't want to
// know we are shutting down
if (serviceRef.getBundle().getState() == Bundle.STOPPING)
{
return;
}
Object sService = bundleContext.getService(serviceRef);
if(sService instanceof NetworkAddressManagerService)
{
switch (serviceEvent.getType())
{
case ServiceEvent.REGISTERED:
if(this.networkAddressManagerService != null)
break;
this.networkAddressManagerService =
(NetworkAddressManagerService)sService;
networkAddressManagerService
.addNetworkConfigurationChangeListener(this);
break;
case ServiceEvent.UNREGISTERING:
((NetworkAddressManagerService)sService)
.removeNetworkConfigurationChangeListener(this);
break;
}
return;
}
// we don't care if the source service is not a protocol provider
if (!(sService instanceof ProtocolProviderService))
return;
switch (serviceEvent.getType())
{
case ServiceEvent.REGISTERED:
this.handleProviderAdded((ProtocolProviderService)sService);
break;
case ServiceEvent.UNREGISTERING:
this.handleProviderRemoved( (ProtocolProviderService) sService);
break;
}
}
/**
* Add listeners to newly registered protocols.
*
* @param provider ProtocolProviderService
*/
private void handleProviderAdded(ProtocolProviderService provider)
{
if (logger.isTraceEnabled())
logger.trace("New protocol provider is comming "
+ provider.getProtocolName());
provider.addRegistrationStateChangeListener(this);
}
/**
* Stop listening for events as the provider is removed.
* Providers are removed this way only when there are modified
* in the configuration. So as the provider is modified we will erase
* every instance we got.
*
* @param provider the ProtocolProviderService that has been unregistered.
*/
private void handleProviderRemoved(ProtocolProviderService provider)
{
if (logger.isTraceEnabled())
logger.trace("Provider modified forget every instance of it");
if(hasAtLeastOneSuccessfulConnection(provider))
{
setAtLeastOneSuccessfulConnection(provider, false);
}
provider.removeRegistrationStateChangeListener(this);
autoReconnEnabledProviders.remove(provider);
needsReconnection.remove(provider);
if(currentlyReconnecting.containsKey(provider))
{
currentlyReconnecting.remove(provider).cancel();
}
}
/**
* Fired when a change has occurred in the computer network configuration.
*
* @param event the change event.
*/
public synchronized void configurationChanged(ChangeEvent event)
{
if(event.getType() == ChangeEvent.IFACE_UP)
{
// no connection so one is up, lets connect
if(connectedInterfaces.isEmpty())
{
Iterator<ProtocolProviderService> iter =
needsReconnection.iterator();
while (iter.hasNext())
{
ProtocolProviderService pp = iter.next();
if(currentlyReconnecting.containsKey(pp))
{
// now lets cancel it and schedule it again
// so it will use this iface
currentlyReconnecting.remove(pp).cancel();
}
reconnect(pp);
}
needsReconnection.clear();
}
connectedInterfaces.add((String)event.getSource());
}
else if(event.getType() == ChangeEvent.IFACE_DOWN)
{
String ifaceName = (String)event.getSource();
connectedInterfaces.remove(ifaceName);
// one is down and at least one more is connected
if(connectedInterfaces.size() > 0)
{
// lets reconnect all that was connected when this one was
// available, cause they maybe using it
Iterator<Map.Entry<ProtocolProviderService, List<String>>> iter =
autoReconnEnabledProviders.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry<ProtocolProviderService, List<String>> entry
= iter.next();
if(entry.getValue().contains(ifaceName))
{
ProtocolProviderService pp = entry.getKey();
// hum someone is reconnecting, lets cancel and
// schedule it again
if(currentlyReconnecting.containsKey(pp))
{
currentlyReconnecting.remove(pp).cancel();
}
reconnect(pp);
}
}
}
else
{
// we must disconnect every pp and put all to be need of reconnecting
needsReconnection.addAll(autoReconnEnabledProviders.keySet());
// there can by and some that are currently going to reconnect
// must take care of them too, cause there is no net and they won't succeed
needsReconnection.addAll(currentlyReconnecting.keySet());
Iterator<ProtocolProviderService> iter =
needsReconnection.iterator();
while (iter.hasNext())
{
ProtocolProviderService pp = iter.next();
// if provider is scheduled for reconnect,
// cancel it there is no network
if(currentlyReconnecting.containsKey(pp))
{
currentlyReconnecting.remove(pp).cancel();
}
// don't reconnect just unregister if needed.
unregister(pp, false, null, null);
}
connectedInterfaces.clear();
if (logger.isTraceEnabled())
logger.trace("Network is down!");
notify("", "plugin.reconnectplugin.NETWORK_DOWN", new String[0]);
}
}
if(logger.isTraceEnabled())
{
logger.trace("Event received " + event
+ " src=" + event.getSource());
traceCurrentPPState();
}
}
/**
* Unregisters the ProtocolProvider. Make sure to do it in separate thread
* so we don't block other processing.
* @param pp the protocol provider to unregister.
* @param reconnect if the protocol provider does not need unregistering
* shall we trigger reconnect. Its true when call called from
* reconnect.
* @param listener the listener used in reconnect method.
* @param task the task to use for reconnection.
*/
private void unregister(final ProtocolProviderService pp,
final boolean reconnect,
final RegistrationStateChangeListener listener,
final ReconnectTask task)
{
unregisteredProviders.add(pp);
new Thread(new Runnable()
{
public void run()
{
try
{
// getRegistrationState() for some protocols(icq) can trigger
// registrationStateChanged so make checks here
// to prevent synchronize in registrationStateChanged
// and deadlock
if(pp.getRegistrationState().equals(
RegistrationState.UNREGISTERING)
|| pp.getRegistrationState().equals(
RegistrationState.UNREGISTERED)
|| pp.getRegistrationState().equals(
RegistrationState.CONNECTION_FAILED))
{
if(reconnect)
{
if(listener != null)
pp.removeRegistrationStateChangeListener(
listener);
if(timer == null || task == null)
return;
// cancel any existing task before overriding it
if(currentlyReconnecting.containsKey(pp))
currentlyReconnecting.remove(pp).cancel();
currentlyReconnecting.put(pp, task);
if (logger.isTraceEnabled())
logger.trace("Reconnect " + pp +
" after " + task.delay + " ms.");
timer.schedule(task, task.delay);
}
return;
}
pp.unregister();
}
catch(Throwable t)
{
logger.error("Error unregistering pp:" + pp, t);
}
}
}).start();
}
/**
* Trace prints of current status of the lists with protocol providers,
* that are currently in interest of the reconnect plugin.
*/
private void traceCurrentPPState()
{
logger.trace("connectedInterfaces: " + connectedInterfaces);
logger.trace("autoReconnEnabledProviders: "
+ autoReconnEnabledProviders.keySet());
logger.trace("currentlyReconnecting: "
+ currentlyReconnecting.keySet());
logger.trace("needsReconnection: " + needsReconnection);
logger.trace("unregisteredProviders: " + unregisteredProviders);
logger.trace("----");
}
/**
* Sends network notification.
* @param title the title.
* @param i18nKey the resource key of the notification.
* @param params and parameters in any.
*/
private void notify(String title, String i18nKey, String[] params)
{
getNotificationService().fireNotification(
NETWORK_NOTIFICATIONS,
title,
getResources().getI18NString(i18nKey, params),
null);
}
/**
* The method is called by a <code>ProtocolProviderService</code>
* implementation whenever a change in the registration state of the
* corresponding provider had occurred.
*
* @param evt the event describing the status change.
*/
public void registrationStateChanged(RegistrationStateChangeEvent evt)
{
// we don't care about protocol providers that don't support
// reconnection and we are interested only in few state changes
if(!(evt.getSource() instanceof ProtocolProviderService)
||
!(evt.getNewState().equals(RegistrationState.REGISTERED)
|| evt.getNewState().equals(RegistrationState.UNREGISTERED)
|| evt.getNewState().equals(RegistrationState.CONNECTION_FAILED)))
return;
synchronized(this) {
try
{
ProtocolProviderService pp = (ProtocolProviderService)evt.getSource();
if(evt.getNewState().equals(RegistrationState.CONNECTION_FAILED))
{
if(!hasAtLeastOneSuccessfulConnection(pp))
{
// ignore providers which haven't registered successfully
// till now, they maybe misconfigured
//String notifyMsg;
if(evt.getReasonCode() ==
RegistrationStateChangeEvent.REASON_NON_EXISTING_USER_ID)
{
notify(
getResources().getI18NString("service.gui.ERROR"),
"service.gui.NON_EXISTING_USER_ID",
new String[]{pp.getAccountID().getService()});
}
else
{
notify(
getResources().getI18NString("service.gui.ERROR"),
"plugin.reconnectplugin.CONNECTION_FAILED_MSG",
new String[]
{ pp.getAccountID().getUserID(),
pp.getAccountID().getService() });
}
return;
}
// if this pp is already in needsReconnection, it means
// we got conn failed cause the pp has tried to unregister
// with sending network packet
// but this unregister is scheduled from us so skip
if(needsReconnection.contains(pp))
return;
if(connectedInterfaces.isEmpty())
{
needsReconnection.add(pp);
if(currentlyReconnecting.containsKey(pp))
currentlyReconnecting.remove(pp).cancel();
}
else
{
// network is up but something happen and cannot reconnect
// strange lets try again after some time
reconnect(pp);
}
// unregister can finish and with connection failed,
// the protocol is unable to unregister
unregisteredProviders.remove(pp);
if(logger.isTraceEnabled())
{
logger.trace("Got Connection Failed for " + pp);
traceCurrentPPState();
}
}
else if(evt.getNewState().equals(RegistrationState.REGISTERED))
{
if(!hasAtLeastOneSuccessfulConnection(pp))
{
setAtLeastOneSuccessfulConnection(pp, true);
}
autoReconnEnabledProviders.put(
pp,
new ArrayList<String>(connectedInterfaces));
if(currentlyReconnecting.containsKey(pp))
currentlyReconnecting.remove(pp).cancel();
unregisteredProviders.remove(pp);
if(logger.isTraceEnabled())
{
logger.trace("Got Registered for " + pp);
traceCurrentPPState();
}
}
else if(evt.getNewState().equals(RegistrationState.UNREGISTERED))
{
autoReconnEnabledProviders.remove(pp);
if(!unregisteredProviders.contains(pp)
&& currentlyReconnecting.containsKey(pp))
{
currentlyReconnecting.remove(pp).cancel();
}
unregisteredProviders.remove(pp);
if(logger.isTraceEnabled())
{
logger.trace("Got Unregistered for " + pp);
traceCurrentPPState();
}
}
}
catch(Throwable ex)
{
logger.error("Error dispatching protocol registration change", ex);
}
}
}
/**
* Method to schedule a reconnect for a protocol provider.
* @param pp the provider.
*/
private void reconnect(final ProtocolProviderService pp)
{
long delay;
if(currentlyReconnecting.containsKey(pp))
{
delay = currentlyReconnecting.get(pp).delay;
// we never stop trying
//if(delay == MAX_RECONNECT_DELAY*1000)
// return;
delay = Math.min(delay * 2, MAX_RECONNECT_DELAY*1000);
}
else
{
delay = (long)(RECONNECT_DELAY_MIN
+ Math.random() * RECONNECT_DELAY_MAX)*1000;
}
final ReconnectTask task = new ReconnectTask(pp);
task.delay = delay;
// start registering after the pp has unregistered
RegistrationStateChangeListener listener =
new RegistrationStateChangeListener()
{
public void registrationStateChanged(RegistrationStateChangeEvent evt)
{
if(evt.getSource() instanceof ProtocolProviderService)
{
if(evt.getNewState().equals(
RegistrationState.UNREGISTERED)
|| evt.getNewState().equals(
RegistrationState.CONNECTION_FAILED))
{
synchronized(this)
{
pp.removeRegistrationStateChangeListener(this);
if(timer == null)
return;
if(connectedInterfaces.size() == 0)
{
// well there is no network we just need
// this provider in needs reconnection when
// there is one
// means we started unregistering while
// network was going down and meanwhile there
// were no connected interface, this happens
// when we have more than one connected
// interface and we got 2 events for down iface
needsReconnection.add(pp);
if(currentlyReconnecting.containsKey(pp))
currentlyReconnecting.remove(pp).cancel();
return;
}
// cancel any existing task before overriding it
if(currentlyReconnecting.containsKey(pp))
currentlyReconnecting.remove(pp).cancel();
currentlyReconnecting.put(pp, task);
if (logger.isTraceEnabled())
logger.trace("Reconnect " + pp +
" after " + task.delay + " ms.");
timer.schedule(task, task.delay);
}
}
else if(evt.getNewState().equals(
RegistrationState.REGISTERED))
{
pp.removeRegistrationStateChangeListener(this);
}
}
}
};
pp.addRegistrationStateChangeListener(listener);
// as we will reconnect, lets unregister
unregister(pp, true, listener, task);
}
/**
* The task executed by the timer when time for reconnect comes.
*/
private class ReconnectTask
extends TimerTask
{
/**
* The provider to reconnect.
*/
private ProtocolProviderService provider;
/**
* The delay with which was this task scheduled.
*/
private long delay;
/**
* The thread to execute this task.
*/
private Thread thread = null;
/**
* Creates the task.
*
* @param provider the <tt>ProtocolProviderService</tt> to reconnect
*/
public ReconnectTask(ProtocolProviderService provider)
{
this.provider = provider;
}
/**
* Reconnects the provider.
*/
public void run()
{
if(thread == null || !Thread.currentThread().equals(thread))
{
thread = new Thread(this);
thread.start();
}
else
{
try
{
if (logger.isInfoEnabled())
logger.info("Start reconnecting "
+ provider.getAccountID().getDisplayName());
provider.register(
getUIService().getDefaultSecurityAuthority(provider));
} catch (OperationFailedException ex)
{
logger.error("cannot re-register provider will keep going",
ex);
}
}
}
}
/**
* Check does the supplied protocol has the property set for at least
* one successful connection.
* @param pp the protocol provider
* @return true if property exists.
*/
private boolean hasAtLeastOneSuccessfulConnection(ProtocolProviderService pp)
{
String value = (String)getConfigurationService().getProperty(
ATLEAST_ONE_CONNECTION_PROP + "."
+ pp.getAccountID().getAccountUniqueID());
if(value == null || !value.equals(Boolean.TRUE.toString()))
return false;
else
return true;
}
/**
* Changes the property about at least one successful connection.
* @param pp the protocol provider
* @param value the new value true or false.
*/
private void setAtLeastOneSuccessfulConnection(
ProtocolProviderService pp, boolean value)
{
getConfigurationService().setProperty(
ATLEAST_ONE_CONNECTION_PROP + "."
+ pp.getAccountID().getAccountUniqueID(),
Boolean.valueOf(value).toString());
}
}
|