Anonymous avatar Anonymous committed 08e4688

proxy handle_[call|cast|info] to gen_bunnies

Comments (0)

Files changed (3)

 Andy Gross
 David Reid
 Bryan Fink (HTML reports for coverage)
-
+Kevin Smith (gen_server proxying code)

src/example_gb.erl

 -export([start_link/1,
          init/1,
          handle_message/2,
+         handle_call/3,
+         handle_cast/2,
+         handle_info/2,
          terminate/2]).
 
 -include_lib("gen_bunny.hrl").
 
 start_link(Type) ->
     setup(Type),
-    gen_bunny:start_link(?MODULE, {direct, "guest", "guest"}, <<"bunny.test">>, []).
+    gen_bunny:start_link(?MODULE, {direct, "guest", "guest2"}, <<"bunny.test">>, []).
 
 init([]) -> 
     {ok, #state{}}.
     NewMessages = [Message|Messages],
     {noreply, State#state{messages=NewMessages}}.
 
+handle_call(_Request, _From, State) -> {reply, ok, State}.
+
+handle_cast(_Msg, State) -> {noreply, State}.
+
+handle_info(_Info, State) -> {noreply, State}.
+
 terminate(_Reason, _State) -> ok.
 
 

src/gen_bunny.erl

 behaviour_info(callbacks) ->
     [{init, 1},
      {handle_message, 2},
+     {handle_call, 3},
+     {handle_cast, 2},
+     {handle_info, 2},
      {terminate, 2}];
 behaviour_info(_) -> 
     undefined.
 handle_call(get_channel, _From, State=#state{channel=Channel}) ->
     {reply, Channel, State};
 handle_call(get_consumer_tag, _From, State=#state{consumer_tag=CTag}) ->
-    {reply, CTag, State}.
+    {reply, CTag, State};
+handle_call(Request, From, State=#state{mod=Module, modstate=ModState}) ->
+    case Module:handle_call(Request, From, ModState) of
+        {reply, Reply, NewModState} ->
+            {reply, Reply, State#state{modstate=NewModState}};
+        {reply, Reply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
+            {reply, Reply, State#state{modstate=NewModState}, A};
+        {noreply, NewModState} ->
+            {noreply, State#state{modstate=NewModState}};
+        {noreply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
+            {noreply, State#state{modstate=NewModState}, A};
+        {stop, Reason, NewModState} ->
+            {stop, Reason, State#state{modstate=NewModState}};
+        {stop, Reason, Reply, NewModState} ->
+            {stop, Reason, Reply, State#state{modstate=NewModState}}
+  end.
 
 handle_cast(stop, State=#state{channel=Channel, consumer_tag=CTag, connection=Connection}) ->
     ok = lib_amqp:unsubscribe(Channel, CTag),
     ok = lib_amqp:teardown(Connection, Channel),
-    {stop, normal, State}.
+    {stop, normal, State};
+handle_cast(Msg, State=#state{mod=Module, modstate=ModState}) ->
+    case Module:handle_cast(Msg, ModState) of
+        {noreply, NewModState} ->
+            {noreply, State#state{modstate=NewModState}};
+        {noreply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
+            {noreply, State#state{modstate=NewModState}, A};
+        {stop, Reason, NewModState} ->
+            {stop, Reason, State#state{modstate=NewModState}}
+    end.
 
 handle_info({#'basic.deliver'{},
             {content, _ClassId, _Props, _PropertiesBin, [Payload]}},
             {stop, Reason, State#state{modstate=NewModState}}
     end;
 handle_info(#'basic.consume_ok'{consumer_tag=CTag}, State=#state{}) ->
-    {noreply, State#state{consumer_tag=CTag}}.
+    {noreply, State#state{consumer_tag=CTag}};
+handle_info(Info, State=#state{mod=Module, modstate=ModState}) ->
+    case Module:handle_info(Info, ModState) of
+        {noreply, NewModState} ->
+            {noreply, State#state{modstate=NewModState}};
+        {noreply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
+            {noreply, State#state{modstate=NewModState}, A};
+        {stop, Reason, NewModState} ->
+            {stop, Reason, State#state{modstate=NewModState}}
+    end.
     
 
 terminate(Reason, #state{mod=Mod, modstate=ModState}) ->
     {ok, ChannelPid, ConnectionPid};
 connect_and_subscribe({network, Host, Port, Username, Password, VHost}, 
                       QueueName) ->
-    
     ConnectionPid = amqp_connection:start_network(Username, Password, Host,
                                                   Port, VHost),
     ChannelPid = amqp_connection:open_channel(ConnectionPid),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.