Created
May 1, 2015 18:08
-
-
Save timgluz/d50a667ef3fe7d901d5d to your computer and use it in GitHub Desktop.
First steps in LFE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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) | |
| ) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OK, i now have working version here: https://gist.github.com/timgluz/bc3d7e18643600da5965