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.

Friday, January 20, 2012

Splitting megaphone_tcp

Previously...
  • I made some plans about how to create megaphone_tcp.
    • Determine if segment contains a complete header
      • If not, then keep reading until you do have a complete header
    • Parse out the connection ID and packet length, 
      • If you have a complete header, then determine if you have a complete megaphone packet
        • If not, then keep reading until you have a complete megaphone packet
    • Send the megaphone process the completed packet.
    • Wait for another packet to arrive.
  • I created receive_loop, which takes care of controlling the process.
  • I created the part that receives headers.

While writing the portion of megaphone_tcp that handles extracting the payload, it occurred to me that the name "megaphone_tcp" was something of a misnomer.  Rather than handling all things TCP, it really wanted to handle just those things relating to receiving a packet from TCP.  

Therefore, to further muddy the waters, I resolved to rename megaphone_tcp to megaphone_receiver.  At some point real soon now (ha!) I will create another module called megaphone_sender or megaphone_ecm to handle all things related to sending packets to ECM.

I modified the receive_header function so that it actually compiles.  It is basically the same, so I will not reprint it here.  Rather I will publish it along with the rest of the megaphone functionality on github.  Note that currently the megaphone-ejabberd account there does not have anything in it.

Here is the code for receiving the body of the message:

receive_packet_body (Socket, Data, Length) ->
    case byte_size(Data) of
        %%
        %% need more data to fill out the packet
        %%
        ContentLength when ContentLength < Length ->
            case gen_tcp:recv(Socket, 0) of
                {ok, NewData} ->
                    Data2 = Data ++ NewData,
                    receive_packet_body (Socket, Data2, Length)
            end;

        %%
        %% we have enough to fill out the packet
        %%
        true ->
            PacketData = binary_part(Data, 0, Length),

            %%
            %% Check for leftovers
            %%
            if
                byte_size(Data) == Length ->
                    { Data, undefined };
                true ->
                    LeftOvers = binary_part(Data, Length, byte_size(Data) - Length),
                    { PacketData, LeftOvers }
            end
    end.

For next time, the code that hands the packet off to the megaphone process.

No comments:

Post a Comment