Calculator Example

Calculator provides an interface for client components to request compute operations over IPC using FIDL. The calculator sample has three parts: the engine, a client, and a library defining a FIDL communication protocol between the components.

Source layout

  • fidl/: Contains the definition of a Fuchsia interprocess communication (IPC) protocol (fuchsia.examples.calculator.Calculator) defined as a Fuchsia Interface Definition Language (FIDL) library. The BUILD.gn file describes how to build the FIDL library that can be used as a dependency for the client and engine.
  • engine/: Contains the source for the calculator engine that accepts compute operations and sends the results back. The engine handles incoming requests as FIDL messages received from the client.
  • client/: Contains the source for the client that sends compute requests to the calculator engine then prints the results to the system log.
  • meta/: Top-level component realm to perform capability routing between the engine and client components.

Before you begin

  1. Start a FEMU instance:

    $ ./third_party/fuchsia-sdk/bin/femu.sh -N
    
  2. Start a local package repository instance:

    $ ./third_party/fuchsia-sdk/bin/fserve.sh --image qemu-x64
    

Build the sample

  1. Run the build script to compile and package the sample:

    $ ./scripts/build.sh
    
  2. Publish the FAR package to your local package repository:

    $ ./third_party/fuchsia-sdk/bin/fpublish.sh out/x64/calculator-example.far
    

Run the sample

  1. Launch the sample component using ffx component run. This resolves the component from the package repository and starts it:

    $ ffx component run fuchsia-pkg://fuchsia.com/calculator-example#meta/calculator_realm.cm
    
  2. View the result messages in the system log using ffx target log:

    $ ffx target log watch
    [calculator_realm/client] INFO: Request: 2 + 2
    [calculator_realm/client] INFO: Result: 4