Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. -module(udp_test).
  2. -behaviour(gen_server).
  3. -define(SERVER, ?MODULE).
  4. -define(PORT, 9876).
  5. -define(TIMEOUT, 10).
  6.  
  7. %% ------------------------------------------------------------------
  8. %% API Function Exports
  9. %% ------------------------------------------------------------------
  10.  
  11. -export([start_link/2, send/1,
  12. test_stuff/0, start_stuff/1]).
  13.  
  14. %% ------------------------------------------------------------------
  15. %% gen_server Function Exports
  16. %% ------------------------------------------------------------------
  17.  
  18. -export([init/1, handle_call/3, handle_cast/2,
  19. handle_info/2, terminate/2, code_change/3]).
  20.  
  21. -record(state, {id = 0,
  22. sock}).
  23.  
  24. %% ------------------------------------------------------------------
  25. %% API Function Definitions
  26. %% ------------------------------------------------------------------
  27.  
  28. start_link(ID, Sock) ->
  29. gen_server:start_link(?MODULE, [{id, ID}, {socket, Sock}], []).
  30.  
  31. %% ------------------------------------------------------------------
  32. %% gen_server Function Definitions
  33. %% ------------------------------------------------------------------
  34.  
  35. init(Args) ->
  36. ID = proplists:get_value(id, Args, 1),
  37. case proplists:get_value(socket, Args) of
  38. undefined ->
  39. {stop, nosock};
  40. Sock ->
  41. {ok, #state{ id=ID, sock=Sock }, 0}
  42. end.
  43.  
  44. handle_call(_Request, _From, State) ->
  45. {stop, {error, unknownmsg}, State}.
  46.  
  47. handle_cast(_Request, State) ->
  48. {stop, {error, unknownmsg}, State}.
  49.  
  50. handle_info(timeout, #state{ sock=undefined } = State) ->
  51. {noreply, State, ?TIMEOUT};
  52. handle_info(timeout, #state{ id=ID, sock=Sock } = State) ->
  53. TO = case gen_udp:recv(Sock, 4) of
  54. {error, _} -> ?TIMEOUT;
  55. _Data ->
  56. catch ets:insert_new(?MODULE, {ID, 0}),
  57. catch ets:update_counter(?MODULE, ID, 1),
  58. ?TIMEOUT
  59. end,
  60. {noreply, State, TO}.
  61.  
  62. terminate(_Reason, _State) ->
  63. ok.
  64.  
  65. code_change(_OldVsn, State, _Extra) ->
  66. {ok, State}.
  67.  
  68. %% ------------------------------------------------------------------
  69. %% Test Function Definitions
  70. %% ------------------------------------------------------------------
  71.  
  72. test_stuff() ->
  73. start_stuff(10),
  74. [udp_test:send("fooobaar") || _ <- lists:seq(1,1000)],
  75. timer:sleep(1000),
  76. io:format("Total: ~p~n", [ets:tab2list(?MODULE)]),
  77. init:stop().
  78.  
  79. start_stuff(N) ->
  80. ets:new(?MODULE, [named_table, public]),
  81. {ok, S} = gen_udp:open(?PORT, [{active, false}, binary]),
  82. [start_link(ID, S) || ID <- lists:seq(1, N)].
  83.  
  84. send(Data) ->
  85. spawn(fun() ->
  86. {ok, S} = gen_udp:open(0, [{active, false}, binary]),
  87. gen_udp:send(S, localhost,?PORT, Data)
  88. end).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement