-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd4_interface.erl
40 lines (35 loc) · 1008 Bytes
/
md4_interface.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
-module(md4_interface).
-export([start/0, stop/0, call/1]).
start() ->
spawn(fun() ->
register(md4_interface, self()),
process_flag(trap_exit, true),
Port = open_port({spawn, "./md4driver"}, [{packet, 2}]),
loop(Port)
end).
stop() ->
md4_interface ! stop.
call(Data) ->
md4_interface ! {self(), Data},
receive
{md4_interface, Result} ->
list_to_binary(Result)
end.
loop(Port) ->
receive
{Caller, Data} ->
Port ! {self(), {command, Data}},
receive
{Port, {data, Reply}} ->
Caller ! {md4_interface, Reply}
end,
loop(Port);
stop ->
Port ! {self(), close},
receive
{Port, closed} ->
exit(normal)
end;
{'EXIT', Port, Reason} ->
exit({port_terminated, Reason})
end.