Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added proper closing of opened ports and test with multiple bindings

  • Loading branch information...
commit d7165defb5f15f16b6b51086bc07fc5c2c576637 1 parent 5616a67
Gianfranco Alongi authored
View
36 Two/Solution/src/server.erl
@@ -5,7 +5,7 @@
stop/1]).
-record(state,{bindings = [] :: [{string(),string()}],
- ports = [] :: [{atom(),non_neg_integer(),pid()}],
+ ports = [] :: [{atom(),#port{},pid()}],
command_port :: non_neg_integer(),
files_dir :: string()
}).
@@ -43,9 +43,13 @@ loop(Sock,ServerState) ->
gen_tcp:close(Session),
loop(Sock,ServerState);
{ok,"stop"} ->
- gen_tcp:send(Session,"stopping"),
+ gen_tcp:send(Session,"stopping"),
gen_tcp:close(Session),
- gen_tcp:close(Sock);
+ gen_tcp:close(Sock),
+ Ports = ServerState#state.ports,
+ lists:foreach(
+ fun({_,#port{socket = PortSocket},_}) -> gen_tcp:close(PortSocket)
+ end,Ports);
{ok,Command} ->
Res = command:parse(Command),
{Reply,NewServerState} = perform_command(Res,ServerState),
@@ -77,7 +81,7 @@ perform_command(#port_command{type = list},State) ->
Last = "\n command-port, "++integer_to_list(State#state.command_port),
{lists:foldl(
fun({Type,Port,_Pid},Acc) ->
- Acc ++ "\n "++Type++", "++integer_to_list(Port)
+ Acc ++ "\n "++Type++", "++integer_to_list(Port#port.number)
end,
"ports:",
State#state.ports)++Last,State};
@@ -90,22 +94,26 @@ perform_command(#port_command{type = open,arguments = Args}, State) ->
Self = self(),
Pid = spawn_link(fun() ->
{ok,PortRecord} = port:open(Dir,Port,File),
- Self ! {opened,Ref,self()},
+ Self ! {opened,Ref,self(),PortRecord},
port_loop(PortRecord)
end),
receive
- {opened,Ref,Pid} -> ok
+ {opened,Ref,Pid,PortRecord} -> ok
end,
- {"port opened",State#state{ports = [{Type,Port,Pid}|Ports]}}.
+ {"port opened",State#state{ports = [{Type,PortRecord,Pid}|Ports]}}.
port_loop(#port{socket = Sock} = Port) ->
- {ok,Active} = gen_tcp:accept(Sock),
- spawn_link(fun() ->
- {ok,Got} = gen_tcp:recv(Active,0),
- gen_tcp:send(Active,port:handle(Port,Got)),
- gen_tcp:close(Active)
- end),
- port_loop(Port).
+ case gen_tcp:accept(Sock) of
+ {error,closed} ->
+ ok;
+ {ok,Active} ->
+ spawn_link(fun() ->
+ {ok,Got} = gen_tcp:recv(Active,0),
+ gen_tcp:send(Active,port:handle(Port,Got)),
+ gen_tcp:close(Active)
+ end),
+ port_loop(Port)
+ end.
pick(What,From) ->
View
18 Two/Solution/test/server_tests.erl
@@ -22,7 +22,8 @@ command_test_() ->
fun list_port/0,
fun open_port/0,
fun open_echo_port_send/0,
- fun open_reverse_port_send/0
+ fun open_reverse_port_send/0,
+ fun open_reverse_uppercase_and_echo_send/0
]}.
list_bind() ->
@@ -63,6 +64,21 @@ open_reverse_port_send() ->
send_receive_command("open 50010 rev"),
?assertEqual("deohce si siht",send_receive_on_port(50010,"this is echoed")).
+open_reverse_uppercase_and_echo_send() ->
+ send_receive_command("bind rev reverse_module"),
+ send_receive_command("bind ech echo"),
+ send_receive_command("bind upp uppercase_module"),
+ send_receive_command("open 50002 rev"),
+ send_receive_command("open 50003 ech"),
+ send_receive_command("open 50004 upp"),
+ ?assertEqual("deohce si siht",send_receive_on_port(50002,"this is echoed")),
+ ?assertEqual("this is echoed",send_receive_on_port(50003,"this is echoed")),
+ ?assertEqual("THIS IS ECHOED",send_receive_on_port(50004,"this is echoed")).
+
+
+
+
+
%% --------------------------------------------------
start() ->
View
4 Two/Solution/test/test_files/uppercase_module.erl
@@ -0,0 +1,4 @@
+-module(uppercase_module).
+-export([handle/1]).
+handle(X) ->
+ string:to_upper(X).
Please sign in to comment.
Something went wrong with that request. Please try again.