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 27, 2012

megaphone IV: Receiving Data

Previously...
  • I created the main loop for the megaphone module.
  • I coded up the interface functions for the megaphone module.
  • I coded up the get_data function

This time I am going to talk about the function to process data that has been received for a virtual connection.  Interestingly enough, this logic is more complex than what I wrote for the client to retrieve a packet.  Here is the main entry point into this subsystem: the function to put a new packet into the system:


put_data(State, ConnectionID, Data) ->
    NewState = update_table(State, ConnectionID, Data),
    case get_waiter_for(ConnectionID, State) of
        undefined ->
            NewState;

        PID ->
            notify_waiter(PID, ConnectionID, State)
    end.

First the method adds the data to table of data (which I will detail in just a sec).  Next the function checks to see if anyone is waiting for data to arrive on that connection.  If so, then the waiter should be sent the data immediately.  


Here is the code for update_table:


update_table(State, ConnectionID, Data) ->
    List = parse_packet(Data),
    Table = State#megaphone_main_state.connectionToResults,
    Old = dict:fetch(ConnectionID, Table),
    NewList = Old ++ List,
    NewTable = dict:store(ConnectionID, NewList, Table),
    State#megaphone_main_state{connectionToResults = NewTable}.


This function updates the table that maps from connections to data.  The table stores a list of results as from erlang:decode_packet.  That function expects a different parameter depending on where in the packet one is at.    To ensure that the function is always called at the start of an HTTP packet, megaphone_receiver only sends complete packets onto the megaphone process.

Next time, more from the internals of the megaphone module.

No comments:

Post a Comment