Math Student app
The student.app consumes math functions exposed by MathTutor app.
You need to run the math tutor app [ NodeJS, Python, .NET].If you run multiple instances of math tutor app, dataBridges will automatically load-balance the rpc.function calls.
The student.app uses dataBridges RPC (call-response), functionality.
- Unary RPC: the client sends a single request and receives a single response.
- Server streaming RPC: the client sends a single request and in return, the server sends a stream of messages.
The student app will be consuming 2 Unary RPC functions :-> add, multiple. The student app will consume a single server streaming RPC function :-> AllMathFunctions and will receive 4 math operations reply.
We will be using io.databridges:databridges-sio-java-client in student app.
Before trying this sample, follow the Setup and initialization . For more information, see the RPC | request-response API reference documentation.
Download source
Download the math_student_app by clicking on this link
Dependencies
We need to install the io.databridges:databridges_sio_java_client dependencies for our app. Open your app-module build.gradle
file and add these:
Create a class inside app/java/com.example.mathstudentapp
and name it as math_student_app
.
Copy paste below code and you are ready to run this example.
package example.com.mathstudentapp;
/*
The student.app consumes math functions exposed by MathTutor app.
The student.app uses dataBridges RPC (call-response), functionality.
You need to run the math tutor app [ NodeJS, Python, .NET].
If you run multiple instances of math tutor app, dataBridges will automatically load-balance the rpc.function calls.
dataBridges supports 2 types of RPC interactions
Unary RPC: the client sends a request and receives a single response.
Server streaming RPC: the client sends a request and in return, the receives multiple responses.
The student app will be consuming 2 Unary RPC functions :-> add, multiple
consume a single server streaming RPC function :-> AllMathFunctions
and will receive 4 math operations reply.
We will be using io.databridges.databridges_sio_java_client in student app.
*/
import android.util.Log;
import java.util.HashMap;
import java.util.Map;
// Include dataBridges client library package
import io.databridges.databridges_sio_java_client.callbacks.connectionHandler;
import io.databridges.databridges_sio_java_client.dBridges;
import io.databridges.databridges_sio_java_client.exception.dBError;
import io.databridges.databridges_sio_java_client.rpc.rpCaller;
import io.databridges.databridges_sio_java_client.callbacks.ServerEventHandler;
import io.databridges.databridges_sio_java_client.utils.serverMetaData;
import io.databridges.databridges_sio_java_client.callbacks.callResponse;
public class math_student_app {
dBridges dbridge;
rpCaller mathFunctions;
public String random(int maxlength) {
Random generator = new Random();
StringBuilder randomStringBuilder = new StringBuilder();
char tempChar;
for (int i = 0; i < maxlength; i++){
tempChar = (char) (generator.nextInt(96) + 32);
randomStringBuilder.append(tempChar);
}
return randomStringBuilder.toString();
}
public void Execute()
{
// Initialize dataBridges client
dbridge = new dBridges();
// Replace your application key below, which you have received from dataBridges management portal.
dbridge.appkey = "____appKey______";
// Replace your authentication url below, which you have received from dataBridges management portal.
// example https://endpoint01.databridges.io/client/v1/authenticate
dbridge.auth_url = "_____URL_____";
//Bind to disconnected event, to get intimation about dataBridges network disconnection.
try {
dbridge.connectionstate.bind("disconnected", new connectionHandler() {
@Override
public void onEvent(Object message) {
Log.i("event:" , "Disconnected from dataBridges real-time network" );
}
});
} catch (dBError dberr){
String exception = "Binding to Disconnected event faced an exception :" + dberr.source + " " + dberr.code + " " + dberr.message;
Log.i("exception: " , exception );
}
try {
dbridge.connectionstate.bind("connected", new connectionHandler() {
@Override
public void onEvent(Object message) {
Log.i("event:" , "Connected to dataBridges real-time network !!!" );
try{
// We need to inform dataBridges network that the app want to consume 'mathTutor's functions.
mathFunctions = dbridge.rpc.connect("MathTutor");
}catch (dBError dberr){
// Connect errors will be handled here.
String exception = "Connecting to MathTutor RPC server faced an exception :" + dberr.source + " " + dberr.code + " " + dberr.message;
Log.i("exception: " , exception );
}
if (mathFunctions != null){
try{
// We will bind to rpc server connection success event. On success, we will call remote functions.
mathFunctions.bind("dbridges:rpc.server.connect.success", new ServerEventHandler() {
@Override
public void onEvent(Object message, serverMetaData metadata) {
Log.i("event:" , "dataBridges granted access to MathTutor RPC server" );
Map<String, Float> mydata = new HashMap<String, Float>();
// Create Random numbers for "a" and "b"
mydata.put("a" , Math.floor(Math.random() * 100) + 1);
mydata.put("b", Math.floor(Math.random() * 100) + 1);
Gson gson = new Gson();
String bodyInStringFormat = gson.toJson(mydata);
// Unary RPC: The client sends a single request and receives a single response.
mathFunctions.call("add", bodyInStringFormat, 1000 * 60 * 2, new callResponse() {
@Override
public void onResult(String response, boolean isEnd) {
Log.i("event:" ,"Received response for mathFunctions.call(add) :" + response);
}
// Catch exception for .call
@Override
public void onError(dBError dberror) {
Log.d("exception:", "Error was encountered while executing mathFunctions.call(add) :" + dberror.source + " " + dberror.code + " " + dberror.message);
}
});
// Unary RPC: The client sends a single request and receives a single response.
mathFunctions.call("multiply", bodyInStringFormat, 1000 * 60 * 2, new callResponse() {
@Override
public void onResult(String response, boolean isEnd) {
Log.i("event:" ,"Received response for mathFunctions.call(multiply) :" + response);
}
// Catch exception for .call
@Override
public void onError(dBError dberror) {
Log.d("exception:", "Error was encountered while executing mathFunctions.call(multiply)" + dberror.source + " " + dberror.code + " " + dberror.message);
}
});
// Server streaming RPC: the client sends a single request and in return, the server sends a stream of messages.
mathFunctions.call("AllMathFunction", bodyInStringFormat, 1000 * 60 * 2, new callResponse() {
@Override
public void onResult(String response, boolean isEnd) {
if(!isEnd)
{
Log.i("event:", "FINAL response for mathFunctions.call(AllMathFunction) : " + response);
}else{
Log.i("event:", " Received response for mathFunctions.call(AllMathFunction) : " + response);
}
}
// Catch exception for .call
@Override
public void onError(dBError dberror) {
Log.d("Server streaming", "Error was encountered while executing mathFunctions.call(AllMathFunction) : " + dberror.source + " " + dberror.code + " " + dberror.message);
}
});
}
});
} catch (dBError dberr){
String exception = "Binding to MathTutor Connect Success event faced an exception :" + dberr.source + " " + dberr.code + " " + dberr.message ;
Log.i("exception: " , exception );
}
try{
// Bind to server.connect.fail event to understand if any issues in Math Server Connection.
mathFunctions.bind("dbridges:rpc.server.connect.fail", new ServerEventHandler() {
@Override
public void onEvent(Object message, serverMetaData metadata) {
if(message instanceof dBError) {
dBError dberr = (dBError) message;
String data ="MathTutor RPC server faced an issue : " + dberr.source + " " + dberr.code + " " + dberr.message;
Log.i("exception: " , data);
}
}
});
}catch (dBError dberr){
String exception = "Binding to MathTutor Connect Fail event faced an exception :" + dberr.source + " " + dberr.code + " " + dberr.message;
Log.i("exception: " , exception );
}
}
}
});
}catch (dBError dberr){
String exception = "Binding to dataBridges Connected event faced an exception :" + dberr.source + " " + dberr.code + " " + dberr.message;
Log.i("exception: " , exception );
}
try {
// Connect to dataBridges. If any runtime error it will be caught in catch().
dbridge.connect();
}catch (dBError dberr){
String exception = "dataBridges Connection exception.." + dberr.source + " " + dberr.code + " " + dberr.message;
Log.i("exception:" , exception );
}
}
}
Save your file and build your project in Release or Debug mode to test.