com.backendless.rt.MethodRequestHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of backendless Show documentation
Show all versions of backendless Show documentation
Android SDK used by developers to provide Backendless API in apps.
package com.backendless.rt;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;
import weborb.exceptions.AdaptingException;
import weborb.types.IAdaptingType;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.logging.Level;
import java.util.logging.Logger;
public abstract class MethodRequestHelper
{
private static final Logger logger = Logger.getLogger( "MethodRequestHelper" );
private final RTClient rtClient = RTClientFactory.get();
private final ConcurrentLinkedDeque methodsToSend = new ConcurrentLinkedDeque<>();
private final ConnectListener connectListener;
protected MethodRequestHelper( ConnectListener connectListener )
{
this.connectListener = connectListener;
}
public abstract RTMethodRequest createMethodRequest( RTCallback rtCallback );
public void connected()
{
RTMethodRequest methodRequest = methodsToSend.poll();
while( methodRequest != null )
{
rtClient.invoke( methodRequest );
methodRequest = methodsToSend.poll();
}
}
public void invoke( final AsyncCallback callback )
{
logger.log( Level.FINE, "Send invocation with options" );
RTMethodRequest rtMethodRequest = createMethodRequest( new RTCallbackWithFault()
{
@Override
public AsyncCallback usersCallback()
{
return callback;
}
@Override
public void handleResponse( IAdaptingType response )
{
logger.info( "invocation sent" );
if( callback != null )
callback.handleResponse( null );
}
} );
invoke( rtMethodRequest );
}
public void invoke( final Class tClass, final AsyncCallback callback )
{
logger.log( Level.FINE, "Send invocation with options" );
RTMethodRequest rtMethodRequest = createMethodRequest( new RTCallbackWithFault()
{
@Override
public AsyncCallback usersCallback()
{
return callback;
}
@Override
public void handleResponse( IAdaptingType response )
{
logger.info( "got result" );
if( callback != null )
{
if(response == null)
{
callback.handleResponse( null );
}
else
{
try
{
callback.handleResponse( (T) response.adapt( tClass ) );
}
catch( AdaptingException e )
{
callback.handleFault( new BackendlessFault( e.getMessage() ) );
}
}
}
}
} );
invoke( rtMethodRequest );
}
public void invokeWithDefaultAdapt( final AsyncCallback