- 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 the part that receives headers.
- I finished the receiver
give_packet_to_megaphone (ConnectionID, Packet) ->
megaphone ! { ConnectionID, Packet }.
Perhaps I need to work on the name a bit, but the function is ready. At least for testing.
Emboldened by this, I went on and started writing the part of the system that sends packets to ECM. In a burst of creativity I named this "megaphone_sender." The startup for this module doesn't do much, just starts up a new process and then registers the PID as "megaphone_sender." Here is the startup:
start(Socket, Config) ->
State = #megaphone_state{socket = Socket, config = Config},
Pid = spawn(megaphone_sender, main_loop, [State]),
register(megaphone_send, Pid).
The main loop basically just sits around waiting for packets and then sends them off to ECM:
main_loop (State) ->
receive
{ ConnectionID, Packet } ->
send_packet(State#megaphone_state.socket, ConnectionID, Packet),
main_loop(State);
shutdown ->
ok
end.
The send_packet function is also pretty simple and just formats the packet:
send_packet(Socket, ConnectionID, Packet) ->
PacketLength = length(Content) + 32,
StrPacketLength = lists:flatten(io_lib:format("~10..0B", [ PacketLength ])),
Packet = StrPacketLength ++ "|" ++ ConnectionId ++ "|" ++ Content,
gen_tcp:send(Socket, Packet).
The next step is to refactor the megaphone module to take into account that the ECM communications logic has been moved to other modules.
No comments:
Post a Comment