Skip to content

Commit f3883d7

Browse files
committed
add timestamps to messenger
1 parent 6028077 commit f3883d7

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

R/messenger.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,23 @@ messenger <- function(dial = NULL, listen = NULL) {
4646
cat("\r", `length<-`(intro, i), sep = " ", file = stdout())
4747
Sys.sleep(0.02)
4848
}
49-
cat(" | type your message:\n", file = stdout())
49+
cat("\n", file = stdout())
5050
s <- .Call(rnng_send, sock, as.raw(0L), 0L)
5151
if (is.integer(s)) {
52-
cat("[ peer offline ] waiting for connection...\n", file = stdout())
52+
cat(sprintf("| peer offline: waiting for connection: %s\n", format.POSIXct(Sys.time())),
53+
file = stderr())
5354
} else {
54-
cat("[ peer online ] connected\n", file = stdout())
55+
cat(sprintf("| peer online: connected: %s\n", format.POSIXct(Sys.time())),
56+
file = stderr())
5557
}
58+
cat("type your message:\n", file = stdout())
5659
repeat {
5760
data <- readline()
5861
if (identical(data, ":q")) break
5962
if (identical(data, "")) next
6063
data <- writeBin(object = data, con = raw())
6164
s <- .Call(rnng_send, sock, data, 0L)
62-
if (is.integer(s)) message(sprintf("%s [ peer offline ] message not sent", format.POSIXct(Sys.time())))
65+
if (is.integer(s)) message(sprintf("| peer offline: message not sent > %s", format.POSIXct(Sys.time())))
6366
}
6467

6568
}

src/sockets.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* nanonext - C level - Sockets and Protocols ------------------------------- */
22

3+
#include <time.h>
34
#include <nng/nng.h>
45
#include <nng/protocol/bus0/bus.h>
56
#include <nng/protocol/pair0/pair.h>
@@ -100,25 +101,38 @@ static void rnng_thread(void *arg) {
100101
unsigned char *buf = NULL;
101102
size_t sz;
102103
int xc;
104+
time_t now;
105+
struct tm *tms;
103106
nng_socket *sock = (nng_socket *) arg;
104107

105108
while (1) {
106109
xc = nng_recv(*sock, &buf, &sz, 1u);
110+
time(&now);
107111
if (xc) {
108-
REprintf("messenger session ended\n");
112+
tms = localtime(&now);
113+
REprintf("| messenger session: ended: %d-%02d-%02d %02d:%02d:%02d\n",
114+
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
115+
tms->tm_hour, tms->tm_min, tms->tm_sec);
109116
break;
110117
}
111118
if (!strcmp((const char *) buf, ":q")) {
112119
nng_free(buf, sz);
113120
break;
114121
}
115122
if (!strcmp((const char *) buf, "")) {
123+
tms = localtime(&now);
124+
REprintf("| peer status: changed: %d-%02d-%02d %02d:%02d:%02d\n",
125+
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
126+
tms->tm_hour, tms->tm_min, tms->tm_sec);
116127
nng_free(buf, sz);
117-
Rprintf("[ peer status ] changed\n");
118128
continue;
119129
}
120130

121-
Rprintf("> %s\n", buf);
131+
tms = localtime(&now);
132+
Rprintf("%s < %d-%02d-%02d %02d:%02d:%02d\n",
133+
buf,
134+
tms->tm_year + 1900, tms->tm_mon + 1, tms->tm_mday,
135+
tms->tm_hour, tms->tm_min, tms->tm_sec);
122136
nng_free(buf, sz);
123137
}
124138

0 commit comments

Comments
 (0)