Math Tutor app

The math tutor app exposes mathematics functions using dataBridges rpc feature. The math tutor app will create an RPC server called MathServer and will expose math functions inside.

dataBridges supports 2 types of RPC interactions

  • 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 Math app will be exposing 2 math function as Unary RPC ( math.add, math.multiply) and 1 math function as server streaming RPC (AllMathFunction)

Exposing RPC server is supported only by dataBridges server library. Hence Tutor will be using databridges-sio-server-lib. dataBridges server sdk for NodeJS, Python, .Net allows you to design powerful realtime backend applications. These applications have access to extra dataBridges features than their corresponding client SDK's. Refer to https://dev.databridges.io/rpc/rpc.concepts.html for concepts.

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_tutor_app by clicking on this link

Dependencies

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

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

Rename Program.cs to math_tutor_app.cs .

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

/*
    The math tutor app exposes mathematics functions using dataBridges rpc feature.
    The math tutor app will create an RPC server called MathServer and will expose math functions inside.

    dataBridges supports 2 types of RPC interactions
        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 Math app will be exposing 2 math function as Unary RPC ( math.add, math.multiply) and 1 math function as server streaming RPC (AllMathFunction)


    Important Note : 
    Exposing RPC server is supported only by dataBridges server library. Hence Tutor will be using databridges-sio-server-lib.
    dataBridges server sdk for NodeJS, Python, .Net allows you to design powerful realtime backend applications. 
    These applications have access to extra dataBridges features than their corresponding client SDK's.

    Refer to https://dev.databridges.io/rpc/rpc.concepts.html for concepts.

*/

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Text;

namespace math.tutor
{
    class CdBridgeServer
    {
        // Include dataBridges server library package
        public dBridges.dBridges dbridge;
        public dBridges.remoteprocedure.Crpcserver mathTutor;

        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();
        }

        // Unary RPC: the client sends a single request and receives a single response.
        public async void add(object inparam, object response)
        {
            dBridges.Utils.dBParams parameter = inparam as dBridges.Utils.dBParams;
            dBridges.responseHandler.CResponseHandler responsehandler = response as dBridges.responseHandler.CResponseHandler;
            dynamic mparameter = JsonConvert.DeserializeObject(parameter.inparama);
            float a = mparameter["a"];
            float b = mparameter["b"];
            Console.WriteLine("Received add for {0}  and  {1}", a, b);

            Console.WriteLine("Sending {0}  +  {1}", a, b);
            await responsehandler.end((a+b).ToString());
        }

        // Unary RPC: the client sends a single request and receives a single response.
        public async void multiply(object inparam, object response)
        {
            dBridges.Utils.dBParams parameter = inparam as dBridges.Utils.dBParams;
            dBridges.responseHandler.CResponseHandler responsehandler = response as dBridges.responseHandler.CResponseHandler;
            dynamic mparameter = JsonConvert.DeserializeObject(parameter.inparama); 
            float a = mparameter["a"];
            float b = mparameter["b"];
            Console.WriteLine("Received multiply for {0}  and  {1}", a, b);

            Console.WriteLine("Sending {0}  x  {1}", a, b);
            await responsehandler.end((a*b).ToString());
        }

        // Server streaming RPC: the client sends a single request and in return, the server sends a stream of messages.
        public async void AllMathFunction(object inparam, object response)
        {
            dBridges.Utils.dBParams parameter = inparam as dBridges.Utils.dBParams;
            dBridges.responseHandler.CResponseHandler responsehandler = response as dBridges.responseHandler.CResponseHandler;
            dynamic mparameter = JsonConvert.DeserializeObject(parameter.inparama); 
            float a = mparameter["a"];
            float b = mparameter["b"];
            Console.WriteLine("Received AllMathFunction for  {0}  and  {1}", a, b);

            Console.WriteLine("Sending {0}  +  {1}", a, b);
            await responsehandler.next( a + " + " + b + " = " + (a + b).ToString() + " from " + this.sessionID);
            await Task.Delay(100);

            Console.WriteLine("Sending {0}  -  {1}", a, b);
            await responsehandler.next( a + " - " + b + " = " + (a - b).ToString() + " from " + this.sessionID);
            await Task.Delay(100);

            Console.WriteLine("Sending {0}  x  {1}", a, b);
            await responsehandler.next(a + " x " + b + " = " + (a * b).ToString() + " from " + this.sessionID);
            await Task.Delay(100);

            Console.WriteLine("Sending {0}  /  {1}", a, b);
            await responsehandler.end(a + " / " + b + " = " + (a / b).ToString() + " from " + this.sessionID);
        }

        // Define all exposed functions.
        public void mathfunction(object eventInfo)
        {
            Action<object, object> iadd = this.add;
            Action<object, object> imultiply = this.multiply;
            Action<object, object> iAllMathFunction = this.AllMathFunction;

            // Bind the exposed function to mathTutor RPC server.
            (eventInfo as dBridges.remoteprocedure.Crpcserver).regfn("add", iadd);
            (eventInfo as dBridges.remoteprocedure.Crpcserver).regfn("multiply", imultiply);
            (eventInfo as dBridges.remoteprocedure.Crpcserver).regfn("AllMathFunction", iAllMathFunction);
        }

        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/server/v1/authenticate
            this.dbridge.appkey = "____appKey______";

            // Replace your application secret below, which you have received from dataBridges management portal.
            this.dbridge.appsecret = "____appSecret______";

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

            // Initialize RPC Server Maths.
            this.mathTutor = dbridge.rpc.init("MathTutor");

            // Define all exposed functions.
            Action<object> ifunctions = this.mathfunction;
            this.mathTutor.functions = ifunctions;

            Action<object> connected_callback;
            this.dbridge.connectionstate.bind("connected", connected_callback = async (object eventinfo) => {
                try
                {
                    // Register RPC Server with dBrdige network.
                    await this.mathTutor.register();
                }
                catch (dBridges.exceptions.dBError er)
                {
                    Console.WriteLine("MathServer registration with dataBridges network faced an exception : {0} , {1} , {2} ", er.source, er.code, er.message);
                }

                Action<object, object> rconnectonline;
                // Bind to server.online, This will be triggered When MathServer is ready to serve.
                this.mathTutor.bind("dbridges:rpc.server.online", rconnectonline = (object payload, object metadata) => {
                    Console.WriteLine("MathServer is now Online and ready for Math function processing ...");
                });

                Action<object, object> rconnectfail;
                // Bind to server.registration.fail, This will be triggered When any registration issue is raised.
                this.mathTutor.bind("dbridges:rpc.server.registration.fail", rconnectfail = (object payload, object metadata) => {
                    dBridges.exceptions.dBError perror = payload as dBridges.exceptions.dBError;
                    Console.WriteLine("MathServer registration with dataBridges network faced an exception : {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)
        {
            CdBridgeServer db = new CdBridgeServer();
            Task.Run(db.init_dataBridges).Wait();
        }
    }
}

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