aboutsummaryrefslogtreecommitdiffstats
path: root/lib/snmp/test/snmp_test_mgr_counter_server.erl
blob: a72eefd6e53e3d4d1d5657e0ca96fc89573fcf06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%% 
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2014-2014. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%% 

%% 
%% The reason for this (test) counter server is that the 
%% agent test suite is implemented in such a way that the 
%% agent is started once and then used for several test cases.
%% Each request is given a request id which *was* generated using 
%% random! It is therefor possible, although unlikely, that a 
%% request may get a request id that has recently been used, 
%% which will cause the agent to silently reject the request.
%% For this reason, we start this server at the start of the
%% agent suite and stop it at the end and all request ids are 
%% generated by this server.
%% 

-module(snmp_test_mgr_counter_server).

-export([start/0, stop/0, increment/4]).

-define(SERVER, ?MODULE).
-define(TAB,    snmp_test_mgr_counter_tab).


%%%-------------------------------------------------------------------
%%% API
%%%-------------------------------------------------------------------

-spec start() -> ok.
    
start() ->
    Parent      = self(),
    ReqIdServer = spawn(fun() -> init(Parent) end),
    receive
	{ReqIdServer, ok} ->
	    ok;
	{ReqIdServer, {error, Reason}} ->
	    exit({failed_starting_counter_server, Reason})
    after 5000 ->
	    exit(ReqIdServer, kill), % Cleanup, just in case
	    exit({failed_starting_counter_server, timeout})
    end.

-spec stop() -> {ok, Counters :: list()} | {error, Reason :: term()}.
    
stop() ->
    request(stop).


-spec increment(Counter  :: atom(), 
	       Initial   :: non_neg_integer(), 
	       Increment :: pos_integer(), 
	       Max       :: pos_integer()) -> 
    Next :: pos_integer().

increment(Counter, Initial, Increment, Max) ->
    Request = {increment, Counter, Initial, Increment, Max}, 
    case request(Request) of
	{ok, ReqId} ->
	    ReqId;
	{error, Reason} ->
	    exit(Reason)
    end.


request(Request) ->
    Id  = make_ref(),
    Msg = {self(), Id, Request}, 
    try
	begin
	    global:send(?SERVER, Msg),
	    receive
		{reply, Id, Reply} ->
		    {ok, Reply}
	    end
	end
    catch
	T:E ->
	    {error, {T, E}}
    end.
		
    
%%%-------------------------------------------------------------------
%%% Internal functions
%%%-------------------------------------------------------------------

init(Parent) ->
    p("starting"),
    case global:register_name(?SERVER, self()) of
	yes ->
	    p("name registration ok"),
	    Parent ! {self(), ok};
	no ->
	    p("name registration failed"),
	    Parent ! {self(), registration_failed},
	    exit(registration_failed)
    end,
    ets:new(?TAB, [set, named_table, {keypos, 1}]),
    loop().

loop() ->
    receive
	{From, Id, {increment, Counter, Initial, Increment, Max}} ->
	    Position  = 2,
	    Threshold = Max,
	    SetValue  = Initial,
	    UpdateOp  = {Position, Increment, Threshold, SetValue},
	    NextVal = 
		try ets:update_counter(?TAB, Counter, UpdateOp) of
		    Next when is_integer(Next) ->
			p("increment ~w: (next) ~w", [Counter, Next]),
			Next
		catch
		    error:badarg ->
			%% Oups, first time
			p("increment ~w: (initial) ~w", [Counter, Initial]),
			ets:insert(?TAB, {Counter, Initial}),
			Initial
		end,
	    From ! {reply, Id, NextVal},
	    loop();

	{From, Id, stop} ->
	    p("stop"),
	    Counters = ets:tab2list(?TAB), 
	    From ! {reply, Id, Counters}, 
	    exit(normal)
    end.


p(F) ->
    p(F, []).

p(F, A) ->
    io:format("*** [~s] COUNTER-SERVER [~w] " ++ F ++ "~n", 
	      [snmp_test_lib:formated_timestamp(), self() | A]).