com.backendless.rt.RTServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk-common Show documentation
Show all versions of java-sdk-common Show documentation
Provides access to Backendless API
The newest version!
package com.backendless.rt;
import com.backendless.BackendlessInjector;
import com.backendless.async.callback.Fault;
import com.backendless.async.callback.Result;
import com.backendless.exceptions.BackendlessFault;
import java.util.concurrent.CopyOnWriteArrayList;
public class RTServiceImpl implements RTService
{
private static final RTServiceImpl instance = new RTServiceImpl();
public static RTServiceImpl getInstance()
{
return instance;
}
private final RTClient rtClient = BackendlessInjector.getInstance().getRtClient();
private final CopyOnWriteArrayList> connectListeners = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList> disconnectListeners = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList> reconnectListeners = new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList connectErrorListeners = new CopyOnWriteArrayList<>();
private RTServiceImpl()
{
if( rtClient.isAvailable() )
{
rtClient.setConnectEventListener( new Result()
{
@Override
public void handle( Void result )
{
RTServiceImpl.this.handle( result, connectListeners );
}
} );
rtClient.setDisconnectEventListener( new Result()
{
@Override
public void handle( String result )
{
RTServiceImpl.this.handle( result, disconnectListeners );
}
} );
rtClient.setConnectErrorEventListener( new Fault()
{
@Override
public void handle( BackendlessFault fault )
{
RTServiceImpl.this.handle( fault, connectErrorListeners );
}
} );
rtClient.setReconnectAttemptEventListener( new Result()
{
@Override
public void handle( ReconnectAttempt result )
{
RTServiceImpl.this.handle( result, reconnectListeners );
}
} );
}
}
@Override
public void addConnectListener( Result callback )
{
if( rtClient.isConnected() )
callback.handle( null );
connectListeners.add( callback );
}
@Override
public void addReconnectAttemptListener( Result callback )
{
reconnectListeners.add( callback );
}
@Override
public void addConnectErrorListener( Fault fault )
{
connectErrorListeners.add( fault );
}
@Override
public void removeConnectionListeners()
{
connectListeners.clear();
}
@Override
public void addDisconnectListener( Result callback )
{
disconnectListeners.add( callback );
}
@Override
@SuppressWarnings( "unchecked" )
public void removeListener( T callback )
{
if( connectErrorListeners.contains( callback ) )
{
removeEventListeners( (Fault) callback, connectErrorListeners );
}
else if( disconnectListeners.contains( callback ) )
{
removeEventListeners( callback, disconnectListeners );
}
else if( connectListeners.contains( callback ) )
{
removeEventListeners( callback, connectListeners );
}
else if( reconnectListeners.contains( callback ) )
{
removeEventListeners( callback, reconnectListeners );
}
}
@Override
public void connect()
{
rtClient.connect();
}
@Override
public void disconnect()
{
rtClient.disconnect();
}
private void removeEventListeners( T callback, CopyOnWriteArrayList listeners )
{
listeners.remove( callback );
}
@SuppressWarnings( "unchecked" )
private void handle( T result, CopyOnWriteArrayList listeners )
{
for( E listener : listeners )
{
listener.handle( result );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy