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
.
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)
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
print.servserSession
, arguments to be passed
on to print.simple.list
. For ensureServerSession
,
arguments to be passed through to startRemoteServer
TRUE
(not the default), raise an error
exception if there is not a valid server session runningserverSession()
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.
ssh
startRemoteServer(host = "mralx2")
blah <- sendExpression(getfame("gdp.q", db = "us"))
endServerSession()
Run the code above in your browser using DataLab