Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 83 lines (69 sloc) 3.156 kb
d0a7425 @tonyg [project @ Copyright and license notices.]
authored
1 %% An example JSON-RPC service
2 %%---------------------------------------------------------------------------
c531e5a @tonyg Update copyrights
authored
3 %% Copyright (c) 2007-2010 Tony Garnock-Jones <tonygarnockjones@gmail.com>
4 %% Copyright (c) 2007-2010 LShift Ltd. <query@lshift.net>
d0a7425 @tonyg [project @ Copyright and license notices.]
authored
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
21d93e0 @tonyg [project @ Split JSONRPC code into transport-neutral, http, and inets…
authored
27 -module(test_jsonrpc_inets).
3545257 @tonyg [project @ Start factoring out JSON-RPC support.]
authored
28
21d93e0 @tonyg [project @ Split JSONRPC code into transport-neutral, http, and inets…
authored
29 -include("rfc4627_jsonrpc.hrl").
3545257 @tonyg [project @ Start factoring out JSON-RPC support.]
authored
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, [], []),
21d93e0 @tonyg [project @ Split JSONRPC code into transport-neutral, http, and inets…
authored
38 rfc4627_jsonrpc:register_service
3545257 @tonyg [project @ Start factoring out JSON-RPC support.]
authored
39 (Pid,
21d93e0 @tonyg [project @ Split JSONRPC code into transport-neutral, http, and inets…
authored
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">>,
51f5aa3 @tonyg Add "Crash" button to example program.
authored
46 type = <<"str">>}]},
47 #service_proc{name = <<"crash">>,
48 idempotent = false,
49 params = []}])).
3545257 @tonyg [project @ Start factoring out JSON-RPC support.]
authored
50
51 start_httpd() ->
fd46793 @tonyg Update test_jsonrpc_inets to run with Erlang/OTP R14B03.
authored
52 ok = case rfc4627_jsonrpc:start() of
53 {ok, _JsonrpcPid} -> ok;
54 {error, {already_started, _JsonrpcPid}} -> ok
55 end,
56 ok = inets:start(),
57 {ok, _HttpdPid} = inets:start(httpd, [{file, "test/server_root/conf/httpd.conf"}]),
3545257 @tonyg [project @ Start factoring out JSON-RPC support.]
authored
58 start().
59
60 %---------------------------------------------------------------------------
61
62 init(_Args) ->
63 {ok, no_state}.
64
65 terminate(_Reason, _State) ->
66 ok.
67
68 code_change(_OldVsn, State, _Extra) ->
416a400 @tonyg Oops. code_change should return {ok, State}.
authored
69 {ok, State}.
3545257 @tonyg [project @ Start factoring out JSON-RPC support.]
authored
70
eccd999 @tonyg [project @ Switch to being mod_jsonrpc instead of a separate ESI serv…
authored
71 handle_call({jsonrpc, <<"test_proc">>, _ModData, [Value]}, _From, State) ->
51f5aa3 @tonyg Add "Crash" button to example program.
authored
72 {reply, {result, <<"ErlangServer: ", Value/binary>>}, State};
73 handle_call({jsonrpc, <<"crash">>, _ModData, []}, _From, _State) ->
74 exit(deliberate_crash).
3545257 @tonyg [project @ Start factoring out JSON-RPC support.]
authored
75
76 handle_cast(Request, State) ->
77 error_logger:error_msg("Unhandled cast in test_jsonrpc: ~p", [Request]),
78 {noreply, State}.
79
80 handle_info(Info, State) ->
81 error_logger:error_msg("Unhandled info in test_jsonrpc: ~p", [Info]),
82 {noreply, State}.
Something went wrong with that request. Please try again.