Skip to content

Instantly share code, notes, and snippets.

@Velrok
Created April 22, 2016 22:49
Show Gist options
  • Save Velrok/e1ff2677250aaa386597cc2a426d8d35 to your computer and use it in GitHub Desktop.
Save Velrok/e1ff2677250aaa386597cc2a426d8d35 to your computer and use it in GitHub Desktop.
learning Erlang
-module(afile_client).
-export([ls/1, get_file/2, put_file/3]).
ls(Server) ->
Server ! {self(), list_dir},
receive
{Server, FileList} ->
FileList
end.
get_file(Server, File) ->
Server ! {self(), {get_file, File}},
receive
{Server, Content} ->
Content
end.
put_file(Server, FileName, Content) ->
Server ! {self(), {put_file, FileName, Content}},
receive
{Server, X} ->
X
end.
% c(afile_client).
% c(afile_server).
% Server = afile_server:start(".").
% afile_client:put_file(Server, "hello.txt", "hello world").
-module(afile_server).
-export([start/1, loop/1]).
start(Dir) -> spawn(afile_server, loop, [Dir]).
loop(Dir) ->
receive
{Client, list_dir} ->
Client ! {self(), file:list_dir(Dir)};
{Client, {get_file, File}} ->
Full = filename:join(Dir, File),
Client ! {self(), file:read_file(Full)};
{Client, {put_file, FileName, Content}} ->
Full = filename:join(Dir, FileName),
Client ! {self(), file:write_file(Full, Content)}
end,
loop(Dir).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment