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/lib/nng/message.rb CHANGED
@@ -1,151 +1,151 @@
1
- # frozen_string_literal: true
2
-
3
- module NNG
4
- # NNG message wrapper
5
- class Message
6
- attr_reader :msg_ptr
7
-
8
- # Create a new message
9
- # @param size [Integer] initial size
10
- def initialize(size: 0)
11
- @msg_ptr = ::FFI::MemoryPointer.new(:pointer)
12
- ret = FFI.nng_msg_alloc(@msg_ptr, size)
13
- FFI.check_error(ret, "Allocate message")
14
- @msg = @msg_ptr.read_pointer
15
- @freed = false
16
- end
17
-
18
- # Append data to message body
19
- # @param data [String] data to append
20
- # @return [self]
21
- def append(data)
22
- check_freed
23
- data_str = data.to_s
24
- data_ptr = ::FFI::MemoryPointer.new(:uint8, data_str.bytesize)
25
- data_ptr.put_bytes(0, data_str)
26
-
27
- ret = FFI.nng_msg_append(@msg, data_ptr, data_str.bytesize)
28
- FFI.check_error(ret, "Append to message")
29
- self
30
- end
31
-
32
- # Insert data at the beginning of message body
33
- # @param data [String] data to insert
34
- # @return [self]
35
- def insert(data)
36
- check_freed
37
- data_str = data.to_s
38
- data_ptr = ::FFI::MemoryPointer.new(:uint8, data_str.bytesize)
39
- data_ptr.put_bytes(0, data_str)
40
-
41
- ret = FFI.nng_msg_insert(@msg, data_ptr, data_str.bytesize)
42
- FFI.check_error(ret, "Insert to message")
43
- self
44
- end
45
-
46
- # Get message body
47
- # @return [String] message body
48
- def body
49
- check_freed
50
- body_ptr = FFI.nng_msg_body(@msg)
51
- length = FFI.nng_msg_len(@msg)
52
- body_ptr.read_bytes(length)
53
- end
54
-
55
- # Get message body length
56
- # @return [Integer] length in bytes
57
- def length
58
- check_freed
59
- FFI.nng_msg_len(@msg)
60
- end
61
- alias size length
62
-
63
- # Get message header
64
- # @return [String] message header
65
- def header
66
- check_freed
67
- header_ptr = FFI.nng_msg_header(@msg)
68
- length = FFI.nng_msg_header_len(@msg)
69
- header_ptr.read_bytes(length)
70
- end
71
-
72
- # Get message header length
73
- # @return [Integer] length in bytes
74
- def header_length
75
- check_freed
76
- FFI.nng_msg_header_len(@msg)
77
- end
78
-
79
- # Append data to message header
80
- # @param data [String] data to append
81
- # @return [self]
82
- def header_append(data)
83
- check_freed
84
- data_str = data.to_s
85
- data_ptr = ::FFI::MemoryPointer.new(:uint8, data_str.bytesize)
86
- data_ptr.put_bytes(0, data_str)
87
-
88
- ret = FFI.nng_msg_header_append(@msg, data_ptr, data_str.bytesize)
89
- FFI.check_error(ret, "Append to message header")
90
- self
91
- end
92
-
93
- # Clear message body
94
- # @return [self]
95
- def clear
96
- check_freed
97
- FFI.nng_msg_clear(@msg)
98
- self
99
- end
100
-
101
- # Clear message header
102
- # @return [self]
103
- def header_clear
104
- check_freed
105
- FFI.nng_msg_header_clear(@msg)
106
- self
107
- end
108
-
109
- # Duplicate message
110
- # @return [Message] duplicated message
111
- def dup
112
- check_freed
113
- dup_ptr = ::FFI::MemoryPointer.new(:pointer)
114
- ret = FFI.nng_msg_dup(dup_ptr, @msg)
115
- FFI.check_error(ret, "Duplicate message")
116
-
117
- new_msg = allocate
118
- new_msg.instance_variable_set(:@msg, dup_ptr.read_pointer)
119
- new_msg.instance_variable_set(:@msg_ptr, dup_ptr)
120
- new_msg.instance_variable_set(:@freed, false)
121
- new_msg
122
- end
123
-
124
- # Free the message
125
- # @return [nil]
126
- def free
127
- return if @freed
128
- FFI.nng_msg_free(@msg)
129
- @freed = true
130
- nil
131
- end
132
-
133
- # Check if message is freed
134
- # @return [Boolean]
135
- def freed?
136
- @freed
137
- end
138
-
139
- # Get the internal message pointer (for use with send/recv)
140
- # @return [FFI::Pointer]
141
- def to_ptr
142
- @msg
143
- end
144
-
145
- private
146
-
147
- def check_freed
148
- raise StateError, "Message has been freed" if @freed
149
- end
150
- end
151
- end
1
+ # frozen_string_literal: true
2
+
3
+ module NNG
4
+ # NNG message wrapper
5
+ class Message
6
+ attr_reader :msg_ptr
7
+
8
+ # Create a new message
9
+ # @param size [Integer] initial size
10
+ def initialize(size: 0)
11
+ @msg_ptr = ::FFI::MemoryPointer.new(:pointer)
12
+ ret = FFI.nng_msg_alloc(@msg_ptr, size)
13
+ FFI.check_error(ret, "Allocate message")
14
+ @msg = @msg_ptr.read_pointer
15
+ @freed = false
16
+ end
17
+
18
+ # Append data to message body
19
+ # @param data [String] data to append
20
+ # @return [self]
21
+ def append(data)
22
+ check_freed
23
+ data_str = data.to_s
24
+ data_ptr = ::FFI::MemoryPointer.new(:uint8, data_str.bytesize)
25
+ data_ptr.put_bytes(0, data_str)
26
+
27
+ ret = FFI.nng_msg_append(@msg, data_ptr, data_str.bytesize)
28
+ FFI.check_error(ret, "Append to message")
29
+ self
30
+ end
31
+
32
+ # Insert data at the beginning of message body
33
+ # @param data [String] data to insert
34
+ # @return [self]
35
+ def insert(data)
36
+ check_freed
37
+ data_str = data.to_s
38
+ data_ptr = ::FFI::MemoryPointer.new(:uint8, data_str.bytesize)
39
+ data_ptr.put_bytes(0, data_str)
40
+
41
+ ret = FFI.nng_msg_insert(@msg, data_ptr, data_str.bytesize)
42
+ FFI.check_error(ret, "Insert to message")
43
+ self
44
+ end
45
+
46
+ # Get message body
47
+ # @return [String] message body
48
+ def body
49
+ check_freed
50
+ body_ptr = FFI.nng_msg_body(@msg)
51
+ length = FFI.nng_msg_len(@msg)
52
+ body_ptr.read_bytes(length)
53
+ end
54
+
55
+ # Get message body length
56
+ # @return [Integer] length in bytes
57
+ def length
58
+ check_freed
59
+ FFI.nng_msg_len(@msg)
60
+ end
61
+ alias size length
62
+
63
+ # Get message header
64
+ # @return [String] message header
65
+ def header
66
+ check_freed
67
+ header_ptr = FFI.nng_msg_header(@msg)
68
+ length = FFI.nng_msg_header_len(@msg)
69
+ header_ptr.read_bytes(length)
70
+ end
71
+
72
+ # Get message header length
73
+ # @return [Integer] length in bytes
74
+ def header_length
75
+ check_freed
76
+ FFI.nng_msg_header_len(@msg)
77
+ end
78
+
79
+ # Append data to message header
80
+ # @param data [String] data to append
81
+ # @return [self]
82
+ def header_append(data)
83
+ check_freed
84
+ data_str = data.to_s
85
+ data_ptr = ::FFI::MemoryPointer.new(:uint8, data_str.bytesize)
86
+ data_ptr.put_bytes(0, data_str)
87
+
88
+ ret = FFI.nng_msg_header_append(@msg, data_ptr, data_str.bytesize)
89
+ FFI.check_error(ret, "Append to message header")
90
+ self
91
+ end
92
+
93
+ # Clear message body
94
+ # @return [self]
95
+ def clear
96
+ check_freed
97
+ FFI.nng_msg_clear(@msg)
98
+ self
99
+ end
100
+
101
+ # Clear message header
102
+ # @return [self]
103
+ def header_clear
104
+ check_freed
105
+ FFI.nng_msg_header_clear(@msg)
106
+ self
107
+ end
108
+
109
+ # Duplicate message
110
+ # @return [Message] duplicated message
111
+ def dup
112
+ check_freed
113
+ dup_ptr = ::FFI::MemoryPointer.new(:pointer)
114
+ ret = FFI.nng_msg_dup(dup_ptr, @msg)
115
+ FFI.check_error(ret, "Duplicate message")
116
+
117
+ new_msg = allocate
118
+ new_msg.instance_variable_set(:@msg, dup_ptr.read_pointer)
119
+ new_msg.instance_variable_set(:@msg_ptr, dup_ptr)
120
+ new_msg.instance_variable_set(:@freed, false)
121
+ new_msg
122
+ end
123
+
124
+ # Free the message
125
+ # @return [nil]
126
+ def free
127
+ return if @freed
128
+ FFI.nng_msg_free(@msg)
129
+ @freed = true
130
+ nil
131
+ end
132
+
133
+ # Check if message is freed
134
+ # @return [Boolean]
135
+ def freed?
136
+ @freed
137
+ end
138
+
139
+ # Get the internal message pointer (for use with send/recv)
140
+ # @return [FFI::Pointer]
141
+ def to_ptr
142
+ @msg
143
+ end
144
+
145
+ private
146
+
147
+ def check_freed
148
+ raise StateError, "Message has been freed" if @freed
149
+ end
150
+ end
151
+ end
data/lib/nng/protocols.rb CHANGED
@@ -1,96 +1,96 @@
1
- # frozen_string_literal: true
2
-
3
- module NNG
4
- # Protocol-specific socket opening functions
5
- module Protocols
6
- # Dynamically load protocol functions from libnng
7
- def self.attach_protocol_functions
8
- extend ::FFI::Library
9
-
10
- # Use the same library as FFI module
11
- ffi_lib NNG::FFI.loaded_lib_path
12
-
13
- # Pair protocols
14
- attach_function :nng_pair0_open, [FFI::NngSocket.by_ref], :int
15
- attach_function :nng_pair0_open_raw, [FFI::NngSocket.by_ref], :int
16
- attach_function :nng_pair1_open, [FFI::NngSocket.by_ref], :int
17
- attach_function :nng_pair1_open_raw, [FFI::NngSocket.by_ref], :int
18
- attach_function :nng_pair1_open_poly, [FFI::NngSocket.by_ref], :int
19
-
20
- # Push/Pull protocols
21
- attach_function :nng_push0_open, [FFI::NngSocket.by_ref], :int
22
- attach_function :nng_push0_open_raw, [FFI::NngSocket.by_ref], :int
23
- attach_function :nng_pull0_open, [FFI::NngSocket.by_ref], :int
24
- attach_function :nng_pull0_open_raw, [FFI::NngSocket.by_ref], :int
25
-
26
- # Pub/Sub protocols
27
- attach_function :nng_pub0_open, [FFI::NngSocket.by_ref], :int
28
- attach_function :nng_pub0_open_raw, [FFI::NngSocket.by_ref], :int
29
- attach_function :nng_sub0_open, [FFI::NngSocket.by_ref], :int
30
- attach_function :nng_sub0_open_raw, [FFI::NngSocket.by_ref], :int
31
-
32
- # Req/Rep protocols
33
- attach_function :nng_req0_open, [FFI::NngSocket.by_ref], :int
34
- attach_function :nng_req0_open_raw, [FFI::NngSocket.by_ref], :int
35
- attach_function :nng_rep0_open, [FFI::NngSocket.by_ref], :int
36
- attach_function :nng_rep0_open_raw, [FFI::NngSocket.by_ref], :int
37
-
38
- # Surveyor/Respondent protocols
39
- attach_function :nng_surveyor0_open, [FFI::NngSocket.by_ref], :int
40
- attach_function :nng_surveyor0_open_raw, [FFI::NngSocket.by_ref], :int
41
- attach_function :nng_respondent0_open, [FFI::NngSocket.by_ref], :int
42
- attach_function :nng_respondent0_open_raw, [FFI::NngSocket.by_ref], :int
43
-
44
- # Bus protocol
45
- attach_function :nng_bus0_open, [FFI::NngSocket.by_ref], :int
46
- attach_function :nng_bus0_open_raw, [FFI::NngSocket.by_ref], :int
47
- rescue ::FFI::NotFoundError => e
48
- # Some protocols might not be available in all builds
49
- warn "Warning: Some NNG protocol functions not available: #{e.message}"
50
- end
51
-
52
- # Protocol name to function mapping
53
- PROTOCOL_FUNCTIONS = {
54
- pair0: :nng_pair0_open,
55
- pair1: :nng_pair1_open,
56
- pair: :nng_pair1_open, # alias for pair1
57
- push0: :nng_push0_open,
58
- push: :nng_push0_open,
59
- pull0: :nng_pull0_open,
60
- pull: :nng_pull0_open,
61
- pub0: :nng_pub0_open,
62
- pub: :nng_pub0_open,
63
- sub0: :nng_sub0_open,
64
- sub: :nng_sub0_open,
65
- req0: :nng_req0_open,
66
- req: :nng_req0_open,
67
- rep0: :nng_rep0_open,
68
- rep: :nng_rep0_open,
69
- surveyor0: :nng_surveyor0_open,
70
- surveyor: :nng_surveyor0_open,
71
- respondent0: :nng_respondent0_open,
72
- respondent: :nng_respondent0_open,
73
- bus0: :nng_bus0_open,
74
- bus: :nng_bus0_open
75
- }.freeze
76
-
77
- # Open a socket for the given protocol
78
- # @param protocol [Symbol] protocol name (:pair0, :pair1, :push, :pull, etc.)
79
- # @param raw [Boolean] open in raw mode
80
- # @return [FFI::NngSocket] opened socket
81
- def self.open_socket(protocol, raw: false)
82
- func_name = PROTOCOL_FUNCTIONS[protocol]
83
- raise ArgumentError, "Unknown protocol: #{protocol}" unless func_name
84
-
85
- func_name = "#{func_name}_raw".to_sym if raw
86
-
87
- socket = FFI.socket_initializer
88
- ret = send(func_name, socket)
89
- FFI.check_error(ret, "Opening #{protocol} socket")
90
- socket
91
- end
92
- end
93
- end
94
-
95
- # Auto-load protocol functions
96
- NNG::Protocols.attach_protocol_functions
1
+ # frozen_string_literal: true
2
+
3
+ module NNG
4
+ # Protocol-specific socket opening functions
5
+ module Protocols
6
+ # Dynamically load protocol functions from libnng
7
+ def self.attach_protocol_functions
8
+ extend ::FFI::Library
9
+
10
+ # Use the same library as FFI module
11
+ ffi_lib NNG::FFI.loaded_lib_path
12
+
13
+ # Pair protocols
14
+ attach_function :nng_pair0_open, [FFI::NngSocket.by_ref], :int
15
+ attach_function :nng_pair0_open_raw, [FFI::NngSocket.by_ref], :int
16
+ attach_function :nng_pair1_open, [FFI::NngSocket.by_ref], :int
17
+ attach_function :nng_pair1_open_raw, [FFI::NngSocket.by_ref], :int
18
+ attach_function :nng_pair1_open_poly, [FFI::NngSocket.by_ref], :int
19
+
20
+ # Push/Pull protocols
21
+ attach_function :nng_push0_open, [FFI::NngSocket.by_ref], :int
22
+ attach_function :nng_push0_open_raw, [FFI::NngSocket.by_ref], :int
23
+ attach_function :nng_pull0_open, [FFI::NngSocket.by_ref], :int
24
+ attach_function :nng_pull0_open_raw, [FFI::NngSocket.by_ref], :int
25
+
26
+ # Pub/Sub protocols
27
+ attach_function :nng_pub0_open, [FFI::NngSocket.by_ref], :int
28
+ attach_function :nng_pub0_open_raw, [FFI::NngSocket.by_ref], :int
29
+ attach_function :nng_sub0_open, [FFI::NngSocket.by_ref], :int
30
+ attach_function :nng_sub0_open_raw, [FFI::NngSocket.by_ref], :int
31
+
32
+ # Req/Rep protocols
33
+ attach_function :nng_req0_open, [FFI::NngSocket.by_ref], :int
34
+ attach_function :nng_req0_open_raw, [FFI::NngSocket.by_ref], :int
35
+ attach_function :nng_rep0_open, [FFI::NngSocket.by_ref], :int
36
+ attach_function :nng_rep0_open_raw, [FFI::NngSocket.by_ref], :int
37
+
38
+ # Surveyor/Respondent protocols
39
+ attach_function :nng_surveyor0_open, [FFI::NngSocket.by_ref], :int
40
+ attach_function :nng_surveyor0_open_raw, [FFI::NngSocket.by_ref], :int
41
+ attach_function :nng_respondent0_open, [FFI::NngSocket.by_ref], :int
42
+ attach_function :nng_respondent0_open_raw, [FFI::NngSocket.by_ref], :int
43
+
44
+ # Bus protocol
45
+ attach_function :nng_bus0_open, [FFI::NngSocket.by_ref], :int
46
+ attach_function :nng_bus0_open_raw, [FFI::NngSocket.by_ref], :int
47
+ rescue ::FFI::NotFoundError => e
48
+ # Some protocols might not be available in all builds
49
+ warn "Warning: Some NNG protocol functions not available: #{e.message}"
50
+ end
51
+
52
+ # Protocol name to function mapping
53
+ PROTOCOL_FUNCTIONS = {
54
+ pair0: :nng_pair0_open,
55
+ pair1: :nng_pair1_open,
56
+ pair: :nng_pair1_open, # alias for pair1
57
+ push0: :nng_push0_open,
58
+ push: :nng_push0_open,
59
+ pull0: :nng_pull0_open,
60
+ pull: :nng_pull0_open,
61
+ pub0: :nng_pub0_open,
62
+ pub: :nng_pub0_open,
63
+ sub0: :nng_sub0_open,
64
+ sub: :nng_sub0_open,
65
+ req0: :nng_req0_open,
66
+ req: :nng_req0_open,
67
+ rep0: :nng_rep0_open,
68
+ rep: :nng_rep0_open,
69
+ surveyor0: :nng_surveyor0_open,
70
+ surveyor: :nng_surveyor0_open,
71
+ respondent0: :nng_respondent0_open,
72
+ respondent: :nng_respondent0_open,
73
+ bus0: :nng_bus0_open,
74
+ bus: :nng_bus0_open
75
+ }.freeze
76
+
77
+ # Open a socket for the given protocol
78
+ # @param protocol [Symbol] protocol name (:pair0, :pair1, :push, :pull, etc.)
79
+ # @param raw [Boolean] open in raw mode
80
+ # @return [FFI::NngSocket] opened socket
81
+ def self.open_socket(protocol, raw: false)
82
+ func_name = PROTOCOL_FUNCTIONS[protocol]
83
+ raise ArgumentError, "Unknown protocol: #{protocol}" unless func_name
84
+
85
+ func_name = "#{func_name}_raw".to_sym if raw
86
+
87
+ socket = FFI.socket_initializer
88
+ ret = send(func_name, socket)
89
+ FFI.check_error(ret, "Opening #{protocol} socket")
90
+ socket
91
+ end
92
+ end
93
+ end
94
+
95
+ # Auto-load protocol functions
96
+ NNG::Protocols.attach_protocol_functions