snmpm_server.erl

来自「OTP是开放电信平台的简称」· ERL 代码 · 共 2,152 行 · 第 1/5 页

ERL
2,152
字号
	    ReqId  = send_get_request(Oids, Vsn, MsgData, Addr, Port, 				      ExtraInfo, State),	    ?vdebug("handle_sync_get -> ReqId: ~p", [ReqId]),	    Msg    = {sync_timeout, ReqId, From},	    Ref    = erlang:send_after(Timeout, self(), Msg),	    MonRef = erlang:monitor(process, Pid),	    ?vtrace("handle_sync_get -> MonRef: ~p", [MonRef]),	    Req    = #request{id      = ReqId,			      user_id = UserId, 			      addr    = Addr,			      port    = Port,			      type    = get, 			      data    = MsgData, 			      ref     = Ref, 			      mon     = MonRef, 			      from    = From},	    ets:insert(snmpm_request_table, Req),	    ok;	Error ->	    ?vinfo("failed retrieving agent data for get:"		   "~n   Addr:  ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [Addr, Port, Error]),	    Error    end.    handle_sync_get_next(Pid, UserId, Addr, Port, CtxName, Oids, Timeout, 		     ExtraInfo, From, State) ->    ?vtrace("handle_sync_get_next -> entry with"	    "~n   Pid:     ~p"	    "~n   UserId:  ~p"	    "~n   Addr:    ~p"	    "~n   Port:    ~p"	    "~n   CtxName: ~p"	    "~n   Oids:    ~p"	    "~n   Timeout: ~p"	    "~n   From:    ~p", 	    [Pid, UserId, Addr, Port, CtxName, Oids, Timeout, From]),    case agent_data(Addr, Port, CtxName) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_sync_get_next -> send a ~p message", [Vsn]),	    ReqId  = send_get_next_request(Oids, Vsn, MsgData, 					   Addr, Port, ExtraInfo, State),	    ?vdebug("handle_sync_get_next -> ReqId: ~p", [ReqId]),	    Msg    = {sync_timeout, ReqId, From},	    Ref    = erlang:send_after(Timeout, self(), Msg),	    MonRef = erlang:monitor(process, Pid),	    ?vtrace("handle_sync_get_next -> MonRef: ~p", [MonRef]),	    Req    = #request{id      = ReqId,			      user_id = UserId, 			      addr    = Addr,			      port    = Port,			      type    = get_next, 			      data    = MsgData, 			      ref     = Ref, 			      mon     = MonRef, 			      from    = From},	    ets:insert(snmpm_request_table, Req),	    ok;	Error ->	    ?vinfo("failed retrieving agent data for get-next:"		   "~n   Addr:  ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [Addr, Port, Error]),	    Error    end.handle_sync_get_bulk(Pid, UserId, Addr, Port, CtxName, 		     NonRep, MaxRep, Oids, Timeout, 		     ExtraInfo, From, State) ->    ?vtrace("handle_sync_get_bulk -> entry with"	    "~n   Pid:     ~p"	    "~n   UserId:  ~p"	    "~n   Addr:    ~p"	    "~n   Port:    ~p"	    "~n   CtxName: ~p"	    "~n   NonRep:  ~p"	    "~n   MaxRep:  ~p"	    "~n   Oids:    ~p"	    "~n   Timeout: ~p"	    "~n   From:    ~p", 	    [Pid, UserId, Addr, Port, CtxName, NonRep, MaxRep, Oids, 	     Timeout, From]),    case agent_data(Addr, Port, CtxName) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_sync_get_bulk -> send a ~p message", [Vsn]),	    ReqId  = send_get_bulk_request(Oids, Vsn, MsgData, Addr, Port, 					   NonRep, MaxRep, ExtraInfo, State),	    ?vdebug("handle_sync_get_bulk -> ReqId: ~p", [ReqId]),	    Msg    = {sync_timeout, ReqId, From},	    Ref    = erlang:send_after(Timeout, self(), Msg),	    MonRef = erlang:monitor(process, Pid),	    ?vtrace("handle_sync_get_bulk -> MonRef: ~p", [MonRef]),	    Req    = #request{id      = ReqId,			      user_id = UserId, 			      addr    = Addr,			      port    = Port,			      type    = get_bulk, 			      data    = MsgData, 			      ref     = Ref, 			      mon     = MonRef, 			      from    = From},	    ets:insert(snmpm_request_table, Req),	    ok;	Error ->	    ?vinfo("failed retrieving agent data for get-bulk:"		   "~n   Addr:  ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [Addr, Port, Error]),	    Error    end.handle_sync_set(Pid, UserId, Addr, Port, CtxName, VarsAndVals, Timeout, 		ExtraInfo, From, State) ->    ?vtrace("handle_sync_set -> entry with"	    "~n   Pid:         ~p"	    "~n   UserId:      ~p"	    "~n   Addr:        ~p"	    "~n   Port:        ~p"	    "~n   CtxName:     ~p"	    "~n   VarsAndVals: ~p"	    "~n   Timeout:     ~p"	    "~n   From:        ~p", 	    [Pid, UserId, Addr, Port, CtxName, VarsAndVals, Timeout, From]),    case agent_data(Addr, Port, CtxName) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_sync_set -> send a ~p message", [Vsn]),	    ReqId  = send_set_request(VarsAndVals, Vsn, MsgData, 				      Addr, Port, ExtraInfo, State),	    ?vdebug("handle_sync_set -> ReqId: ~p", [ReqId]),	    Msg    = {sync_timeout, ReqId, From},	    Ref    = erlang:send_after(Timeout, self(), Msg),            MonRef = erlang:monitor(process, Pid),	    ?vtrace("handle_sync_set -> MonRef: ~p", [MonRef]),	    Req    = #request{id      = ReqId,			      user_id = UserId, 			      addr    = Addr,			      port    = Port,			      type    = set, 			      data    = MsgData, 			      ref     = Ref, 			      mon     = MonRef, 			      from    = From},	    ets:insert(snmpm_request_table, Req),	    ok;	Error ->	    ?vinfo("failed retrieving agent data for set:"		   "~n   Addr:  ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [Addr, Port, Error]),	    Error    end. handle_async_get(Pid, UserId, Addr, Port, CtxName, Oids, Expire, ExtraInfo, 		 State) ->    ?vtrace("handle_async_get -> entry with"	    "~n   Pid:     ~p"	    "~n   UserId:  ~p"	    "~n   Addr:    ~p"	    "~n   Port:    ~p"	    "~n   CtxName: ~p"	    "~n   Oids:    ~p"	    "~n   Expire:  ~p",	    [Pid, UserId, Addr, Port, CtxName, Oids, Expire]),    case agent_data(Addr, Port, CtxName) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_async_get -> send a ~p message", [Vsn]),	    ReqId  = send_get_request(Oids, Vsn, MsgData, Addr, Port, 				      ExtraInfo, State),	    ?vdebug("handle_async_get -> ReqId: ~p", [ReqId]),	    Req    = #request{id      = ReqId,			      user_id = UserId, 			      addr    = Addr,			      port    = Port,			      type    = get, 			      data    = MsgData, 			      expire  = t() + Expire},	    ets:insert(snmpm_request_table, Req),	    gct_activate(State#state.gct),	    {ok, ReqId};	Error ->	    ?vinfo("failed retrieving agent data for get:"		   "~n   Addr:  ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [Addr, Port, Error]),	    Error    end.handle_async_get_next(Pid, UserId, Addr, Port, CtxName, Oids, Expire, 		      ExtraInfo, State) ->    ?vtrace("handle_async_get_next -> entry with"	    "~n   Pid:     ~p"	    "~n   UserId:  ~p"	    "~n   Addr:    ~p"	    "~n   Port:    ~p"	    "~n   CtxName: ~p"	    "~n   Oids:    ~p"	    "~n   Expire:  ~p",	    [Pid, UserId, Addr, Port, CtxName, Oids, Expire]),    case agent_data(Addr, Port, CtxName) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_async_get_next -> send a ~p message", [Vsn]),	    ReqId  = send_get_next_request(Oids, Vsn, MsgData, 					   Addr, Port, ExtraInfo, State),	    ?vdebug("handle_async_get_next -> ReqId: ~p", [ReqId]),	    Req    = #request{id      = ReqId,			      user_id = UserId, 			      addr    = Addr,			      port    = Port,			      type    = get_next, 			      data    = MsgData, 			      expire  = t() + Expire},	    ets:insert(snmpm_request_table, Req),	    gct_activate(State#state.gct),	    {ok, ReqId};	Error ->	    ?vinfo("failed retrieving agent data for get-next:"		   "~n   Addr:  ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [Addr, Port, Error]),	    Error    end.handle_async_get_bulk(Pid, UserId, Addr, Port, CtxName, 		      NonRep, MaxRep, Oids, Expire, 		      ExtraInfo, State) ->    ?vtrace("handle_async_get_bulk -> entry with"	    "~n   Pid:     ~p"	    "~n   UserId:  ~p"	    "~n   Addr:    ~p"	    "~n   Port:    ~p"	    "~n   CtxName: ~p"	    "~n   NonRep:  ~p"	    "~n   MaxRep:  ~p"	    "~n   Oids:    ~p"	    "~n   Expire:  ~p", 	    [Pid, UserId, Addr, Port, CtxName, NonRep, MaxRep, Oids, Expire]),    case agent_data(Addr, Port, CtxName) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_async_get_bulk -> send a ~p message", [Vsn]),	    ReqId  = send_get_bulk_request(Oids, Vsn, MsgData, Addr, Port, 					   NonRep, MaxRep, ExtraInfo, State),	    ?vdebug("handle_async_get_bulk -> ReqId: ~p", [ReqId]),	    Req    = #request{id      = ReqId,			      user_id = UserId, 			      addr    = Addr,			      port    = Port,			      type    = get_bulk, 			      data    = MsgData, 			      expire  = t() + Expire},	    ets:insert(snmpm_request_table, Req),	    gct_activate(State#state.gct),	    {ok, ReqId};	Error ->	    ?vinfo("failed retrieving agent data for get-bulk:"		   "~n   Addr:  ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [Addr, Port, Error]),	    Error    end.handle_async_set(Pid, UserId, Addr, Port, CtxName, VarsAndVals, Expire, 		 ExtraInfo, State) ->    ?vtrace("handle_async_set -> entry with"	    "~n   Pid:         ~p"	    "~n   UserId:      ~p"	    "~n   Addr:        ~p"	    "~n   Port:        ~p"	    "~n   CtxName:     ~p"	    "~n   VarsAndVals: ~p"	    "~n   Expire:      ~p",	    [Pid, UserId, Addr, Port, CtxName, VarsAndVals, Expire]),    case agent_data(Addr, Port, CtxName) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_async_set -> send a ~p message", [Vsn]),	    ReqId  = send_set_request(VarsAndVals, Vsn, MsgData, 				      Addr, Port, ExtraInfo, State),	    ?vdebug("handle_async_set -> ReqId: ~p", [ReqId]),	    Req    = #request{id      = ReqId,			      user_id = UserId, 			      addr    = Addr,			      port    = Port,			      type    = set, 			      data    = MsgData, 			      expire  = t() + Expire},	    ets:insert(snmpm_request_table, Req),	    gct_activate(State#state.gct),	    {ok, ReqId};	Error ->	    ?vinfo("failed retrieving agent data for set:"		   "~n   Addr:  ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [Addr, Port, Error]),	    Error    end.handle_cancel_async_request(UserId, ReqId, _State) ->    ?vtrace("handle_cancel_async_request -> entry with"	    "~n   UserId: ~p"	    "~n   ReqId:  ~p", [UserId, ReqId]),    case ets:lookup(snmpm_request_table, ReqId) of	[#request{user_id = UserId,		  ref     = Ref}] ->	    ?vdebug("handle_cancel_async_request -> demonitor and cancel timer"		    "~n   Ref: ~p", [Ref]),	    cancel_timer(Ref),	    ets:delete(snmpm_request_table, ReqId),	    ok;		[#request{user_id = OtherUserId}] ->	    ?vinfo("handle_cancel_async_request -> Not request owner"		    "~n   OtherUserId: ~p", [OtherUserId]),	    {error, {not_owner, OtherUserId}};		[] ->	    ?vlog("handle_cancel_async_request -> not found", []),	    {error, not_found}    end.    handle_discovery(Pid, UserId, BAddr, Port, Config, Expire, ExtraInfo, State) ->    ?vtrace("handle_discovery -> entry with"	    "~n   Pid:         ~p"	    "~n   UserId:      ~p"	    "~n   BAddr:       ~p"	    "~n   Port:        ~p"	    "~n   Config:      ~p"	    "~n   Expire:      ~p",	    [Pid, UserId, BAddr, Port, Config, Expire]),    case agent_data(default, default, "", Config) of	{ok, Vsn, MsgData} ->	    ?vtrace("handle_discovery -> send a ~p disco message", [Vsn]),	    ReqId  = send_discovery(Vsn, MsgData, BAddr, Port, ExtraInfo, 				    State),	    ?vdebug("handle_discovery -> ReqId: ~p", [ReqId]),	    MonRef = erlang:monitor(process, Pid),	    ?vtrace("handle_discovery -> MonRef: ~p", [MonRef]),	    Req    = #request{id        = ReqId,			      user_id   = UserId, 			      addr      = BAddr, 			      port      = Port,			      type      = get, 			      data      = MsgData, 			      mon       = MonRef,			      discovery = true, 			      expire    = t() + Expire},	    ets:insert(snmpm_request_table, Req),	    gct_activate(State#state.gct),	    {ok, ReqId};	Error ->	    ?vinfo("failed retrieving agent data for discovery (get):"		   "~n   BAddr: ~p"		   "~n   Port:  ~p"		   "~n   Error: ~p", [BAddr, Port, Error]),	    Error    end.handle_sync_timeout(ReqId, From, State) ->    ?vtrace("handle_sync_timeout -> entry with"	    "~n   ReqId: ~p"	    "~n   From:  ~p", [ReqId, From]),    case ets:lookup(snmpm_request_table, ReqId) of	[#request{mon = MonRef, from = From} = Req0] ->	    ?vdebug("handle_sync_timeout -> "		    "deliver reply (timeout) and demonitor: "		    "~n   Monref: ~p"		    "~n   From:   ~p", [MonRef, From]),	    gen_server:reply(From, {error, {timeout, ReqId}}),	    maybe_demonitor(MonRef),	    	    %% 	    %% Instead of deleting the request record now,	    %% we leave it to the gc. But for that to work 	    %% we must update the expire value (which for	    %% sync requests is infinity).	    %% 	    Req = Req0#request{ref    = undefined, 			       mon    = undefined, 			       from   = undefined, 			       expire = t()},	    ets:insert(snmpm_request_table, Req),	    gct_activate(State#state.gct),	    ok;	_ ->	    ok    end.    handle_snmp_error(#pdu{request_id = ReqId} = Pdu, Reason, State) ->    ?vtrace("handle_snmp_error -> entry with"	    "~n   Reason: ~p"	    "~n   Pdu:    ~p", [Reason, Pdu]),    case ets:lookup(snmpm_request_table, ReqId) of	%% Failed async request	[#request{user_id   = UserId, 		  from      = undefined, 		  ref       = undefined, 		  mon       = MonRef,		  discovery = Disco}] ->	    ?vdebug("handle_snmp_error -> "		    "found corresponding request: "		    "~n   failed async request"		    "~n   UserId: ~p"		    "~n   ModRef: ~p"		    "~n   Disco:  ~p", [UserId, MonRef, Disco]),

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?