Last active
August 29, 2015 14:20
-
-
Save timgluz/bc3d7e18643600da5965 to your computer and use it in GitHub Desktop.
first working version of afile_server.erl 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
| (defmodule afile_client | |
| (export [ls 1] | |
| [get_file 2])) | |
| (defun ls [server] | |
| (! server (tuple (self) 'list_dir)) | |
| (receive | |
| ((tuple server msg) msg) | |
| (after 3000 | |
| (lfe_io:format "ls: no response~n" ())))) | |
| (defun get_file [server file_name] | |
| (! server (tuple (self) | |
| (tuple 'get_file file_name))) | |
| (receive | |
| ((tuple server content) content) | |
| (after 3000 | |
| (lfe_io:format "get_file: no response~n" ())))) |
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 (list dir))) | |
| (defun loop [dir] | |
| (receive | |
| ((tuple client 'list_dir) | |
| (lfe_io:format "pwd: ~p~n" (list dir)) | |
| (! client (tuple (self) (file:list_dir dir)))) | |
| ((tuple client (tuple 'get_file file_name)) | |
| (let [(file_path (filename:join dir file_name))] | |
| (lfe_io:format "file requested: ~p~n" (list file_name)) | |
| (! client (tuple (self) (file:read_file file_path)))))) | |
| (loop dir)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment