nng-ruby 0.1.2 → 1.0.1

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.
data/examples/reqrep.rb CHANGED
@@ -1,54 +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!"
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/ext/nng/extconf.rb CHANGED
@@ -1,71 +1,80 @@
1
- # frozen_string_literal: true
2
-
3
- require 'mkmf'
4
-
5
- # This extconf.rb is used to capture gem install options
6
- # It doesn't compile anything, just saves configuration
7
-
8
- def save_config(config)
9
- config_file = File.join(__dir__, 'nng_config.rb')
10
- File.write(config_file, <<~RUBY)
11
- # frozen_string_literal: true
12
- # Auto-generated by extconf.rb during gem installation
13
- # DO NOT EDIT THIS FILE MANUALLY
14
-
15
- module NNG
16
- module InstallConfig
17
- CONFIG = #{config.inspect}
18
- end
19
- end
20
- RUBY
21
- puts "NNG configuration saved to #{config_file}"
22
- end
23
-
24
- # Parse installation options
25
- config = {}
26
-
27
- # --with-nng-dir=/path/to/nng
28
- if nng_dir = with_config('nng-dir')
29
- config[:nng_dir] = nng_dir
30
- puts "Using NNG directory: #{nng_dir}"
31
- end
32
-
33
- # --with-nng-lib=/path/to/libnng.so
34
- if nng_lib = with_config('nng-lib')
35
- config[:nng_lib] = nng_lib
36
- puts "Using NNG library: #{nng_lib}"
37
- end
38
-
39
- # --with-nng-include=/path/to/nng/include (for future use)
40
- if nng_include = with_config('nng-include')
41
- config[:nng_include] = nng_include
42
- puts "Using NNG include directory: #{nng_include}"
43
- end
44
-
45
- # Save configuration
46
- save_config(config)
47
-
48
- # We don't actually compile anything since we use FFI
49
- # Just create a dummy Makefile
50
- File.write('Makefile', <<~MAKEFILE)
51
- .PHONY: all install clean
52
-
53
- all:
54
- \t@echo "NNG Ruby uses FFI, no compilation needed"
55
-
56
- install:
57
- \t@echo "NNG configuration saved"
58
-
59
- clean:
60
- \t@echo "Nothing to clean"
61
- MAKEFILE
62
-
63
- puts "\nNNG Ruby gem configuration complete!"
64
- puts "No native compilation needed (using FFI)"
65
- puts ""
66
- if config.any?
67
- puts "Custom configuration:"
68
- config.each { |k, v| puts " #{k}: #{v}" }
69
- else
70
- puts "Using default bundled libnng.so.1.8.0"
71
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkmf'
4
+
5
+ # This extconf.rb is used to capture gem install options
6
+ # It doesn't compile anything, just saves configuration
7
+
8
+ def save_config(config)
9
+ config_file = File.join(__dir__, 'nng_config.rb')
10
+ File.write(config_file, <<~RUBY)
11
+ # frozen_string_literal: true
12
+ # Auto-generated by extconf.rb during gem installation
13
+ # DO NOT EDIT THIS FILE MANUALLY
14
+
15
+ module NNG
16
+ module InstallConfig
17
+ CONFIG = #{config.inspect}
18
+ end
19
+ end
20
+ RUBY
21
+ puts "NNG configuration saved to #{config_file}"
22
+ end
23
+
24
+ # Parse installation options
25
+ config = {}
26
+
27
+ # --with-nng-dir=/path/to/nng
28
+ if nng_dir = with_config('nng-dir')
29
+ config[:nng_dir] = nng_dir
30
+ puts "Using NNG directory: #{nng_dir}"
31
+ end
32
+
33
+ # --with-nng-lib=/path/to/libnng.so
34
+ if nng_lib = with_config('nng-lib')
35
+ config[:nng_lib] = nng_lib
36
+ puts "Using NNG library: #{nng_lib}"
37
+ end
38
+
39
+ # --with-nng-include=/path/to/nng/include (for future use)
40
+ if nng_include = with_config('nng-include')
41
+ config[:nng_include] = nng_include
42
+ puts "Using NNG include directory: #{nng_include}"
43
+ end
44
+
45
+ # Save configuration
46
+ save_config(config)
47
+
48
+ # We don't actually compile anything since we use FFI
49
+ # Just create a dummy Makefile
50
+ File.write('Makefile', <<~MAKEFILE)
51
+ .PHONY: all install clean
52
+
53
+ all:
54
+ \t@echo "NNG Ruby uses FFI, no compilation needed"
55
+
56
+ install:
57
+ \t@echo "NNG configuration saved"
58
+
59
+ clean:
60
+ \t@echo "Nothing to clean"
61
+ MAKEFILE
62
+
63
+ puts "\nNNG Ruby gem configuration complete!"
64
+ puts "No native compilation needed (using FFI)"
65
+ puts ""
66
+ if config.any?
67
+ puts "Custom configuration:"
68
+ config.each { |k, v| puts " #{k}: #{v}" }
69
+ else
70
+ require 'rbconfig'
71
+ platform = case RbConfig::CONFIG['host_os']
72
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
73
+ 'nng.dll (Windows)'
74
+ when /darwin|mac os/
75
+ 'libnng.dylib (macOS)'
76
+ else
77
+ 'libnng.so.1.11.0 (Linux)'
78
+ end
79
+ puts "Using default bundled #{platform}"
80
+ end
Binary file
data/ext/nng/nng.dll ADDED
Binary file
data/lib/nng/errors.rb CHANGED
@@ -1,48 +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
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