Class: Async::Bus::Protocol::Connection
- Inherits:
-
Object
- Object
- Async::Bus::Protocol::Connection
- Defined in:
- lib/async/bus/protocol/connection.rb
Instance Attribute Summary collapse
-
#packer ⇒ Object
readonly
Returns the value of attribute packer.
-
#transactions ⇒ Object
readonly
Returns the value of attribute transactions.
-
#unpacker ⇒ Object
readonly
Returns the value of attribute unpacker.
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #bind(name, object) ⇒ Object
- #close ⇒ Object
-
#initialize(peer, id) ⇒ Connection
constructor
A new instance of Connection.
- #invoke(name, arguments, options, &block) ⇒ Object
- #next_id ⇒ Object
- #proxy(object) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(peer, id) ⇒ Connection
Returns a new instance of Connection.
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/async/bus/protocol/connection.rb', line 46 def initialize(peer, id) @peer = peer @wrapper = Wrapper.new(self) @unpacker = @wrapper.unpacker(peer) @packer = @wrapper.packer(peer) @transactions = {} @id = id @objects = {} end |
Instance Attribute Details
#packer ⇒ Object (readonly)
Returns the value of attribute packer.
60 61 62 |
# File 'lib/async/bus/protocol/connection.rb', line 60 def packer @packer end |
#transactions ⇒ Object (readonly)
Returns the value of attribute transactions.
69 70 71 |
# File 'lib/async/bus/protocol/connection.rb', line 69 def transactions @transactions end |
#unpacker ⇒ Object (readonly)
Returns the value of attribute unpacker.
59 60 61 |
# File 'lib/async/bus/protocol/connection.rb', line 59 def unpacker @unpacker end |
Class Method Details
.client(peer) ⇒ Object
38 39 40 |
# File 'lib/async/bus/protocol/connection.rb', line 38 def self.client(peer) self.new(peer, 1) end |
.server(peer) ⇒ Object
42 43 44 |
# File 'lib/async/bus/protocol/connection.rb', line 42 def self.server(peer) self.new(peer, 2) end |
Instance Method Details
#[](name) ⇒ Object
83 84 85 |
# File 'lib/async/bus/protocol/connection.rb', line 83 def [](name) Proxy.new(self, name) end |
#bind(name, object) ⇒ Object
79 80 81 |
# File 'lib/async/bus/protocol/connection.rb', line 79 def bind(name, object) @objects[name] = object end |
#close ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/async/bus/protocol/connection.rb', line 129 def close @transactions.each do |id, transaction| transaction.close end @peer.close end |
#invoke(name, arguments, options, &block) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/async/bus/protocol/connection.rb', line 87 def invoke(name, arguments, , &block) id = self.next_id transaction = Transaction.new(self, id) @transactions[id] = transaction transaction.invoke(name, arguments, , &block) end |
#next_id ⇒ Object
62 63 64 65 66 67 |
# File 'lib/async/bus/protocol/connection.rb', line 62 def next_id id = @id @id += 2 return id end |
#proxy(object) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/async/bus/protocol/connection.rb', line 71 def proxy(object) name = "proxy:#{object_id}" bind(name, object) return name end |
#run ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/async/bus/protocol/connection.rb', line 96 def run # @unpacker.each do |message| @unpacker.each do || id = .shift if transaction = @transactions[id] transaction.received.enqueue() elsif .first == :invoke .shift transaction = Transaction.new(self, id) @transactions[id] = transaction name = .shift object = @objects[name] Async do transaction.accept(object, *) ensure transaction.close end else raise "Out of order message: #{}" end end ensure @transactions.each do |id, transaction| transaction.close end @transactions.clear end |