1
%% ``The contents of this file are subject to the Erlang Public License,
2
%% Version 1.1, (the "License"); you may not use this file except in
3
%% compliance with the License. You should have received a copy of the
4
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
7
%% Software distributed under the License is distributed on an "AS IS"
8
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
%% the License for the specific language governing rights and limitations
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
18
%%----------------------------------------------------------------------
19
%% Purpose: Utility functions for the (snmp manager) user test(s).
20
%%----------------------------------------------------------------------
22
-module(snmp_manager_user_test_lib).
24
-behaviour(snmpm_user).
27
%%----------------------------------------------------------------------
29
%%----------------------------------------------------------------------
30
-include("test_server.hrl").
31
-include("snmp_test_lib.hrl").
34
%%----------------------------------------------------------------------
36
%%----------------------------------------------------------------------
40
register/2, register_monitor/2, unregister/1, unregister/2
43
%%----------------------------------------------------------------------
45
%%----------------------------------------------------------------------
50
-export([handle_error/3,
58
-record(state, {parent, ids = []}).
61
%%----------------------------------------------------------------------
62
%% The user simulation API
63
%%----------------------------------------------------------------------
66
S = #state{parent = self()},
67
proc_lib:start_link(?MODULE, user, [S]).
72
simulate_crash(Pid, Reason) ->
73
call(Pid, {simulate_crash, Reason}).
76
call(Pid, {register, Id}).
78
register_monitor(Pid, Id) ->
79
call(Pid, {register_monitor, Id}).
82
call(Pid, unregister).
84
unregister(Pid, Id) ->
85
call(Pid, {unregister, Id}).
87
user(#state{parent = Pid} = S) ->
88
proc_lib:init_ack(Pid, {ok, self()}),
91
user_loop(#state{parent = Parent} = S) ->
96
{{simulate_crash, Reason}, Parent, Ref} ->
97
reply(Parent, ok, Ref),
100
{{register, Id}, Parent, Ref} ->
102
case lists:member(Id, IDs) of
104
Res = snmpm:register_user(Id, ?MODULE, self()),
105
reply(Parent, Res, Ref),
106
user_loop(S#state{ids = [Id|IDs]});
108
reply(Parent, {error, already_registered}, Ref),
112
{{register_monitor, Id}, Parent, Ref} ->
114
case lists:member(Id, IDs) of
116
Res = snmpm:register_user_monitor(Id, ?MODULE, self()),
117
reply(Parent, Res, Ref),
118
user_loop(S#state{ids = [Id|IDs]});
120
reply(Parent, {error, already_registered}, Ref),
124
{unregister, Parent, Ref} ->
125
Res = [snmpm:unregister_user(Id) || Id <- S#state.ids],
126
reply(Parent, {ok, Res}, Ref),
129
{{unregister, Id}, Parent, Ref} ->
132
case lists:member(Id, IDs) of
134
Res = snmpm:unregister_user(Id),
135
reply(Parent, Res, Ref),
136
lists:delete(Id, IDs);
138
reply(Parent, {error, not_registered}, Ref),
141
user_loop(S#state{ids = IDs2});
144
%% SNMP manager callback messages (from our callback API):
146
{handle_error, Pid, ReqId, Reason} ->
147
do_handle_error(Pid, ReqId, Reason),
150
{handle_agent, Pid, Addr, Port, SnmpInfo} ->
151
do_handle_agent(Pid, Addr, Port, SnmpInfo),
154
{handle_pdu, Pid, Addr, Port, ReqId, SnmpResponse} ->
155
do_handle_pdu(Pid, Addr, Port, ReqId, SnmpResponse),
158
{handle_trap, Pid, Addr, Port, SnmpTrap} ->
159
do_handle_trap(Pid, Addr, Port, SnmpTrap),
162
{handle_inform, Pid, Addr, Port, SnmpInform} ->
163
do_handle_inform(Pid, Addr, Port, SnmpInform),
166
{handle_report, Pid, Addr, Port, SnmpReport} ->
167
do_handle_report(Pid, Addr, Port, SnmpReport),
171
info("received unknown message: ~n~p", [Unknown]),
178
do_handle_error(Pid, ReqId, Reason) ->
179
info("received error callback:"
181
"~n Reason: ~p", [ReqId, Reason]),
182
Pid ! {ignore, self()},
186
do_handle_agent(Pid, Addr, Port, SnmpInfo) ->
187
info("received agent callback:"
190
"~n SnmpInfo: ~p", [Addr, Port, SnmpInfo]),
191
Pid ! {ignore, self()},
195
do_handle_pdu(Pid, Addr, Port, ReqId, SnmpResponse) ->
196
info("received pdu callback:"
200
"~n SnmpResponse: ~p", [Addr, Port, ReqId, SnmpResponse]),
201
Pid ! {ignore, self()},
205
do_handle_trap(Pid, Addr, Port, SnmpTrap) ->
206
info("received trap callback:"
209
"~n SnmpTrap: ~p", [Addr, Port, SnmpTrap]),
210
Pid ! {ignore, self()},
214
do_handle_inform(Pid, Addr, Port, SnmpInform) ->
215
info("received inform callback:"
218
"~n SnmpInform: ~p", [Addr, Port, SnmpInform]),
219
Pid ! {ignore, self()},
223
do_handle_report(Pid, Addr, Port, SnmpReport) ->
224
info("received report callback:"
227
"~n SnmpReport: ~p", [Addr, Port, SnmpReport]),
228
Pid ! {ignore, self()},
233
error_logger:info_msg("USER SIMULATOR " ++ F ++ "~n", A).
238
call(UserPid, Req) ->
239
call(UserPid, Req, 5000).
241
call(UserPid, Req, To) ->
243
UserPid ! {Req, self(), Ref},
245
{Reply, UserPid, Ref} ->
251
reply(Pid, Reply, Ref) ->
252
Pid ! {Reply, self(), Ref}.
254
cast(UserPid, Msg) ->
255
UserPid ! {Msg, self()},
259
%%----------------------------------------------------------------------
260
%% User callback functions:
261
%%----------------------------------------------------------------------
263
handle_error(ReqId, Reason, UserPid) ->
264
UserPid ! {handle_error, self(), ReqId, Reason},
268
handle_agent(Addr, Port, SnmpInfo, UserPid) ->
269
UserPid ! {handle_agent, self(), Addr, Port, SnmpInfo},
273
handle_pdu(Addr, Port, ReqId, SnmpResponse, UserPid) ->
274
UserPid ! {handle_pdu, self(), Addr, Port, ReqId, SnmpResponse},
278
handle_trap(Addr, Port, SnmpTrap, UserPid) ->
279
UserPid ! {handle_trap, self(), Addr, Port, SnmpTrap},
283
handle_inform(Addr, Port, SnmpInform, UserPid) ->
284
UserPid ! {handle_inform, self(), Addr, Port, SnmpInform},
288
handle_report(Addr, Port, SnmpReport, UserPid) ->
289
UserPid ! {handle_report, self(), Addr, Port, SnmpReport},