Commit 39ac1218 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

Merge pull request #71 from challengepost/inbound_udp_server

Optional UDP server
parents 5f663d21 0b403c5b
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ module FnordMetric
      :redis_url => "redis://localhost:6379",
      :redis_prefix => "fnordmetric",
      :inbound_stream => ["0.0.0.0", "1337"],
      :inbound_protocol => :tcp,
      :web_interface => ["0.0.0.0", "4242"],
      :web_interface_server => "thin",
      :start_worker => true,
@@ -122,11 +123,12 @@ module FnordMetric
      end

      if opts[:inbound_stream]
        inbound_class = opts[:inbound_protocol] == :udp ? InboundDatagram : InboundStream
        begin
          inbound_stream = InboundStream.start(opts)
          log "listening on tcp##{opts[:inbound_stream].join(":")}"
          inbound_stream = inbound_class.start(opts)
          log "listening on #{opts[:inbound_protocol]}##{opts[:inbound_stream].join(":")}"
        rescue
          log "cant start FnordMetric::InboundStream. port in use?"
          log "cant start #{inbound_class.name}. port in use?"
        end
      end

@@ -145,6 +147,7 @@ end

require "fnordmetric/api"
require "fnordmetric/inbound_stream"
require "fnordmetric/inbound_datagram"
require "fnordmetric/worker"
require "fnordmetric/widget"
require "fnordmetric/timeline_widget"
+35 −0
Original line number Diff line number Diff line
class FnordMetric::InboundDatagram < EventMachine::Connection

  class << self
    attr_accessor :opts
  end

  def self.start(opts)
    self.opts = opts
    EM.open_datagram_socket(*opts[:inbound_stream], self, opts)
  end

  def receive_data(event)
    events << event
    push_next_event
  end

  def push_next_event
    return true if events.empty?
    api.event(@events.pop)
    EM.next_tick(&method(:push_next_event))
  end

  def unbind
    api.disconnect
  end

  def api
    @api ||= FnordMetric::API.new(self.class.opts)
  end

  def events
    @events ||= []
  end

end
 No newline at end of file
+34 −0
Original line number Diff line number Diff line
require ::File.expand_path('../spec_helper.rb', __FILE__)

describe FnordMetric::InboundDatagram do

  let(:inbound_datagram) { FnordMetric::InboundDatagram.new(nil) }

  before(:all) do
    @redis = Redis.new
    @redis_wrap = RedisWrap.new(@redis)
    FnordMetric::InboundDatagram.opts = {
      :redis_url => "redis://localhost:6379",
      :redis_prefix => "fnordmetric-test",
      :event_queue_ttl => 120
    }
  end

  describe "pushing new events" do
    it "should add parsable event to the queue" do
      data = %Q{{"_type": "started"}}

      lambda {
        inbound_datagram.receive_data data
      }.should change { @redis.llen("fnordmetric-test-queue") }.by +1
    end

    it "should reject non parsable events" do
      broken_data = %Q{{"_type" => "started"}}

      lambda {
        inbound_datagram.receive_data broken_data
      }.should_not change { @redis.llen("fnordmetric-test-queue") }
    end
  end
end
 No newline at end of file
+1 −1

File changed.

Contains only whitespace changes.

+39 −39

File changed.

Contains only whitespace changes.

+8 −8

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

Loading