module Plug::Base

Attributes

kind[RW]
no_stop_on_unbind[RW]
peers[RW]
tls[RW]
tls_opts[RW]
transport[RW]

Public Class Methods

new(transport, opts={}) click to toggle source
# File lib/rbkb/plug/plug.rb, line 46
    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

Public Instance Methods

connection_completed() click to toggle source
# File lib/rbkb/plug/plug.rb, line 117
def connection_completed
  peer = plug_peer
  UI.log "** #{name} CONNECTED TO #{peer.name}"
  if tls
    start_tls(tls_opts || {})
  end
  return peer
end
name() click to toggle source
# File lib/rbkb/plug/plug.rb, line 61
def name
  sn = get_sockname
  addr = sn ? Socket.unpack_sockaddr_in(sn).reverse.join(":") : "PENDING"
  "#{kind.to_s.upcase}-#{addr}(#{@transport})"
end
plug_peer() click to toggle source

plug_peer creates a peering association for a given peer based on get_peername. The existing or newly created peer object is returned.

# File lib/rbkb/plug/plug.rb, line 70
def plug_peer
  paddr = get_peername
  peer = (@peers.find_peer(paddr) || @peers.add_peer(paddr) )
end
plug_receive(dat) click to toggle source

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
# File lib/rbkb/plug/plug.rb, line 87
def plug_receive(dat)
  peer = plug_peer
  return peer unless peer.mute
end
post_init() click to toggle source
# File lib/rbkb/plug/plug.rb, line 100
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) click to toggle source
# File lib/rbkb/plug/plug.rb, line 110
def receive_data(dat)
  if peer=plug_receive(dat)
    say(dat, peer) 
  end
  return peer
end
say(dat, sender) click to toggle source

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.

# File lib/rbkb/plug/plug.rb, line 96
def say(dat, sender)
  UI.dump(sender.name, self.name, dat)
end
unbind() click to toggle source
# File lib/rbkb/plug/plug.rb, line 126
def unbind
  UI.log "** Connection " + ((@peers.empty?)? "refused." : "closed.")
  unless @no_stop_on_unbind
    UI.log "STOPPING!!"
    EM.stop  
  end
end