Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement callback mechanism in Rserve?

Tags:

r

sockets

rserve

i want to know a simple way of implementing callback mechanism in Rserve for a java client . According to Rserve docs :

Rserve provides no callback functionality. Your application could implement callbacks via TCP/IP and the R sockets but it is not a part of Rserve.

This means my java client can call functions on the remote Session through Rconnection reference , but the remote Session cannot call back the java client which has instantiated it . How can i develop such a mechanism . If its through R sockets or a tcp/ip server , does that mean for every connection there will be a socket server open ?

like image 780
sanre6 Avatar asked May 23 '12 07:05

sanre6


1 Answers

Ok, so this is how I think it is possible to implement reactive R.

Non-blocking calls from java

You need to fork RServe java client and split method request into two parts in this line [1]. First part write request to the socket and the second waits for response. We need to make waiting optional by for example some boolean flag.

Returning result from R

You will need some kind of active communication to Java. One possibility is to use plain sockets or something on higher level as HTTP. I thought about httpRequest package [2]. So the call from java should look like:

connection.eval(s"""simplePostToHost(
"192.168.12.12","/listener/results/",
try(eval(parse(text="$code")),silent=TRUE),port=8080""")

Listening for result in Java

The request and response should share some kind of unique ID so we know which response is for which request. You should run some service that listen on path /listener/results for incoming results and tells Java that result is ready. It should also enable to reuse RConnection that previously should be marked as "busy". I recommend to use it this part scala Promise[T] .

Hope it helps somebody. I'm probably going to implement it once my company needs it.

[1]https://github.com/s-u/REngine/blob/a74e184c051c2d2e850430cd2d0526656d3a6c48/Rserve/protocol/RTalk.java#L211

[2]https://cran.r-project.org/web/packages/httpRequest/httpRequest.pdf

like image 175
Przemek Avatar answered Sep 28 '22 10:09

Przemek