Skip to content
Permalink
Browse files

added decoding long frames + tests + small refactor

  • Loading branch information...
Dreat committed Apr 2, 2017
1 parent 362fb27 commit 086302655af2e017161bce6077eacfbf36e029bb
Showing with 40 additions and 20 deletions.
  1. +26 −16 zmq_ex/lib/zmq_ex.ex
  2. +14 −4 zmq_ex/test/zmq_ex_test.exs
@@ -5,23 +5,33 @@ defmodule ZmqEx do
<<encode_flags(flags), size, body>>
end

def decode(<<flags, size, body>>) do
case flags &&& 7 do
0 -> %{flags: %{reserved: 0, command: :message, long: :short, more: false}, size: size, body: body}
1 -> %{flags: %{reserved: 0, command: :message, long: :short, more: true}, size: size, body: body}
2 -> %{flags: %{reserved: 0, command: :message, long: :long, more: false}, size: size, body: body}
3 -> %{flags: %{reserved: 0, command: :message, long: :long, more: true}, size: size, body: body}
4 -> %{flags: %{reserved: 0, command: :command, long: :short, more: false}, size: size, body: body}
5 -> %{flags: %{reserved: 0, command: :command, long: :long, more: false}, size: size, body: body}
_ -> {:error}
end
def decode(<<flags :: size(8), rest :: binary>>) do
decode_flags(flags)
|> decode(rest)
end

defp encode_flags(%{reserved: 0, command: :message, long: :short, more: false}), do: 0
defp encode_flags(%{reserved: 0, command: :message, long: :short, more: true}), do: 1
defp encode_flags(%{reserved: 0, command: :message, long: :long, more: false}), do: 2
defp encode_flags(%{reserved: 0, command: :message, long: :long, more: true}), do: 3
defp encode_flags(%{reserved: 0, command: :command, long: :short, more: false}), do: 4
defp encode_flags(%{reserved: 0, command: :command, long: :long, more: false}), do: 5
defp decode(flags = %{type: _, long: :short, more: _}, <<size :: size(8), body :: binary>>) do
%{flags: flags, size: size, body: body}
end

defp decode(flags = %{type: _, long: :long, more: _}, <<size :: (64), body :: binary>>) do
%{flags: flags, size: size, body: body}
end

defp encode_flags(%{type: :message, long: :short, more: false}), do: 0
defp encode_flags(%{type: :message, long: :short, more: true}), do: 1
defp encode_flags(%{type: :message, long: :long, more: false}), do: 2
defp encode_flags(%{type: :message, long: :long, more: true}), do: 3
defp encode_flags(%{type: :command, long: :short, more: false}), do: 4
defp encode_flags(%{type: :command, long: :long, more: false}), do: 5
defp encode_flags(_), do: {:error}

defp decode_flags(0), do: %{type: :message, long: :short, more: false}
defp decode_flags(1), do: %{type: :message, long: :short, more: true}
defp decode_flags(2), do: %{type: :message, long: :long, more: false}
defp decode_flags(3), do: %{type: :message, long: :long, more: true}
defp decode_flags(4), do: %{type: :command, long: :short, more: false}
defp decode_flags(5), do: %{type: :command, long: :long, more: false}
defp decode_flags(_), do: {:error}

end
@@ -5,21 +5,31 @@ defmodule ZmqExTest do

test "decode basic message frame" do
result = ZmqEx.decode(<<0, 1, 1>>)
assert result == %{flags: %{reserved: 0, command: :message, long: :short, more: false}, size: 1, body: 1}
assert result == %{flags: %{type: :message, long: :short, more: false}, size: 1, body: <<1>>}
end

test "decode basic command frame" do
result = ZmqEx.decode(<<4, 1, 3>>)
assert result == %{flags: %{reserved: 0, command: :command, long: :short, more: false}, size: 1, body: 3}
assert result == %{flags: %{type: :command, long: :short, more: false}, size: 1, body: <<3>>}
end

test "decode long message frame" do
result = ZmqEx.decode(<<2, 256 :: size(64), 256>>)
assert result == %{flags: %{type: :message, long: :long, more: false}, size: 256, body: <<256>>}
end

test "decode long command frame" do
result = ZmqEx.decode(<<5, 256 :: size(64), 256>>)
assert result == %{flags: %{type: :command, long: :long, more: false}, size: 256, body: <<256>>}
end

test "encode basic message frame" do
result = ZmqEx.encode(%{flags: %{reserved: 0, command: :message, long: :short, more: false}, size: 1, body: 1})
result = ZmqEx.encode(%{flags: %{type: :message, long: :short, more: false}, size: 1, body: 1})
assert result == (<<0, 1, 1>>)
end

test "encode basic command frame" do
result = ZmqEx.encode(%{flags: %{reserved: 0, command: :command, long: :short, more: false}, size: 1, body: 3})
result = ZmqEx.encode(%{flags: %{type: :command, long: :short, more: false}, size: 1, body: 3})
assert result == (<<4, 1, 3>>)
end
end

0 comments on commit 0863026

Please sign in to comment.
You can’t perform that action at this time.