swim rust
1.0.0
Swim Rust SDK包含用於構建可以通過多路復用流API進行交互的狀態應用程序的軟件框架。它建立在Tokio異步運行時的頂部,任何游泳應用都需要Tokio運行時。
每個應用程序都由一定數量的狀態代理組成,每個代理都作為單獨的Tokio任務運行,並且可以由URI單獨解決。代理商可能具有公共狀態和私人狀態,可以僅在內存中保存,也可以選擇在持續存儲中。代理商的公共狀態由許多車道組成,類似於記錄中的字段。有多種車道,例如,其中包含單個值的車道和包含鍵值對圖的泳道。
可以通過建立指向它的鏈接(來自另一個代理實例或專門的客戶端)來觀察任何車道的狀態。已建立的鏈接將將所有更新向該車道的狀態推向訂戶,還將允許訂戶向州請求更改(對於支持此局的車道類型)。鏈接通過Web插座連接操作並具有多路復用,這意味著指向同一主機上多個車道的鏈接可以共享單個Web插座連接。
網站|開發人員指南|服務器API文檔|客戶端API文檔
在生鏽中實施游泳特工
構建游泳服務器應用程序
參考文檔
以下示例應用程序運行了一個託管單個代理路由的泳道服務器,每個代理實例都有單車道,稱為lane 。每次對車道進行更改時,它都會由服務器在控制台上打印。
[ dependencies ]
swimos = { version = " 0.1.1 " , features = [ " server " , " agent " ] } use swimos :: {
agent :: {
agent_lifecycle :: HandlerContext ,
agent_model :: AgentModel ,
event_handler :: { EventHandler , HandlerActionExt } ,
lanes :: ValueLane ,
lifecycle , AgentLaneModel ,
} ,
route :: RoutePattern ,
server :: { until_termination , Server , ServerBuilder } ,
} ;
# [ tokio :: main ]
pub async fn main ( ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
// An agent route consists of the agent definition and a lifecycle.
let model = AgentModel :: new ( ExampleAgent :: default , ExampleLifecycle . into_lifecycle ( ) ) ;
let server = ServerBuilder :: with_plane_name ( "Example Plane" )
. set_bind_addr ( "127.0.0.1:8080" . parse ( ) ? ) // Bind the server to this address.
. add_route ( RoutePattern :: parse_str ( "/examples/{id}" ) ? , model ) // Register the agent we have defined.
. build ( )
. await ? ;
// Run the server until we terminate it with Ctrl-C.
let ( task , handle ) = server . run ( ) ;
let ( ctrl_c_result , server_result ) = tokio :: join! ( until_termination ( handle , None ) , task ) ;
ctrl_c_result? ;
server_result? ;
Ok ( ( ) )
}
// Deriving the `AgentLaneModel` trait makes this type into an agent.
# [ derive ( AgentLaneModel ) ]
struct ExampleAgent {
lane : ValueLane < i32 > ,
}
// Any agent type can have any number of lifecycles defined for it. A lifecycle describes
// how the agent will react to events that occur as it executes.
# [ derive ( Default , Clone , Copy ) ]
struct ExampleLifecycle ;
// The `lifecycle` macro creates an method called `into_lifecycle` for the type, using the
// annotated event handlers methods in the block.
# [ lifecycle ( ExampleAgent ) ]
impl ExampleLifecycle {
# [ on_event ( lane ) ]
fn lane_event (
& self ,
context : HandlerContext < ExampleAgent > ,
value : & i32 ,
) -> impl EventHandler < ExampleAgent > {
let n = * value ;
context . get_agent_uri ( ) . and_then ( move |uri| {
context . effect ( move || {
println ! ( "Received value: {} for 'lane' on agent at URI: {}." , n , uri ) ;
} )
} )
}
}例如,如果游泳客戶端將帶有值5的更新發送給代理商,則將使用ExampleLifecycle啟動Lane lane的URI /examples/name , ExampleAgent的實例將由服務器啟動。然後,車道的值將設置為5 ,以下將在控制台上打印:
Received value: 5 for 'lane' on agent at URI: /examples/name.
example_apps目錄中提供了許多示例應用程序,這些應用程序演示了單個功能以及更全面的應用程序。
請參閱《開髮指南》。
該項目是根據Apache 2.0許可證獲得許可的。