-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.erl
101 lines (88 loc) · 3.51 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
-module (client).
-export ([up/3, read/2, loop/2, gc/1, sleep/1, start/1, read_input/2]).
% [Manager] is the PID of the TransactionManager, this could be changed if we
% start using Sockets instead, for real Distributed System
% NOTE : Might be transformed as a process, for now it's just a module offering
% functions to talk with a TransactionManager
%%----------------------------------------------------------------------
%% Function: up/2
%% Purpose: Ask TransactionManager to update a key/value pair in the Store
%% Args: Manager pid
%% Key
%% Value
%% Returns: Server response "ok"
%%----------------------------------------------------------------------
up(Manager, Key, Value) ->
gen_server:call(Manager, {up, Key, Value}).
%%----------------------------------------------------------------------
%% Function: read/2
%% Purpose: Ask TransactionManager to read a series of keys to get Values
%% Args: Manager pid
%% Keys
%% Returns: List of Values corresponding to asked Keys, order preserved
%%----------------------------------------------------------------------
read(Manager, Keys) ->
gen_server:call(Manager, {read, Keys}).
%%----------------------------------------------------------------------
%% Function: gc/1
%% Purpose: Ask TransactionManager to launch a GrabageCollection
%% Args: Manager pid
%% Returns: Server response "ok"
%%----------------------------------------------------------------------
gc(Manager) ->
gen_server:call(Manager, {gc}).
%%----------------------------------------------------------------------
%% Function: sleep/1
%% Purpose: Sleep a certain amount of time
%% Args: Time of sleep desired
%% Returns: Response "ok"
%%----------------------------------------------------------------------
sleep(Time) ->
timer:sleep(Time),
ok.
%%----------------------------------------------------------------------
%% Function: start/1
%% Purpose: Start the process
%% Args: PID of the manager
%% Returns:
%%----------------------------------------------------------------------
start(Manager) -> spawn(client, read_input, [Manager]).
%%----------------------------------------------------------------------
%% Function: read_input/1
%% Purpose: read the input file and transform it in a readable list
%% Args: PID of the manager
%% Returns:
%%----------------------------------------------------------------------
read_input(Manager, File) ->
%% read the input files to read and write to the manager
{ok, Binary} = file:read_file(File),
Content = unicode:characters_to_list(Binary),
Lines = string:tokens(Content, "\n"),
loop(Manager, Lines).
%%----------------------------------------------------------------------
%% Function: loop/2
%% Purpose: iterate over each line of the list in parameter and launch the appropriate request
%% Args: PID of the manager
%% List of requests to make
%% Returns:
%%----------------------------------------------------------------------
loop(_, []) -> "End of File";
loop(Manager, [H|T]) ->
List = string:tokens(H," "),
case List of
[A|B] when A =:= "up" ->
[X|Y] = B,
%%erlang:display(binary_to_list(X)),
%% Erreur de conversion ici
[Z|_] = Y,
erlang:display(up(Manager,X,Z));
[A|B] when A =:= "read" ->
erlang:display(read(Manager,B));
[A|B] when A =:= "sleep" ->
[X|_] = B,
erlang:display(sleep(list_to_integer(X)));
[A] when A =:= "gc" ->
erlang:display(gc(Manager));
[] -> io:format("Other~n")
end,
loop(Manager, T).