@@ -27,7 +27,85 @@ host can share a single web-socket connection.
27
27
28
28
## Examples
29
29
30
- TODO
30
+ The following example application runs a SwimOS server that hosts a single agent route where each agent instance
31
+ has single lane, called ` lane ` . Each time a changes is made to the lane, it will be printed on the console by the
32
+ server.
33
+
34
+ ``` rust
35
+ use swimos :: {
36
+ agent :: {
37
+ agent_lifecycle :: HandlerContext ,
38
+ agent_model :: AgentModel ,
39
+ event_handler :: {EventHandler , HandlerActionExt },
40
+ lanes :: ValueLane ,
41
+ lifecycle, AgentLaneModel ,
42
+ },
43
+ route :: RoutePattern ,
44
+ server :: {until_termination, Server , ServerBuilder },
45
+ };
46
+
47
+ #[tokio:: main]
48
+ pub async fn main () -> Result <(), Box <dyn std :: error :: Error >> {
49
+
50
+ // An agent route consists of the agent definition and a lifecycle.
51
+ let model = AgentModel :: new (ExampleAgent :: default , ExampleLifecycle . into_lifecycle ());
52
+
53
+ let server = ServerBuilder :: with_plane_name (" Example Plane" )
54
+ . set_bind_addr (" 127.0.0.1:8080" . parse ()? ) // Bind the server to this address.
55
+ . add_route (RoutePattern :: parse_str (" /examples/{id}" )? , model ) // Register the agent we have defined.
56
+ . build ()
57
+ . await ? ;
58
+
59
+ // Run the server until we terminate it with Ctrl-C.
60
+ let (task , handle ) = server . run ();
61
+ let (ctrl_c_result , server_result ) = tokio :: join! (until_termination (handle , None ), task );
62
+
63
+ ctrl_c_result ? ;
64
+ server_result ? ;
65
+ Ok (())
66
+ }
67
+
68
+ // Deriving the `AgentLaneModel` trait makes this type into an agent.
69
+ #[derive(AgentLaneModel )]
70
+ struct ExampleAgent {
71
+ lane : ValueLane <i32 >,
72
+ }
73
+
74
+ // Any agent type can have any number of lifecycles defined for it. A lifecycle describes
75
+ // how the agent will react to events that occur as it executes.
76
+ #[derive(Default , Clone , Copy )]
77
+ struct ExampleLifecycle ;
78
+
79
+ // The `lifecycle` macro creates an method called `into_lifecycle` for the type, using the
80
+ // annotated event handlers methods in the block.
81
+ #[lifecycle(ExampleAgent )]
82
+ impl ExampleLifecycle {
83
+
84
+ #[on_event(lane)]
85
+ fn lane_event (
86
+ & self ,
87
+ context : HandlerContext <ExampleAgent >,
88
+ value : & i32 ,
89
+ ) -> impl EventHandler <ExampleAgent > {
90
+ let n = * value ;
91
+ context . get_agent_uri (). and_then (move | uri | {
92
+ context . effect (move || {
93
+ println! (" Received value: {} for 'lane' on agent at URI: {}." , n , uri );
94
+ })
95
+ })
96
+ }
97
+
98
+ }
99
+ ```
100
+
101
+ For example, if a Swim client sends an update, with the value ` 5 ` , to the agent at the URI ` /examples/name ` for the
102
+ lane ` lane ` , an instance of ` ExampleAgent ` , using ` ExampleLifecycle ` , will be started by the server. The value of the
103
+ lane will then be set to ` 5 ` and the following will be printed on the console:
104
+
105
+ ```
106
+ Received value: 5 for 'lane' on agent at URI: /examples/name.
107
+ ```
108
+
31
109
## Development
32
110
33
- See the [ development guide] ( DEVELOPMENT.md ) ;
111
+ See the [ development guide] ( DEVELOPMENT.md ) .
0 commit comments