diff options
| author | Eli Zaretskii | 2019-04-13 10:42:14 +0300 |
|---|---|---|
| committer | Eli Zaretskii | 2019-04-13 10:42:14 +0300 |
| commit | d82d4fb9157dd86359e4489c8da100943754a4d0 (patch) | |
| tree | ab348c7c463077a4e23b7292f267b128288441a9 | |
| parent | 2475687d2f0ca6a605d091bb7acb723d0dace27b (diff) | |
| download | emacs-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.texi | 109 |
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 | ||
| 5201 | The @code{jsonrpc} library implements the @acronym{JSONRPC} | 5202 | The @code{jsonrpc} library implements the @acronym{JSONRPC} |
| 5202 | specification, version 2.0, as it is described in | 5203 | specification, 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 |
| 5204 | generic @code{Remote Procedure Call} protocol designed around | 5205 | generic @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 | |||
| 5220 | process, over sockets, over http, or in many various message passing | 5221 | process, over sockets, over http, or in many various message passing |
| 5221 | environments." | 5222 | environments." |
| 5222 | 5223 | ||
| 5224 | @findex jsonrpc-connection | ||
| 5223 | To model this agnosticism, the @code{jsonrpc} library uses objects of | 5225 | To model this agnosticism, the @code{jsonrpc} library uses objects of |
| 5224 | a @code{jsonrpc-connection} class, which represent a connection the | 5226 | a @code{jsonrpc-connection} class, which represent a connection to a |
| 5225 | remote JSON endpoint (for details on Emacs's object system, | 5227 | remote 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, |
| 5227 | this class is ``abstract'', i.e. the actual class of a useful | 5229 | this class is ``abstract'', i.e.@: the actual class of a useful |
| 5228 | connection object used is always a subclass of it. Nevertheless, we | 5230 | connection object is always a subclass of @code{jsonrpc-connection}. |
| 5229 | can define two distinct API's around the @code{jsonrpc-connection} | 5231 | Nevertheless, we can define two distinct APIs around the |
| 5230 | class: | 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 | ||
| 5236 | In this scenario, the JSONRPC application selects a concrete subclass | 5244 | In this scenario, the JSONRPC application selects a concrete subclass |
| 5237 | of @code{jsonrpc-connection}, and proceeds to create objects of that | 5245 | of @code{jsonrpc-connection}, and proceeds to create objects of that |
| 5238 | subclass using @code{make-instance}. To initiate a contact to the | 5246 | subclass using @code{make-instance}. To initiate a contact to the |
| 5239 | remote endpoint, the JSONRPC application passes this object to the | 5247 | remote endpoint, the JSONRPC application passes this object to the |
| 5240 | functions @code{jsonrpc-notify'}, @code{jsonrpc-request} and | 5248 | functions @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 |
| 5242 | contacts, which generally come in asynchronously, the instantiation | 5250 | contacts, which generally come in asynchronously, the instantiation |
| 5243 | should include @code{:request-dispatcher} and | 5251 | should 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 |
| 5245 | 3 arguments: the connection object; a symbol naming the JSONRPC method | 5253 | 3 arguments: the connection object; a symbol naming the JSONRPC method |
| 5246 | invoked remotely; and a JSONRPC "params" object. | 5254 | invoked remotely; and a JSONRPC @code{params} object. |
| 5247 | 5255 | ||
| 5256 | @findex jsonrpc-error | ||
| 5248 | The function passed as @code{:request-dispatcher} is responsible for | 5257 | The function passed as @code{:request-dispatcher} is responsible for |
| 5249 | handling the remote endpoint's requests, which expect a reply from the | 5258 | handling the remote endpoint's requests, which expect a reply from the |
| 5250 | local endpoint (in this case, the program you're building). Inside | 5259 | local endpoint (in this case, the program you're building). Inside |
| 5251 | that function, you may either return locally (normally) or non-locally | 5260 | that function, you may either return locally (a normal return) or |
| 5252 | (error). A local return value must be a Lisp object serializable as | 5261 | non-locally (an error return). A local return value must be a Lisp |
| 5253 | JSON (@pxref{Parsing JSON}). This determines a success response, and | 5262 | object that can be serialized as JSON (@pxref{Parsing JSON}). This |
| 5254 | the object is forwarded to the server as the JSONRPC "result" object. | 5263 | determines a success response, and the object is forwarded to the |
| 5255 | A non-local return, achieved by calling the function | 5264 | server as the JSONRPC @code{result} object. A non-local return, |
| 5256 | @code{jsonrpc-error}, causes an error response to be sent to the | 5265 | achieved by calling the function @code{jsonrpc-error}, causes an error |
| 5257 | server. The details of the accompanying JSONRPC "error" are filled | 5266 | response to be sent to the server. The details of the accompanying |
| 5258 | out with whatever was passed to @code{jsonrpc-error}. A non-local | 5267 | JSONRPC @code{error} are filled out with whatever was passed to |
| 5259 | return triggered by an unexpected error of any other type also causes | 5268 | @code{jsonrpc-error}. A non-local return triggered by an unexpected |
| 5260 | an error response to be sent (unless you have set | 5269 | error 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 |
| 5262 | debugger, @pxref{Error Debugging}). | 5271 | the 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 | ||
| 5266 | In this scenario, @code{jsonrpc-connection} is subclassed to implement | 5275 | In this scenario, @code{jsonrpc-connection} is subclassed to implement |
| 5267 | a different underlying transport strategy (for details on how to | 5276 | a different underlying transport strategy (for details on how to |
| 5268 | subclass, @pxref{Inheritance,Inheritance,,eieio}). Users of the | 5277 | subclass, see @ref{Inheritance,Inheritance,,eieio}.). Users of the |
| 5269 | application-building interface can then instantiate objects of this | 5278 | application-building interface can then instantiate objects of this |
| 5270 | concrete class (using the @code{make-instance} function) and connect | 5279 | concrete class (using the @code{make-instance} function) and connect |
| 5271 | to JSONRPC endpoints using that strategy. | 5280 | to JSONRPC endpoints using that strategy. |
| 5272 | 5281 | ||
| 5273 | This API has mandatory and optional parts. | 5282 | This API has mandatory and optional parts. |
| 5274 | 5283 | ||
| 5284 | @findex jsonrpc-connection-send | ||
| 5275 | To allow its users to initiate JSONRPC contacts (notifications or | 5285 | To allow its users to initiate JSONRPC contacts (notifications or |
| 5276 | requests) or reply to endpoint requests, the method | 5286 | requests) or reply to endpoint requests, the subclass must have an |
| 5277 | @code{jsonrpc-connection-send} must be implemented for the subclass. | 5287 | implementation of the @code{jsonrpc-connection-send} method. |
| 5278 | 5288 | ||
| 5289 | @findex jsonrpc-connection-receive | ||
| 5279 | Likewise, for handling the three types of remote contacts (requests, | 5290 | Likewise, for handling the three types of remote contacts (requests, |
| 5280 | notifications and responses to local requests) the transport | 5291 | notifications, and responses to local requests), the transport |
| 5281 | implementation must arrange for the function | 5292 | implementation 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 |
| 5283 | JSONRPC message on the wire (whatever that "wire" may be). | 5294 | JSONRPC message on the wire (whatever that "wire" may be). |
| 5284 | 5295 | ||
| 5296 | @findex jsonrpc-shutdown | ||
| 5297 | @findex jsonrpc-running-p | ||
| 5285 | Finally, and optionally, the @code{jsonrpc-connection} subclass should | 5298 | Finally, and optionally, the @code{jsonrpc-connection} subclass should |
| 5286 | implement @code{jsonrpc-shutdown} and @code{jsonrpc-running-p} if | 5299 | implement the @code{jsonrpc-shutdown} and @code{jsonrpc-running-p} |
| 5287 | these concepts apply to the transport. If they do, then any system | 5300 | methods if these concepts apply to the transport. If they do, then |
| 5288 | resources (e.g. processes, timers, etc..) used listen for messages on | 5301 | any system resources (e.g.@: processes, timers, etc.) used to listen for |
| 5289 | the wire should be released in @code{jsonrpc-shutdown}, i.e. they | 5302 | messages on the wire should be released in @code{jsonrpc-shutdown}, |
| 5290 | should only be needed while @code{jsonrpc-running-p} is non-nil. | 5303 | i.e.@: they should only be needed while @code{jsonrpc-running-p} is |
| 5304 | non-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 | ||
| 5297 | For convenience, the @code{jsonrpc} library comes built-in with a | 5312 | @findex jsonrpc-process-connection |
| 5313 | For 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 |
| 5299 | talk to local subprocesses (using the standard input and standard | 5315 | talk to local subprocesses (using the standard input and standard |
| 5300 | output); or TCP hosts (using sockets); or any other remote endpoint | 5316 | output); 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, |
| 5310 | Language Server Protocol}. | 5326 | Language Server Protocol}. |
| 5311 | 5327 | ||
| 5328 | @cindex JSONRPC connection initargs | ||
| 5312 | Along with the mandatory @code{:request-dispatcher} and | 5329 | Along 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 |
| 5319 | Value must be a live process object or a function of no arguments | 5336 | Value must be a live process object or a function of no arguments |
| 5320 | producing one such object. If passed a process object, that is | 5337 | producing one such object. If passed a process object, the object is |
| 5321 | expected to contain an pre-established connection; otherwise, the | 5338 | expected to contain a pre-established connection; otherwise, the |
| 5322 | function is called immediately after the object is made. | 5339 | function is called immediately after the object is made. |
| 5323 | 5340 | ||
| 5324 | @item :on-shutdown | 5341 | @item :on-shutdown |
| 5325 | Value must be a function of a single argument, the | 5342 | Value 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 |
| 5327 | after the underlying process object has been deleted (either | 5344 | after the underlying process object has been deleted (either |
| 5328 | deliberately by @code{jsonrpc-shutdown} or unexpectedly, because of | 5345 | deliberately by @code{jsonrpc-shutdown}, or unexpectedly, because of |
| 5329 | some external cause). | 5346 | some 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 | ||
| 5335 | JSON objects are exchanged as Lisp plists (@pxref{Parsing JSON}): | 5353 | JSONRPC JSON objects are exchanged as Lisp plists (@pxref{Property |
| 5336 | JSON-compatible plists are handed to the dispatcher functions and, | 5354 | Lists}): JSON-compatible plists are handed to the dispatcher functions |
| 5337 | likewise, JSON-compatible plists should be given to | 5355 | and, 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 | ||
| 5341 | To 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 | 5360 | To facilitate handling plists, this library makes liberal use of |
| 5361 | @code{cl-lib} library (@pxref{Top,cl-lib,,cl,Common Lisp Extensions | ||
| 5362 | for GNU Emacs Lisp}) and suggests (but doesn't force) its clients to | ||
| 5343 | do the same. A macro @code{jsonrpc-lambda} can be used to create a | 5363 | do the same. A macro @code{jsonrpc-lambda} can be used to create a |
| 5344 | lambda for destructuring a JSON-object like in this example: | 5364 | lambda 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 | ||
| 5360 | In many @acronym{RPC} situations, synchronization between the two | 5381 | In many @acronym{RPC} situations, synchronization between the two |
| 5361 | communicating endpoints is a matter of correctly designing the RPC | 5382 | communicating endpoints is a matter of correctly designing the RPC |
| @@ -5367,6 +5388,7 @@ is still uncertainty about the state of the remote endpoint. | |||
| 5367 | Furthermore, acting on these events may only sometimes demand | 5388 | Furthermore, acting on these events may only sometimes demand |
| 5368 | synchronization, depending on the event's specific nature. | 5389 | synchronization, depending on the event's specific nature. |
| 5369 | 5390 | ||
| 5391 | @findex :deferred@r{, JSONRPC keyword} | ||
| 5370 | The @code{:deferred} keyword argument to @code{jsonrpc-request} and | 5392 | The @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 |
| 5372 | that the specific request needs synchronization and its actual | 5394 | that the specific request needs synchronization and its actual |
| @@ -5377,9 +5399,10 @@ isn't sent immediately, @code{jsonrpc} will make renewed efforts to | |||
| 5377 | send it at certain key times during communication, such as when | 5399 | send it at certain key times during communication, such as when |
| 5378 | receiving or sending other messages to the endpoint. | 5400 | receiving or sending other messages to the endpoint. |
| 5379 | 5401 | ||
| 5402 | @findex jsonrpc-connection-ready-p | ||
| 5380 | Before any attempt to send the request, the application-specific | 5403 | Before any attempt to send the request, the application-specific |
| 5381 | conditions are checked. Since the @code{jsonrpc} library can't known | 5404 | conditions are checked. Since the @code{jsonrpc} library can't know |
| 5382 | what these conditions are, the programmer may use the | 5405 | what 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 |
| 5384 | Functions}) to specify them. The default method for this function | 5407 | Functions}) to specify them. The default method for this function |
| 5385 | returns @code{t}, but you can add overriding methods that return | 5408 | returns @code{t}, but you can add overriding methods that return |