nng-ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +39 -0
- data/Gemfile +10 -0
- data/LICENSE +21 -0
- data/README.md +383 -0
- data/Rakefile +22 -0
- data/examples/pair.rb +48 -0
- data/examples/pubsub.rb +51 -0
- data/examples/reqrep.rb +54 -0
- data/lib/nng/errors.rb +48 -0
- data/lib/nng/ffi.rb +376 -0
- data/lib/nng/message.rb +151 -0
- data/lib/nng/protocols.rb +96 -0
- data/lib/nng/socket.rb +193 -0
- data/lib/nng/version.rb +5 -0
- data/lib/nng.rb +52 -0
- data/nng.gemspec +65 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4e86c823d0aba2bb0e30c5bd055c604d4fdd309873bb28da77aea1bd76bc361c
|
4
|
+
data.tar.gz: 52cbdd15b10332011993c95f3911145ebd358edf2d894ba2aee0337d3bcb1ad6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d10a1870a56c24ce1cb7f6128e1c21b3ddc8d4ecb4cd1658d645b3fcc4577de1b7cb1c3fe5fc5d56df3289a60c80156b110099ecae7b7ba90730a14b252bbab
|
7
|
+
data.tar.gz: 8336bada3d8f6212e1f7849cb7cd4e8256c42135311ebcd85c290c011bad085a4d184dbefe9f2f0f7418e841fcc8034934d1957b07576821f5eaed9366de80d6
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [0.1.0] - 2025-10-03
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- Initial release of NNG Ruby bindings
|
12
|
+
- Complete FFI bindings for NNG 1.8.0 (libnng 1.9.0)
|
13
|
+
- Support for all scalability protocols:
|
14
|
+
- Pair (pair0, pair1)
|
15
|
+
- Push/Pull (push0, pull0)
|
16
|
+
- Pub/Sub (pub0, sub0)
|
17
|
+
- Req/Rep (req0, rep0)
|
18
|
+
- Surveyor/Respondent (surveyor0, respondent0)
|
19
|
+
- Bus (bus0)
|
20
|
+
- Support for all transports:
|
21
|
+
- TCP
|
22
|
+
- IPC
|
23
|
+
- Inproc
|
24
|
+
- WebSocket
|
25
|
+
- TLS
|
26
|
+
- High-level Ruby API with automatic resource management
|
27
|
+
- Message-based communication (NNG::Message)
|
28
|
+
- Socket options and configuration
|
29
|
+
- Comprehensive error handling
|
30
|
+
- Bundled libnng shared library (no external dependencies)
|
31
|
+
- Full async I/O support
|
32
|
+
- Complete documentation and examples
|
33
|
+
- RSpec test suite
|
34
|
+
|
35
|
+
### Infrastructure
|
36
|
+
- Gem packaging with bundled shared library
|
37
|
+
- CI/CD ready structure
|
38
|
+
- YARD documentation support
|
39
|
+
- Example programs for all protocols
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Claude Code
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,383 @@
|
|
1
|
+
# NNG Ruby Bindings
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/nng-ruby)
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
5
|
+
|
6
|
+
Ruby bindings for [NNG (nanomsg-next-generation)](https://nng.nanomsg.org/), a lightweight messaging library.
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
- ✅ Complete FFI bindings for NNG 1.8.0 (libnng 1.9.0)
|
11
|
+
- ✅ All scalability protocols: Pair, Push/Pull, Pub/Sub, Req/Rep, Surveyor/Respondent, Bus
|
12
|
+
- ✅ All transports: TCP, IPC, Inproc, WebSocket, TLS
|
13
|
+
- ✅ High-level Ruby API with automatic resource management
|
14
|
+
- ✅ Message-based and byte-based communication
|
15
|
+
- ✅ Bundled libnng shared library (no external dependencies)
|
16
|
+
- ✅ Thread-safe
|
17
|
+
- ✅ Full async I/O support
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'nng-ruby'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
bundle install
|
31
|
+
```
|
32
|
+
|
33
|
+
Or install it yourself as:
|
34
|
+
|
35
|
+
```bash
|
36
|
+
gem install nng-ruby
|
37
|
+
```
|
38
|
+
|
39
|
+
## Quick Start
|
40
|
+
|
41
|
+
### Pair Protocol (Bidirectional)
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
require 'nng'
|
45
|
+
|
46
|
+
# Server
|
47
|
+
server = NNG::Socket.new(:pair1)
|
48
|
+
server.listen("tcp://127.0.0.1:5555")
|
49
|
+
|
50
|
+
# Client
|
51
|
+
client = NNG::Socket.new(:pair1)
|
52
|
+
client.dial("tcp://127.0.0.1:5555")
|
53
|
+
|
54
|
+
# Send and receive
|
55
|
+
client.send("Hello, NNG!")
|
56
|
+
puts server.recv # => "Hello, NNG!"
|
57
|
+
|
58
|
+
server.send("Hello back!")
|
59
|
+
puts client.recv # => "Hello back!"
|
60
|
+
|
61
|
+
# Cleanup
|
62
|
+
server.close
|
63
|
+
client.close
|
64
|
+
```
|
65
|
+
|
66
|
+
### Request/Reply Protocol
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require 'nng'
|
70
|
+
|
71
|
+
# Server (replier)
|
72
|
+
rep = NNG::Socket.new(:rep)
|
73
|
+
rep.listen("tcp://127.0.0.1:5556")
|
74
|
+
|
75
|
+
# Client (requester)
|
76
|
+
req = NNG::Socket.new(:req)
|
77
|
+
req.dial("tcp://127.0.0.1:5556")
|
78
|
+
|
79
|
+
# Send request and get reply
|
80
|
+
req.send("What is the answer?")
|
81
|
+
puts rep.recv # => "What is the answer?"
|
82
|
+
|
83
|
+
rep.send("42")
|
84
|
+
puts req.recv # => "42"
|
85
|
+
|
86
|
+
rep.close
|
87
|
+
req.close
|
88
|
+
```
|
89
|
+
|
90
|
+
### Publish/Subscribe Protocol
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'nng'
|
94
|
+
|
95
|
+
# Publisher
|
96
|
+
pub = NNG::Socket.new(:pub)
|
97
|
+
pub.listen("tcp://127.0.0.1:5557")
|
98
|
+
|
99
|
+
# Subscriber
|
100
|
+
sub = NNG::Socket.new(:sub)
|
101
|
+
sub.dial("tcp://127.0.0.1:5557")
|
102
|
+
sub.set_option("sub:subscribe", "") # Subscribe to all topics
|
103
|
+
|
104
|
+
# Publish messages
|
105
|
+
pub.send("Hello, subscribers!")
|
106
|
+
puts sub.recv # => "Hello, subscribers!"
|
107
|
+
|
108
|
+
pub.close
|
109
|
+
sub.close
|
110
|
+
```
|
111
|
+
|
112
|
+
### Push/Pull Protocol (Pipeline)
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
require 'nng'
|
116
|
+
|
117
|
+
# Producer
|
118
|
+
push = NNG::Socket.new(:push)
|
119
|
+
push.listen("tcp://127.0.0.1:5558")
|
120
|
+
|
121
|
+
# Consumer
|
122
|
+
pull = NNG::Socket.new(:pull)
|
123
|
+
pull.dial("tcp://127.0.0.1:5558")
|
124
|
+
|
125
|
+
# Send work
|
126
|
+
push.send("Task 1")
|
127
|
+
puts pull.recv # => "Task 1"
|
128
|
+
|
129
|
+
push.close
|
130
|
+
pull.close
|
131
|
+
```
|
132
|
+
|
133
|
+
## Supported Protocols
|
134
|
+
|
135
|
+
- **Pair** - Bidirectional 1:1 communication (`pair0`, `pair1`)
|
136
|
+
- **Push/Pull** - Unidirectional pipeline (`push0`, `pull0`)
|
137
|
+
- **Pub/Sub** - One-to-many distribution (`pub0`, `sub0`)
|
138
|
+
- **Req/Rep** - Request/reply pattern (`req0`, `rep0`)
|
139
|
+
- **Surveyor/Respondent** - Survey pattern (`surveyor0`, `respondent0`)
|
140
|
+
- **Bus** - Many-to-many (`bus0`)
|
141
|
+
|
142
|
+
## Supported Transports
|
143
|
+
|
144
|
+
- **TCP** - `tcp://host:port`
|
145
|
+
- **IPC** - `ipc:///path/to/socket`
|
146
|
+
- **Inproc** - `inproc://name`
|
147
|
+
- **WebSocket** - `ws://host:port/path`
|
148
|
+
- **TLS** - `tls+tcp://host:port`
|
149
|
+
|
150
|
+
## Advanced Usage
|
151
|
+
|
152
|
+
### Setting Timeouts
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
socket = NNG::Socket.new(:pair1)
|
156
|
+
socket.send_timeout = 5000 # 5 seconds
|
157
|
+
socket.recv_timeout = 5000 # 5 seconds
|
158
|
+
```
|
159
|
+
|
160
|
+
### Non-blocking I/O
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
require 'nng'
|
164
|
+
|
165
|
+
socket = NNG::Socket.new(:pair1)
|
166
|
+
socket.listen("tcp://127.0.0.1:5555")
|
167
|
+
|
168
|
+
begin
|
169
|
+
data = socket.recv(flags: NNG::FFI::NNG_FLAG_NONBLOCK)
|
170
|
+
puts "Received: #{data}"
|
171
|
+
rescue NNG::Error => e
|
172
|
+
puts "No data available: #{e.message}"
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
### Using Messages
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
require 'nng'
|
180
|
+
|
181
|
+
# Create a message
|
182
|
+
msg = NNG::Message.new
|
183
|
+
msg.append("Hello, ")
|
184
|
+
msg.append("World!")
|
185
|
+
puts msg.body # => "Hello, World!"
|
186
|
+
|
187
|
+
# Add header
|
188
|
+
msg.header_append("Type: Greeting")
|
189
|
+
|
190
|
+
# Duplicate message
|
191
|
+
msg2 = msg.dup
|
192
|
+
|
193
|
+
# Free message
|
194
|
+
msg.free
|
195
|
+
msg2.free
|
196
|
+
```
|
197
|
+
|
198
|
+
### Socket Options
|
199
|
+
|
200
|
+
```ruby
|
201
|
+
socket = NNG::Socket.new(:pub)
|
202
|
+
|
203
|
+
# Set options
|
204
|
+
socket.set_option("send-buffer", 8192)
|
205
|
+
socket.set_option("tcp-nodelay", true)
|
206
|
+
socket.set_option_ms("send-timeout", 1000)
|
207
|
+
|
208
|
+
# Get options
|
209
|
+
buffer_size = socket.get_option("send-buffer", type: :int)
|
210
|
+
nodelay = socket.get_option("tcp-nodelay", type: :bool)
|
211
|
+
```
|
212
|
+
|
213
|
+
## Examples
|
214
|
+
|
215
|
+
See the `examples/` directory for complete working examples:
|
216
|
+
|
217
|
+
- `examples/pair.rb` - Pair protocol
|
218
|
+
- `examples/reqrep.rb` - Request/Reply protocol
|
219
|
+
- `examples/pubsub.rb` - Publish/Subscribe protocol
|
220
|
+
|
221
|
+
Run examples:
|
222
|
+
|
223
|
+
```bash
|
224
|
+
ruby examples/pair.rb
|
225
|
+
ruby examples/reqrep.rb
|
226
|
+
ruby examples/pubsub.rb
|
227
|
+
```
|
228
|
+
|
229
|
+
## API Documentation
|
230
|
+
|
231
|
+
### NNG Module
|
232
|
+
|
233
|
+
- `NNG.version` - Gem version
|
234
|
+
- `NNG.lib_version` - NNG library version
|
235
|
+
- `NNG.fini` - Cleanup NNG (called automatically)
|
236
|
+
|
237
|
+
### NNG::Socket
|
238
|
+
|
239
|
+
#### Creating Sockets
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
socket = NNG::Socket.new(:pair1)
|
243
|
+
socket = NNG::Socket.new(:req, raw: false)
|
244
|
+
```
|
245
|
+
|
246
|
+
#### Connection Methods
|
247
|
+
|
248
|
+
- `listen(url, flags: 0)` - Listen on address
|
249
|
+
- `dial(url, flags: 0)` - Connect to address
|
250
|
+
- `close` - Close socket
|
251
|
+
- `closed?` - Check if closed
|
252
|
+
|
253
|
+
#### Send/Receive Methods
|
254
|
+
|
255
|
+
- `send(data, flags: 0)` - Send data
|
256
|
+
- `recv(flags: NNG::FFI::NNG_FLAG_ALLOC)` - Receive data
|
257
|
+
|
258
|
+
#### Option Methods
|
259
|
+
|
260
|
+
- `set_option(name, value)` - Set socket option
|
261
|
+
- `get_option(name, type: :int)` - Get socket option
|
262
|
+
- `set_option_ms(name, ms)` - Set timeout option
|
263
|
+
- `send_timeout=(ms)` - Set send timeout
|
264
|
+
- `recv_timeout=(ms)` - Set receive timeout
|
265
|
+
|
266
|
+
#### Information Methods
|
267
|
+
|
268
|
+
- `id` - Get socket ID
|
269
|
+
|
270
|
+
### NNG::Message
|
271
|
+
|
272
|
+
#### Creating Messages
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
msg = NNG::Message.new(size: 0)
|
276
|
+
```
|
277
|
+
|
278
|
+
#### Body Methods
|
279
|
+
|
280
|
+
- `append(data)` - Append to body
|
281
|
+
- `insert(data)` - Insert at beginning
|
282
|
+
- `body` - Get body content
|
283
|
+
- `length` / `size` - Get body length
|
284
|
+
- `clear` - Clear body
|
285
|
+
|
286
|
+
#### Header Methods
|
287
|
+
|
288
|
+
- `header_append(data)` - Append to header
|
289
|
+
- `header` - Get header content
|
290
|
+
- `header_length` - Get header length
|
291
|
+
- `header_clear` - Clear header
|
292
|
+
|
293
|
+
#### Other Methods
|
294
|
+
|
295
|
+
- `dup` - Duplicate message
|
296
|
+
- `free` - Free message
|
297
|
+
- `freed?` - Check if freed
|
298
|
+
|
299
|
+
### Error Handling
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
begin
|
303
|
+
socket.send("data")
|
304
|
+
rescue NNG::TimeoutError => e
|
305
|
+
puts "Timeout: #{e.message}"
|
306
|
+
rescue NNG::ConnectionRefused => e
|
307
|
+
puts "Connection refused: #{e.message}"
|
308
|
+
rescue NNG::Error => e
|
309
|
+
puts "NNG error: #{e.message}"
|
310
|
+
end
|
311
|
+
```
|
312
|
+
|
313
|
+
Available error classes:
|
314
|
+
- `NNG::Error` - Base error
|
315
|
+
- `NNG::TimeoutError`
|
316
|
+
- `NNG::ConnectionRefused`
|
317
|
+
- `NNG::ConnectionAborted`
|
318
|
+
- `NNG::ConnectionReset`
|
319
|
+
- `NNG::Closed`
|
320
|
+
- `NNG::AddressInUse`
|
321
|
+
- `NNG::NoMemory`
|
322
|
+
- `NNG::MessageSize`
|
323
|
+
- `NNG::ProtocolError`
|
324
|
+
- `NNG::StateError`
|
325
|
+
|
326
|
+
## Requirements
|
327
|
+
|
328
|
+
- Ruby 2.5 or later
|
329
|
+
- FFI gem
|
330
|
+
|
331
|
+
The NNG shared library (libnng.so.1.8.0) is bundled with the gem, so no external installation is required.
|
332
|
+
|
333
|
+
## Development
|
334
|
+
|
335
|
+
```bash
|
336
|
+
# Clone repository
|
337
|
+
git clone https://github.com/yourusername/nng-ruby.git
|
338
|
+
cd nng-ruby
|
339
|
+
|
340
|
+
# Install dependencies
|
341
|
+
bundle install
|
342
|
+
|
343
|
+
# Run tests
|
344
|
+
bundle exec rspec
|
345
|
+
|
346
|
+
# Run examples
|
347
|
+
ruby examples/pair.rb
|
348
|
+
```
|
349
|
+
|
350
|
+
## Contributing
|
351
|
+
|
352
|
+
1. Fork it
|
353
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
354
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
355
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
356
|
+
5. Create new Pull Request
|
357
|
+
|
358
|
+
## License
|
359
|
+
|
360
|
+
MIT License - see LICENSE file for details.
|
361
|
+
|
362
|
+
## Credits
|
363
|
+
|
364
|
+
- NNG library: https://nng.nanomsg.org/
|
365
|
+
- Original nanomsg: https://nanomsg.org/
|
366
|
+
- Created by Claude Code
|
367
|
+
|
368
|
+
## Links
|
369
|
+
|
370
|
+
- [NNG Documentation](https://nng.nanomsg.org/man/)
|
371
|
+
- [GitHub Repository](https://github.com/yourusername/nng-ruby)
|
372
|
+
- [RubyGems Page](https://rubygems.org/gems/nng-ruby)
|
373
|
+
|
374
|
+
## Version History
|
375
|
+
|
376
|
+
### 0.1.0 (2025-10-03)
|
377
|
+
- Initial release
|
378
|
+
- Complete NNG 1.8.0 API bindings
|
379
|
+
- All protocols and transports supported
|
380
|
+
- Bundled libnng.so.1.8.0
|
381
|
+
- High-level Ruby API
|
382
|
+
- Message support
|
383
|
+
- Examples and documentation
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
task default: :spec
|
9
|
+
|
10
|
+
desc 'Run examples'
|
11
|
+
task :examples do
|
12
|
+
Dir.glob('examples/*.rb').each do |file|
|
13
|
+
puts "\n=== Running #{file} ==="
|
14
|
+
system("ruby #{file}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'Generate YARD documentation'
|
19
|
+
task :yard do
|
20
|
+
require 'yard'
|
21
|
+
YARD::Rake::YardocTask.new
|
22
|
+
end
|
data/examples/pair.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative '../lib/nng'
|
5
|
+
|
6
|
+
# Example: Pair protocol (bidirectional communication)
|
7
|
+
|
8
|
+
puts "NNG Pair Protocol Example"
|
9
|
+
puts "=" * 50
|
10
|
+
|
11
|
+
# Create server socket
|
12
|
+
server = NNG::Socket.new(:pair1)
|
13
|
+
server.listen("tcp://127.0.0.1:5555")
|
14
|
+
puts "Server listening on tcp://127.0.0.1:5555"
|
15
|
+
|
16
|
+
# Create client socket in a thread
|
17
|
+
client_thread = Thread.new do
|
18
|
+
sleep 0.5 # Give server time to start
|
19
|
+
client = NNG::Socket.new(:pair1)
|
20
|
+
client.dial("tcp://127.0.0.1:5555")
|
21
|
+
puts "Client connected"
|
22
|
+
|
23
|
+
# Send message from client
|
24
|
+
client.send("Hello from client!")
|
25
|
+
puts "Client sent: Hello from client!"
|
26
|
+
|
27
|
+
# Receive response
|
28
|
+
response = client.recv
|
29
|
+
puts "Client received: #{response}"
|
30
|
+
|
31
|
+
client.close
|
32
|
+
end
|
33
|
+
|
34
|
+
# Server receives and responds
|
35
|
+
data = server.recv
|
36
|
+
puts "Server received: #{data}"
|
37
|
+
|
38
|
+
server.send("Hello from server!")
|
39
|
+
puts "Server sent: Hello from server!"
|
40
|
+
|
41
|
+
# Wait for client thread
|
42
|
+
client_thread.join
|
43
|
+
|
44
|
+
# Cleanup
|
45
|
+
server.close
|
46
|
+
|
47
|
+
puts "=" * 50
|
48
|
+
puts "Example completed!"
|
data/examples/pubsub.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative '../lib/nng'
|
5
|
+
|
6
|
+
# Example: Publish/Subscribe protocol
|
7
|
+
|
8
|
+
puts "NNG Publish/Subscribe Protocol Example"
|
9
|
+
puts "=" * 50
|
10
|
+
|
11
|
+
# Create publisher socket
|
12
|
+
pub = NNG::Socket.new(:pub)
|
13
|
+
pub.listen("tcp://127.0.0.1:5557")
|
14
|
+
puts "Publisher listening on tcp://127.0.0.1:5557"
|
15
|
+
|
16
|
+
# Create subscribers in threads
|
17
|
+
sub_threads = 3.times.map do |i|
|
18
|
+
Thread.new do
|
19
|
+
sleep 0.5 # Give publisher time to start
|
20
|
+
sub = NNG::Socket.new(:sub)
|
21
|
+
sub.dial("tcp://127.0.0.1:5557")
|
22
|
+
sub.set_option("sub:subscribe", "") # Subscribe to all topics
|
23
|
+
puts "Subscriber #{i + 1} connected"
|
24
|
+
|
25
|
+
# Receive 5 messages
|
26
|
+
5.times do
|
27
|
+
msg = sub.recv
|
28
|
+
puts "Subscriber #{i + 1} received: #{msg}"
|
29
|
+
end
|
30
|
+
|
31
|
+
sub.close
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Publisher sends 5 messages
|
36
|
+
sleep 1 # Give subscribers time to connect
|
37
|
+
5.times do |i|
|
38
|
+
message = "Broadcast message #{i + 1}"
|
39
|
+
pub.send(message)
|
40
|
+
puts "Publisher sent: #{message}"
|
41
|
+
sleep 0.2
|
42
|
+
end
|
43
|
+
|
44
|
+
# Wait for all subscribers
|
45
|
+
sub_threads.each(&:join)
|
46
|
+
|
47
|
+
# Cleanup
|
48
|
+
pub.close
|
49
|
+
|
50
|
+
puts "=" * 50
|
51
|
+
puts "Example completed!"
|
data/examples/reqrep.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative '../lib/nng'
|
5
|
+
|
6
|
+
# Example: Request/Reply protocol
|
7
|
+
|
8
|
+
puts "NNG Request/Reply Protocol Example"
|
9
|
+
puts "=" * 50
|
10
|
+
|
11
|
+
# Create reply (server) socket
|
12
|
+
rep = NNG::Socket.new(:rep)
|
13
|
+
rep.listen("tcp://127.0.0.1:5556")
|
14
|
+
puts "Reply server listening on tcp://127.0.0.1:5556"
|
15
|
+
|
16
|
+
# Create request (client) socket in a thread
|
17
|
+
req_thread = Thread.new do
|
18
|
+
sleep 0.5 # Give server time to start
|
19
|
+
req = NNG::Socket.new(:req)
|
20
|
+
req.dial("tcp://127.0.0.1:5556")
|
21
|
+
puts "Request client connected"
|
22
|
+
|
23
|
+
# Send 3 requests
|
24
|
+
3.times do |i|
|
25
|
+
request = "Request #{i + 1}"
|
26
|
+
req.send(request)
|
27
|
+
puts "Client sent: #{request}"
|
28
|
+
|
29
|
+
response = req.recv
|
30
|
+
puts "Client received: #{response}"
|
31
|
+
sleep 0.1
|
32
|
+
end
|
33
|
+
|
34
|
+
req.close
|
35
|
+
end
|
36
|
+
|
37
|
+
# Server handles 3 requests
|
38
|
+
3.times do |i|
|
39
|
+
request = rep.recv
|
40
|
+
puts "Server received: #{request}"
|
41
|
+
|
42
|
+
reply = "Reply to #{request}"
|
43
|
+
rep.send(reply)
|
44
|
+
puts "Server sent: #{reply}"
|
45
|
+
end
|
46
|
+
|
47
|
+
# Wait for client thread
|
48
|
+
req_thread.join
|
49
|
+
|
50
|
+
# Cleanup
|
51
|
+
rep.close
|
52
|
+
|
53
|
+
puts "=" * 50
|
54
|
+
puts "Example completed!"
|
data/lib/nng/errors.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NNG
|
4
|
+
# Base error class for all NNG errors
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
# Connection errors
|
8
|
+
class ConnectionError < Error; end
|
9
|
+
class ConnectionRefused < ConnectionError; end
|
10
|
+
class ConnectionAborted < ConnectionError; end
|
11
|
+
class ConnectionReset < ConnectionError; end
|
12
|
+
|
13
|
+
# Timeout error
|
14
|
+
class TimeoutError < Error; end
|
15
|
+
|
16
|
+
# Resource errors
|
17
|
+
class ResourceError < Error; end
|
18
|
+
class AddressInUse < ResourceError; end
|
19
|
+
class NoMemory < ResourceError; end
|
20
|
+
|
21
|
+
# Protocol errors
|
22
|
+
class ProtocolError < Error; end
|
23
|
+
class MessageSize < ProtocolError; end
|
24
|
+
|
25
|
+
# State errors
|
26
|
+
class StateError < Error; end
|
27
|
+
class Closed < StateError; end
|
28
|
+
|
29
|
+
# Map error codes to exception classes
|
30
|
+
ERROR_MAP = {
|
31
|
+
FFI::NNG_ETIMEDOUT => TimeoutError,
|
32
|
+
FFI::NNG_ECONNREFUSED => ConnectionRefused,
|
33
|
+
FFI::NNG_ECONNABORTED => ConnectionAborted,
|
34
|
+
FFI::NNG_ECONNRESET => ConnectionReset,
|
35
|
+
FFI::NNG_ECLOSED => Closed,
|
36
|
+
FFI::NNG_EADDRINUSE => AddressInUse,
|
37
|
+
FFI::NNG_ENOMEM => NoMemory,
|
38
|
+
FFI::NNG_EMSGSIZE => MessageSize,
|
39
|
+
FFI::NNG_EPROTO => ProtocolError,
|
40
|
+
FFI::NNG_ESTATE => StateError
|
41
|
+
}.freeze
|
42
|
+
|
43
|
+
# Raise appropriate exception for error code
|
44
|
+
def self.raise_error(code, message)
|
45
|
+
error_class = ERROR_MAP[code] || Error
|
46
|
+
raise error_class, message
|
47
|
+
end
|
48
|
+
end
|