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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

239<br />

Notice at annotation #1 that we have added resource discovery as a dependency.<br />

With the boilerplate out of the way how do we go about gett<strong>in</strong>g a hold of the other cache<br />

<strong>in</strong>stances <strong>in</strong> the cloud of nodes. Not a problem, after go<strong>in</strong>g through chapter 8 <strong>and</strong> build<strong>in</strong>g<br />

our resource discovery system this answer should come right to m<strong>in</strong>d. We will simply add an<br />

“I have” or more exactly we add a local resource where the resource_type() is ‘simple_cache’<br />

<strong>and</strong> the resource_<strong>in</strong>stance() is node(), our local node. We do that simply by call<strong>in</strong>g:<br />

resource_discovery:add_local_resource(simple_cache, node())<br />

The next step is to tell the system about our “I want” or target resource type which is<br />

simple_cache as well. We do this by add<strong>in</strong>g the l<strong>in</strong>e:<br />

resource_discovery:add_target_resource_type(simple_cache)<br />

The third <strong>and</strong> f<strong>in</strong>al step to mak<strong>in</strong>g us simple_cache aware is to add <strong>in</strong> the call to trade<br />

resources with the rest of the cloud <strong>and</strong> then to wait a reasonable time for those resources to<br />

be shared. Remember our resource discovery system is very asynchronous. I leave it to the<br />

reader to improve on this timeout model <strong>and</strong> make the resource discovery system more<br />

synchronous if need be.<br />

resource_discovery:trade_resources(),<br />

timer:sleep(WAIT_FOR_RESOURCES),<br />

This code, predictably I am sure, goes <strong>in</strong>to our sc_app file right after the call to jo<strong>in</strong> the<br />

cloud of nodes. This is listed <strong>in</strong> list<strong>in</strong>g 9.9:<br />

list<strong>in</strong>g 9.9 – sc_app With Resource Discovery<br />

start(_StartType, _StartArgs) -><br />

ensure_contact(),<br />

sc_resource_discovery:add_local_resource(simple_cache, node()),<br />

sc_resource_discovery:add_target_resource_type(simple_cache),<br />

sc_resource_discovery:trade_resources(),<br />

timer:sleep(WAIT_FOR_RESOURCES),<br />

sc_store:<strong>in</strong>it(),<br />

case sc_sup:start_l<strong>in</strong>k() of<br />

{ok, Pid} -><br />

{ok, Pid};<br />

Error -><br />

Error<br />

end.<br />

How simple was that! We now know about all the other simple cache <strong>in</strong>stances <strong>in</strong> our<br />

network <strong>and</strong> they know about us. Time to replicate.<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!