Skip to content

Commit

Permalink
Add basic UDP connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbt committed Nov 24, 2015
1 parent a57e30b commit 458205d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/linr.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "linr/version"
require "linr/connection"

# A simple UDP client for InfluxDB
module Linr
Expand Down
1 change: 1 addition & 0 deletions lib/linr/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "linr/connection/udp"
22 changes: 22 additions & 0 deletions lib/linr/connection/udp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Linr
module Connection
class UDP
# Use non-custom flags
SEND_FLAGS = 0

# Establishes a connection
# @param host [String]
# @param port [Fixnum]
def initialize(host, port)
@socket = UDPSocket.new
@socket.connect(host, port)
end

# Send arbitrary data ober the UDP connection
# @param data [String]
def send(data)
@socket.send(data, SEND_FLAGS)
end
end
end
end
18 changes: 18 additions & 0 deletions spec/connection/udp_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "spec_helper"

describe ::Linr::Connection::UDP do
subject { ::Linr::Connection::UDP.new(host, port) }
let(:host) { "127.0.0.1" }
let(:port) { 44_000 }
let(:socket) { UDPSocket.new }

before do
socket.bind(host, port)
end

it "sends data as passed" do
payload = "measurement,foo=bar,bat=baz value=12,otherval=21 1439587925"
subject.send(payload)
socket.recvfrom(60).first.must_equal payload
end
end

0 comments on commit 458205d

Please sign in to comment.