-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add abstraction Payload to use in Publish
- Loading branch information
Showing
6 changed files
with
187 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require "./spec_helper" | ||
|
||
describe MQTT::Protocol::Payload do | ||
it ".new(Bytes) returns a BytesPayload" do | ||
obj = MQTT::Protocol::Payload.new("foo".to_slice) | ||
obj.should be_a(MQTT::Protocol::BytesPayload) | ||
end | ||
|
||
it ".new(IO) returns a IOPayload" do | ||
io = IO::Memory.new | ||
io.write "foo".to_slice | ||
obj = MQTT::Protocol::Payload.new(io, 3) | ||
obj.should be_a(MQTT::Protocol::IOPayload) | ||
end | ||
|
||
describe "#==" do | ||
it "should return true for two BytePayload with same bytes" do | ||
one = MQTT::Protocol::BytesPayload.new("foo".to_slice) | ||
two = MQTT::Protocol::BytesPayload.new("foo".to_slice) | ||
|
||
(one == two).should be_true | ||
end | ||
|
||
it "should return false for two BytePayload with different bytes" do | ||
one = MQTT::Protocol::BytesPayload.new("foo".to_slice) | ||
two = MQTT::Protocol::BytesPayload.new("bar".to_slice) | ||
|
||
(one == two).should be_false | ||
end | ||
|
||
it "should return true for two IOPayload with same content" do | ||
io_one = IO::Memory.new("foo".to_slice) | ||
io_two = IO::Memory.new("foo".to_slice) | ||
|
||
io_one.rewind | ||
io_two.rewind | ||
|
||
one = MQTT::Protocol::IOPayload.new(io_one, 3) | ||
two = MQTT::Protocol::IOPayload.new(io_two, 3) | ||
|
||
(one == two).should be_true | ||
end | ||
|
||
it "should return false for two IOPayload with different content" do | ||
io_one = IO::Memory.new("foo".to_slice) | ||
io_two = IO::Memory.new("bar".to_slice) | ||
|
||
io_one.rewind | ||
io_two.rewind | ||
|
||
one = MQTT::Protocol::IOPayload.new(io_one, 3) | ||
two = MQTT::Protocol::IOPayload.new(io_two, 3) | ||
|
||
(one == two).should be_false | ||
end | ||
|
||
it "should return true for one BytesPayload and one IOPayload with same content" do | ||
io_two = IO::Memory.new("foo".to_slice) | ||
io_two.rewind | ||
|
||
one = MQTT::Protocol::BytesPayload.new("foo".to_slice) | ||
two = MQTT::Protocol::IOPayload.new(io_two, 3) | ||
|
||
(one == two).should be_true | ||
end | ||
|
||
it "should return false for one BytesPayload and one IOPayload with different content" do | ||
io_two = IO::Memory.new("bar".to_slice) | ||
io_two.rewind | ||
|
||
one = MQTT::Protocol::BytesPayload.new("foo".to_slice) | ||
two = MQTT::Protocol::IOPayload.new(io_two, 3) | ||
|
||
(one == two).should be_false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require "./io" | ||
|
||
module MQTT | ||
module Protocol | ||
abstract struct Payload | ||
def self.new(bytes : Bytes) | ||
BytesPayload.new(bytes) | ||
end | ||
|
||
def self.new(io : ::IO, bytesize : Int32) | ||
IOPayload.new(MQTT::Protocol::IO.new(io), bytesize) | ||
end | ||
|
||
def self.new(io : MQTT::Protocol::IO, bytesize : Int32) | ||
IOPayload.new(io, bytesize) | ||
end | ||
|
||
def size | ||
bytesize | ||
end | ||
|
||
abstract def bytesize : Int32 | ||
abstract def to_slice : Bytes | ||
abstract def to_io(io, format : ::IO::ByteFormat = IO::ByteFormat::SystemEndian) | ||
|
||
def ==(other) | ||
return false unless other.is_a?(Payload) | ||
to_slice == other.to_slice | ||
end | ||
end | ||
|
||
struct BytesPayload < Payload | ||
def initialize(@bytes : Bytes) | ||
end | ||
|
||
def bytesize : Int32 | ||
@bytes.bytesize | ||
end | ||
|
||
def to_slice : Bytes | ||
@bytes | ||
end | ||
|
||
def to_io(io, format : ::IO::ByteFormat = IO::ByteFormat::SystemEndian) | ||
io.write @bytes | ||
end | ||
end | ||
|
||
struct IOPayload < Payload | ||
getter bytesize : Int32 | ||
|
||
@data : Bytes? = nil | ||
|
||
def initialize(@io : MQTT::Protocol::IO, @bytesize : Int32) | ||
end | ||
|
||
def initialize(io : ::IO, @bytesize : Int32) | ||
@io = MQTT::Protocol::IO.new(io) | ||
end | ||
|
||
def to_slice : Bytes | ||
if peeked = @io.peek.try &.[0, bytesize]? | ||
return peeked | ||
end | ||
return @data || begin | ||
data = Bytes.new(bytesize) | ||
@io.read(data) | ||
data | ||
end | ||
end | ||
|
||
def to_io(io, format : ::IO::ByteFormat = IO::ByteFormat::SystemEndian) | ||
if data = @data | ||
io.write data | ||
else | ||
copied = ::IO.copy(@io, io, bytesize) | ||
raise "Failed to copy payload" if copied != bytesize | ||
end | ||
end | ||
end | ||
end | ||
end |