CodeSonar C++ API
[For improved navigation, enable JavaScript.]
Public Member Functions | List of all members
cs::rpc_handler Class Referenceabstract

Handles remote procedure calls (RPCs). More...

Public Member Functions

 rpc_handler ()
 Constructor.
 
 rpc_handler (const rpc_handler &)
 Copy constructor.
 
virtual std::string operator() (std::string)=0
 When the RPC handler is invoked, CodeSonar will apply this function to the specified request string. More...
 

Detailed Description

Handles remote procedure calls (RPCs).

The CodeSonar hub can request analysis information that is not stored on the hub via a remote procedure call (RPC) mechanism. There are numerous predefined RPC handlers which provide the information required by the standard features of the CodeSonar hub, but if you want to process hub requests from a plug-in you need to register your own RPC handler.

To add a remote procedure call handler to the hub, create a plug-in that does the following.

  1. Subclass rpc_handler, and define operator() to perform actions you require and then return a response.
  2. Create an instance of your subclass, and add it to the analysis with analysis::add_hub_rpc_handler().

For example, consider the following small plug-in.

// ncallers_handler.cpp
//
// A small RPC example.
// Must precede #include "csonar_api.hpp".
#define CS_CPP_IMPL
// Include the entire CodeSonar C++ API.
#include "csonar_api.hpp"
#include <string>
// Subclass cs::rpc_handler and implement operator()
class count_callers: public cs::rpc_handler{
public:
std::string operator()(std::string request){
try{
cs::procedure proc = proj.find_procedure(request);
return std::to_string(proc.callers_count());
}
catch ( const cs::result &r ) {
return "-1";
}
}
};
// Create an instance of count_callers, and add it to the hub.
static char dummy = (cs::analysis::add_hub_rpc_handler("alex_handler",
new count_callers()),
0);
// Every C++ plug-in for CodeSonar must define this function.
static void cs_plug_main(void){}
// All CodeSonar C++ plug-ins must finish with a line of this form;
// the name must match the compiled plug-in.
CS_DEFINE_PLUGIN(ncallers_plugin)

This plug-in subclasses rpc_handler to create class count_callers, which interprets the request string as a procedure name and uses it to look up that procedure and report its caller count. It then invokes analysis::add_hub_rpc_handler() on a count_callers instance to register the handler.

To try out this plug-in:

  1. Copy the plug-in source and save as a C++ source file.
  2. Compile into a plug-in. The output file name must be ncallers_plugin.{dll,so,bundle}.
  3. Install the plug-in.
  4. Build and analyze a CodeSonar project. Make a note of the analysis ID.
  5. Navigate to http://<host>:<port>/rpc/test/ (or https://<host>:<port>/rpc/test/, if your hub is running HTTPS).
  6. Fill out the form as follows.
    • Analysis ID: enter your analysis ID
    • Message Kind: leave empty
    • Message Content:
      {
      "jsonrpc": "2.0",
      "method": "plugin_message",
      "params": {
      "arguments": "main",
      "plugin_handler_name": "alex_handler"
      }
      }
  7. Click Submit Query.

    The last key-value pair in the reported Message Response will be 'string':'<num>', where <num> is the number of callers to main() in the analyzed project, or -1 if there is no such procedure. Note that the handler obtains the procedure with project::find_procedure(), which uses the procedure's verbose name (as returned by procedure::verbose_name()).

  8. Try with other procedure names from your analyzed project.

Member Function Documentation

◆ operator()()

virtual std::string cs::rpc_handler::operator() ( std::string  )
pure virtual

When the RPC handler is invoked, CodeSonar will apply this function to the specified request string.

Parameters
[in]requestThe request string passed to the handler.
Returns
A response string.

When you subclass rpc_handler to create an RPC handler, provide a definition for this. (If you don't, the handler won't do anything!)


The documentation for this class was generated from the following file: