[erlang-questions] Asterisk manager parser question

Juan Jose Comellas <>
Thu Jun 26 19:26:01 CEST 2008


I did something similar but for the FreeSWITCH mod_event_socket interface
(if you're looking into using Asterisk for anything serious, you owe
yourself to check what FreeSWITCH has to offer).

The code that handles all of this is a bit big for an email, but the code
that parses the buffer used to read from the socket is what you're probably
more interested in. If you want more help with this you can contact me
off-list.


parse_headers(Headers, Buffer) when is_list(Headers), is_binary(Buffer) ->
    parse_headers(Headers, Buffer, undefined, 0).

parse_headers(Headers, Buffer, _SepOffset, Offset) when Offset >
size(Buffer) ->
    {incomplete, Headers, Buffer};
parse_headers(Headers, Buffer, SepOffset, Offset) ->
    case Buffer of
        % Match the end of a text line.
        <<Line:Offset/binary, $\n, Tail/binary>> ->
            handle_header_line(Headers, Buffer, Line, Tail, SepOffset);
        <<Line:Offset/binary, $\r, $\n, Tail/binary>> ->
            handle_header_line(Headers, Buffer, Line, Tail, SepOffset);

        % Match the first separator between the header name and value.
        <<_Line:Offset/binary, $:, _Tail/binary>> when (SepOffset =:=
undefined) ->
            parse_headers(Headers, Buffer, Offset, Offset + 1);

        _ ->
            parse_headers(Headers, Buffer, SepOffset, Offset + 1)
    end.


handle_header_line(Headers, _Buffer, <<>>, Tail, undefined) ->
    case Headers of
        [] ->
            % Empty lines at the beginning of the header lines are not
considered errors.
            {incomplete, Headers, Tail};
        _ ->
            % An empty line after finding more than one non-empty header
means we're
            % done parsing headers.
            {ok, lists:reverse(Headers), Tail}
    end;
handle_header_line(Headers, Buffer, Line, _Tail, undefined) when size(Line)
> 0 ->
    % Return an error if the separator was not found.
    {error, Headers, Buffer};
handle_header_line(Headers, Buffer, Line, Tail, SepOffset) ->
    case Line of
        <<Name:SepOffset/binary, $:, $\s, Value/binary>> ->
            parse_headers([{Name, Value} | Headers], Tail);
        <<Name:SepOffset/binary, $:, Value/binary>> ->
            parse_headers([{Name, Value} | Headers], Tail);
        _ ->
            {error, Headers, Buffer}
    end.




2008/6/26 Jon Gretar Borgthorsson <>:

> Hi..
> I've been playing around with erlang for a month and playing around with
> gen_tcp and more. Now I've decided to redo a script that I have that parses
> events from the asterisk management api. The old script tended to be a bit
> unstable and I think that Erlang would be perfect for the job.
>
> However I'm having a little problem solving how to read the information in
> an elegant way.
>
> Basically for those who don't know the Asterisk manager API then it's
> basically a tcp port you connect to and read events as they come. Each event
> is formatted like this:
>
> Event: Agentlogoff
> Agent: <agent>
> Logintime: <logintime>
> Uniqueid: <uniqueid>
>
>
> And events are seperated by 2 returns. What I was thinking of doing is
> something that listens to the port and sends events clause in whole to
> another process that analyzes it. What I'm having problems with is just the
> first part. Recognizing the event clouses in some elegant way and sending
> them on to the parsing function. I have to try to save the message up in a
> buffer and when it senses 2 returns then creates a process to parse it and
> clears the buffer to read the next event. I'm sure that there is some
> elegant way to do this but since this is my first real erlang project then
> I'm a bit lost on how the best way is. Almost everything else I have figured
> out and it's just this last problem that needs solving.
>
> Anyone have a few free minutes to assist me on this?
>
>
> Regards
> - Jon
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080626/bb0238ba/attachment.html>


More information about the erlang-questions mailing list