What is Megaphone?

What is Megaphone?
The Megaphone project is about enhancing open source chat software. Specifically, the goal is to allow ejabberd to support 1,000,000 simultaneous users. See The Plan page for more details on how I plan to solve this problem. See the About this Blog page for more details on why I created this blog.

Monday, January 30, 2012

megaphone VII: Parsing Packets

Previously...
  • I coded up the put_data function
  • I coded up the put_data function...again
  • I fixed some problems with remove_waiter and wrote the notify_wait function.

After the amount of coding that I had to do for some of the other functions, the packet decoding stuff was kind of anti-climactic:


parse_packet(Data) ->
    {ok, Request, Remainder} = erlang:decode_packet(http, [], Data),
    parse_headers([Request], Remainder).

parse_headers(Headers, Data) ->
    case erlang:decode_packet(httph, [], Data) of
        { ok, http_eoh, Rest } -> { Headers ++ http_eoh, Rest };
        { ok, Header, Rest } -> parse_headers(Headers ++ Header, Rest)
    end.

Not having any error handling might have something to do with it, but cryptic messages filled with weird braces are the erlang way!  Seriously, I'm going to put a reminder in the parking lot to go back and do a better job at error handling for this.

The erlang decode_packet function expects the caller to maintain some state as to where the parse is at between calls.  Hence parse_packet makes the first call to decode_packet with the http argument.  Subsequent calls are made in parse_headers and the httph argument is used.

This loop continues until decode_packet returns http_eoh, at which point the only thing left is the actual body of the request.

Next time: some odds and ends.

No comments:

Post a Comment