Use listener instead of polling to delay handling a URI

fix-message-formatting
Ingo Bauersachs 11 years ago committed by Daniel Pocock
parent d0d881bc61
commit 6b1f3bbdb3

@ -7,7 +7,6 @@
import java.text.*; import java.text.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*;
import net.java.sip.communicator.service.argdelegation.*; import net.java.sip.communicator.service.argdelegation.*;
import net.java.sip.communicator.service.gui.*; import net.java.sip.communicator.service.gui.*;
@ -77,12 +76,6 @@ public class UriHandlerSipImpl
*/ */
private List<String> uris; private List<String> uris;
/**
* ExecutorService used to schedule a dialing attempt after a delay.
*/
private static final ScheduledExecutorService worker =
Executors.newSingleThreadScheduledExecutor();
/** /**
* Creates an instance of this uri handler, so that it would start handling * Creates an instance of this uri handler, so that it would start handling
* URIs by passing them to the providers registered by <tt>protoFactory</tt> * URIs by passing them to the providers registered by <tt>protoFactory</tt>
@ -284,7 +277,7 @@ public void handleUri(String uri)
} }
} }
ProtocolProviderService provider; final ProtocolProviderService provider;
try try
{ {
provider = selectHandlingProvider(uri); provider = selectHandlingProvider(uri);
@ -315,62 +308,47 @@ public void handleUri(String uri)
{ {
// Allow a grace period for the provider to register in case // Allow a grace period for the provider to register in case
// we have just started up // we have just started up
DelayedHandler task = new DelayedHandler(uri, provider); final DelayRegistrationStateChangeListener listener =
task.schedule(); new DelayRegistrationStateChangeListener(uri, provider);
provider.addRegistrationStateChangeListener(listener);
new Timer().schedule(new TimerTask()
{
@Override
public void run()
{
provider.removeRegistrationStateChangeListener(listener);
}
}, 2000);
} }
} }
/** /**
* This class is a Runnable for checking if the provider is ready * Listener on provider state changes that handles the passed URI if the
* (which may take a second or two after startup) rather * provider becomes registered.
* than immediately failing the dialing attempt with an error.
*/ */
private class DelayedHandler private class DelayRegistrationStateChangeListener
implements Runnable implements RegistrationStateChangeListener
{ {
final static int RETRIES = 8;
final static int RETRY_DELAY = 250;
private String uri; private String uri;
private ProtocolProviderService provider; private ProtocolProviderService provider;
int retries = RETRIES; private boolean handled = false;
/** public DelayRegistrationStateChangeListener(String uri,
* Initialize this Runnable, storing the values needed ProtocolProviderService provider)
* when the timeout occurs.
*
* @param uri the URI to try and dial
* @param provider the provider to use
*/
public DelayedHandler(String uri, ProtocolProviderService provider)
{ {
this.uri = uri; this.uri = uri;
this.provider = provider; this.provider = provider;
} }
/** @Override
* Tells the Runnable to schedule itself public void registrationStateChanged(RegistrationStateChangeEvent evt)
*/
public void schedule()
{ {
worker.schedule(this, RETRY_DELAY, TimeUnit.MILLISECONDS); if (evt.getNewState() == RegistrationState.REGISTERED && !handled)
}
/**
* Executed after the timeout elapses.
*
* Checks if the registration is complete or if we had enough retries
* and then tries to dial.
*/
public void run() {
if(retries-- == 0 ||
provider.getRegistrationState() == RegistrationState.REGISTERED)
{ {
provider.removeRegistrationStateChangeListener(this);
handled = true;
handleUri(uri, provider); handleUri(uri, provider);
return;
} }
// retry after another delay
schedule();
} }
} }

Loading…
Cancel
Save