Permalink
Browse files

[project @ Split out rfc4627_jsonrpc_registry.erl]

  • Loading branch information...
1 parent f5b35e4 commit 01bbf59db091b701b0bea517d9e49588c808f21b @tonyg committed May 25, 2008
Showing with 95 additions and 97 deletions.
  1. +5 −42 doc/rfc4627_jsonrpc.html
  2. +4 −55 src/rfc4627_jsonrpc.erl
  3. +86 −0 src/rfc4627_jsonrpc_registry.erl
View
47 doc/rfc4627_jsonrpc.html
@@ -16,7 +16,6 @@
<p><b>Introduced in:</b> 1.2.0
</p>
-<p><b>Behaviours:</b> <a href="gen_server.html"><tt>gen_server</tt></a>.</p>
<p><b>Authors:</b> Tony Garnock-Jones (<a href="mailto:tonyg@kcbbs.gen.nz"><tt>tonyg@kcbbs.gen.nz</tt></a>), LShift Ltd. (<a href="mailto:query@lshift.net"><tt>query@lshift.net</tt></a>).</p>
<p><b>References</b><ul><li>the <a href="http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html">JSON-RPC specification</a> (draft)
</li></ul></p>
@@ -205,15 +204,10 @@ <h3 class="typedecl"><a name="type-service">service()</a></h3>
</p>
<h2><a name="index">Function Index</a></h2>
-<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#code_change-3">code_change/3</a></td><td>gen_server behaviour callback.</td></tr>
-<tr><td valign="top"><a href="#error_response-2">error_response/2</a></td><td>Constructs an error response as per the JSON-RPC specification.</td></tr>
+<table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#error_response-2">error_response/2</a></td><td>Constructs an error response as per the JSON-RPC specification.</td></tr>
<tr><td valign="top"><a href="#error_response-3">error_response/3</a></td><td>Constructs an error response as per the JSON-RPC specification.</td></tr>
<tr><td valign="top"><a href="#expand_jsonrpc_reply-2">expand_jsonrpc_reply/2</a></td><td></td></tr>
<tr><td valign="top"><a href="#gen_object_name-0">gen_object_name/0</a></td><td>Generates a unique name that can be used for otherwise unnamed JSON-RPC services.</td></tr>
-<tr><td valign="top"><a href="#handle_call-3">handle_call/3</a></td><td>gen_server behaviour callback.</td></tr>
-<tr><td valign="top"><a href="#handle_cast-2">handle_cast/2</a></td><td>gen_server behaviour callback.</td></tr>
-<tr><td valign="top"><a href="#handle_info-2">handle_info/2</a></td><td>gen_server behaviour callback.</td></tr>
-<tr><td valign="top"><a href="#init-1">init/1</a></td><td>gen_server behaviour callback.</td></tr>
<tr><td valign="top"><a href="#invoke_service_method-8">invoke_service_method/8</a></td><td>Calls a method defined on a JSON-RPC service.</td></tr>
<tr><td valign="top"><a href="#jsonrpc_post-3">jsonrpc_post/3</a></td><td>Calls <a href="#jsonrpc_post-4"><code>jsonrpc_post/4</code></a> with a <code>Timeout</code> of <code>default</code>.</td></tr>
<tr><td valign="top"><a href="#jsonrpc_post-4">jsonrpc_post/4</a></td><td>Performs a POST-style invocation of a JSON-RPC service method.</td></tr>
@@ -223,19 +217,13 @@ <h3 class="typedecl"><a name="type-service">service()</a></h3>
<tr><td valign="top"><a href="#service-4">service/4</a></td><td>Constructs a service description record.</td></tr>
<tr><td valign="top"><a href="#service-5">service/5</a></td><td>As for <a href="#service-4"><code>service/4</code></a>, but supplying a handler for use with
an experimental "stateless" service implementation.</td></tr>
-<tr><td valign="top"><a href="#start-0">start/0</a></td><td>Starts the registry service.</td></tr>
-<tr><td valign="top"><a href="#start_link-0">start_link/0</a></td><td>Starts the registry service, linking it to the calling process.</td></tr>
+<tr><td valign="top"><a href="#start-0">start/0</a></td><td>Starts the registry process.</td></tr>
+<tr><td valign="top"><a href="#start_link-0">start_link/0</a></td><td>Starts the registry process, linking it to the calling process.</td></tr>
<tr><td valign="top"><a href="#system_describe-2">system_describe/2</a></td><td>Builds a JSON-RPC service description JSON object.</td></tr>
-<tr><td valign="top"><a href="#terminate-2">terminate/2</a></td><td>gen_server behaviour callback.</td></tr>
</table>
<h2><a name="functions">Function Details</a></h2>
-<h3 class="function"><a name="code_change-3">code_change/3</a></h3>
-<div class="spec">
-<p><tt>code_change() -&gt; term()</tt></p>
-</div><p>gen_server behaviour callback.</p>
-
<h3 class="function"><a name="error_response-2">error_response/2</a></h3>
<div class="spec">
<p><tt>error_response(CodeOrMessage::integer() | string() | binary(), ErrorValue::<a href="#type-json">json()</a>) -&gt; {error, <a href="#type-jsonobj">jsonobj()</a>}</tt></p>
@@ -269,26 +257,6 @@ <h3 class="function"><a name="gen_object_name-0">gen_object_name/0</a></h3>
<p><tt>gen_object_name() -&gt; string()</tt></p>
</div><p>Generates a unique name that can be used for otherwise unnamed JSON-RPC services.</p>
-<h3 class="function"><a name="handle_call-3">handle_call/3</a></h3>
-<div class="spec">
-<p><tt>handle_call() -&gt; term()</tt></p>
-</div><p>gen_server behaviour callback.</p>
-
-<h3 class="function"><a name="handle_cast-2">handle_cast/2</a></h3>
-<div class="spec">
-<p><tt>handle_cast() -&gt; term()</tt></p>
-</div><p>gen_server behaviour callback.</p>
-
-<h3 class="function"><a name="handle_info-2">handle_info/2</a></h3>
-<div class="spec">
-<p><tt>handle_info() -&gt; term()</tt></p>
-</div><p>gen_server behaviour callback.</p>
-
-<h3 class="function"><a name="init-1">init/1</a></h3>
-<div class="spec">
-<p><tt>init() -&gt; term()</tt></p>
-</div><p>gen_server behaviour callback.</p>
-
<h3 class="function"><a name="invoke_service_method-8">invoke_service_method/8</a></h3>
<div class="spec">
<p><tt>invoke_service_method(ServiceRec, RequestId, PostOrGet, RequestInfo, EndpointAddress, Method, Args, Timeout) -&gt; <a href="#type-jsonrpc_response">jsonrpc_response()</a></tt>
@@ -406,12 +374,12 @@ <h3 class="function"><a name="service-5">service/5</a></h3>
<h3 class="function"><a name="start-0">start/0</a></h3>
<div class="spec">
<p><tt>start() -&gt; {ok, pid()} | {error, {already_started, pid()}}</tt></p>
-</div><p>Starts the registry service.</p>
+</div><p>Starts the registry process.</p>
<h3 class="function"><a name="start_link-0">start_link/0</a></h3>
<div class="spec">
<p><tt>start_link() -&gt; {ok, pid()} | {error, {already_started, pid()}}</tt></p>
-</div><p>Starts the registry service, linking it to the calling process.</p>
+</div><p>Starts the registry process, linking it to the calling process.</p>
<h3 class="function"><a name="system_describe-2">system_describe/2</a></h3>
<div class="spec">
@@ -426,11 +394,6 @@ <h3 class="function"><a name="system_describe-2">system_describe/2</a></h3>
in the resulting description. Otherwise, it is included
verbatim. The other fields in the description are constructed using
the information in the <code>Service</code> record.</p>
-
-<h3 class="function"><a name="terminate-2">terminate/2</a></h3>
-<div class="spec">
-<p><tt>terminate() -&gt; term()</tt></p>
-</div><p>gen_server behaviour callback.</p>
<hr>
<div class="navbar"><a name="#navbar_bottom"></a><table width="100%" border="0" cellspacing="0" cellpadding="2" summary="navigation bar"><tr><td><a href="overview-summary.html" target="overviewFrame">Overview</a></td><td><a href="http://www.erlang.org/"><img src="erlang.png" align="right" border="0" alt="erlang logo"></a></td></tr></table></div>
View
59 src/rfc4627_jsonrpc.erl
@@ -218,10 +218,7 @@
-include("rfc4627.hrl").
-include("rfc4627_jsonrpc.hrl").
--behaviour(gen_server).
-
-export([start/0, start_link/0]).
--export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2]).
-export([lookup_service/1, register_service/2]).
-export([gen_object_name/0, system_describe/2]).
@@ -231,14 +228,14 @@
-define(SERVICE, ?MODULE).
%% @spec () -> {ok, pid()} | {error, {already_started, pid()}}
-%% @doc Starts the registry service.
+%% @doc Starts the registry process.
start() ->
- gen_server:start({local, ?SERVICE}, ?MODULE, [], []).
+ gen_server:start({local, ?SERVICE}, rfc4627_jsonrpc_registry, [], []).
%% @spec () -> {ok, pid()} | {error, {already_started, pid()}}
-%% @doc Starts the registry service, linking it to the calling process.
+%% @doc Starts the registry process, linking it to the calling process.
start_link() ->
- gen_server:start_link({local, ?SERVICE}, ?MODULE, [], []).
+ gen_server:start_link({local, ?SERVICE}, rfc4627_jsonrpc_registry, [], []).
%% @spec (binary()) -> not_found | service()
%% @doc Calls the registry to look up a service by name.
@@ -555,51 +552,3 @@ system_describe_proc(P = #service_proc{params = Params}) ->
system_describe_proc_param(P = #service_proc_param{}) ->
remove_undefined(?RFC4627_FROM_RECORD(service_proc_param, P)).
-
-%---------------------------------------------------------------------------
-
-%% @doc gen_server behaviour callback.
-init(_Args) ->
- {ok, no_jsonrpc_state}.
-
-%% @doc gen_server behaviour callback.
-terminate(_Reason, _State) ->
- %% FIXME: should we notify services here?
- ok.
-
-%% @doc gen_server behaviour callback.
-code_change(_OldVsn, State, _Extra) ->
- State.
-
-%% @doc gen_server behaviour callback.
-handle_call({lookup_service, Service}, _From, State) ->
- case get({service, Service}) of
- undefined ->
- {reply, not_found, State};
- ServiceRec ->
- {reply, ServiceRec, State}
- end;
-
-handle_call({register_service, Pid, ServiceDescription}, _From, State) ->
- SD = ServiceDescription#service{handler = {pid, Pid}},
- erlang:monitor(process, Pid),
- put({service_pid, Pid}, SD#service.name),
- put({service, SD#service.name}, SD),
- {reply, ok, State}.
-
-%% @doc gen_server behaviour callback.
-handle_cast(Request, State) ->
- error_logger:error_msg("Unhandled cast in ~p: ~p", [?MODULE, Request]),
- {noreply, State}.
-
-%% @doc gen_server behaviour callback.
-handle_info({'DOWN', _MonitorRef, process, DownPid, _Reason}, State) ->
- case get({service_pid, DownPid}) of
- undefined ->
- %% How strange.
- {noreply, State};
- ServiceName ->
- erase({service_pid, DownPid}),
- erase({service, ServiceName}),
- {noreply, State}
- end.
View
86 src/rfc4627_jsonrpc_registry.erl
@@ -0,0 +1,86 @@
+%% JSON-RPC service registry
+%%---------------------------------------------------------------------------
+%% @author Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
+%% @author LShift Ltd. <query@lshift.net>
+%% @copyright 2007, 2008 Tony Garnock-Jones and LShift Ltd.
+%% @license
+%%
+%% Permission is hereby granted, free of charge, to any person
+%% obtaining a copy of this software and associated documentation
+%% files (the "Software"), to deal in the Software without
+%% restriction, including without limitation the rights to use, copy,
+%% modify, merge, publish, distribute, sublicense, and/or sell copies
+%% of the Software, and to permit persons to whom the Software is
+%% furnished to do so, subject to the following conditions:
+%%
+%% The above copyright notice and this permission notice shall be
+%% included in all copies or substantial portions of the Software.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+%% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+%% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+%% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+%% BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+%% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+%% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+%% SOFTWARE.
+%%---------------------------------------------------------------------------
+%% @since 1.2.0
+
+%% @private
+%% @doc JSON-RPC service registry implementation.
+%%
+%% Started and managed by functions defined in module {@link rfc4627_jsonrpc}.
+
+-module(rfc4627_jsonrpc_registry).
+-include("rfc4627_jsonrpc.hrl").
+
+-behaviour(gen_server).
+
+-export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2]).
+
+%% @doc gen_server behaviour callback.
+init(_Args) ->
+ {ok, no_jsonrpc_state}.
+
+%% @doc gen_server behaviour callback.
+terminate(_Reason, _State) ->
+ %% FIXME: should we notify services here?
+ ok.
+
+%% @doc gen_server behaviour callback.
+code_change(_OldVsn, State, _Extra) ->
+ State.
+
+%% @doc gen_server behaviour callback.
+handle_call({lookup_service, Service}, _From, State) ->
+ case get({service, Service}) of
+ undefined ->
+ {reply, not_found, State};
+ ServiceRec ->
+ {reply, ServiceRec, State}
+ end;
+
+handle_call({register_service, Pid, ServiceDescription}, _From, State) ->
+ SD = ServiceDescription#service{handler = {pid, Pid}},
+ erlang:monitor(process, Pid),
+ put({service_pid, Pid}, SD#service.name),
+ put({service, SD#service.name}, SD),
+ {reply, ok, State}.
+
+%% @doc gen_server behaviour callback.
+handle_cast(Request, State) ->
+ error_logger:error_msg("Unhandled cast in ~p: ~p", [?MODULE, Request]),
+ {noreply, State}.
+
+%% @doc gen_server behaviour callback.
+handle_info({'DOWN', _MonitorRef, process, DownPid, _Reason}, State) ->
+ case get({service_pid, DownPid}) of
+ undefined ->
+ %% How strange.
+ {noreply, State};
+ ServiceName ->
+ erase({service_pid, DownPid}),
+ erase({service, ServiceName}),
+ {noreply, State}
+ end.

0 comments on commit 01bbf59

Please sign in to comment.