|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working |
| 4 | +with code in this repository. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +**nanonext** is an R package providing a binding to NNG (Nanomsg Next |
| 9 | +Gen), a high-performance messaging library. The package is implemented |
| 10 | +almost entirely in C, wrapping the NNG C library to provide R interfaces |
| 11 | +for socket-based messaging, asynchronous I/O operations, and distributed |
| 12 | +computing primitives. |
| 13 | + |
| 14 | +## Core Architecture |
| 15 | + |
| 16 | +### Dual Interface Design |
| 17 | + |
| 18 | +The package provides two equivalent interfaces: |
| 19 | + |
| 20 | +1. **Functional Interface**: Socket objects passed as first argument to |
| 21 | + functions like |
| 22 | + [`send()`](https://nanonext.r-lib.org/dev/reference/send.md), |
| 23 | + [`recv()`](https://nanonext.r-lib.org/dev/reference/recv.md), |
| 24 | + [`dial()`](https://nanonext.r-lib.org/dev/reference/dial.md), |
| 25 | + [`listen()`](https://nanonext.r-lib.org/dev/reference/listen.md) |
| 26 | +2. **Object-Oriented Interface**: Nano objects with methods accessible |
| 27 | + via `$send()`, `$recv()`, etc. |
| 28 | + |
| 29 | +### Key C Source Files |
| 30 | + |
| 31 | +- `src/nanonext.h` - Main header with type definitions, macros, and |
| 32 | + function declarations |
| 33 | +- `src/core.c` - Core serialization/unserialization, hooks, and |
| 34 | + fundamental utilities |
| 35 | +- `src/proto.c` - Protocol implementations (socket opening, option |
| 36 | + handling) |
| 37 | +- `src/aio.c` - Asynchronous I/O operations and callbacks |
| 38 | +- `src/comms.c` - Send/receive operations and data marshaling |
| 39 | +- `src/sync.c` - Synchronization primitives (condition variables, |
| 40 | + mutexes) |
| 41 | +- `src/thread.c` - Thread management and concurrent operations |
| 42 | +- `src/ncurl.c` - HTTP client implementation |
| 43 | +- `src/net.c` - Network utilities (IP address retrieval) |
| 44 | +- `src/tls.c` - TLS configuration and certificate handling |
| 45 | +- `src/utils.c` - Utility functions |
| 46 | +- `src/init.c` - Package initialization and registration |
| 47 | + |
| 48 | +### Key R Source Files |
| 49 | + |
| 50 | +- `R/socket.R` - Socket creation and management (functional interface) |
| 51 | +- `R/nano.R` - Nano object creation (OO interface) |
| 52 | +- `R/sendrecv.R` - Send/receive operations |
| 53 | +- `R/aio.R` - Async I/O wrappers |
| 54 | +- `R/context.R` - Context management for concurrent operations |
| 55 | +- `R/sync.R` - Synchronization primitives |
| 56 | +- `R/ncurl.R` - HTTP client functions |
| 57 | +- `R/stream.R` - WebSocket and stream interfaces |
| 58 | +- `R/tls.R` - TLS configuration |
| 59 | + |
| 60 | +### External Dependencies |
| 61 | + |
| 62 | +The package bundles and can compile: - `libnng` v1.11.0 (messaging |
| 63 | +library) - in `src/nng/` - `libmbedtls` v3.6.2 (TLS implementation) - in |
| 64 | +`src/mbedtls/` |
| 65 | + |
| 66 | +These bundled libraries are compiled during installation if system |
| 67 | +libraries are not found or if `NANONEXT_LIBS=1` is set. |
| 68 | + |
| 69 | +## Common Development Commands |
| 70 | + |
| 71 | +### Building and Testing |
| 72 | + |
| 73 | +``` r |
| 74 | +# Install package from source |
| 75 | +R CMD INSTALL . |
| 76 | + |
| 77 | +# Build package |
| 78 | +R CMD build . |
| 79 | + |
| 80 | +# Check package (run all tests and CRAN checks) |
| 81 | +R CMD check nanonext_*.tar.gz |
| 82 | + |
| 83 | +# Run tests only |
| 84 | +Rscript tests/tests.R |
| 85 | +``` |
| 86 | + |
| 87 | +### Development Workflow |
| 88 | + |
| 89 | +``` r |
| 90 | +# In R session - load package for development |
| 91 | +devtools::load_all() |
| 92 | + |
| 93 | +# Build documentation |
| 94 | +devtools::document() |
| 95 | + |
| 96 | +# Run tests |
| 97 | +devtools::test() |
| 98 | + |
| 99 | +# Check package |
| 100 | +devtools::check() |
| 101 | +``` |
| 102 | + |
| 103 | +### Compilation |
| 104 | + |
| 105 | +The package uses a configure script (`configure`) that: 1. Detects or |
| 106 | +compiles `libmbedtls` (requires \>= 2.5.0) 2. Detects or compiles |
| 107 | +`libnng` (requires \>= 1.9.0) 3. Generates `src/Makevars` with |
| 108 | +appropriate flags |
| 109 | + |
| 110 | +For forced compilation of bundled libraries: |
| 111 | + |
| 112 | +``` bash |
| 113 | +Sys.setenv(NANONEXT_LIBS = 1) |
| 114 | +``` |
| 115 | + |
| 116 | +### Testing Notes |
| 117 | + |
| 118 | +- Tests are in `tests/tests.R` using a minimal custom testing framework |
| 119 | +- Tests cover: sockets, protocols, async I/O, serialization, HTTP |
| 120 | + client, TLS, streams, contexts |
| 121 | +- Network-dependent tests may fail if external services are unavailable |
| 122 | +- Some tests only run when `NOT_CRAN=true` or on specific platforms |
| 123 | + (Linux) |
| 124 | + |
| 125 | +## Key Concepts |
| 126 | + |
| 127 | +### Protocols |
| 128 | + |
| 129 | +Implements scalability protocols: - **Bus** (mesh): `"bus"` - **Pair** |
| 130 | +(1-to-1): `"pair"` - **Poly** (polyamorous 1-to-1): `"poly"` - |
| 131 | +**Pipeline**: `"push"`, `"pull"` - **Pub/Sub**: `"pub"`, `"sub"` - |
| 132 | +**Request/Reply**: `"req"`, `"rep"` - **Survey**: `"surveyor"`, |
| 133 | +`"respondent"` |
| 134 | + |
| 135 | +### Transports |
| 136 | + |
| 137 | +Supported URL schemes: - `inproc://` - in-process (zero-copy) - |
| 138 | +`ipc://` - inter-process - `tcp://` - TCP/IP - `ws://` - WebSocket - |
| 139 | +`wss://` - WebSocket over TLS - `tls+tcp://` - TLS over TCP |
| 140 | + |
| 141 | +### Async I/O (AIO) |
| 142 | + |
| 143 | +Async operations return “aio” objects that auto-resolve: - |
| 144 | +[`send_aio()`](https://nanonext.r-lib.org/dev/reference/send_aio.md), |
| 145 | +[`recv_aio()`](https://nanonext.r-lib.org/dev/reference/recv_aio.md) - |
| 146 | +async send/receive - |
| 147 | +[`request()`](https://nanonext.r-lib.org/dev/reference/request.md) - |
| 148 | +async request/reply - |
| 149 | +[`ncurl_aio()`](https://nanonext.r-lib.org/dev/reference/ncurl_aio.md) - |
| 150 | +async HTTP - Objects have `$data`, `$result` fields - Use |
| 151 | +[`call_aio()`](https://nanonext.r-lib.org/dev/reference/call_aio.md) to |
| 152 | +wait/retrieve results - Use |
| 153 | +[`unresolved()`](https://nanonext.r-lib.org/dev/reference/unresolved.md) |
| 154 | +to check status |
| 155 | + |
| 156 | +### Serialization Modes |
| 157 | + |
| 158 | +Data can be sent in multiple modes: - `mode = "serial"` or `1` - R |
| 159 | +serialization (default, handles any R object) - `mode = "raw"` or `2` - |
| 160 | +raw bytes (for atomic vectors) - `mode = "string"` or `8` - character |
| 161 | +conversion - Various receive modes for type coercion |
| 162 | + |
| 163 | +Custom serialization supported via |
| 164 | +[`serial_config()`](https://nanonext.r-lib.org/dev/reference/serial_config.md) |
| 165 | +for specific object classes. |
| 166 | + |
| 167 | +### Object System |
| 168 | + |
| 169 | +The package uses R’s S3 object system with heavy use of external |
| 170 | +pointers tagged with specific symbols (defined in `src/init.c`): - |
| 171 | +`nano_SocketSymbol` - Socket objects - `nano_ContextSymbol` - Context |
| 172 | +objects - `nano_AioSymbol` - AIO objects - `nano_CvSymbol` - Condition |
| 173 | +variables - etc. |
| 174 | + |
| 175 | +External pointers are stored in pairlists with finalizers for automatic |
| 176 | +cleanup. |
| 177 | + |
| 178 | +## Important Implementation Details |
| 179 | + |
| 180 | +### Memory Management |
| 181 | + |
| 182 | +- C code uses `NANO_ALLOC`, `NANO_FREE` macros for buffer management |
| 183 | +- R objects protected via `nano_precious` environment (prevents GC) |
| 184 | +- Finalizers registered for cleanup of C resources |
| 185 | +- Careful use of `PROTECT`/`UNPROTECT` in C code |
| 186 | + |
| 187 | +### Thread Safety |
| 188 | + |
| 189 | +- Package implements its own threading via NNG’s platform layer |
| 190 | +- Condition variables |
| 191 | + ([`cv()`](https://nanonext.r-lib.org/dev/reference/cv.md)) for |
| 192 | + cross-thread signaling |
| 193 | +- Mutexes used internally for shared state |
| 194 | +- R interrupt handling on both Unix (via `R_interrupts_pending`) and |
| 195 | + Windows |
| 196 | + |
| 197 | +### Error Handling |
| 198 | + |
| 199 | +- NNG errors returned as integer error codes |
| 200 | +- `mk_error()` creates error values of class `"errorValue"` |
| 201 | +- Error messages via `nng_strerror()` |
| 202 | +- Use `ERROR_OUT()` macro to error, `ERROR_RET()` to return error value |
| 203 | + |
| 204 | +### Configuration Options |
| 205 | + |
| 206 | +Set/get socket/dialer/listener options via: - |
| 207 | +`opt(object, "option-name")` - get - |
| 208 | +`opt(object, "option-name") <- value` - set |
| 209 | + |
| 210 | +Options are protocol-specific (e.g., `"req:resend-time"`, |
| 211 | +`"sub:prefnew"`). |
| 212 | + |
| 213 | +## Build System |
| 214 | + |
| 215 | +- `configure` - POSIX shell script for Unix-like systems |
| 216 | +- `configure.win` - Windows configuration |
| 217 | +- `configure.ucrt` - UCRT-specific Windows config |
| 218 | +- Requires `cmake` if compiling bundled libraries (no longer requires |
| 219 | + `xz` as libraries are pre-extracted) |
| 220 | +- Environment variables: `INCLUDE_DIR`, `LIB_DIR`, `NANONEXT_LIBS` |
| 221 | + |
| 222 | +## Documentation |
| 223 | + |
| 224 | +- Roxygen2-based documentation in R files |
| 225 | +- Main vignette: `vignettes/nanonext.Rmd` |
| 226 | +- Website: <https://nanonext.r-lib.org> |
| 227 | +- Generated with pkgdown |
| 228 | + |
| 229 | +## Code Style |
| 230 | + |
| 231 | +- C code follows NNG conventions with `nano_` prefixes for package |
| 232 | + functions |
| 233 | +- R code uses snake_case for functions |
| 234 | +- Clear separation between user-facing and internal functions |
| 235 | + (`.function` prefix for internal) |
| 236 | +- Extensive use of macros for code clarity (`NANO_PTR`, `NANO_DATAPTR`, |
| 237 | + etc.) |
0 commit comments