anchor.diff:  -include("precursors_pb.hrl").
   
   % API
  --export([start_link/3, behavior_info/1, call/2, call/3, cast/2,
  -	client_event/3, client_request/3, client_response/3, reply/2]).
  +-export([start_link/1, client_event/3, client_request/3, client_response/3,
  +	add_handler/3, add_sup_handler/3, notify/2, sync_notify/2, call/2,
  +	call/3, delete_handler/3, swap_handler/3, swap_sup_handler/3,
  +	which_handlers/1
  +]).
   % gen_server
   -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
   
   -record(state, {
   	connection :: pid(),
  -	callback :: atom(),
  -	substate :: any()
  +	gen_event_mgr :: pid()
   }).
   
   %% ==================================================================
  
positive.code: %% ------------------------------------------------------------------
  
  %% @private
  init({Callback, Connection, Args, _Options}) ->
  	case Callback:init(Args) of
  		ignore ->
  			ignore;
  		{stop, Reason} ->
  			{stop, Reason};
  		{ok, Substate} ->
  			State = #state{
  				callback = Callback,
  				substate = Substate,
  				connection = Connection
  			},
  			{ok, State}
  	end.
  
  %% ------------------------------------------------------------------
  %% handle_call
  %% ------------------------------------------------------------------
  
  %% @private
  handle_call(Req, From, State) ->
  	#state{callback = Mod, substate = SubState} = State,
  	ModResponse = Mod:handle_call(Req, From, SubState),
  	handle_callback_return(ModResponse, State).
  
  %% ------------------------------------------------------------------
  %% handle_cast
  %% ------------------------------------------------------------------
  
  %% @private
  handle_cast({{'$pre_client', client_event}, {Channel, Event}}, State) ->
  	#state{callback = Mod, substate = SubState} = State,
  	ModResponse = Mod:handle_client_event(Channel, Event, SubState),
  	handle_callback_return(ModResponse, State);
  
  handle_cast({{'$pre_client', client_request}, {Channel, Request}}, State) ->
  	#state{callback = Mod, substate = Substate} = State,
  	ModResponse = Mod:handle_client_request(Channel, Request, Substate),
  	handle_callback_return(ModResponse, State);
  
  handle_cast({{'$pre_client', client_response}, {Channel, Response}}, State) ->
  	#state{callback = Mod, substate = Substate} = State,
  	ModResponse = Mod:handle_client_response(Channel, Response, Substate),
  	handle_callback_return(ModResponse, State);
  
  handle_cast(Msg, State) ->
  	#state{callback = Mod, substate = Substate} = State,
  	ModResponse = Mod:handle_cast(Msg, Substate),
  	handle_callback_return(ModResponse, State).
  
  %% ------------------------------------------------------------------
  %% handle_call
  %% ------------------------------------------------------------------
  
  %% @private
  handle_info(Msg, State) ->
  	#state{callback = Mod, substate= Substate} = State,
  	ModResponse = Mod:handle_info(Msg, Substate),
  	handle_callback_return(ModResponse, State).
  
  %% ------------------------------------------------------------------
  %% handle_call
  %% ------------------------------------------------------------------
  
  %% @private
  terminate(Reason, State) ->
  	#state{callback = Mod, substate = Sub} = State,
  	Mod:terminate(Reason, Sub).
  
  %% ------------------------------------------------------------------
  %% handle_call
  %% ------------------------------------------------------------------
  
  %% @private
  code_change(OldVsn, State, Extra) ->
  	#state{callback = Mod, substate = Sub} = State,
  	{ok, NewSub} = Mod:code_change(OldVsn, Sub, Extra),
  	State0 = State#state{substate = NewSub},
  	{ok, State0}.
  
  %% ==================================================================
  %% Internal Function Definitions
  %% ==================================================================
  
  %% ------------------------------------------------------------------
  %% handle_callback_return
  %% ------------------------------------------------------------------
  
  handle_callback_return({mutate, Reply, NewMod, NewArgs}, State) ->
  	case handle_mutate(NewMod, NewArgs) of
  		{error, Err} -> {stop, Err, Reply, State};
  		{ok, NewSub} ->
  			{reply, Reply, State#state{callback = NewMod, substate = NewSub}}
  	end;
  
  handle_callback_return({mutate, NewMod, NewArgs}, State) ->
  	case handle_mutate(NewMod, NewArgs) of
  		{error, Err} -> {stop, Err, State};
  		{ok, NewSub} ->
  			{noreply, State#state{callback = NewMod, substate = NewSub}}
  	end;
  
  handle_callback_return({send, Reply, Sends, NewSub}, State) ->
  	{noreply, State0} = handle_callback_return({send, Sends, NewSub}, State),
  	{reply, Reply, State0};
  
  handle_callback_return({send, [], NewSub}, State) ->
  	State0 = State#state{substate = NewSub},
  	{noreply, State0};
  
  handle_callback_return({send, [{Sock, Bin}|Tail], NewSub}, State) ->
  	#state{connection = Conn} = State,
  	pre_client_connection:send(Conn, Sock, Bin),
  	handle_callback_return({send, Tail, NewSub}, State).
  
  %% ------------------------------------------------------------------
  
  handle_mutate(NewMod, NewArgs) ->
  	case NewMod:init(NewArgs) of
  		{stop, Reason} -> {error, Reason};
  		ignore -> {error, bad_mutate};
  		{ok, _} = Ok -> Ok
  	end.
  
negative.code: client_response(Pid, Channel, Response) when is_record(Response, response) ->
  	gen_server:cast(Pid, {{'$pre_client', client_response}, {Channel, Response}}).
  
  add_handler(Client, Handler, Args) ->
  	gen_server:call(Client, {{'$pre_client', add_handler}, {Handler, Args}}).
  
  add_sup_handler(Client, Handler, Args) ->
  	gen_server:call(Client, {{'$pre_client', add_sup_handler}, {Handler, Args}}).
  
  async_add_handler(Client, Handler, Args) ->
  	gen_server:cast(Client, {{'$pre_client', add_handler}, {Handler, Args}}).
  
  async_add_sup_handler(Client, Handler, Args) ->
  	gen_server:cast(Client, {{'$pre_client', add_sup_handler}, {Handler, Args}}).
  
  notify(Client, Event) ->
  	gen_server:cast(Client, {{'$pre_client', notify}, Event}).
  
  sync_notify(Client, Event) ->
  	gen_server:call(Client, {{'$pre_client', sync_notify}, Event}).
  
  call(Client, Request) ->
  	call(Client, Request, infinity).
  
  call(Client, Request, Timeout) ->
  	gen_server:call(Client, {{'$pre_client', call}, {Request, Timeout}}).
  
  delete_handler(Client, Handler, Args) ->
  	gen_server:call(Client, {{'$pre_client', delete_handler}, {Handler, Args}}).
  
  swap_handler(Client, {Handler1, Args1}, {Handler2, Args2}) ->
  	gen_server:call(Client, {{'$pre_client', swap_handler}, {{Handler1, Args1}, {Handler2, Args2}}}).
  
  swap_sup_handler(Client, {Handler1, Args1}, {Handler2, Args2}) ->
  	gen_server:call(Client, {{'$pre_client', swap_sup_handler}, {{Handler1, Args1}, {Handler2, Args2}}}).
  
  which_handlers(Client) ->
  	List = gen_server:call(Client, {{'$pre_client', which_handlers}, undefined}),
  	[X || {pre_gen_event, X} <- List].
  
  %% ==================================================================
  %% gen_server Function Definitions
  