Dranna
Dranna

Reputation: 505

Erlang match integer then string

I'm looking to update my Erlang server by adding a timestamp. Before, I had commands then parameters sent to the server :

server_loop(Socket) ->
  inet:setopts(Socket, [{active, once}]),
  receive
    {tcp, Socket, <<"read", Content/binary>>} ->
      error_logger:info_msg("Reading ~w", [Content]),
      read(Socket, Content),
      server_loop(Socket);
    {tcp, Socket, <<"up", Content/binary>>} ->
      update(Socket, Content),
      server_loop(Socket);
    ...
end

As you can see, I send messages to the correct method depending on the first keyword of the message.

Now, my client sends the timestamp (in seconds with os:system_time()) before the command. How could I match my messages like "ANY_TIMESTAMP read", "ANY_TIMESTAMP up", ... And still be able to pass it to my method like read(Socket, Timestamp, Content) ?

Upvotes: 1

Views: 235

Answers (1)

Hynek -Pichi- Vychodil
Hynek -Pichi- Vychodil

Reputation: 26121

Encode timestamp into fixed length prefix. You can choose if it will be human readable or binary.

make_read_packet(Timestamp, Content) ->
    <<Timestamp:64, "read", Content/bytes>>.

or

-define(TIMESTAMP_LENGTH, 20). % or any suitable size
make_read_packet(Timestamp, Content) ->
    B = integer_to_binary(Timestamp),
    P = binary:copy(<<$0>>, ?TIMESTAMP_LENGTH - byte_size(B)),
    <<P/bytes, B/bytes, "read", Content/bytes>>.

and then you can easily detect it

    {tcp, Socket, <<Timestamp:64, "read", Content/binary>>} ->
      error_logger:info_msg("Reading ~w", [Content]),
      read(Socket, Timestamp, Content),
      server_loop(Socket);

or

    {tcp, Socket, <<TBin:?TIMESTAMP_LENGTH/bytes, "read", Content/binary>>} ->
      try binary_to_integer(TBin) of
        Timestamp -> 
          error_logger:info_msg("Reading ~w", [Content]),
          read(Socket, Timestamp, Content)
      catch error:badarg -> badarg end,
      server_loop(Socket);

otherwise, you would need something like

  {tcp, Socket, Packet} ->
    case parse_timestamp(Packet) of
        {Timestamp, <<"read", Content/bytes>>} ->
          error_logger:info_msg("Reading ~w", [Content]),
          read(Socket, Timestamp, Content);
        _ -> error
     end,
     server_loop(Socket);

parse_timestamp(Packet) ->
    parse_timestamp(Packet, 0).

parse_timestamp(<<D, Rest/bytes>>, N) when D >= $0, D <= $9 ->
    parse_timestamp(Rest, 10 * N + D - $0);
parse_timestamp(Rest, N) -> {N, Rest}.

Upvotes: 4

Related Questions