[erlang-questions] Not looping after processing read_file request

Justin HANEKOM justin.hanekom@REDACTED
Sun Sep 7 18:29:20 CEST 2014


Hi,

BEGINNER QUESTION ALERT

I'm trying to create a file server that loops, responding to 2 requests: list_dir; and read_file. For some reason the server loops sometimes, but for some unknown reason after successfully  responding to a read_file request does not receive/respond to any further requests.

I've studied this code extensively looking for my error, but to no avail. Could someone please put me out of my misery?

Thanks,

Justin

-module(afile_server).
-export([start/1]).

start(Dir) ->
    spawn_loop_in_dir(Dir).

spawn_loop_in_dir(Dir) ->
    Fn = fun() -> loop(Dir) end,
    spawn(Fn).

loop(Dir) ->
    receive
        {Client, list_dir}          -> list_dir(Client, Dir);
        {Client, {read_file, File}} -> read_file(Client, Dir, File)
    end,
    loop(Dir).

list_dir(Client, Dir) ->
    Client ! prefix_with_self(list_dir_reply(Dir)).

list_dir_reply(Dir) ->
    DirList = file:list_dir(Dir),
    {list_dir, DirList}.

read_file(Client, Dir, File) ->
    Client ! prefix_with_self(read_file_reply(Dir, File)).

read_file_reply(Dir, File) ->
    Fullname = filename:join(Dir, File),
    Contents = file:read_file(Fullname),
    {read_file, Contents}.

prefix_with_self(Msg) ->
    {self(), Msg}.

 

Justin

http://180movie.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140907/8c4e3a5c/attachment.htm>


More information about the erlang-questions mailing list