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.

Tuesday, January 17, 2012

Parsing Headers and Bodies

Previously...
  • I resolved to use a single process to respond to ejabberd_http.
  • I did an analysis of the event sequence that should take place in order to use a single process.
  • I made some plans about how to create megaphone_tcp.

I remember a comment that James Rumbaugh, one of guys behind UML, once said:

Some objects tell others to do stuff, other objects do what they're told.

I thought that contrast nicely summarizes the first part of coding for megaphone_tcp.

At a larger level, meaphone_tcp is very much a "do what you're told" style of module: on startup, it fires up a new process for itself, but doesn't try to start up anything else:


start(Socket) ->

    Pid = spawn(megaphone, start_receive, [Socket]),
    gen_tcp:controlling_process(Socket, Pid).

start_receive just sets up the system for the first iteration through the main loop:


start_receive (Socket) ->

    receive_loop(Socket, start, undefined).

The module is currently centered around going through the steps outlined in the post previous to this one.  The first step is to figure out if you have a complete header:


receive_loop (Socket, OldState, LeftOvers) ->

    case gen_tcp:recv(Socket, 0) of
        { ok, Binary } ->
            { PacketLength, ConnectionID, Data } = receive_header(Socket, LeftOvers, Binary),
            { Packet, NewLeftOvers } = receive_packet_body (Socket, Data, PacketLength, ConnectionID),
            give_packet_to_megaphone (ConnectionID, Packet),
            receive_loop(Socket, undefined, NewLeftOvers);

        Other ->
            gen_tcp:close(Socket)
    end.

The rest of the code is left as an exercise for the reader.

That didn't work, did it?

I'll post the rest of the code as I write it.  Which will be real soon.  Honest.

No comments:

Post a Comment