Module: Plug::Base
- Included in:
- ArrayFeeder, Blit, Proxy, Telson, UdpServer
- Defined in:
- lib/rbkb/plug/plug.rb
Instance Attribute Summary collapse
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#no_stop_on_unbind ⇒ Object
Returns the value of attribute no_stop_on_unbind.
-
#peers ⇒ Object
Returns the value of attribute peers.
-
#tls ⇒ Object
Returns the value of attribute tls.
-
#tls_opts ⇒ Object
Returns the value of attribute tls_opts.
-
#transport ⇒ Object
Returns the value of attribute transport.
Instance Method Summary collapse
- #connection_completed ⇒ Object
- #initialize(transport, opts = {}) ⇒ Object
- #name ⇒ Object
-
#plug_peer ⇒ Object
plug_peer creates a peering association for a given peer based on get_peername.
-
#plug_receive(dat) ⇒ Object
plug_receive is used by receive_data to divert incoming messages.
- #post_init ⇒ Object
- #receive_data(dat) ⇒ Object
-
#say(dat, sender) ⇒ Object
This instance of the say method is an abstract stub and just “dumps” the message.
- #unbind ⇒ Object
Instance Attribute Details
#kind ⇒ Object
Returns the value of attribute kind.
40 41 42 |
# File 'lib/rbkb/plug/plug.rb', line 40 def kind @kind end |
#no_stop_on_unbind ⇒ Object
Returns the value of attribute no_stop_on_unbind.
40 41 42 |
# File 'lib/rbkb/plug/plug.rb', line 40 def no_stop_on_unbind @no_stop_on_unbind end |
#peers ⇒ Object
Returns the value of attribute peers.
40 41 42 |
# File 'lib/rbkb/plug/plug.rb', line 40 def peers @peers end |
#tls ⇒ Object
Returns the value of attribute tls.
40 41 42 |
# File 'lib/rbkb/plug/plug.rb', line 40 def tls @tls end |
#tls_opts ⇒ Object
Returns the value of attribute tls_opts.
40 41 42 |
# File 'lib/rbkb/plug/plug.rb', line 40 def tls_opts @tls_opts end |
#transport ⇒ Object
Returns the value of attribute transport.
40 41 42 |
# File 'lib/rbkb/plug/plug.rb', line 40 def transport @transport end |
Instance Method Details
#connection_completed ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/rbkb/plug/plug.rb', line 113 def connection_completed peer = plug_peer UI.log "** #{name} CONNECTED TO #{peer.name}" if tls start_tls(tls_opts || {}) end return peer end |
#initialize(transport, opts = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rbkb/plug/plug.rb', line 42 def initialize(transport, opts={}) # raise "Invalid transport #{transport.inspect}" unless (:UDP, :TCP).include?(transport) @transport = transport @peers = PeerList.new(self) opts.each_pair do |k,v| accessor = k.to_s + "=" if self.respond_to?(accessor) self.send(accessor, v) else raise "Bad attribute: #{k}" end end end |
#name ⇒ Object
57 58 59 60 61 |
# File 'lib/rbkb/plug/plug.rb', line 57 def name sn = get_sockname addr = sn ? Socket.unpack_sockaddr_in(sn).reverse.join(":") : "PENDING" "#{kind.to_s.upcase}-#{addr}(#{@transport})" end |
#plug_peer ⇒ Object
plug_peer creates a peering association for a given peer based on get_peername. The existing or newly created peer object is returned.
66 67 68 69 |
# File 'lib/rbkb/plug/plug.rb', line 66 def plug_peer paddr = get_peername peer = (@peers.find_peer(paddr) || @peers.add_peer(paddr) ) end |
#plug_receive(dat) ⇒ Object
plug_receive is used by receive_data to divert incoming messages. The “peer” is added if it is not already present. This instance will check whether # a peer is “muted” and will return the peer if not. This method can be overriden by child classes to implement additional checks. It receives “dat” so that such checks can optionally make forwarding decisions based on message data contents as well.
Returns:
- nil : indicates that the message should be stifled
- A peer object : indicates that the message should be processed
further
83 84 85 86 |
# File 'lib/rbkb/plug/plug.rb', line 83 def plug_receive(dat) peer = plug_peer return peer unless peer.mute end |
#post_init ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/rbkb/plug/plug.rb', line 96 def post_init UI.verbose "** #{name} Started" if @kind==:server and peer=plug_peer UI.log "** #{name} CONNECTED TO #{peer.name}" if tls start_tls(tls_opts || {}) end end end |
#receive_data(dat) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/rbkb/plug/plug.rb', line 106 def receive_data(dat) if peer=plug_receive(dat) say(dat, peer) end return peer end |
#say(dat, sender) ⇒ Object
This instance of the say method is an abstract stub and just “dumps” the message. It should be overridden and optionally called with super() if you actually want to do anything useful when incoming messages are received.
92 93 94 |
# File 'lib/rbkb/plug/plug.rb', line 92 def say(dat, sender) UI.dump(sender.name, self.name, dat) end |