You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # C Dataflow Example
  2. This examples shows how to create and connect dora operators and custom nodes in C.
  3. ## Overview
  4. The [`dataflow.yml`](./dataflow.yml) defines a simple dataflow graph with the following three nodes:
  5. - [`node.c`](./node.c) is a custom node, i.e., it has its own main function and runs as a separate process. It uses the [`dora-node-api-c` crate](../../apis/c/node/) to interact with the dora dataflow.
  6. - The node has a single input named `timer` that is mapped to a dora-provided periodic timer (`dora/timer/secs/1`).
  7. - Whenever the node receives a timer tick, it sends out a message with ID `tick` and a counter value as data (just a single byte).
  8. - After receiving 10 timer inputs, the node exits.
  9. - The [`operator.c`](./operator.c) file defines a dora _operator_ that is plugged as a shared library into a dora runtime. Instead of defining a `main` function, it implements a template of `dora_*` functions, which are invoked by the dora runtime, e.g. when new input is available.
  10. - The operator takes the `tick` messages created by the `node.c` node as input. For each input value, it checks the ID and then prints the received message to `stdout`.
  11. - It counts the received values and outputs a string of the format _"The current counter value is ..."_.
  12. - The [`sink.c`](./sink.c) file defines a custom node again, which takes the output string of the operator as input. It prints each received input to stdout and exits as soon as the input stream is closed.
  13. ## Compile and Run
  14. To try it out, you can use the [`run.rs`](./run.rs) binary. It performs all required build steps and then starts the dataflow. Use the following command to run it: `cargo run --example c-dataflow`.
  15. For a manual build, follow these steps:
  16. **Build the custom nodes:**
  17. - Create a `build` folder in this directory (i.e., next to the `node.c` file)
  18. - Compile the `dora-node-api-c` crate into a static library.
  19. - Run `cargo build -p dora-node-api-c --release`
  20. - The resulting staticlib is then available under `../../target/release/libdora-node-api-c.a`.
  21. - Compile the `node.c` (e.g. using `clang`) and link the staticlib
  22. - For example, use the following command:
  23. ```
  24. clang node.c <FLAGS> -ldora_node_api_c -L ../../target/release --output build/c_node
  25. ```
  26. - The `<FLAGS>` depend on the operating system and the libraries that the C node uses. The following flags are required for each OS:
  27. - Linux: `-lm -lrt -ldl -pthread`
  28. - macOS: `-framework CoreServices -framework Security -l System -l resolv -l pthread -l c -l m`
  29. - Windows:
  30. ```
  31. -ladvapi32 -luserenv -lkernel32 -lws2_32 -lbcrypt -lncrypt -lschannel -lntdll -liphlpapi
  32. -lcfgmgr32 -lcredui -lcrypt32 -lcryptnet -lfwpuclnt -lgdi32 -lmsimg32 -lmswsock -lole32
  33. -lopengl32 -lsecur32 -lshell32 -lsynchronization -luser32 -lwinspool
  34. -Wl,-nodefaultlib:libcmt -D_DLL -lmsvcrt
  35. ```
  36. Also: On Windows, the output file should have an `.exe` extension: `--output build/c_node.exe`
  37. - Repeat the previous step for the `sink.c` executable
  38. **Build the operator:**
  39. - Compile the `operator.c` file into a shared library.
  40. - For example, use the following commands:
  41. ```
  42. clang -c operator.c -o build/operator.o -fdeclspec -fPIC
  43. clang -shared build/operator.o -o build/liboperator.so
  44. ```
  45. Omit the `-fPIC` argument on Windows. Replace the `liboperator.so` name with the shared library standard library prefix/extensions used on your OS, e.g. `.dll` on Windows.
  46. **Build the dora coordinator and runtime:**
  47. - Build the `dora-coordinator` executable using `cargo build -p dora-coordinator --release`
  48. - Build the `dora-runtime` executable using `cargo build -p dora-runtime --release`
  49. **Run the dataflow:**
  50. - Start the `dora-coordinator`, passing the paths to the dataflow file and the `dora-runtime` as arguments:
  51. ```
  52. ../../target/release/dora-daemon --run-dataflow dataflow.yml ../../target/release/dora-runtime
  53. ```