Skip to content

Commit

Permalink
allow user interrupts
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Oct 2, 2024
1 parent fa9335b commit 25ca741
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
7 changes: 4 additions & 3 deletions R/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
#' string (if of the appropriate type), or otherwise a serialized R object,
#' which should be passed to \code{\link{unserialize}}.
#'
#' Use only in a new session. Use \sQuote{ctrl + \\} to forcibly quit
#' when finished as the function blocks with no means of interruption.
#'
#' If the expression could not be parsed or evaluated, the response will be
#' returned with a status code of 500 and a blank body.
#'
#' User interrupts will only be processed after the next query has been
#' completed, hence return from the function may not be immediate. Use
#' \sQuote{ctrl + \\} to forcibly quit the entire R session if required.
#'
#' @return This function never returns.
#'
#' @examples
Expand Down
7 changes: 4 additions & 3 deletions man/server.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 13 additions & 19 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,25 +243,30 @@ void parse_eval_safe(void *data) {
nano_parse_eval_res = R_ParseEvalString((const char *) data, R_GlobalEnv);
}

void inproc_server(const char* url) {
SEXP rnng_rest_server(SEXP url) {

const char *addr[2] = {CHAR(STRING_ELT(url, 0)), "inproc://n-a-n-o-serv"};
nng_thread *thr;
nng_socket s;
nng_msg *msg;
int xc;

if ((xc = nng_rep0_open(&s)) || (xc = nng_listen(s, url, NULL, 0)))
if ((xc = nng_thread_create(&thr, rest_start, (void *) addr)))
ERROR_OUT(xc);

if ((xc = nng_rep0_open(&s)) ||
(xc = nng_listen(s, addr[1], NULL, 0)))
fatal("unable to set up inproc", xc);

for (;;) {

if ((xc = nng_recvmsg(s, &msg, 0)))
fatal("inproc recvmsg", xc);

const char *body = nng_msg_body(msg);
nano_buf buf;

nano_parse_eval_res = R_BlankScalarString;
R_ToplevelExec(parse_eval_safe, (void *) body);
R_ToplevelExec(parse_eval_safe, (void *) nng_msg_body(msg));

nano_buf buf;
if (TYPEOF(nano_parse_eval_res) == STRSXP) {
const char *string = NANO_STRING(nano_parse_eval_res);
buf.buf = (unsigned char *) string;
Expand All @@ -274,20 +279,9 @@ void inproc_server(const char* url) {
if ((xc = nng_sendmsg(s, msg, 0)))
fatal("inproc sendmsg", xc);

}
R_CheckUserInterrupt();

}

SEXP rnng_rest_server(SEXP url) {

const char *addr[2] = {CHAR(STRING_ELT(url, 0)), "inproc://n-a-n-o-serv"};
nng_thread *thr;
int xc;

if ((xc = nng_thread_create(&thr, rest_start, (void *) addr)))
ERROR_OUT(xc);

inproc_server(addr[1]);
}

nng_thread_destroy(thr);
return R_NilValue;
Expand Down

0 comments on commit 25ca741

Please sign in to comment.