/lss_parse_utils/src/binl.erl

https://github.com/ashneyderman/lss · Erlang · 76 lines · 55 code · 12 blank · 9 comment · 1 complexity · 04f30e0b95fbfb4e362b40fb0916da50 MD5 · raw file

  1. % @author Alex Shneyderman <a.shneyderman@gmail.com>
  2. % @copyright (C) 2011, Alex Shneyderman
  3. % @since May 26, 2011
  4. -module(binl).
  5. -export([show_length/0,new_ping/0,simple_bin/0,simple_bin1/0,produce_bin/0,consume_bin/1,create_messages/1]).
  6. -define(START_MSG_MARKER,<<16#FF,16#FF,16#FF,16#FF,16#FF,16#FF,16#FF,16#FF>>).
  7. show_length() ->
  8. PingMsg = { ping, "CID-1231231:1122:12312312" },
  9. PingBin = term_to_binary( PingMsg ),
  10. PingBinLength = byte_size( PingBin ),
  11. io:format( "Binary length is ~p~n", [ PingBinLength ] ).
  12. new_ping() ->
  13. PingMsg = { ping, "CID-1231231:1122:12312312" },
  14. PingBin = term_to_binary( PingMsg ),
  15. PingBinLength = byte_size( PingBin ),
  16. Msg = <<PingBinLength:2, PingBin>>,
  17. io:format("BP1~n"),
  18. MsgLen = byte_size( Msg ),
  19. io:format("MsgLen: ~p, Msg: ~p~n", [MsgLen,Msg]).
  20. simple_bin() ->
  21. Bin = <<1,2,3,4,5>>,
  22. Length = byte_size( Bin ),
  23. io:format("~p:~p~n", [Bin,Length]),
  24. Msg = <<Length:16>>,
  25. io:format( "~p~n", [ Msg ] ).
  26. simple_bin1() ->
  27. Bin = <<0,0,0,0,1,2,3,4,5,0,0,0,0,1,2,3,4,0,0,0,0,1,2,3>>,
  28. Parts = binary:split( Bin, <<0,0,0,0>>, [global] ),
  29. io:format("~p~n",[Parts]).
  30. %% Length = byte_size( Bin ),
  31. %% io:format("~p:~p~n", [Bin,Length]),
  32. %% <<L1:16>> = binary:part( Bin, 0, 2 ),
  33. %% TheRest = binary:part( Bin, 2, byte_size( Bin ) - 2 ),
  34. %% io:format("~p:~p~n", [L1,TheRest]).
  35. produce_bin() ->
  36. Marker = <<16#FF,16#FF,16#FF,16#FF,16#FF,16#FF,16#FF,16#FF>>,
  37. Term1 = {ping, [{client_id, "CID-123123:12312:2299"},
  38. {message_id, "MSG-29992382:29922:39993"}]},
  39. Term2 = {ping, [{client_id, "CID-123123:12312:2299"},
  40. {message_id, "MSG-8588330:29922:39993"}]},
  41. Result = list_to_binary( [ Marker, term_to_binary( Term1 ), Marker, term_to_binary( Term2 ), Marker ] ),
  42. %%io:format("~p:~p:~p~n",[byte_size(term_to_binary( Term1 )),byte_size(term_to_binary( Term2 )),byte_size(Result)]),
  43. Result.
  44. consume_bin( <<>> ) ->
  45. [];
  46. consume_bin( Binary ) ->
  47. Marker = <<16#FF,16#FF,16#FF,16#FF,16#FF,16#FF,16#FF,16#FF>>,
  48. BinChunks = binary:split( Binary, Marker, [ global ] ),
  49. produce_messages( [], BinChunks ).
  50. produce_messages( SoFar, [BinTerm | Tail] ) ->
  51. case produce_msg( BinTerm ) of
  52. undefined -> produce_messages( SoFar, Tail );
  53. Term -> produce_messages( lists:append( SoFar, [Term] ), Tail )
  54. end;
  55. produce_messages( SoFar, [] ) ->
  56. SoFar.
  57. produce_msg( <<>> ) ->
  58. undefined;
  59. produce_msg( BinTerm ) ->
  60. binary_to_term( BinTerm ).
  61. create_messages(Term) when not is_list(Term) ->
  62. create_messages([Term]);
  63. create_messages(Terms) when is_list(Terms) ->
  64. lists:flatten( [ [ term_to_binary(Term), ?START_MSG_MARKER ] || Term <- Terms ] ).