Permalink
Browse files

Changed spawn to spawn_link and added send rcv test with echo

  • Loading branch information...
1 parent cbd188a commit 94d2b1b37b445987eb166ae83697ea3335ff7274 Gianfranco Alongi committed Mar 13, 2012
Showing with 25 additions and 15 deletions.
  1. +2 −1 Two/Solution/src/port.erl
  2. +10 −10 Two/Solution/src/server.erl
  3. +13 −4 Two/Solution/test/server_tests.erl
@@ -36,7 +36,8 @@ try_load(ModuleName,Module,ErlSource,Binary,Port) ->
false ->
{error,no_handle_function_exported};
true ->
- {ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
+ {ok,Sock} = gen_tcp:listen(Port,[{reuseaddr,true},
+ {active,false}]),
{ok,#port{type = ModuleName,
socket = Sock,
number = Port}}
@@ -86,19 +86,19 @@ perform_command(#port_command{type = open,arguments = Args}, State) ->
[Port,Type] = pick([port,type],Args),
#state{bindings = Bindings, files_dir = Dir, ports = Ports} = State,
File = proplists:get_value(Type,Bindings),
- Pid = spawn(fun() ->
- {ok,PortRecord} = port:open(Dir,Port,File),
- port_loop(PortRecord)
- end),
+ Pid = spawn_link(fun() ->
+ {ok,PortRecord} = port:open(Dir,Port,File),
+ port_loop(PortRecord)
+ end),
{"port opened",State#state{ports = [{Type,Port,Pid}|Ports]}}.
-
+
port_loop(#port{socket = Sock} = Port) ->
{ok,Active} = gen_tcp:accept(Sock),
- spawn(fun() ->
- {ok,Got} = gen_tcp:recv(Active,0),
- gen_tcp:send(Active,port:handle(Port,Got)),
- gen_tcp:close(Active)
- end),
+ 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).
@@ -19,7 +19,8 @@ command_test_() ->
fun bind_type/0,
fun unbind_type/0,
fun list_port/0,
- fun open_port/0
+ fun open_port/0,
+ fun open_echo_port_send/0
]}.
list_bind() ->
@@ -49,7 +50,11 @@ open_port() ->
" ab, 50002\n"
" command-port, 50001",
send_receive_command("list port")).
-
+
+open_echo_port_send() ->
+ send_receive_command("bind ab echo"),
+ send_receive_command("open 50003 ab"),
+ ?assertEqual("this is echoed",send_receive_on_port(50003,"this is echoed")).
%% --------------------------------------------------
start() ->
@@ -69,7 +74,11 @@ assert_port_closed(Port) ->
{error,econnrefused} = gen_tcp:connect("localhost",Port,[]).
send_receive_command(Command) ->
- {ok,Sock} = gen_tcp:connect("localhost",?COMMAND_PORT,[{active,false}]),
- gen_tcp:send(Sock,Command),
+ send_receive_on_port(?COMMAND_PORT,Command).
+
+send_receive_on_port(Port,Send) ->
+ {ok,Sock} = gen_tcp:connect("localhost",Port,[{active,false}]),
+ gen_tcp:send(Sock,Send),
{ok,Socket} = gen_tcp:recv(Sock,0),
Socket.
+

0 comments on commit 94d2b1b

Please sign in to comment.