Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added port close functionality

  • Loading branch information...
commit e1df50677c94317cfda7b7bacbec6d49c587c9db 1 parent d7165de
Gianfranco Alongi authored
Showing with 24 additions and 7 deletions.
  1. +15 −2 Two/Solution/src/server.erl
  2. +9 −5 Two/Solution/test/server_tests.erl
View
17 Two/Solution/src/server.erl
@@ -100,7 +100,12 @@ perform_command(#port_command{type = open,arguments = Args}, State) ->
receive
{opened,Ref,Pid,PortRecord} -> ok
end,
- {"port opened",State#state{ports = [{Type,PortRecord,Pid}|Ports]}}.
+ {"port opened",State#state{ports = [{Type,PortRecord,Pid}|Ports]}};
+
+perform_command(#port_command{type = close,arguments = Args}, State) ->
+ [Port] = pick([port],Args),
+ Ports = close_and_remove_selected_port(Port,State#state.ports),
+ {"port closed",State#state{ports = Ports}}.
port_loop(#port{socket = Sock} = Port) ->
case gen_tcp:accept(Sock) of
@@ -124,7 +129,15 @@ pick_aux([],_,Res) ->
pick_aux([P|T],From,Res) ->
pick_aux(T,From,[proplists:get_value(P,From)|Res]).
-
+close_and_remove_selected_port(PortNumber,[#port{number = PortNumber,
+ socket = Sock} | Ports]) ->
+ gen_tcp:close(Sock),
+ Ports;
+close_and_remove_selected_port(PortNumber,[X|Ports]) ->
+ [X|close_and_remove_selected_port(PortNumber,Ports)];
+close_and_remove_selected_port(_,[]) -> [].
+
+
View
14 Two/Solution/test/server_tests.erl
@@ -23,7 +23,8 @@ command_test_() ->
fun open_port/0,
fun open_echo_port_send/0,
fun open_reverse_port_send/0,
- fun open_reverse_uppercase_and_echo_send/0
+ fun open_reverse_uppercase_and_echo_send/0,
+ fun open_close/0
]}.
list_bind() ->
@@ -74,10 +75,13 @@ open_reverse_uppercase_and_echo_send() ->
?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")).
-
-
-
-
+
+open_close() ->
+ send_receive_command("bind ech echo"),
+ send_receive_command("open 50003 ech"),
+ Res = send_receive_command("close 50003"),
+ ?assertEqual("port closed",Res),
+ assert_port_closed(5003).
%% --------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.