Rserve::Protocol

This module encapsulates methods and constants related to QAP1 protocol used by Rserv. Follows almost exactly the interface on RTalk class on Java version, except for use of undescores instead of CamelCase Implementation could differ if a cleaner/faster ruby version is available

Policy: No other class should know about the internal of protocol! See Rtalk class on Java version.

Constants

CMD_RESP

Defines from Rsrv.h

RESP_OK
RESP_ERR
ERR_auth_failed
ERR_conn_broken
ERR_inv_cmd
ERR_inv_par
ERR_Rerror
ERR_IOerror
ERR_notOpen
ERR_accessDenied
ERR_unsupportedCmd
ERR_unknownCmd
ERR_data_overflow
ERR_object_too_big
ERR_out_of_mem
ERR_ctrl_closed
ERR_session_busy
ERR_detach_failed
CMD_login
CMD_voidEval
CMD_eval
CMD_shutdown
CMD_openFile

/* file I/O routines. server may answe */

CMD_createFile
CMD_closeFile
CMD_readFile
CMD_writeFile

server is free to choose any value - usually it uses the size of its static buffer */

CMD_removeFile
CMD_setSEXP

/* object manipulation */

CMD_assignSEXP
CMD_detachSession

/* session management (since 0.4-0) */

CMD_detachedVoidEval
CMD_attachSession
CMD_ctrl

control commands (since 0.6-0) - passed on to the master process */ Note: currently all control commands are asychronous, i.e. RESP_OK indicates that the command was enqueued in the master pipe, but there is no guarantee that it will be processed. Moreover non-forked connections (e.g. the default debug setup) don’t process any control commands until the current client connection is closed so the connection issuing the control command will never see its result.

CMD_ctrlEval
CMD_ctrlSource
CMD_ctrlShutdown
CMD_setBufferSize

/* ‘internal’ commands (since 0.1-9) */

CMD_setEncoding
CMD_SPECIAL_MASK

/* special commands - the payload of packages with this mask does not contain defined parameters */

CMD_serEval
CMD_serAssign
CMD_serEEval
DT_INT

data types for the transport protocol (QAP1)do NOT confuse with XT_.. values.

DT_CHAR
DT_DOUBLE
DT_STRING
DT_BYTESTREAM
DT_SEXP
DT_ARRAY
DT_LARGE
ERROR_DESCRIPTIONS
MAX_LONG_SIGNED

I have to use to support different archs

MAX_LONG_UNSIGNED

Public Instance Methods

doubleToRawLongBits(double) click to toggle source
# File lib/rserve/protocol.rb, line 223
def doubleToRawLongBits(double)
  [double].pack("d").unpack("Q")[0]
end
get_int(buf, o) click to toggle source

converts bit-wise stored int in Intel-endian form into ruby int

@param buf buffer containg the representation @param o offset where to start (4 bytes will be used) @return the int value. no bounds checking is done so you need to make sure that the buffer is big enough

# File lib/rserve/protocol.rb, line 172
def get_int(buf, o)
  buf[o,4].pack("C*").unpack("l")[0]
end
get_len(buf, o) click to toggle source

converts bit-wise stored length from a header. “long” format is supported up to 32-bit @param buf buffer @param o offset of the header (length is at o+1) @return length */

# File lib/rserve/protocol.rb, line 184
def get_len(buf, o)
  # // "long" format; still - we support 32-bit only
  if  ((buf[o]&64)>0)
    #p "buf:#{buf} --- o: #{o} -- [#{buf[o+4]}]"
    raise "Buffer without enough values" if buf[o+4].nil?
    (buf[o+1]&255)|((buf[o+2]&255)<<8)|((buf[o+3]&255)<<16)|((buf[o+4]&255)<<24)
  else
    (buf[o+1]&255)|((buf[o+2]&255)<<8)|((buf[o+3]&255)<<16)
  end
end
get_long(buf, o) click to toggle source

converts bit-wise Intel-endian format into long @param buf buffer @param o offset (8 bytes will be used) @return long value */

# File lib/rserve/protocol.rb, line 199
def get_long(buf, o)
  buf[o,8].pack("CCCCCCCC").unpack("Q")[0]
end
longBitsToDouble(bits) click to toggle source
# File lib/rserve/protocol.rb, line 209
def longBitsToDouble(bits)
  (bits==LONG_NA) ? Rserve::REXP::Double::NA : [bits].pack("Q").unpack("d")[0]
end
new_hdr(ty, len) click to toggle source

creates a new header according to the type and length of the parameter @param ty type/cmd/resp byte @param len length

# File lib/rserve/protocol.rb, line 159
def new_hdr(ty, len)
  hdr=Array.new(  (len>0xfffff0)?8:4)
  set_hdr(ty,len,hdr,0);
  hdr
end
set_hdr(ty, len, buf, o) click to toggle source

writes cmd/resp/type byte + 3/7 bytes len into a byte buffer at specified offset. @param ty type/cmd/resp byte @param len length @param buf buffer @param o offset @return offset in buf just after the header. Please note that since Rserve 0.3 the header can be either 4 or 8 bytes long, depending on the len parameter.

# File lib/rserve/protocol.rb, line 143
def set_hdr(ty, len, buf, o)
  buf[o]=((ty&255)|((len>0xfffff0) ? DT_LARGE : 0)); o+=1;
  buf[o]=(len&255); o+=1;
  buf[o]=((len&0xff00)>>8); o+=1;
  buf[o]=((len&0xff0000)>>16); o+=1;
  if (len>0xfffff0)  # for large data we need to set the next 4 bytes as well
    buf[o]=((len&0xff000000)>>24); o+=1;
    buf[o]=0; o+=1; # since len is int, we get 32-bits only
    buf[o]=0; o+=1;
    buf[o]=0; o+=1;
  end
  o
end
set_int(v, buf, o) click to toggle source

writes bit-wise int to a byte buffer at specified position in Intel-endian form Internal: byte buffer will be the result of unpack(“CCCC”) an integer. @param v value to be written @param buf buffer @param o offset in the buffer to start at. An int takes always 4 bytes */

# File lib/rserve/protocol.rb, line 130
def  set_int(v, buf, o)
  buf[o]=(v&255);o+=1
  buf[o]=((v&0xff00)>>8); o+=1
  buf[o]=((v&0xff0000)>>16); o+=1
  buf[o]=((v&0xff000000)>>24);
end
set_long(l, buf, o) click to toggle source
# File lib/rserve/protocol.rb, line 226
def set_long(l, buf, o)
  set_int((l&0xffffffff), buf, o)
  set_int((l>>32),buf,o+4)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.