Remote Procedure Calls (RPC): Difference between revisions

From MidasWiki
Jump to navigation Jump to search
(The midas documentation has moved to https://midas.triumf.ca/docs/)
Tag: Replaced
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Pagelinks}}
The midas documentation has moved. The new documentation about remote procedure calls is in:
 
* [https://midas.triumf.ca/docs/develop/features/core_system/writing_programs/communicating_between_programs/#remote-procedure-calls-rpcs Remote procedure calls (RPCs)] in Communicating between programs (JRPC and BRPC, with examples)
= RPC in a nutshell =
* [https://midas.triumf.ca/docs/develop/features/acquiring_data/frontends/programming/#rpc-handling RPC handling] in Frontends: programming (handling RPCs in a frontend)
 
* [https://midas.triumf.ca/docs/develop/api/cpp/rpc/ Midas C++ API - RPC functions] (function reference)
Midas uses a Remote Procedure Call (RPC) system to allow different midas clients to talk to each other directly. One client can issue a command to another, and the second client will perform some action and send a response back to the caller.
 
= Internal usage in midas =
 
Internally, midas uses the RPC system to handle run transitions, ODB hotlinks and more.  
 
For example, if you start a run using the web interface, mhttpd (the webserver) will send an <code>RPC_RC_TRANSITION</code> message to clients that want to handle the begin-of-run transition. Each client responds with whether it's okay for the transition to proceed.
 
Any clients that are running on a remote server use RPC for all their communications with midas. The special mserver program (running on the main host) is responsible for handling these requests. For example, whereas a local client can read an ODB value directly from shared memory on the main host; a remote client instead issues an RPC call to mserver, which reads the value from shared memory and returns it to the caller.
 
= Use in user code - JRPC =
 
One of the RPC commands, <code>RPC_JRPC</code>, is very flexible and allows the user to easily integrate custom commands into their programs. There are interfaces in C++, python and javascript.
 
The calling code specifies:
* The program it wants to talk to
* The command it wants to issue (generally a short string)
* Some arguments (either plain text or JSON)
 
The client then:
* Decides what to do based on the command and arguments
* Responds with a status code and text message (either plain text or JSON)
 
Some example uses of this system are:
* Have a client turn some equipment off when the user clicks a button on a webpage.
* Offload some complex calculations to another program (e.g. if your main client is in C++ but it's much easier to implement the calculations in python).
* Implement a web-based plot display that gets data as JSON from a python/C++ client
* ...
 
== Benefits vs ODB hotlinks ==
 
Often users implement inter-process communication using ODB hotlinks (where a client can say "tell me whenever /Path/to/something in the ODB changes"). Although this works for simple cases, the JRPC system is much more flexible and robust.
 
* Much easier to specify arguments. Avoids race conditions if you were to use multiple ODB keys to tell the target what to do.
* There is no "competition" for the ODB key(s). Multiple clients can call the same RPC without interfering with each other.
* More flexible response format as you can return a (very) long string. Alternatives would be to put the response in an ODB value (limited maximum length) or in a file on disk (extra overhead, and possibly annoying to deal with stale files over NFS).
 
== Registering your JRPC handler ==
 
=== C++ ===
 
=== C++ (TMFE) ===
 
=== Python ===
 
== Calling JRPC on other clients ==
 
=== C++ ===
 
=== Python ===
 
=== Javascript ===

Latest revision as of 22:01, 30 October 2026

The midas documentation has moved. The new documentation about remote procedure calls is in: