14 Aug 2013, 16:52
Generic-user-small

David Rouchy (3 posts)

Hi,

I’m trying to implement the exercise on the file_server, adding a method put_file.

I’ve modified the server

-module(a_file_server).
-export([start/1, loop/1]).

start(Dir) -> spawn(a_file_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, File, Binary}} ->
      Full = filename:join(Dir, File),
      file:write_file(Full, Binary),
      Client ! {self(), file_sent}
  end,
  loop(Dir).

and the client

-module(a_file_client).
-export([ls/1, get_file/2, put_file/2]).

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, File) ->
  Content = file:read_file(File),
  Server ! { self(), {put_file, File, Content}},
  receive
    { Server, Result} ->
      Result
  end.

it compiles, and the execution looks ok

Server = a_file_server("/tmp").
a_file_client:put_file(Server, "a_file_client.erl").
file_sent

but the file does not appear in the /tmp directory.

Thanks,

David

21 Aug 2013, 17:23
Generic-user-small

Kamil Kieliszczyk (3 posts)

TIP: take a look at file:read_file/1 it returns either {ok, Binary} or {error, Reason} not just Binary.

Write if you have any other problems

22 Aug 2013, 08:32
Generic-user-small

David Rouchy (3 posts)

As simple as that.

Thanks for the answer.

David

  You must be logged in to comment