module Plug::Blit

Constants

BLIT_HANDLERS
DEFAULT_IPADDR
DEFAULT_PORT
DEFAULT_PROTOCOL
OPCODES
SIG

Blit protocol stuff

Attributes

kind[RW]

Public Class Methods

blit_header(op) click to toggle source
# File lib/rbkb/plug/blit.rb, line 64
def self.blit_header(op)
  return nil unless opno = OPCODES.invert[op]
  SIG + opno.chr
end
blit_init(opts={}) click to toggle source
# File lib/rbkb/plug/blit.rb, line 204
def self.blit_init(opts={})
  @blit_addr = (opts[:addr] || DEFAULT_IPADDR)
  @blit_port = (opts[:port] || DEFAULT_PORT)
  proto = (opts[:protocol] || DEFAULT_PROTOCOL)
  @blit_handler = BLIT_HANDLERS[ proto ]
  raise "invalid blit transport protocol" unless @blit_handler
end
blit_raw(buf) click to toggle source
# File lib/rbkb/plug/blit.rb, line 226
def self.blit_raw(buf)
  raise "use blit_init first!" unless self.initialized?
  @blit_handler.call buf
end
blit_send(data, idx=0) click to toggle source
# File lib/rbkb/plug/blit.rb, line 216
def self.blit_send(data, idx=0)
  msg = make_sendmsg(idx, data)
  blit_raw(msg)
end
blit_starttls(idx = 0) click to toggle source
# File lib/rbkb/plug/blit.rb, line 221
def self.blit_starttls(idx = 0)
  msg = make_starttls(idx)
  blit_raw(msg)
end
initialized?() click to toggle source
# File lib/rbkb/plug/blit.rb, line 212
def self.initialized?
  @blit_addr and @blit_port and @blit_handler
end
make_clear() click to toggle source
# File lib/rbkb/plug/blit.rb, line 161
def self.make_clear
  self.blit_header(:clear)
end
make_delete(idx=0) click to toggle source
# File lib/rbkb/plug/blit.rb, line 169
def self.make_delete(idx=0)
  self.blit_header(:delete) +
    idx.to_bytes(:big, 2)
end
make_kill(idx=nil) click to toggle source
# File lib/rbkb/plug/blit.rb, line 152
def self.make_kill(idx=nil)
  self.blit_header(:kill)
end
make_list_peers() click to toggle source
# File lib/rbkb/plug/blit.rb, line 181
def self.make_list_peers
  self.blit_header(:list_peers)
end
make_mute(peerno) click to toggle source
# File lib/rbkb/plug/blit.rb, line 93
def self.make_mute(peerno)
  self.blit_header(:squelch) +
    peerno.to_bytes(:big, 2)
end
make_sendmsg(idx, dat) click to toggle source

Blit packed message format is (SUBJECT TO CHANGE):

"BLT"
char   opcode
uint16be idx   = index of slave peer to send to
uint32le size  = length of data
str      data
# File lib/rbkb/plug/blit.rb, line 140
def self.make_sendmsg(idx, dat)
  self.blit_header(:sendmsg) +
    idx.to_bytes(:big, 2) + 
    dat.size.to_bytes(:big, 4) + 
    dat
end
make_squelch(peerno) click to toggle source
# File lib/rbkb/plug/blit.rb, line 106
def self.make_squelch(peerno)
  self.blit_header(:squelch) +
    peerno.to_bytes(:big, 2)
end
make_starttls(peerno) click to toggle source
# File lib/rbkb/plug/blit.rb, line 80
def self.make_starttls(peerno)
  self.blit_header(:starttls) + peerno.to_bytes(:big, 2)
end
new(transport, slave) click to toggle source
Calls superclass method Plug::Base::new
# File lib/rbkb/plug/blit.rb, line 26
def initialize(transport, slave)
  super(transport)

  @kind = :blitsrv
  @slave = slave
  @peers = slave.peers
  initbuf
end

Public Instance Methods

clear() click to toggle source
# File lib/rbkb/plug/blit.rb, line 156
def clear
  @peers.each { |p| p.close }
  @peers.replace []
end
delete(peerno) click to toggle source
# File lib/rbkb/plug/blit.rb, line 165
def delete(peerno)
  @peers.delete(peerno)
end
initbuf() click to toggle source

(re)initializes the blit buffer

# File lib/rbkb/plug/blit.rb, line 48
def initbuf
  @buf = StringIO.new
end
kill() click to toggle source
# File lib/rbkb/plug/blit.rb, line 147
def kill
  UI.log("** BLIT-KILL - Received shutdown command")
  EM.stop
end
list_peers() click to toggle source
# File lib/rbkb/plug/blit.rb, line 174
def list_peers
  UI.log("** BLIT-LISTPEERS - Received list peers command")

  @peers.each_index {|i| UI.log "**   #{i} - #{@peers[i].name}"}
  UI.log("** BLIT-LISTPEERS-END - End of peer list")
end
mute() click to toggle source
# File lib/rbkb/plug/blit.rb, line 84
def mute
  unless ( peerno=@buf.read(2) and peerno.size == 2 and
           peer=@peers[peerno.dat_to_num(:big)] )

    UI.log "** BLIT-ERROR(Malformed or missing peer for mute)"
    return true
  end
end
post_init() click to toggle source
# File lib/rbkb/plug/blit.rb, line 35
def post_init
  # override so we don't get unneccessary "Start" message from Base
end
receive_data(dat) click to toggle source
# File lib/rbkb/plug/blit.rb, line 52
def receive_data dat
  return unless (@buf.write(dat) > SIG.size) or (@buf.pos > (SIG.size + 1))

  @buf.rewind

  return unless @buf.read(SIG.size) == SIG and
                op = OPCODES[ @buf.read(1)[0] ]

  initbuf if self.send(op)
end
sendmsg() click to toggle source
# File lib/rbkb/plug/blit.rb, line 111
def sendmsg
  unless peerno=@buf.read(2) and peerno.size == 2 and
         bufsiz=@buf.read(4) and bufsiz.size == 4
    UI.log "** BLIT-ERROR(Malformed sendmsg)"
    return true
  end

  peerno = peerno.dat_to_num(:big)
  bufsiz = bufsiz.dat_to_num(:big)

  if (rdat=@buf.read(bufsiz)).size == bufsiz
    if peer=@peers[peerno]
      peer.say(rdat, self)
      return true
    else
      UI.log "** BLIT-ERROR(Invalid peer index #{peerno})"
      return true
    end
  else
    return nil
  end
end
starttls() click to toggle source
# File lib/rbkb/plug/blit.rb, line 69
def starttls
  unless ( peerno=@buf.read(2) and peerno.size == 2 and
           peer=@peers[peerno.dat_to_num(:big)] )

    UI.log "** BLIT-ERROR(Malformed or missing peer for starttls)"
    return true
  end

  peer.start_tls(self)
end
unbind() click to toggle source
# File lib/rbkb/plug/blit.rb, line 39
def unbind
  # override so we don't get unneccessary "closed" message from Base
end
unmute() click to toggle source
# File lib/rbkb/plug/blit.rb, line 98
def unmute
  unless ( peerno=@buf.read(2) and peerno.size == 2 and
           peer=@peers[peerno.dat_to_num(:big)] )
    UI.log "** BLIT-ERROR(Malformed or missing peer for unmute)"
    return true
  end
end