class Relp

Constants

RelpSoftware
RelpVersion

Public Instance Methods

frame_read(socket) click to toggle source
# File lib/logstash/util/relp.rb, line 64
def frame_read(socket)
  begin
    frame = Hash.new
    frame['txnr'] = socket.readline(' ').strip.to_i
    frame['command'] = socket.readline(' ').strip

    #Things get a little tricky here because if the length is 0 it is not followed by a space.
    leading_digit=socket.read(1)
    if leading_digit=='0' then
      frame['datalen'] = 0
      frame['message'] = ''
    else
      frame['datalen'] = (leading_digit + socket.readline(' ')).strip.to_i
      frame['message'] = socket.read(frame['datalen'])
    end
    @logger.debug? and @logger.debug("Read frame", :frame => frame)
  rescue Errno::ECONNRESET
    raise ConnectionClosed
  rescue EOFError,IOError
    raise FrameReadException
  end
  if ! self.valid_command?(frame['command'])#TODO: is this enough to catch framing errors?
    if self.server?
      self.serverclose(socket)
    else
      self.close
    end
    raise InvalidCommand,frame['command']
  end
  return frame
end
frame_write(socket, frame) click to toggle source
# File lib/logstash/util/relp.rb, line 36
def frame_write(socket, frame)
  unless self.server? #I think we have to trust a server to be using the correct txnr
    #Only allow txnr to be 0 or be determined automatically
    frame['txnr'] = self.nexttxnr() unless frame['txnr']==0
  end
  frame['txnr'] = frame['txnr'].to_s
  frame['message'] = '' if frame['message'].nil?
  frame['datalen'] = frame['message'].length.to_s
  wiredata=[
    frame['txnr'],
    frame['command'],
    frame['datalen'],
    frame['message']
  ].join(' ').strip
  begin
    @logger.debug? and @logger.debug("Writing to socket", :data => wiredata)
    socket.write(wiredata)
    #Ending each frame with a newline is required in the specifications
    #Doing it a separately is useful (but a bit of a bodge) because
    #for some reason it seems to take 2 writes after the server closes the
    #connection before we get an exception
    socket.write("\n")
  rescue Errno::EPIPE,IOError,Errno::ECONNRESET#TODO: is this sufficient to catch all broken connections?
    raise ConnectionClosed
  end
  return frame['txnr'].to_i
end
server?() click to toggle source
# File lib/logstash/util/relp.rb, line 96
def server?
  @server
end
valid_command?(command) click to toggle source
# File lib/logstash/util/relp.rb, line 17
def valid_command?(command)
  valid_commands = Array.new

  #Allow anything in the basic protocol for both directions
  valid_commands << 'open'
  valid_commands << 'close'

  #These are things that are part of the basic protocol, but only valid in one direction (rsp, close etc.) TODO: would they be invalid or just innapropriate?
  valid_commands += @basic_relp_commands

  #These are extra commands that we require, otherwise refuse the connection TODO: some of these are only valid on one direction
  valid_commands += @required_relp_commands

  #TODO: optional_relp_commands

  #TODO: vague mentions of abort and starttls commands in spec need looking into
  return valid_commands.include?(command)
end