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.

112<br />

I use telnet to connect via TCP on port 1055 to my now runn<strong>in</strong>g tr_server. Once<br />

connected I run the follow<strong>in</strong>g comm<strong>and</strong> obey<strong>in</strong>g our protocol, really <strong>Erlang</strong>s protocol,<br />

<strong>in</strong>it:stop(). This should result <strong>in</strong> a function call of the form <strong>in</strong>it:stop(), or actually, apply(<strong>in</strong>it,<br />

stop, []), be<strong>in</strong>g generated <strong>and</strong> called. S<strong>in</strong>ce <strong>in</strong>it:stop/0 returns the atom ok we expect to<br />

see the str<strong>in</strong>g “ok” sent back to our telnet session – which we do aga<strong>in</strong>. The next th<strong>in</strong>g we<br />

see is “Connection closed by foreign host”. This is pr<strong>in</strong>ted by the telnet utility because the<br />

socket it was connected to was close by the server, our <strong>Erlang</strong> VM. The reason for that is<br />

that the function <strong>in</strong>it:stop() shuts down ERTS. Wonderful <strong>in</strong> that we have just demonstrated<br />

that our RPC server works <strong>and</strong> we have also shown just how dangerous it can be! That was<br />

a meaty section. We have built <strong>in</strong> just a few l<strong>in</strong>es of code a very powerful application that is<br />

quite useful <strong>in</strong> the real world.<br />

3.4 – Summary<br />

We have covered quite a bit <strong>in</strong> this chapter. We have learned a little more about<br />

processes, modules, functions, <strong>and</strong> messages. We have also learned about behaviours <strong>and</strong><br />

the three parts that make them what they are the:<br />

1. Behaviour Interface<br />

2. Behaviour Implementation<br />

3. Behaviour Conta<strong>in</strong>er<br />

We have covered the gen_server behaviour specifically at some depth <strong>and</strong> we did this all<br />

through a real world example <strong>in</strong> our tcp_rpc service. In the next chapter we are go<strong>in</strong>g to<br />

take our little st<strong>and</strong> alone generic RPC server <strong>and</strong> we are go<strong>in</strong>g to hook it <strong>in</strong>to a structure<br />

that is go<strong>in</strong>g to render it enterprise grade. We are go<strong>in</strong>g to hook it <strong>in</strong>to an <strong>OTP</strong> application.<br />

Once that is complete our little server will be part of an application that is versioned, fault<br />

tolerant, ready for use by others <strong>in</strong> their projects, <strong>and</strong> ready to go to production. Chapter 4<br />

will take what you have learned <strong>in</strong> Chapter 2 <strong>and</strong> here <strong>in</strong> chapter 3 <strong>and</strong> will add on another<br />

layer <strong>in</strong> the basic underst<strong>and</strong><strong>in</strong>g of <strong>Erlang</strong>/<strong>OTP</strong> as a framework by teach<strong>in</strong>g you the<br />

fundamental fault tolerant structure, the supervisor, <strong>and</strong> by teach<strong>in</strong>g you how to roll up your<br />

functionality <strong>in</strong>to nice <strong>OTP</strong> packages. From there we will ref<strong>in</strong>e that underst<strong>and</strong><strong>in</strong>g <strong>in</strong><br />

Chapter 5 expla<strong>in</strong><strong>in</strong>g the theory beh<strong>in</strong>d some of the th<strong>in</strong>gs you have taken for granted up<br />

until that po<strong>in</strong>t. Once that is complete we will be ready to move on <strong>in</strong>to creat<strong>in</strong>g someth<strong>in</strong>g<br />

seriously powerful <strong>Erlang</strong> software.<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!