- I was able to send and receive messages.
- I changed the pass-through from single to multi-threaded.
- IT ACTUALLY WORKED!
One of the things I came across while developing megaphone and working with erlang has been the notion of "active mode" when dealing with TCP sockets. When using gen_tcp, you can open a socket in active mode or passive mode.
In active mode, when a packet of data arrives at the socket, the process associated with the socket gets an erlang message. In passive mode, the process must ask for data via gen_tcp:recv.
In a language like C or Java one would always use what is more or less passive mode: you perform a read to get the next block of data. Coming from that background I had trouble understanding what the rationale behind active mode was. I think now I understand a little better.
Languages like C and Java do not have the notion of message passing that erlang does, hence active mode does not make as much sense. With erlang, I can use active mode and create one process (thread) that handles both sending and receiving data:
loop(Socket) ->
receive
{ tcp, ReceiveSocket, Data } -> do something...
{ write, Data } ->
gen_tcp:send(Socket, Data)
end,
loop(Socket).
Note that the "write" message would have to be sent from another process.
I find this a more natural way of handling socket I/O than the C/Java approaches that I've used.
Next time: some results from testing with ejabberd.
No comments:
Post a Comment