| 1 |
\section[A Basic Client/Server]{A Basic Client/Server} |
| 2 |
\label{sec:cs} |
| 3 |
\addcontentsline{toc}{section}{\thesection. A Basic Client/Server} |
| 4 |
|
| 5 |
|
| 6 |
In this section, we will develop a more complicated and realistic example using |
| 7 |
\pkg{pbdZMQ}. The example will show the construction of a basic client and |
| 8 |
server. To do so, we will (eventually) use the Request/Reply pattern, where a |
| 9 |
message is passed from client to server, executed on the server, and then the |
| 10 |
result is passed back to the client as a message. |
| 11 |
|
| 12 |
All server code is meant to be executed in |
| 13 |
batch; though it can be used from an interactive R session, we feel this |
| 14 |
somewhat misses the point. To do this, save the server code as, say, |
| 15 |
\code{server.r} and start the server by running |
| 16 |
\begin{Command} |
| 17 |
Rscript server.r |
| 18 |
\end{Command} |
| 19 |
from a terminal. One could use \code{R CMD BATCH} in place of \code{Rscript}, |
| 20 |
though by default it will suppress some messages on the server that we will |
| 21 |
want to see. Finally, the client should be run inside of an interactive |
| 22 |
\proglang{R} session. This can be from RStudio, the Windows/Mac \proglang{R} |
| 23 |
guis, or by running the command \code{R} at the terminal --- the way you are |
| 24 |
used to using \proglang{R}. |
| 25 |
|
| 26 |
Throughout our examples here, we will be using the \pkg{rzmq}-like bindings |
| 27 |
available in \pkg{pbdZMQ}. The other interfaces are easy enough to figure out |
| 28 |
once you understand one of them; and the \pkg{rzmq} ones are arguably the most |
| 29 |
\proglang{R}-like of the three. |
| 30 |
The source code for the final example can be found in the |
| 31 |
\code{inst/examples/reqrep/} subtree of the \pkg{pbdZMQ} source, or under |
| 32 |
\code{examples/reqrep/} of the binary installation. The same directory |
| 33 |
contains versions of this example using the other \pkg{pbdZMQ} interfaces. |
| 34 |
|
| 35 |
|
| 36 |
\subsection{Our First Client/Server} |
| 37 |
|
| 38 |
\paragraph{The Server} |
| 39 |
Our first server will be very humble. It will receive one command from the |
| 40 |
client, print that command, and send back a success message before terminating |
| 41 |
--- nothing more. Save the following in a file, say \code{server.r}, and |
| 42 |
execute it by running \code{Rscript server.r} from a terminal: |
| 43 |
|
| 44 |
\begin{lstlisting}[language=R,title=Server] |
| 45 |
library(pbdZMQ) |
| 46 |
ctxt <- init.context() |
| 47 |
socket <- init.socket(ctxt, "ZMQ_REP") |
| 48 |
bind.socket(socket, "tcp://*:55555") |
| 49 |
|
| 50 |
cat("Client command: ") |
| 51 |
msg <- receive.socket(socket) |
| 52 |
cat(msg, "\n") |
| 53 |
|
| 54 |
send.socket(socket, "Message received!") |
| 55 |
\end{lstlisting} |
| 56 |
|
| 57 |
Unfortunately, the first 4 lines are just boilerplate; see the package manual |
| 58 |
for an explanation. The good news is that this is about as complicated as |
| 59 |
it gets on the ZeroMQ side; everything beyond this is just \proglang{R} |
| 60 |
programming. |
| 61 |
|
| 62 |
\paragraph{The Client} |
| 63 |
From and interactive R session (\emph{not} in batch!), enter the |
| 64 |
following: |
| 65 |
|
| 66 |
\begin{lstlisting}[language=R,title=Client] |
| 67 |
library(pbdZMQ) |
| 68 |
ctxt <- init.context() |
| 69 |
socket <- init.socket(ctxt, "ZMQ_REQ") |
| 70 |
connect.socket(socket, "tcp://localhost:55555") |
| 71 |
|
| 72 |
send.socket(socket, "1+1") |
| 73 |
receive.socket(socket) |
| 74 |
\end{lstlisting} |
| 75 |
|
| 76 |
If all goes well, your message (namely, \code{"1+1"}) should be sent from the |
| 77 |
client to the server, and the response \code{"Message received!"} should be |
| 78 |
sent from server to client. Afterwards, the server will terminate and you are |
| 79 |
free to exit your interactive \proglang{R} session (i.e., the client). |
| 80 |
|
| 81 |
This example is deliberately as basic as can be, and lacks 2 crucial |
| 82 |
features: server persistence, and remote execution of commands. We will |
| 83 |
develop examples with these features in the remainder of this section. |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
\subsection{A Persistent Server} |
| 89 |
|
| 90 |
\paragraph{The Server} |
| 91 |
Next, we make the server \emph{persistent}, in the sense that it will not |
| 92 |
immediately die after receiving its first command. This is trivial, as all |
| 93 |
we need to do is encapsulate the receive/send piece inside a \code{while} loop. |
| 94 |
As before, save the following to a file and execute in batch via \code{Rscript}: |
| 95 |
|
| 96 |
\begin{lstlisting}[language=R,title=Server] |
| 97 |
library(pbdZMQ) |
| 98 |
ctxt <- init.context() |
| 99 |
socket <- init.socket(ctxt, "ZMQ_REP") |
| 100 |
bind.socket(socket, "tcp://*:55555") |
| 101 |
|
| 102 |
|
| 103 |
while(TRUE) |
| 104 |
{ |
| 105 |
cat("Client command: ") |
| 106 |
msg <- receive.socket(socket) |
| 107 |
cat(msg, "\n") |
| 108 |
|
| 109 |
send.socket(socket, "Message received!") |
| 110 |
} |
| 111 |
\end{lstlisting} |
| 112 |
|
| 113 |
The \code{receive.socket()} command does not use \emph{busy waiting}. You can |
| 114 |
verify this by starting up the server and then looking at a process monitor for |
| 115 |
your operating system; you should see no elevated activity. |
| 116 |
|
| 117 |
|
| 118 |
\paragraph{The Client} |
| 119 |
|
| 120 |
Set up the client as above (everything but the \code{send.socket()} line is |
| 121 |
necessary) in an interaction \proglang{R} session. Now that we have a |
| 122 |
persistent server, we can make a shorthand function that encapsulates |
| 123 |
sending a message (from client to server) and receiving a response (from |
| 124 |
server to client): |
| 125 |
|
| 126 |
\begin{lstlisting}[language=R,title=Client Send/Receive] |
| 127 |
sendrecv <- function(socket, data) |
| 128 |
{ |
| 129 |
send.socket(socket, data) |
| 130 |
receive.socket(socket) |
| 131 |
} |
| 132 |
\end{lstlisting} |
| 133 |
|
| 134 |
This assumes that the various optional arguments in \code{send.socket()} and |
| 135 |
\code{receive.socket()} are acceptable; and for the purposes of this |
| 136 |
demonstration they are. But the reader is encouraged to consult the |
| 137 |
\pkg{pbdZMQ} manual for more details about these two functions. |
| 138 |
|
| 139 |
Now, with the convenience function, we can simply execute: |
| 140 |
|
| 141 |
\begin{lstlisting}[language=R,title=Client Usage] |
| 142 |
sendrecv(socket, "1+1") |
| 143 |
sendrecv(socket, "rnorm(10)") |
| 144 |
\end{lstlisting} |
| 145 |
|
| 146 |
or any other valid \proglang{R} command. |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
\subsection{More Than Messaging}\label{fullcs} |
| 152 |
|
| 153 |
|
| 154 |
The final piece is to actually execute commands that are sent to the server, |
| 155 |
and to pass the result back to the client. This is very easy, and only |
| 156 |
requires a slight modification to the server code. Modify the server piece |
| 157 |
above to do the following just after receiving (and printing) the client's |
| 158 |
message: |
| 159 |
|
| 160 |
\begin{lstlisting}[language=R,title=Server Modification] |
| 161 |
result <- eval(parse(text=msg)) |
| 162 |
|
| 163 |
send.socket(socket, result) |
| 164 |
\end{lstlisting} |
| 165 |
|
| 166 |
Of course, you will also need to remove the original \code{send.socket()} line |
| 167 |
with the one here. A nasty source of bugs in client/server programming is |
| 168 |
sending when you should receive or vice-versa, leading to deadlocks. |
| 169 |
|
| 170 |
One additional thing the observant reader may have already realized is that our |
| 171 |
client/server framework leaves the server running perpetually, with no |
| 172 |
reasonable way for the client to terminate it. This just requires basic |
| 173 |
filtering of incoming messages (from the client, on the server). So for |
| 174 |
example, we might want the message \code{"EXIT"} to terminate the server. |
| 175 |
Modifying the server to handle this is trivial, and we present the full server |
| 176 |
below: |
| 177 |
|
| 178 |
\begin{lstlisting}[language=R,title=Full Server] |
| 179 |
library(pbdZMQ) |
| 180 |
ctxt <- init.context() |
| 181 |
socket <- init.socket(ctxt, "ZMQ_REP") |
| 182 |
bind.socket(socket, "tcp://*:55555") |
| 183 |
|
| 184 |
|
| 185 |
while(TRUE) |
| 186 |
{ |
| 187 |
cat("Client command: ") |
| 188 |
msg <- receive.socket(socket) |
| 189 |
cat(msg, "\n") |
| 190 |
|
| 191 |
if (msg == "EXIT") |
| 192 |
break |
| 193 |
|
| 194 |
result <- eval(parse(text=msg)) |
| 195 |
|
| 196 |
send.socket(socket, result) |
| 197 |
} |
| 198 |
|
| 199 |
send.socket(socket, "shutting down!") |
| 200 |
\end{lstlisting} |
| 201 |
|
| 202 |
Notice that we essentially added just a few lines. The first and more obvious |
| 203 |
is the check on \code{msg} for the magic word \code{"EXIT"}. The addition |
| 204 |
of the final \code{send.socket()} line at the end, which returns the string |
| 205 |
\code{"shutting down!"} to the client is necessary to prevent the client from |
| 206 |
hanging after the server shuts down. Recall, the client expects a response |
| 207 |
from the server for every message the client sends! |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
\subsection{Other Issues} |
| 212 |
|
| 213 |
The above examples are all very basic, but should illustrate how one could |
| 214 |
proceed to a more complex client/server design using ZeroMQ from R. Of course, |
| 215 |
there are a host of issues that we have not gone into here that are very |
| 216 |
important. For example, we perform no scrubbing of inputs that are to be |
| 217 |
executed on the server. This could be more or less important depending on the |
| 218 |
application. |
| 219 |
|
| 220 |
Another important issue we have not addressed is error and warning handling. |
| 221 |
The reader is encouraged to return to the example in Subsection~\ref{fullcs} |
| 222 |
and try executing something like |
| 223 |
\code{sendrecv(socket, "object_does_not_exist")} |
| 224 |
or |
| 225 |
\code{sendrecv(socket, "warning('uh oh')")} |
| 226 |
to see what happens. |
| 227 |
|
| 228 |
Additionally, each time the client wanted to send a message, |
| 229 |
the user had to manually pass it as an argument to the function |
| 230 |
\code{sendrecv()}. It is possible --- though complicated --- to create a |
| 231 |
custom REPL which will automatically handle the client/server |
| 232 |
send-evaluate-receive pattern as though the user were at a standard \proglang{R} |
| 233 |
terminal. |
| 234 |
|
| 235 |
Finally, we have not addressed the important issue of logging user commands |
| 236 |
sent to the server. Although anyone comfortable with \proglang{R} should |
| 237 |
see the path forward. |
| 238 |
|
| 239 |
For a more detailed example illustrating these points, see the \pkg{pbdCS} |
| 240 |
package. |