Skip to content

Commit a0c143e

Browse files
committed
messenger timestamp and initial connection improvements
1 parent 33b2534 commit a0c143e

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

R/messenger.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#' @section Usage:
1313
#'
1414
#' Type outgoing messages and hit return to send.
15-
#' Incoming messages are prefixed by \code{>}.
15+
#'
16+
#' The timestamps of outgoing messages are prefixed by \code{>} and that of
17+
#' incoming messages by \code{<}.
1618
#'
1719
#' \code{:q} is the command to quit.
1820
#'
@@ -34,20 +36,19 @@ messenger <- function(url) {
3436
close(sock)
3537
invisible()
3638
})
37-
cat("\014", file = stdout())
39+
cat("\n", file = stdout())
3840
intro <- unlist(strsplit("nanonext messenger", ""))
3941
for (i in seq_along(intro)) {
4042
cat("\r", `length<-`(intro, i), sep = " ", file = stdout())
4143
Sys.sleep(0.02)
4244
}
4345
cat(sprintf("\n| url: %s\n", url), file = stdout())
44-
s <- .Call(rnng_send, sock, writeBin(":c ", raw()), 0L)
46+
cat("| connecting... ", file = stderr())
47+
s <- .Call(rnng_send, sock, writeBin(":c ", raw()), 2000L)
4548
if (is.integer(s)) {
46-
cat(sprintf("| peer offline: %s\n", format.POSIXct(Sys.time())),
47-
file = stderr())
49+
cat(sprintf("\r| peer offline: %s\n", format.POSIXct(Sys.time())), file = stderr())
4850
} else {
49-
cat(sprintf("| peer online: %s\n", format.POSIXct(Sys.time())),
50-
file = stderr())
51+
cat(sprintf("\r| peer online: %s\n", format.POSIXct(Sys.time())), file = stderr())
5152
}
5253
cat("type your message:\n", file = stdout())
5354
repeat {
@@ -57,10 +58,11 @@ messenger <- function(url) {
5758
rdata <- writeBin(object = data, con = raw())
5859
s <- .Call(rnng_send, sock, rdata, 0L)
5960
if (is.integer(s)) {
60-
cat(sprintf("| peer offline: message not sent > %s\n", format.POSIXct(Sys.time())),
61+
cat(sprintf("%*s > not sent: peer offline: %s\n", nchar(data), "", format.POSIXct(Sys.time())),
6162
file = stderr())
6263
} else {
63-
cat(sprintf(" > %s\n", format.POSIXct(Sys.time())), file = stdout())
64+
cat(sprintf("%*s > %s\n", nchar(data), "", format.POSIXct(Sys.time())),
65+
file = stdout())
6466
}
6567
}
6668

man/messenger.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sockets.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static void rnng_thread(void *arg) {
118118
if (!strcmp(buf, ":c ")) {
119119
time(&now);
120120
tms = localtime(&now);
121-
REprintf("| peer connected: %d-%02d-%02d %02d:%02d:%02d\n",
121+
REprintf("| <- peer connected: %d-%02d-%02d %02d:%02d:%02d\n",
122122
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
123123
tms->tm_hour, tms->tm_min, tms->tm_sec);
124124
nng_free(buf, sz);
@@ -127,7 +127,7 @@ static void rnng_thread(void *arg) {
127127
if (!strcmp(buf, ":d ")) {
128128
time(&now);
129129
tms = localtime(&now);
130-
REprintf("| peer disconnected: %d-%02d-%02d %02d:%02d:%02d\n",
130+
REprintf("| -> peer disconnected: %d-%02d-%02d %02d:%02d:%02d\n",
131131
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
132132
tms->tm_hour, tms->tm_min, tms->tm_sec);
133133
nng_free(buf, sz);
@@ -136,8 +136,8 @@ static void rnng_thread(void *arg) {
136136

137137
time(&now);
138138
tms = localtime(&now);
139-
Rprintf("%s\n < %d-%02d-%02d %02d:%02d:%02d\n",
140-
buf,
139+
Rprintf("%s\n%*s< %d-%02d-%02d %02d:%02d:%02d\n",
140+
buf, sz, "",
141141
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
142142
tms->tm_hour, tms->tm_min, tms->tm_sec);
143143
nng_free(buf, sz);

0 commit comments

Comments
 (0)