Another Erlang XML Project, Part 2 - Converted to gen_server

Joe Williams - - January 07, 2009

A while back I wrote some code in Erlang to grab your public Amazon wishlist based off of your email address. Today I had the urge to rewrite it using OTP, specifically gen_server. The code is up on github. Switching it over to use gen_server is fairly trivial since the sequential code is already written. Basically all that needs to be done is to setup the handle_call method to send any email addresses over to the get_wishlist method for retrieval. Here’s what the handle_call looks like:

handle_call(Email_addr, _From, State) ->
%Reply back with the result from get_wishlist
Reply = get_wishlist(Email_addr),
{reply, Reply, State}.

The rest of the functions look pretty much as they did before so I won’t put them here. One other change I made was to start_link to have it also run inets:start() so we can make the http requests in the other functions.

Here’s how the thing works:

[zeusfaber@der-dieb ebin]$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [64-bit] [smp:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.6.5 (abort with ^G)
1> c(’../src/amazon_wishlist_otp.erl’).
{ok,amazon_wishlist_otp}
2> amazon_wishlist_otp:start_link().
ok
3> gen_server:call(amazon_wishlist_otp, “SOMEADDR@SOMEPLACE.com”).
[[“Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology”,
“Algorithms: Sequential, Parallel, and Distributed”,
“Starting Forth: An Introduction to the Forth Language and Operating System for Beginners and Professionals (Prentice-Hall Software Series)”,
“Biobazaar: The Open Source Revolution and Biotechnology”,
“Nmap Network Scanning: The Official Nmap Project Guide to Network Discovery and Security Scanning”,
“American Nerd: The Story of My People”,
“FreeDarko presents The Macrophenomenal Pro Basketball Almanac: Styles, Stats, and Stars in Today’s Game”,
“CP Technologie USB TO SERIAL ADAPTER ( CP-US-03 )”,
“Made to Stick: Why Some Ideas Survive and Others Die”]]



Categories: Blogs  Joe Williams  

Comments

anonymous avatar

Nice job Joe. I just tested this and it works as expected. I’m emailing it to a co-worker now as an example of how to use xmerl in the real world.

-Andrew

Posted by AndrewStone on 13 Jan 2009 at 05:58



 
anonymous avatar

Give Thanks you therefore much for the web logs enjoy! Observed to be a part of overmuch a talented radical of wedding professionals. Fantastic fashion to beginning off the another year! Thank you!
montreal erotic massage

Posted by Atwater on 05 May 2010 at 17:54



 
anonymous avatar

Amazing posting because it shows that on that point is fantastic separate of residential area spirit out in that respect.
stubby holders

Posted by Albern on 22 May 2010 at 11:24



 


Add comment

Name:

Email:

URL:

Smileys

Remember my personal information

Notify me of follow-up comments?