Permalink
Browse files

updated udp_test with message

  • Loading branch information...
1 parent 487e195 commit 19edb3dd7fb8462810375aab0bdb989241a97350 @meeusr committed Aug 17, 2012
Showing with 31 additions and 14 deletions.
  1. +31 −14 networking/gen_udp_test.erl
@@ -2,7 +2,7 @@
-export([start_server/0, stop_server/0, client_sync/1,client_async/1]).
start_server() ->
- register(udp_server,spawn(fun() -> server(6001) end)).
+ register(udp_server,spawn(fun() -> server(9000) end)).
stop_server() ->
udp_server ! stop.
@@ -17,8 +17,9 @@ server(Port) ->
loop(Socket,MsgCount) ->
receive
{udp, Socket, Host, Port, Data} ->
- %% io:format("server received:~p~n",[Msg]),
- gen_udp:send(Socket, Host, Port, Data),
+ {RxId,_,TS} = message:parse(binary_to_list(Data)),
+ NewData = message:compose(RxId,MsgCount,TS),
+ gen_udp:send(Socket, Host, Port, NewData),
loop(Socket,MsgCount+1);
{stats} ->
io:format("~p~n",[MsgCount]),
@@ -28,15 +29,15 @@ loop(Socket,MsgCount) ->
client_sync(N) ->
{ok, Socket} = gen_udp:open(0, [binary]),
- %% io:format("client opened socket=~p~n",[Socket]),
+ io:format("client opened socket=~p~n",[Socket]),
Start = erlang:now(),
sendmessage_sync(Socket,N),
io:format("stop : ~p\n",[timer:now_diff(erlang:now(),Start)]),
gen_udp:close(Socket).
client_async(N) ->
{ok, Socket} = gen_udp:open(0, [binary]),
- %% io:format("client opened socket=~p~n",[Socket]),
+ io:format("client opened socket=~p~n",[Socket]),
Start = erlang:now(),
sendmessage_async(Socket,N),
recvmessage_async(Socket,N),
@@ -45,26 +46,42 @@ client_async(N) ->
sendmessage_sync(_Socket,0) -> ok;
sendmessage_sync(Socket,N) ->
- ok = gen_udp:send(Socket, "127.0.0.1", 9000, "test"),
+ Data = message:compose(N,0,erlang:now()),
+ ok = gen_udp:send(Socket, "127.0.0.1", 9000,Data),
receive
- {udp, Socket, _, _, Data} = Msg ->
- %% io:format("client received:~p data:~p ~n",[Msg,Data]),
- sendmessage_sync(Socket,N-1)
+ {udp, Socket, _, _, ReplyData} ->
+ {ClientId,ServerId,TS} = message:parse(binary_to_list(ReplyData)),
+ io:format("RT=~p ~n",[timer:now_diff(now(),TS)]),
+ case N>0 of
+ true -> sendmessage_sync(Socket,N-1);
+ _ -> ok
+ end;
+
+ Msg ->
+ io:format("sendmessagesync unknown ~p~n",[Msg])
+
after 2000 ->
io:format("sendmessagesync timeout~n")
end.
sendmessage_async(_Socket,0) -> ok;
sendmessage_async(Socket,N) ->
- ok = gen_udp:send(Socket, "127.0.0.1", 9000, "test"),
+ Data = message:compose(N,0,erlang:now()),
+ ok = gen_udp:send(Socket, "127.0.0.1", 9000, Data),
sendmessage_async(Socket,N-1).
recvmessage_async(_Socket,0) -> ok;
recvmessage_async(Socket,N) ->
receive
- {udp, Socket, _, _, Data} = Msg ->
- %% io:format("client received:~p data:~p ~n",[Msg,Data]),
- recvmessage_async(Socket,N-1)
+ {udp, Socket, _, _, ReplyData} ->
+ {ClientId,ServerId,TS} = message:parse(binary_to_list(ReplyData)),
+ io:format("RT=~p ~n",[timer:now_diff(now(),TS)]),
+ sendmessage_sync(Socket,N-1);
+
+ Msg ->
+ io:format("sendmessageasync unknown ~p~n",[Msg])
+
after 2000 ->
- io:format("recvmessage_async timeout~n")
+ io:format("sendmessageasync timeout~n")
+
end.

0 comments on commit 19edb3d

Please sign in to comment.