clientServerR {fame}R Documentation

Client - Server R

Description

These functions implement R client 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 R objects 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 R process, 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 R server 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 R session.

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, ...)
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 with quitAfter = FALSE to see what is happening on the far end.
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 R object to send to the server
expr an expression (entered without quotes) to send to the remote R server, 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.

Note

Getting the detailed sequencing right for blocking sockets can be very frustrating, since the R processes at both ends tend to hang on every mistake. The easiest way to get the sequence right is to just use startRemoteServer() from the client end to get everything started, and then use sendExpression() to send expressions to the server and get results on the client. Finally, you can call endServerSession() when you are finished to clean up, as in the example below. An even better idea is to leave the server running, but call endServerSession() in your .Last function.

The remote server is started via ssh, and this will usually require you to interactively supply a password. No facility to store your password and use it when needed has been provided, as that would just be asking for security trouble. On Windows, the ssh function uses the plink program to establish the ssh connection, and uses it's password prompt as well.

Author(s)

Jeff Hallman

See Also

ssh

Examples

## Not run: 
startRemoteServer(host = "mralx2")
blah <- sendExpression(getfame("gdp.q", db = "us"))
endServerSession()
## End(Not run)

[Package fame version 1.05 Index]