1 -module (lwes_channel_manager
).
3 -behaviour (gen_server
).
5 -include_lib ("lwes.hrl").
6 -include_lib ("lwes_internal.hrl").
9 -export ([ start_link
/0,
17 %% gen_server callbacks
26 -define (TABLE
, lwes_channels
).
29 %%====================================================================
31 %%====================================================================
33 gen_server:start_link ( { local
, ?MODULE
}, ?MODULE
, [], []).
35 open_channel (Channel
) ->
36 lwes_channel_sup:open_channel (Channel
).
38 register_channel (Channel
, Pid
) ->
39 gen_server:call (?MODULE
, {reg
, Channel
, Pid
}).
41 unregister_channel (Channel
) ->
42 gen_server:call (?MODULE
, {unreg
, Channel
}).
44 find_channel (Channel
) ->
45 case ets:lookup (?TABLE
, Channel
) of
46 [] -> {error
, not_open
} ;
47 [{_Channel
, Pid
}] -> Pid
50 close_channel (Channel
) ->
51 gen_server:call (find_channel (Channel
), stop
).
53 %%====================================================================
54 %% gen_server callbacks
55 %%====================================================================
57 ets:new (?TABLE
, [ named_table
, { read_concurrency
, true
} ]),
60 handle_call ({reg
, Key
, Val
}, _From
, State
) ->
61 { reply
, ets:insert (?TABLE
, {Key
, Val
}), State
};
62 handle_call ({unreg
, Key
}, _From
, State
) ->
63 {reply
, ets:delete (?TABLE
, Key
), State
};
64 handle_call (Request
, From
, State
) ->
65 error_logger:warning_msg
66 ("lwes_channel_manager unrecognized call ~p from ~p~n",[Request
, From
]),
69 handle_cast (Request
, State
) ->
70 error_logger:warning_msg
71 ("lwes_channel_manager unrecognized cast ~p~n",[Request
]),
74 handle_info (Request
, State
) ->
75 error_logger:warning_msg
76 ("lwes_channel_manager unrecognized info ~p~n",[Request
]),
79 terminate (_Reason
, _State
) ->
83 code_change (_OldVsn
, State
, _Extra
) ->
86 %%====================================================================
88 %%====================================================================
90 %%====================================================================
92 %%====================================================================
94 -include_lib ("eunit/include/eunit.hrl").