17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

278<br />

User-Agent: curl/7.16.3 (powerpc-apple-darw<strong>in</strong>9.0) libcurl/7.16.3<br />

OpenSSL/0.9.7l zlib/1.2.3<br />

Host: localhost:1156<br />

Accept: */*<br />

The DELETE request is very similar to that of GET with the only major difference be<strong>in</strong>g<br />

that <strong>in</strong> the <strong>in</strong>itial request l<strong>in</strong>e the method is shown as DELETE <strong>in</strong>stead of GET. The semantics<br />

are quite different. With our GET request we asked for the resource referenced by /foo <strong>and</strong><br />

now we are ask<strong>in</strong>g to remove it. This exam<strong>in</strong>ation of packets puts us <strong>in</strong> a good place. We<br />

are now ready to move forward <strong>and</strong> actually implement our scaled down web server.<br />

11.2.3 – Build<strong>in</strong>g the HTTP Server<br />

Each request to the web server will be h<strong>and</strong>led by a pair of processes. One will h<strong>and</strong>le<br />

the TCP socket <strong>and</strong> the other, the fsm, will h<strong>and</strong>le pars<strong>in</strong>g the HTTP protocol. When each<br />

HTTP connection is made a simple one for one supervisor will spawn a gen_server called<br />

ri_web_socket. That process will spawn <strong>and</strong> l<strong>in</strong>k with an FSM called ri_web_fsm. If one<br />

goes down so does the other <strong>and</strong> the connection is automatically cleaned up for us. Figure<br />

11.4 shows the process structure.<br />

Figure 11.5<br />

ri_sup<br />

spawn_l<strong>in</strong>k<br />

spawn_l<strong>in</strong>k<br />

ri_web_socket<br />

ri_web_fsm<br />

The listen socket is created <strong>in</strong> the ri_app module <strong>and</strong> passed through the supervisor<br />

structure <strong>in</strong>to each of the spawned ri_web_socket servers so that is can accept <strong>in</strong>com<strong>in</strong>g<br />

connections on it. The design <strong>and</strong> <strong>in</strong>teraction of the ri_sup <strong>and</strong> the ri_web_socket server <strong>in</strong><br />

terms of how connections are accepted is exactly the same is it was for ti_sup <strong>and</strong> ti_server<br />

earlier on <strong>in</strong> this chapter. The only major difference here is the way that ti_server reads<br />

data off of the socket. Instead of us<strong>in</strong>g {active, true} option we are us<strong>in</strong>g the active once<br />

pattern. This is a very nice pattern because it offers the flow control you get with direct<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!