Skip to content

Instantly share code, notes, and snippets.

@timgluz
Created May 1, 2015 18:08
Show Gist options
  • Select an option

  • Save timgluz/d50a667ef3fe7d901d5d to your computer and use it in GitHub Desktop.

Select an option

Save timgluz/d50a667ef3fe7d901d5d to your computer and use it in GitHub Desktop.
First steps in LFE
-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)}
end,
loop(Dir).
(defmodule afile_server
(export [start 1]
[loop 1]))
;;usage:
;; (set ftp (afile_server:start "."))
;; (! ftp (tuple client-pid 'list_dir))
(defun start [dir]
(spawn 'afile_server 'loop '(dir)))
(defun loop [dir]
(receive
((tuple client 'list_dir)
(lfe_io:format "Showing files in ~w ~n" (list dir))
(lfe_io:format "Client: ~w~n" (list client))
(! client 'files)
(loop dir))
((tuple client 'get_file)
(lfe_io:format "Get a file~n" ())
(let [(file_path (filename:join dir "hello.lfe"))]
(! client (file:read_file file_path))))
(other ; handling not matching messages
(lfe_io:format "Got unknown message.~n" ())))
;(loop dir)
)
@timgluz
Copy link
Author

timgluz commented May 2, 2015

OK, i now have working version here: https://gist.github.com/timgluz/bc3d7e18643600da5965

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment