Learn R Programming

fame (version 1.13)

clientServerR: Client - Server R

Description

These functions implement Rclient and server sessions.

The first function, serveHostAndPort() runs on the server end of the session. It finds an available port on the host it is running on, and uses that host and port as the server end of a serverSession with the given client host and port. It returns the serverSession to the client and begins a loop listening on the serverPort for Robjects to be sent through.

When an object arrives on the port, what happens depends on its mode. If the new arrival is an expression or call, it gets evaluated and the result is sent back to the client. If the arriving object is a character string, it gets parsed and evaluated and the result sent back. Any other mode of arriving object is just sent back. All attempted evaluations take place in a try block, and a try-error gets returned to the client if the parsing or evaluating fails.

After handling the incoming object, the server returns to the top of the loop. There are two ways to break out of the loop. The first is to send the expresssion break to the server. The other way, timing out, works only if the server runs on a real operating system, i.e., Unix or Linux. If so, the server sets an alarm timer when first starting the loop, and resets it after each pass through the loop. If the timer expires due to inactivity, the SIGALRM signal is sent to the server Rprocess, which kills it.

All of the other functions documented here run on the client end of the session. startRemoteServer() assumes that the machine it is running on is to be the client end of an Rserver session. It uses ssh to invoke serveHostAndPort() on the remote host, and waits for the remote server to return a serverSession object, which it then stores in the global environment as ".serverSession".

print.serverSession is a print method for serverSession objects.

serverSession() returns the serverSession stored in the global environment.

hasExpired() checks a serverSessions timestamp, which is updated on each send or receive operation, against it's timeout and the current time. Note that there is no good way to check with the server itself, since it may not be running. If the server has already died, attempting to contact it will likely hang the current Rsession.

endServerSession() checks to see if there is a serverSession stored in the global environment, exiting if there isn't. Assuming there is a session and it has not yet expired, the break expression is sent to kill it. If the session timestamp indicates that the session has expired, the function asks the remote machine to kill the session serverPid process. If the process has already died, this does no harm.

validServerIsRunning() answers TRUE if serverSession() returns a serverSession that has not timed out yet.

ensureValidServer returns the current serverSession if there is one, or starts a new one and returns that.

sendToServer() and receiveFromServer do what their names imply.

Finally, sendExpression sends its unevaluated argument (retrieved via substitute) to the server, and waits for the server to return a result, which becomes the return value of sendExpression.

Usage

serveHostAndPort(clientHost, clientPort, timeout = 3600, quitAfter = T)
startRemoteServer(host = getOption("remoteHost"), user. = user(), timeout = 3600)
hasExpired(ss)
## S3 method for class 'serverSession':
print(x, \dots)
serverSession()
endServerSession()
validServerIsRunning(fail = F)
ensureValidServer(...)
sendToServer(object)
receiveFromServer()
sendExpression(expr)

Arguments

clientHost
DNS name of the machine the R client is running on
clientPort
Port number on the client
timeout
number of seconds of inactivity after which the R server commits suicide
quitAfter
if TRUE (the default), upon breaking out of the receive-eval-return result loop, the server invokes q("no") to kill itself. For testing, you may want to start the remote server by hand and invode serveHostAndPort
host
DNS name of the machine the R server is to run on
user.
user name the R server is to run under
ss
a serverSession object
x
a serverSession object
...
For print.servserSession, arguments to be passed on to print.simple.list. For ensureServerSession, arguments to be passed through to startRemoteServer
fail
logical. If TRUE (not the default), raise an error exception if there is not a valid server session running
object
arbitrary Robject to send to the server
expr
an expression (entered without quotes) to send to the remote Rserver, where it will be evaluated and the result returned.

Value

  • serverSession() returns the stored serverSession, while hasExpired() and validServerIsRunning() return TRUE or FALSE. sendExpression() returns the value that the given expression evaluated to on the server.

    The other functions return nothing of interest.

See Also

ssh

Examples

Run this code
startRemoteServer(host = "mralx2")
blah <- sendExpression(getfame("gdp.q", db = "us"))
endServerSession()

Run the code above in your browser using DataLab