aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEli Zaretskii2019-04-13 10:42:14 +0300
committerEli Zaretskii2019-04-13 10:42:14 +0300
commitd82d4fb9157dd86359e4489c8da100943754a4d0 (patch)
treeab348c7c463077a4e23b7292f267b128288441a9
parent2475687d2f0ca6a605d091bb7acb723d0dace27b (diff)
downloademacs-d82d4fb9157dd86359e4489c8da100943754a4d0.tar.gz
emacs-d82d4fb9157dd86359e4489c8da100943754a4d0.zip
Improve documentation of JSONRPC
* doc/lispref/text.texi (JSONRPC Overview) (Process-based JSONRPC connections) (JSONRPC JSON object format): Fix wording and markup. Add indexing.
-rw-r--r--doc/lispref/text.texi109
1 files changed, 66 insertions, 43 deletions
diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi
index e8de8177e30..500df1f8f0d 100644
--- a/doc/lispref/text.texi
+++ b/doc/lispref/text.texi
@@ -5197,11 +5197,12 @@ the value if contains a valid JSON object; otherwise it signals the
5197@node JSONRPC 5197@node JSONRPC
5198@section JSONRPC communication 5198@section JSONRPC communication
5199@cindex JSON remote procedure call protocol 5199@cindex JSON remote procedure call protocol
5200@cindex JSONRPC
5200 5201
5201The @code{jsonrpc} library implements the @acronym{JSONRPC} 5202The @code{jsonrpc} library implements the @acronym{JSONRPC}
5202specification, version 2.0, as it is described in 5203specification, version 2.0, as it is described in
5203@uref{http://www.jsonrpc.org/}. As the name suggests, JSONRPC is a 5204@uref{http://www.jsonrpc.org/}. As the name suggests, JSONRPC is a
5204generic @code{Remote Procedure Call} protocol designed around 5205generic @dfn{Remote Procedure Call} protocol designed around
5205@acronym{JSON} objects, which you can convert to and from Lisp objects 5206@acronym{JSON} objects, which you can convert to and from Lisp objects
5206(@pxref{Parsing JSON}). 5207(@pxref{Parsing JSON}).
5207 5208
@@ -5220,81 +5221,96 @@ transport agnostic in that the concepts can be used within the same
5220process, over sockets, over http, or in many various message passing 5221process, over sockets, over http, or in many various message passing
5221environments." 5222environments."
5222 5223
5224@findex jsonrpc-connection
5223To model this agnosticism, the @code{jsonrpc} library uses objects of 5225To model this agnosticism, the @code{jsonrpc} library uses objects of
5224a @code{jsonrpc-connection} class, which represent a connection the 5226a @code{jsonrpc-connection} class, which represent a connection to a
5225remote JSON endpoint (for details on Emacs's object system, 5227remote JSON endpoint (for details on Emacs's object system,
5226@pxref{Top,EIEIO,,eieio,EIEIO}). In modern object-oriented parlance, 5228@pxref{Top,EIEIO,,eieio,EIEIO}). In modern object-oriented parlance,
5227this class is ``abstract'', i.e. the actual class of a useful 5229this class is ``abstract'', i.e.@: the actual class of a useful
5228connection object used is always a subclass of it. Nevertheless, we 5230connection object is always a subclass of @code{jsonrpc-connection}.
5229can define two distinct API's around the @code{jsonrpc-connection} 5231Nevertheless, we can define two distinct APIs around the
5230class: 5232@code{jsonrpc-connection} class:
5231 5233
5234@cindex JSONRPC application interfaces
5232@enumerate 5235@enumerate
5233 5236
5234@item A user interface for building JSONRPC applications 5237@item A user interface for building JSONRPC applications
5235 5238
5239@findex :request-dispatcher
5240@findex :notification-dispatcher
5241@findex jsonrpc-notify
5242@findex jsonrpc-request
5243@findex jsonrpc-async-request
5236In this scenario, the JSONRPC application selects a concrete subclass 5244In this scenario, the JSONRPC application selects a concrete subclass
5237of @code{jsonrpc-connection}, and proceeds to create objects of that 5245of @code{jsonrpc-connection}, and proceeds to create objects of that
5238subclass using @code{make-instance}. To initiate a contact to the 5246subclass using @code{make-instance}. To initiate a contact to the
5239remote endpoint, the JSONRPC application passes this object to the 5247remote endpoint, the JSONRPC application passes this object to the
5240functions @code{jsonrpc-notify'}, @code{jsonrpc-request} and 5248functions @code{jsonrpc-notify}, @code{jsonrpc-request}, and/or
5241@code{jsonrpc-async-request}. For handling remotely initiated 5249@code{jsonrpc-async-request}. For handling remotely initiated
5242contacts, which generally come in asynchronously, the instantiation 5250contacts, which generally come in asynchronously, the instantiation
5243should include @code{:request-dispatcher} and 5251should include @code{:request-dispatcher} and
5244@code{:notification-dispatcher} initargs, which are both functions of 5252@code{:notification-dispatcher} initargs, which are both functions of
52453 arguments: the connection object; a symbol naming the JSONRPC method 52533 arguments: the connection object; a symbol naming the JSONRPC method
5246invoked remotely; and a JSONRPC "params" object. 5254invoked remotely; and a JSONRPC @code{params} object.
5247 5255
5256@findex jsonrpc-error
5248The function passed as @code{:request-dispatcher} is responsible for 5257The function passed as @code{:request-dispatcher} is responsible for
5249handling the remote endpoint's requests, which expect a reply from the 5258handling the remote endpoint's requests, which expect a reply from the
5250local endpoint (in this case, the program you're building). Inside 5259local endpoint (in this case, the program you're building). Inside
5251that function, you may either return locally (normally) or non-locally 5260that function, you may either return locally (a normal return) or
5252(error). A local return value must be a Lisp object serializable as 5261non-locally (an error return). A local return value must be a Lisp
5253JSON (@pxref{Parsing JSON}). This determines a success response, and 5262object that can be serialized as JSON (@pxref{Parsing JSON}). This
5254the object is forwarded to the server as the JSONRPC "result" object. 5263determines a success response, and the object is forwarded to the
5255A non-local return, achieved by calling the function 5264server as the JSONRPC @code{result} object. A non-local return,
5256@code{jsonrpc-error}, causes an error response to be sent to the 5265achieved by calling the function @code{jsonrpc-error}, causes an error
5257server. The details of the accompanying JSONRPC "error" are filled 5266response to be sent to the server. The details of the accompanying
5258out with whatever was passed to @code{jsonrpc-error}. A non-local 5267JSONRPC @code{error} are filled out with whatever was passed to
5259return triggered by an unexpected error of any other type also causes 5268@code{jsonrpc-error}. A non-local return triggered by an unexpected
5260an error response to be sent (unless you have set 5269error of any other type also causes an error response to be sent
5261@code{debug-on-error}, in which case this should land you in the 5270(unless you have set @code{debug-on-error}, in which case this calls
5262debugger, @pxref{Error Debugging}). 5271the Lisp debugger, @pxref{Error Debugging}).
5263 5272
5264@item A inheritance interface for building JSONRPC transport implementations 5273@item A inheritance interface for building JSONRPC transport implementations
5265 5274
5266In this scenario, @code{jsonrpc-connection} is subclassed to implement 5275In this scenario, @code{jsonrpc-connection} is subclassed to implement
5267a different underlying transport strategy (for details on how to 5276a different underlying transport strategy (for details on how to
5268subclass, @pxref{Inheritance,Inheritance,,eieio}). Users of the 5277subclass, see @ref{Inheritance,Inheritance,,eieio}.). Users of the
5269application-building interface can then instantiate objects of this 5278application-building interface can then instantiate objects of this
5270concrete class (using the @code{make-instance} function) and connect 5279concrete class (using the @code{make-instance} function) and connect
5271to JSONRPC endpoints using that strategy. 5280to JSONRPC endpoints using that strategy.
5272 5281
5273This API has mandatory and optional parts. 5282This API has mandatory and optional parts.
5274 5283
5284@findex jsonrpc-connection-send
5275To allow its users to initiate JSONRPC contacts (notifications or 5285To allow its users to initiate JSONRPC contacts (notifications or
5276requests) or reply to endpoint requests, the method 5286requests) or reply to endpoint requests, the subclass must have an
5277@code{jsonrpc-connection-send} must be implemented for the subclass. 5287implementation of the @code{jsonrpc-connection-send} method.
5278 5288
5289@findex jsonrpc-connection-receive
5279Likewise, for handling the three types of remote contacts (requests, 5290Likewise, for handling the three types of remote contacts (requests,
5280notifications and responses to local requests) the transport 5291notifications, and responses to local requests), the transport
5281implementation must arrange for the function 5292implementation must arrange for the function
5282@code{jsonrpc-connection-receive} to be called after noticing a new 5293@code{jsonrpc-connection-receive} to be called after noticing a new
5283JSONRPC message on the wire (whatever that "wire" may be). 5294JSONRPC message on the wire (whatever that "wire" may be).
5284 5295
5296@findex jsonrpc-shutdown
5297@findex jsonrpc-running-p
5285Finally, and optionally, the @code{jsonrpc-connection} subclass should 5298Finally, and optionally, the @code{jsonrpc-connection} subclass should
5286implement @code{jsonrpc-shutdown} and @code{jsonrpc-running-p} if 5299implement the @code{jsonrpc-shutdown} and @code{jsonrpc-running-p}
5287these concepts apply to the transport. If they do, then any system 5300methods if these concepts apply to the transport. If they do, then
5288resources (e.g. processes, timers, etc..) used listen for messages on 5301any system resources (e.g.@: processes, timers, etc.) used to listen for
5289the wire should be released in @code{jsonrpc-shutdown}, i.e. they 5302messages on the wire should be released in @code{jsonrpc-shutdown},
5290should only be needed while @code{jsonrpc-running-p} is non-nil. 5303i.e.@: they should only be needed while @code{jsonrpc-running-p} is
5304non-nil.
5291 5305
5292@end enumerate 5306@end enumerate
5293 5307
5294@node Process-based JSONRPC connections 5308@node Process-based JSONRPC connections
5295@subsection Process-based JSONRPC connections 5309@subsection Process-based JSONRPC connections
5310@cindex JSONRPC process-based connections
5296 5311
5297For convenience, the @code{jsonrpc} library comes built-in with a 5312@findex jsonrpc-process-connection
5313For convenience, the @code{jsonrpc} library comes with a built-in
5298@code{jsonrpc-process-connection} transport implementation that can 5314@code{jsonrpc-process-connection} transport implementation that can
5299talk to local subprocesses (using the standard input and standard 5315talk to local subprocesses (using the standard input and standard
5300output); or TCP hosts (using sockets); or any other remote endpoint 5316output); or TCP hosts (using sockets); or any other remote endpoint
@@ -5309,6 +5325,7 @@ JSONRPC, see the
5309@uref{https://microsoft.github.io/language-server-protocol/specification, 5325@uref{https://microsoft.github.io/language-server-protocol/specification,
5310Language Server Protocol}. 5326Language Server Protocol}.
5311 5327
5328@cindex JSONRPC connection initargs
5312Along with the mandatory @code{:request-dispatcher} and 5329Along with the mandatory @code{:request-dispatcher} and
5313@code{:notification-dispatcher} initargs, users of the 5330@code{:notification-dispatcher} initargs, users of the
5314@code{jsonrpc-process-connection} class should pass the following 5331@code{jsonrpc-process-connection} class should pass the following
@@ -5317,29 +5334,32 @@ initargs as keyword-value pairs to @code{make-instance}:
5317@table @code 5334@table @code
5318@item :process 5335@item :process
5319Value must be a live process object or a function of no arguments 5336Value must be a live process object or a function of no arguments
5320producing one such object. If passed a process object, that is 5337producing one such object. If passed a process object, the object is
5321expected to contain an pre-established connection; otherwise, the 5338expected to contain a pre-established connection; otherwise, the
5322function is called immediately after the object is made. 5339function is called immediately after the object is made.
5323 5340
5324@item :on-shutdown 5341@item :on-shutdown
5325Value must be a function of a single argument, the 5342Value must be a function of a single argument, the
5326@code{jsonrpc-process-connection} object. The function is called 5343@code{jsonrpc-process-connection} object. The function is called
5327after the underlying process object has been deleted (either 5344after the underlying process object has been deleted (either
5328deliberately by @code{jsonrpc-shutdown} or unexpectedly, because of 5345deliberately by @code{jsonrpc-shutdown}, or unexpectedly, because of
5329some external cause). 5346some external cause).
5330@end table 5347@end table
5331 5348
5332@node JSONRPC JSON object format 5349@node JSONRPC JSON object format
5333@subsection JSON object format 5350@subsection JSONRPC JSON object format
5351@cindex JSONRPC object format
5334 5352
5335JSON objects are exchanged as Lisp plists (@pxref{Parsing JSON}): 5353JSONRPC JSON objects are exchanged as Lisp plists (@pxref{Property
5336JSON-compatible plists are handed to the dispatcher functions and, 5354Lists}): JSON-compatible plists are handed to the dispatcher functions
5337likewise, JSON-compatible plists should be given to 5355and, likewise, JSON-compatible plists should be given to
5338@code{jsonrpc-notify}, @code{jsonrpc-request} and 5356@code{jsonrpc-notify}, @code{jsonrpc-request}, and
5339@code{jsonrpc-async-request}. 5357@code{jsonrpc-async-request}.
5340 5358
5341To facilitate handling plists, this library make liberal use of 5359@findex jsonrpc-lambda
5342@code{cl-lib} library and suggests (but doesn't force) its clients to 5360To facilitate handling plists, this library makes liberal use of
5361@code{cl-lib} library (@pxref{Top,cl-lib,,cl,Common Lisp Extensions
5362for GNU Emacs Lisp}) and suggests (but doesn't force) its clients to
5343do the same. A macro @code{jsonrpc-lambda} can be used to create a 5363do the same. A macro @code{jsonrpc-lambda} can be used to create a
5344lambda for destructuring a JSON-object like in this example: 5364lambda for destructuring a JSON-object like in this example:
5345 5365
@@ -5355,7 +5375,8 @@ lambda for destructuring a JSON-object like in this example:
5355@end example 5375@end example
5356 5376
5357@node JSONRPC deferred requests 5377@node JSONRPC deferred requests
5358@subsection Deferred requests 5378@subsection Deferred JSONRPC requests
5379@cindex JSONRPC deferred requests
5359 5380
5360In many @acronym{RPC} situations, synchronization between the two 5381In many @acronym{RPC} situations, synchronization between the two
5361communicating endpoints is a matter of correctly designing the RPC 5382communicating endpoints is a matter of correctly designing the RPC
@@ -5367,6 +5388,7 @@ is still uncertainty about the state of the remote endpoint.
5367Furthermore, acting on these events may only sometimes demand 5388Furthermore, acting on these events may only sometimes demand
5368synchronization, depending on the event's specific nature. 5389synchronization, depending on the event's specific nature.
5369 5390
5391@findex :deferred@r{, JSONRPC keyword}
5370The @code{:deferred} keyword argument to @code{jsonrpc-request} and 5392The @code{:deferred} keyword argument to @code{jsonrpc-request} and
5371@code{jsonrpc-async-request} is designed to let the caller indicate 5393@code{jsonrpc-async-request} is designed to let the caller indicate
5372that the specific request needs synchronization and its actual 5394that the specific request needs synchronization and its actual
@@ -5377,9 +5399,10 @@ isn't sent immediately, @code{jsonrpc} will make renewed efforts to
5377send it at certain key times during communication, such as when 5399send it at certain key times during communication, such as when
5378receiving or sending other messages to the endpoint. 5400receiving or sending other messages to the endpoint.
5379 5401
5402@findex jsonrpc-connection-ready-p
5380Before any attempt to send the request, the application-specific 5403Before any attempt to send the request, the application-specific
5381conditions are checked. Since the @code{jsonrpc} library can't known 5404conditions are checked. Since the @code{jsonrpc} library can't know
5382what these conditions are, the programmer may use the 5405what these conditions are, the program can use the
5383@code{jsonrpc-connection-ready-p} generic function (@pxref{Generic 5406@code{jsonrpc-connection-ready-p} generic function (@pxref{Generic
5384Functions}) to specify them. The default method for this function 5407Functions}) to specify them. The default method for this function
5385returns @code{t}, but you can add overriding methods that return 5408returns @code{t}, but you can add overriding methods that return