Class: Async::Bus::Protocol::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/async/bus/protocol/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#packerObject (readonly)

Returns the value of attribute packer.



60
61
62
# File 'lib/async/bus/protocol/connection.rb', line 60

def packer
  @packer
end

#transactionsObject (readonly)

Returns the value of attribute transactions.



69
70
71
# File 'lib/async/bus/protocol/connection.rb', line 69

def transactions
  @transactions
end

#unpackerObject (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

#closeObject



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, options, &block)
	id = self.next_id
	
	transaction = Transaction.new(self, id)
	@transactions[id] = transaction
	
	transaction.invoke(name, arguments, options, &block)
end

#next_idObject



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

#runObject



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 |message|
		id = message.shift
		
		if transaction = @transactions[id]
			transaction.received.enqueue(message)
		elsif message.first == :invoke
			message.shift
			
			transaction = Transaction.new(self, id)
			@transactions[id] = transaction
			
			name = message.shift
			object = @objects[name]
			
			Async do
				transaction.accept(object, *message)
			ensure
				transaction.close
			end
		else
			raise "Out of order message: #{message}"
		end
	end
ensure
	@transactions.each do |id, transaction|
		transaction.close
	end
	
	@transactions.clear
end