Swim Rust SDKには、多重化されたストリーミングAPIを介して相互作用できるステートフルアプリケーションを構築するためのソフトウェアフレームワークが含まれています。 Tokioの非同期ランタイムの上に構築されており、SwimアプリケーションにはTokioランタイムが必要です。
各アプリケーションは、いくつかのステートフルエージェントで構成されており、それぞれが別のトキオタスクとして実行され、URIが個別にアドレス指定できます。エージェントには、メモリのみに保持されるか、オプションでは永続的なストレージのいずれかに保持できる公共および民間の両方の状態を持つ場合があります。エージェントの公共の状態は、レコード内のフィールドに類似した多くのレーンで構成されています。たとえば、単一の値を含む車線とキー価値ペアのマップを含むレーンには、複数の種類があります。
レーンの状態は、それへのリンクを確立することで観察できます(別のエージェントインスタンスまたは専用クライアントのいずれかから)。確立されたリンクは、すべての更新をその車線の状態にサブスクライバーにプッシュし、サブスクライバーが(これをサポートする車線の種類の場合)州への変更を要求することもできます。リンクはWebソケット接続で動作し、多重化されています。つまり、同じホストの複数のレーンへのリンクは、単一のWebソケット接続を共有できることを意味します。
ウェブサイト|開発者ガイド|サーバーAPIドキュメント|クライアントAPIドキュメント
さびに水泳エージェントを実装します
水泳サーバーアプリケーションの構築
参照ドキュメント
次の例では、各エージェントインスタンスがlaneと呼ばれる単一のレーンを持つ単一のエージェントルートをホストするSWIMOSサーバーを実行します。レーンに変更が行われるたびに、サーバーによってコンソールに印刷されます。
[ 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 ) ;
} )
} )
}
}たとえば、Swimクライアントが値5のアップデートを、レーンlaneのURI /examples/nameのエージェントに送信する場合、 ExampleLifecycleを使用したExampleAgentのインスタンスはサーバーによって開始されます。レーンの値は5に設定され、以下はコンソールに印刷されます。
Received value: 5 for 'lane' on agent at URI: /examples/name.
個々の機能とより包括的なアプリケーションを示すexample_appsディレクトリで、多くの例を使用できます。
開発ガイドを参照してください。
このプロジェクトは、Apache 2.0ライセンスの下でライセンスされています。