-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.erl
42 lines (38 loc) · 1.13 KB
/
client.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-module(client).
-include("config.hrl").
-mode(compile).
-export([main/1]).
main(_) ->
{ok, Socket} = gen_tcp:connect(?HOST, ?PORT, ?OPTIONS),
command(Socket).
command(Socket) ->
case io:get_line("Enter command: ") of
Data ->
gen_tcp:send(Socket, Data),
case wait_for_command(Socket) of
exit ->
ok;
_ ->
command(Socket)
end
end.
switch_command(Socket, Filename, Offset, Command) ->
case Command of
?UPLOAD ->
utils:send_file(Socket, Filename, Offset, ?CLIENT_FOLDER);
?DOWNLOAD ->
ClientOffset = filelib:file_size(?CLIENT_FOLDER ++ Filename),
gen_tcp:send(Socket, <<?APPROVEMENT:8/integer, ClientOffset:32/integer>>),
{ok, IoDevice} = file:open(?CLIENT_FOLDER ++ Filename, [append]),
utils:wait_for_file(Socket, IoDevice)
end.
wait_for_command(Socket) ->
case gen_tcp:recv(Socket, 0, ?TIMEOUT) of
{ok, <<Offset:32/integer, Command:8/integer, Filename/binary>>} ->
switch_command(Socket, binary_to_list(Filename), Offset, Command);
{error, closed} ->
io:format("You exited."),
exit;
{error, timeout} ->
ok
end.