test/test_jsonrpc_inets.erl
author Tony Garnock-Jones <tonyg@lshift.net>
Mon Mar 08 02:33:16 2010 +0000 (4 days ago)
changeset 102 a800c54e7ff3
parent 40test/test_jsonrpc.erl@912d93f8845d
permissions -rw-r--r--
Avoid echoing of the echo command
     1 %% An example JSON-RPC service
     2 %%---------------------------------------------------------------------------
     3 %% Copyright (c) 2007 Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
     4 %% Copyright (c) 2007 LShift Ltd. <query@lshift.net>
     5 %%
     6 %% Permission is hereby granted, free of charge, to any person
     7 %% obtaining a copy of this software and associated documentation
     8 %% files (the "Software"), to deal in the Software without
     9 %% restriction, including without limitation the rights to use, copy,
    10 %% modify, merge, publish, distribute, sublicense, and/or sell copies
    11 %% of the Software, and to permit persons to whom the Software is
    12 %% furnished to do so, subject to the following conditions:
    13 %%
    14 %% The above copyright notice and this permission notice shall be
    15 %% included in all copies or substantial portions of the Software.
    16 %%
    17 %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    18 %% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    19 %% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    20 %% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
    21 %% BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
    22 %% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    23 %% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    24 %% SOFTWARE.
    25 %%---------------------------------------------------------------------------
    26 
    27 -module(test_jsonrpc_inets).
    28 
    29 -include("rfc4627_jsonrpc.hrl").
    30 
    31 -behaviour(gen_server).
    32 
    33 -export([start/0, start_httpd/0]).
    34 -export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2]).
    35 
    36 start() ->
    37     {ok, Pid} = gen_server:start(?MODULE, [], []),
    38     rfc4627_jsonrpc:register_service
    39       (Pid,
    40        rfc4627_jsonrpc:service(<<"test">>,
    41 			       <<"urn:uuid:afe1b4b5-23b0-4964-a74a-9168535c96b2">>,
    42 			       <<"1.0">>,
    43 			       [#service_proc{name = <<"test_proc">>,
    44 					      idempotent = true,
    45 					      params = [#service_proc_param{name = <<"value">>,
    46 									    type = <<"str">>}]}])).
    47 
    48 start_httpd() ->
    49     rfc4627_jsonrpc:start(),
    50     httpd:start("test/server_root/conf/httpd.conf"),
    51     start().
    52 
    53 %---------------------------------------------------------------------------
    54 
    55 init(_Args) ->
    56     {ok, no_state}.
    57 
    58 terminate(_Reason, _State) ->
    59     ok.
    60 
    61 code_change(_OldVsn, State, _Extra) ->
    62     State.
    63 
    64 handle_call({jsonrpc, <<"test_proc">>, _ModData, [Value]}, _From, State) ->
    65     {reply, {result, <<"ErlangServer: ", Value/binary>>}, State}.
    66 
    67 handle_cast(Request, State) ->
    68     error_logger:error_msg("Unhandled cast in test_jsonrpc: ~p", [Request]),
    69     {noreply, State}.
    70 
    71 handle_info(Info, State) ->
    72     error_logger:error_msg("Unhandled info in test_jsonrpc: ~p", [Info]),
    73     {noreply, State}.