forester_http/lib.rs
1//! # Forester HTTP
2//! The library provides a contract to implement a remote action alongside with the
3//! API to the Forester instance of http server.
4//! It is used to get access to the blackboard and to trace events.
5//!
6//! The library composes three main parts:
7//! * `ForesterRemoteAction` - the contract for the remote action that is expected by the Forester instance
8//! * `ForesterHttpApi` - the api to Forester instance of http server
9//! * `ForesterHttpClient` - the client to the Forester instance of http server
10//!
11//! Client uses api to get access to the blackboard and to trace events.
12//! Under the hood it uses reqwest(blocking) to send requests to the Forester instance.
13//! If the library is willing to use another client it can use only api.
14//!
15pub mod api;
16pub mod client;
17
18use serde::{Deserialize, Serialize};
19use serde_json::Value;
20
21/// The result that the node returns
22/// It can be Success, Failure or Running
23/// The Failure contains the error message
24/// The Running means that the node is still running
25/// and it will be executed on the next tick
26#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
27pub enum TickResult {
28 Success,
29 Failure(String),
30 Running,
31}
32
33/// The request that is sent from the Forester instance
34/// It has the current tick and the arguments in the action from tree
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct RemoteActionRequest {
37 pub tick: usize,
38 pub args: Vec<RtArgument>,
39 pub serv_url: String,
40}
41
42/// The argument that is sent from the Forester instance
43#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
44pub struct RtArgument {
45 name: String,
46 value: Value,
47}
48
49/// Describes the contract for the remote action that is expected by the Forester instance
50/// The remote action is a remote implementation of the action node in the tree.
51///
52/// Therefore the implementation of the remote action should be integrated to the http api
53/// that is provided on the other side.
54///
55/// # Example
56/// Having the following b-tree:
57/// ```f-tree
58///
59/// root main sequence {
60/// remote_action(1,2,3)
61/// }
62///
63/// impl remote_action(a:num, b:num, c:num);
64///
65/// ```
66///
67/// Register the action in the Forester instance:
68/// ```pseudocode
69///
70/// fn build_forester(mut fb: ForesterBuilder){
71/// ...
72/// let action = RemoteHttpAction::new("http://localhost:10000/remote_action/".to_string());
73/// fb.register_remote_action("remote_action", action);
74/// ...
75/// }
76///
77/// ```
78/// Thus, now we need to implement
79/// the remote action in the http api: `http://localhost:10000/remote_action/`
80/// that will be called by the Forester instance.
81///
82pub trait ForesterRemoteAction {
83 fn tick(&self, request: RemoteActionRequest) -> TickResult;
84}