Class: Pants::Writers::UDPWriter

Inherits:
BaseWriter show all
Includes:
LogSwitch::Mixin
Defined in:
lib/pants/writers/udp_writer.rb

Overview

This is the interface to UDPWriterConnections. It defines what happens when you want to start it up and stop it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseWriter

#running?, #starter, #stopper, #write_object

Constructor Details

#initialize(host, port, read_from_channel) ⇒ UDPWriter

Returns a new instance of UDPWriter.

Parameters:

  • host (String)
  • port (Fixnum)
  • read_from_channel (EventMachine::Channel)

95
96
97
98
99
100
101
102
# File 'lib/pants/writers/udp_writer.rb', line 95

def initialize(host, port, read_from_channel)
  @host = host
  @port = port
  @connection = nil
  @write_object = "udp://#{@host}:#{@port}"

  super(read_from_channel)
end

Instance Attribute Details

#hostString (readonly)

Returns The IP address that's being written to.

Returns:

  • (String)

    The IP address that's being written to.


85
86
87
# File 'lib/pants/writers/udp_writer.rb', line 85

def host
  @host
end

#portFixnum (readonly)

Returns The port that's being written to.

Returns:

  • (Fixnum)

    The port that's being written to.


88
89
90
# File 'lib/pants/writers/udp_writer.rb', line 88

def port
  @port
end

Instance Method Details

#startObject

Readies the writer for data to write and waits for data to write.


105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pants/writers/udp_writer.rb', line 105

def start
  log "#{__id__} Adding a #{self.class} at #{@host}:#{@port}..."

  EM.defer do
    @connection = EM.open_datagram_socket('0.0.0.0', 0, UDPWriterConnection,
      @read_from_channel, @host, @port)

    start_loop = EM.tick_loop { :stop if @connection }
    start_loop.on_stop { starter.call }
  end
end

#stopObject

Closes the connection and notifies the reader that it's done.


118
119
120
121
122
# File 'lib/pants/writers/udp_writer.rb', line 118

def stop
  log "Finishing ID #{__id__}"
  @connection.close_connection_after_writing
  stopper.call
end