<erl module=sample_mod>
-compile(export_all).

out(A) ->
    Peer = case yaws_api:get_sslsocket(A#arg.clisock) of
               {ok, SslSocket} ->
                   ssl:peername(SslSocket);
               _ ->
                   inet:peername(A#arg.clisock)
           end,

    {ok,{IP,_}} = Peer,
    A2=A#arg{state = [{ip, IP}]},
    yaws_rpc:handler_session(A2, {?MODULE, counter}).



counter([{ip, IP}] = _State, {call, errortest, Value} = _Request, Session) ->
    io:format("Request = ~p~n", [_Request]),
    { false, { error, "Expected failure" } };

counter([{ip, IP}] = _State, {call, test1, Value} = _Request, Session) ->
    io:format("Request = ~p~n", [_Request]),
    IPStr = io_lib:format("Client ip is  ~p~n" , [ IP ]),
    OldSession = io_lib:format("Request is: ~p~nOld session value "
                               "is ~p~n", [ _Request, Session ]),

    case Session of
        undefined -> % create new session
            NewSession = 0;
        10 ->        % reset session after reaching 10
            NewSession = undefined;
        N ->
            NewSession = N + 1
    end,

    NewVal = io_lib:format("New session value is ~p ~n", [ NewSession ]),
    Str = lists:flatten([IPStr,OldSession,NewVal]),
    {true, 0, NewSession, {response,  Str }}.


</erl>
