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).<br><br>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.<br>
<br><br>parse_headers(Headers, Buffer) when is_list(Headers), is_binary(Buffer) -><br>    parse_headers(Headers, Buffer, undefined, 0).<br><br>parse_headers(Headers, Buffer, _SepOffset, Offset) when Offset > size(Buffer) -><br>

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

            handle_header_line(Headers, Buffer, Line, Tail, SepOffset);<br>        <<Line:Offset/binary, $\r, $\n, Tail/binary>> -><br>            handle_header_line(Headers, Buffer, Line, Tail, SepOffset);<br>

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

<br>        _ -><br>            parse_headers(Headers, Buffer, SepOffset, Offset + 1)<br>    end.<br><br><br>handle_header_line(Headers, _Buffer, <<>>, Tail, undefined) -><br>    case Headers of<br>        [] -><br>

            % Empty lines at the beginning of the header lines are not considered errors.<br>            {incomplete, Headers, Tail};<br>        _ -><br>            % An empty line after finding more than one non-empty header means we're<br>

            % done parsing headers.<br>            {ok, lists:reverse(Headers), Tail}<br>    end;<br>handle_header_line(Headers, Buffer, Line, _Tail, undefined) when size(Line) > 0 -><br>    % Return an error if the separator was not found.<br>

    {error, Headers, Buffer};<br>handle_header_line(Headers, Buffer, Line, Tail, SepOffset) -><br>    case Line of<br>        <<Name:SepOffset/binary, $:, $\s, Value/binary>> -><br>
            parse_headers([{Name, Value} | Headers], Tail);<br>
        <<Name:SepOffset/binary, $:, Value/binary>> -><br>            parse_headers([{Name, Value} | Headers], Tail);<br>        _ -><br>            {error, Headers, Buffer}<br>    end.<br><br><br><br><br>
<div class="gmail_quote">2008/6/26 Jon Gretar Borgthorsson <<a href="mailto:jongretar@jongretar.com">jongretar@jongretar.com</a>>:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi..<div><br></div><div>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.</div>

<div><br></div><div>However I'm having a little problem solving how to read the information in an elegant way.</div><div><br></div><div>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:</div>

<div><br></div><blockquote style="border: medium none ; margin: 0pt 0pt 0pt 40px; padding: 0px;">Event: Agentlogoff <br>Agent: <agent> <br>Logintime: <logintime> <br>Uniqueid: <uniqueid> </blockquote>
<div><div><br></div><div>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.</div>

<div><br></div><div>Anyone have a few free minutes to assist me on this?</div><div><br></div><div><br></div><div>Regards</div><div>- Jon</div></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><br>