Search code examples
javascriptjavaapachethrift

Array with Apache Thrift using server Java & Javascript Client


im using Javascript to show info generated in Java. Im talking of 10,000 datas, all of them generated in Java and for porpouse of testing im using random values. Well i want to see this 10,000 values in my javascript page.

I already have a simple Server Java and a Client Javascript to share 2 doubles.

This is the Thrift file for 2 doubles:

namespace java test_thrift
service test{
    double number(1:double n1, 2:double n2)
}

This is the code of my Javascript Client

function calc() {
var transport = new Thrift.Transport("/service");
var protocol  = new Thrift.Protocol(transport);
var client    = new testClient(protocol);

var workbench = Math.random()*1000;
    try {
      result = client.number(workbench);
      $('#result').val(result);
      $('#result').css('color', 'black');
       document.getElementById("demo").innerHTML = result;
    } catch(ouch){
      $('#result').val(ouch.why);
      $('#result').css('color', 'red');
    }
}

Im sending a Random only to get the range of the return. Example: 1 return a value from 3 to 9, 2 return a 9 to 15 value, etc.

And in java i have a testHandler class:

public double number(double n1, double n2) throws TException {
    //System.out.println(n1 + " - " + n2);
    Random rnd = new Random();
    n1 = rnd.nextDouble() * 10 + 1;
    n2 = rnd.nextDouble() * 15 + 10;
    return n2;
}

Well this returns 1 value. And i want to see all in my Javascript Page. But with 10,000 elements. How can i do this?

Also i want to add that the final struct to share is like this:

 dis[10000][3]={
     ABC,12.5,13.5,
     ACD,14.4,11.5,
     .....ETC......}

Im stuck

Found this, but i dont know how to get it work :/

namespace java test_thrift

typedef list<double> Vector

struct test
{
    1:i32 rows,
    2:i32 cols,
    3:list<Vector> data,
}

Solution

  • So, i managed to fix it. Here's a Client JavasCript that make a reequest and a Java server Give back a 10000 x 18 Matrix.

    Thrift file:

    namespace java test_thrift

    struct Cell {
      1: string did
      2: double l_x
      3: double l_y
      4: double l_z
      5: double m_x
      6: double m_y
      7: double m_z
      8: double a_x
      9: double a_y
      10: double a_z
      11: double g_x
      12: double g_y
      13: double g_z
      14: string d_t
      15: double tp
      16: double r_q
      17: string o_m
      18: double b_v
    }
    
    service test{
        list<Cell>  number( 1 : i16 req)
    }
    

    Then in Javascript i send only the request:

    function calc() {
    var transport = new Thrift.Transport("/service");
    var protocol  = new Thrift.Protocol(transport);
    var client    = new pruebaClient(protocol);
    
    
    var workbench = Math.random()*1000;
    var div = document.getElementById('deltat');
    
    
        try {
    
          result = client.number(1);
          div.innerHTML = div.innerHTML + '\nReady';
    
        } catch(ouch){
    
          $('#result').val("ERROR");
          $('#result').css('color', 'red');
        }
    }
    

    And in result y have the [10000][18] answer and print it like this:

    for (var i = result.length - 1; i >= 0; i--) {
          //div.innerHTML = div.innerHTML + '\n' + result[i];
          div.insertAdjacentHTML('beforeend', '\n'+result[i].did+' '+result[i].l_x+' '+result[i].l_y+' '+result[i].l_z+' '+result[i].m_x+' '+result[i].m_y+' '+result[i].m_z+' '+result[i].a_x+' '+result[i].a_y+' '+result[i].a_z+' '+result[i].g_x+' '+result[i].g_y+' '+result[i].g_z+' '+result[i].d_t+' '+result[i].tp+' '+result[i].r_q+' '+result[i].o_m+' '+result[i].b_v);
        };
      }
    

    Finally the handler in my Java Server is like this:

    public class TestHandler implements test.Iface {
      public Random rnd = new Random();
      public static List<Cell> p = new ArrayList<Cell>();
    
      public void test() {
        for (int i = 0; i < 10000; i++) {
            Cell a = new Cell();
            a.did = "SomeString";
            a.l_x = rnd.nextDouble()*10+1;
            a.l_y = rnd.nextDouble()*10+1;
            a.l_z = 0.0;
            a.m_x = 0.0;
            a.m_y = 0.0;
            a.m_z = 0.0;
            a.a_x = 0.0;
            a.a_y = 0.0;
            a.a_z = 0.0;
            a.g_x = 0.0;
            a.g_y = 0.0;
            a.g_z = 0.0;
            a.d_t = "String here";
            a.tp = 0.0;
            a.r_q = 0.0;
            a.o_m = "A";
            a.b_v = 0.0;
            p.add(a);
        }
    }
    @Override
    public List<Cell> number(short req) throws TException {
        test();
        return ips.ReminderBeep.list_d;
    }
    

    I hope this will be useful for someone