Debian Codegraph @

ruby/lib/core/session.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 module Qpid::Proton
20
21 # A session is the parent for senders and receivers.
22 #
23 # A Session has a single parent Qpid::Proton::Connection instance.
24 #
25 class Session < Endpoint
26 include Util::Deprecation
27
28 # @private
29 PROTON_METHOD_PREFIX = "pn_session"
30 # @private
31 include Util::Wrapper
32
33 # @!attribute incoming_capacity
34 #
35 # The incoming capacity of a session determines how much incoming message
36 # data the session will buffer. Note that if this value is less than the
37 # negotatied frame size of the transport, it will be rounded up to one full
38 # frame.
39 #
40 # @return [Integer] The incoing capacity of the session, measured in bytes.
41 #
42 proton_set_get :incoming_capacity
43
44 # @private
45 proton_get :attachments
46
47 # @!attribute [r] outgoing_bytes
48 #
49 # @return [Integer] The number of outgoing bytes currently being buffered.
50 #
51 proton_caller :outgoing_bytes
52
53 # @!attribute [r] incoming_bytes
54 #
55 # @return [Integer] The number of incomign bytes currently being buffered.
56 #
57 proton_caller :incoming_bytes
58
59 # Open the session
60 proton_caller :open
61
62 # @!attribute [r] state
63 #
64 # @return [Integer] The endpoint state.
65 #
66 proton_caller :state
67
68 # @private
69 def self.wrap(impl)
70 return nil if impl.nil?
71 self.fetch_instance(impl, :pn_session_attachments) || Session.new(impl)
72 end
73
74 # @private
75 def initialize(impl)
76 @impl = impl
77 self.class.store_instance(self, :pn_session_attachments)
78 end
79
80 # Close the local end of the session. The remote end may or may not be closed.
81 # @param error [Condition] Optional error condition to send with the close.
82 def close(error=nil)
83 Condition.assign(_local_condition, error)
84 Cproton.pn_session_close(@impl)
85 end
86
87 # @deprecated use {Connection#each_session}
88 def next(state_mask)
89 deprecated __method__, "Connection#each_session"
90 Session.wrap(Cproton.pn_session_next(@impl, state_mask))
91 end
92
93 # Returns the parent connection.
94 #
95 # @return [Connection] The connection.
96 #
97 def connection
98 Connection.wrap(Cproton.pn_session_connection(@impl))
99 end
100
101 # @deprecated use {#open_sender}
102 def sender(name)
103 deprecated __method__, "open_sender"
104 Sender.new(Cproton.pn_sender(@impl, name));
105 end
106
107 # @deprecated use {#open_receiver}
108 def receiver(name)
109 deprecated __method__, "open_receiver"
110 Receiver.new(Cproton.pn_receiver(@impl, name))
111 end
112
113 # Create and open a {Receiver} link, see {Receiver#open}
114 # @param opts [Hash] receiver options, see {Receiver#open}
115 # @return [Receiver]
116 def open_receiver(opts=nil)
117 Receiver.new(Cproton.pn_receiver(@impl, link_name(opts))).open(opts)
118 end
119
120 # Create and open a {Sender} link, see {#open}
121 # @param opts [Hash] sender options, see {Sender#open}
122 # @return [Sender]
123 def open_sender(opts=nil)
124 Sender.new(Cproton.pn_sender(@impl, link_name(opts))).open(opts)
125 end
126
127 # Get the links on this Session.
128 # @overload each_link
129 # @yieldparam l [Link] pass each link to block
130 # @overload each_link
131 # @return [Enumerator] enumerator over links
132 def each_link
133 return enum_for(:each_link) unless block_given?
134 l = Cproton.pn_link_head(Cproton.pn_session_connection(@impl), 0);
135 while l
136 link = Link.wrap(l)
137 yield link if link.session == self
138 l = Cproton.pn_link_next(l, 0)
139 end
140 self
141 end
142
143 # Get the {Sender} links - see {#each_link}
144 def each_sender() each_link.select { |l| l.sender? }; end
145
146 # Get the {Receiver} links - see {#each_link}
147 def each_receiver() each_link.select { |l| l.receiver? }; end
148
149 private
150
151 def link_name(opts)
152 (opts.respond_to?(:to_hash) && opts[:name]) || connection.link_name
153 end
154
155 def _local_condition
156 Cproton.pn_session_condition(@impl)
157 end
158
159 def _remote_condition # :nodoc:
160 Cproton.pn_session_remote_condition(@impl)
161 end
162
163 end
164
165 end