From romain.francois at dbmail.com  Sun Nov  8 10:14:18 2009
From: romain.francois at dbmail.com (Romain Francois)
Date: Sun, 08 Nov 2009 10:14:18 +0100
Subject: [Rd] getConnection, R_outpstream_st
In-Reply-To: <4AF54AD2.6020407@dbmail.com>
References: <4AF54AD2.6020407@dbmail.com>
Message-ID: <4AF68BEA.2000507@dbmail.com>

Hello,

Maybe I should give more background about what we are trying to do with
this. We (Dirk, Saptarshi and I) are interfacing R with the protocol
buffer C++ library from google [1,2] .

protocol buffers are google's data interchange format, designed to be
efficient (much more efficient than XML) and language neutral.

Google supports bindings to java, c++ and python, and there are dozens
of third party bindings (perl, C, C#, ...) available. We are doing the
one for R, allowing to create and manipulate protocol buffer messages
from R. For example with this message type :

package tutorial ;
message Person {
   required int32 id = 1;
   required string name = 2;
   optional string email = 3;
}

you can create a message like this :

message <- new( tutorial.Person, id = 0, name = "Romain", email =
"francoisromain at free.fr" )

We can also read/write messages from/to files :

tutorial.Person$read( "somefile" )
serialize( message, "somefile" )

Now we'd like to be able to use the extensive IO support R provides
(connections) and essentially read and write messages from and to
arbitrary binary connections. Practically, it means calling this method
of the google::protobuf::Message c++ class [3,4]

bool Message::ParseFromIstream(istream * input)
bool Message::SerializeToOstream( ostream * output) const

This means we need to be able to build istream and ostream pointers
working together with a binary R connection. It seemed to me from
reading the code of serialize.c in R that we would be able to achieve
this by sort of wrapping R_inpstream_st and R_outpstream_st as c++
streams.

The only thing I cannot figure out at that point is how to access a
connection pointer (Rconnection). From this :

#ifdef NEED_CONNECTION_PSTREAMS
/* The connection interface is not yet available to packages.  To
    allow limited use of connection pointers this defines the opaque
    pointer type. */
#ifndef HAVE_RCONNECTION_TYPEDEF
typedef struct Rconn  *Rconnection;
#define HAVE_RCONNECTION_TYPEDEF
#endif
void R_InitConnOutPStream(R_outpstream_t stream, Rconnection con,
			  R_pstream_format_t type, int version,
			  SEXP (*phook)(SEXP, SEXP), SEXP pdata);
void R_InitConnInPStream(R_inpstream_t stream,  Rconnection con,
			 R_pstream_format_t type,
			 SEXP (*phook)(SEXP, SEXP), SEXP pdata);
#endif

I get that if I do have a Rconnection, then I can initialize a
R_outpstream_t and just use this, but I can't find a way to get one. The
internal "getConnection" seems to do just this, but it is not accessible
from packages.

We'd appreciate any help.

Romain


[1] http://r-forge.r-project.org/projects/rprotobuf/
[2] http://code.google.com/p/protobuf/
[3]
http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.message.html#Message.ParseFromIstream.details
[4]
http://code.google.com/apis/protocolbuffers/docs/reference/cpp/google.protobuf.message.html#Message.SerializeToOstream.details


On 11/07/2009 11:24 AM, Romain Francois wrote:
> Hello,
>
> I'm trying to use the limited connections api defined in
> Rinternals.h. I have code that looks like this (inspired from
> do_serializeToConn) :
>
> SEXP serialize_to_connection( SEXP xp, SEXP connection ){
>
> Rconnection con ; struct R_outpstream_st out; R_pstream_format_t type
> = R_pstream_binary_format ; SEXP (*hook)(SEXP, SEXP) = NULL ; con =
> getConnection(Rf_asInteger(connection)); R_InitConnOutPStream(&out,
> con, type, 0, hook, R_NilValue );
>
> return R_NilValue ; }
>
> The problem I have is that I cannot actually call getConnection since
> it is not part of the api. Is there another way to get the
> Rconnection that is associated with a number.
>
> Many thanks.
>
> Romain
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/BcPw : celebrating R commit #50000
|- http://tr.im/ztCu : RGG #158:161: examples of package IDPmisc
`- http://tr.im/yw8E : New R package : sos



[Rd] getConnection, R_outpstream_st
Romain Francois