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 Databridges.Sio.Client.Lib in student app.

Before trying this sample, follow the Setup and initialization . For more information, see the Channels Pub/Sub API reference documentation.

Download source

Download the math_student_app by clicking on this link

Dependencies

For this application, you will require to use Databridges.Sio.Client.Lib (dataBridges Client Library).

  • Install dataBridges client library from "Manage NuGet Packages..." from the Solution -> Application Name context menu.

Rename Program.cs to math_student_app.cs.

Copy paste below code and you are ready to run this example.

/*
    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 Databridges.Sio.Client.Lib in student app.
*/


using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using RSG;
using Newtonsoft.Json;
using System.Text;

namespace cli.rpc.call
{
    class CdBridgeClient
    { 
        public dBridges.dBridges dbridge;
        public string sessionID;

        public  string RandomString(int size, bool lowerCase = false)
        {
            var builder = new StringBuilder(size);
            Random _random = new Random();
            char offset = lowerCase ? 'a' : 'A';
            const int lettersOffset = 26;
            for (var i = 0; i < size; i++)
            {
                var @char = (char)_random.Next(offset, offset + lettersOffset);
                builder.Append(@char);
            }
            return lowerCase ? builder.ToString().ToLower() : builder.ToString();
        }

        public async Task init_dataBridges()
        {
            // Initialize dataBridges client
            this.dbridge = new dBridges.dBridges();

            // Replace your authentication url below, which you have received from dataBridges management portal.
            this.dbridge.auth_url = "_____URL_____";

            // Replace your application key below, which you have received from dataBridges management portal.
            // example https://endpoint01.databridges.io/client/v1/authenticate
            this.dbridge.appkey = "____appKey______";

            // lets create a sessionID
            this.sessionID = this.RandomString(36).Substring(2, 7);
            Console.WriteLine("sessionID is " + sessionID);

            Action<object> connected_callback;
            this.dbridge.connectionstate.bind("connected", connected_callback = async (object eventinfo) => {
                Console.WriteLine("Connected to dataBridges real-time network !!!");

                dBridges.remoteprocedure.CrpCaller mathFunctions = null;
                try
                {
                    // We need to inform dataBridges network that the app want to consume 'mathTutor's functions.
                    mathFunctions = await dbridge.rpc.connect("MathTutor");
                }catch (dBridges.exceptions.dBError err)
                {
                    // Connect errors will be handled here.
                    Console.WriteLine("Connecting to MathTutor RPC server faced an exception : {0} {1} {2}", err.source, err.code, err.message);
                }

                // We will bind to rpc server connection success event. On success, we will call remote functions.
                Action<object, object> connetsuccess;
                mathFunctions.bind("dbridges:rpc.server.connect.success", connetsuccess = async (payload, metadata) =>{
                    Console.WriteLine("dataBridges granted access to MathTutor RPC server");
                    Action<object> iprogress , iprogressF;
                    Dictionary<string, float> funParam = new Dictionary<string, float>();
                    Random rand = new Random();

                    // Create Random numbers for "a" and "b"
                    funParam.Add("a",  (float)(rand.NextDouble()*100 + 1));
                    funParam.Add("b", (float)(rand.NextDouble()*100 + 1));

                    // Unary RPC: The client sends a single request and receives a single response.
                    IPromise<object> p = await mathFunctions.call("add", JsonConvert.SerializeObject(funParam), 1000 * 60 * 2, null);
                    p.Then((response) => {
                        Console.WriteLine("Received response for mathFunctions.call(add) : {0} ", response);
                    })
                    .Catch((err) => {
                        // Catch exception for .call
                        dBridges.exceptions.dBError er = err as dBridges.exceptions.dBError;
                        Console.WriteLine("Error was encountered while executing mathFunctions.call(add) : {0} , {1} , {2} ", er.source, er.code, er.message);
                    });

                    // Unary RPC: The client sends a single request and receives a single response.
                    IPromise<object> r = await mathFunctions.call("multiply", JsonConvert.SerializeObject(funParam), 1000 * 60 * 2, null);
                    r.Then((response) => {
                        Console.WriteLine("Received response for mathFunctions.call(multiply) : {0} ",  response);
                    })
                    .Catch((err) => {
                        // Catch exception for .call
                        dBridges.exceptions.dBError er = err as dBridges.exceptions.dBError;
                        Console.WriteLine("Error was encountered while executing mathFunctions.call(multiply) : {0} , {1} , {2} ", er.source, er.code, er.message);
                    });

                    // Server streaming RPC: the client sends a request and in return, the receives multiple responses.
                    IPromise<object> s = await mathFunctions.call("AllMathFunction", JsonConvert.SerializeObject(funParam), 1000 * 60 * 2, iprogressF = (response) => {
                        Console.WriteLine("Received response for mathFunctions.call(AllMathFunction) : {0} ",  response);
                    });
                    s.Then((response) => {
                        Console.WriteLine("FINAL response for mathFunctions.call(AllMathFunction) : {0} ",  response);
                    })
                    .Catch((err) => {
                        // Catch exception for .call
                        dBridges.exceptions.dBError er = err as dBridges.exceptions.dBError;
                        Console.WriteLine("Error was encountered while executing mathFunctions.call(AllMathFunction) : {0} , {1} , {2} ", er.source, er.code, er.message);
                    });
                });

                // Bind to server.connect.fail event to understand if any issues in Math Server Connection.
                Action<object, object> connetfail;
                mathFunctions.bind("dbridges:rpc.server.connect.fail", connetfail = (payload, metadata) => {
                    dBridges.exceptions.dBError perror = payload as dBridges.exceptions.dBError;
                    Console.WriteLine("MathTutor RPC server faced an issue : {0} {1} {2}", perror.source, perror.code, perror.message);
                });
            });

            //Bind to disconnected event, to get intimation about dataBridges network disconnection.
            Action<object> disconnected_callback;
            this.dbridge.connectionstate.bind("disconnected", disconnected_callback = (object eventinfo) => {
                Console.WriteLine("Disconnected from dataBridges real-time network");
            });

            // Connect to dataBridges. If any runtime error it will be caught in catch().
            try
            {
                await this.dbridge.connect();
                string ch = Console.ReadLine();
            }
            catch (dBridges.exceptions.dBError er)
            {
                Console.WriteLine("dataBridges Connection exception..  {0} , {1} , {2} ", er.source, er.code, er.message);
            }
        }
    } 

    class Program
    {
        static void Main(string[] args)
        {
            CdBridgeClient db = new CdBridgeClient();
            Task.Run(db.init_dataBridges).Wait();
        }
    }
}

Save your file and build your project in Release or Debug mode to test.