錆の俳優フレームワーク
Any )actixを使用するには、これをCargo.tomlに追加します。toml:
[ dependencies ]
actix = " 0.13 "Actixを使用するには、最初にSystemを作成する必要があります。
fn main ( ) {
let system = actix :: System :: new ( ) ;
system . run ( ) ;
} ActixはTokioランタイムを使用します。 System::new()新しいイベントループを作成します。 System.run()はTokioイベントループを開始し、 SystemアクターがSystemExitメッセージを受信すると終了します。
俳優を定義するためには、構造体を定義し、 Actor特性を実装させる必要があります。
use actix :: { Actor , Context , System } ;
struct MyActor ;
impl Actor for MyActor {
type Context = Context < Self > ;
fn started ( & mut self , _ctx : & mut Self :: Context ) {
println ! ( "I am alive!" ) ;
System :: current ( ) . stop ( ) ; // <- stop system
}
}
fn main ( ) {
let system = System :: new ( ) ;
let _addr = system . block_on ( async { MyActor . start ( ) } ) ;
system . run ( ) . unwrap ( ) ;
}新しい俳優を生み出すことは、俳優の特性の方法とstart create介して達成されます。俳優を作成するいくつかの異なる方法を提供します。詳細については、ドキュメントを確認してください。俳優特性の方法をstarted 、 stopping 、 stoppedことができます。俳優が開始したときにstartedが呼び出され、俳優が終了したときにstopping 。俳優のライフサイクルの詳細については、APIドキュメントを確認してください。
俳優は、メッセージを送信することで別の俳優とコミュニケーションを取ります。 Actixでは、すべてのメッセージが入力されます。 2つのusizeパラメーターとこのメッセージを受け入れ、これら2つの数字の合計を返すアクターを使用して、単純なSumメッセージを定義しましょう。ここでは、 #[actix::main]属性をSystemを簡単に開始し、メイン機能を駆動して、 Actorから送信された応答を簡単に.awaitできるようにします。
use actix :: prelude :: * ;
// this is our Message
// we have to define the response type (rtype)
# [ derive ( Message ) ]
# [ rtype ( usize ) ]
struct Sum ( usize , usize ) ;
// Actor definition
struct Calculator ;
impl Actor for Calculator {
type Context = Context < Self > ;
}
// now we need to implement `Handler` on `Calculator` for the `Sum` message.
impl Handler < Sum > for Calculator {
type Result = usize ; // <- Message response type
fn handle ( & mut self , msg : Sum , _ctx : & mut Context < Self > ) -> Self :: Result {
msg . 0 + msg . 1
}
}
# [ actix :: main ] // <- starts the system and block until future resolves
async fn main ( ) {
let addr = Calculator . start ( ) ;
let res = addr . send ( Sum ( 10 , 5 ) ) . await ; // <- send message and get future for result
match res {
Ok ( result ) => println ! ( "SUM: {}" , result ) ,
_ => println ! ( "Communication to the actor has failed" ) ,
}
}俳優とのすべての通信は、 Addrオブジェクトを通過します。応答を待たずにメッセージをdo_sendことも、俳優に特定のメッセージsendこともできます。 Message特性は、メッセージの結果タイプを定義します。
ActorとHandler特性の方法が自己を受け入れ、 &mut self受け入れていることに気付いたかもしれません。したがって、俳優に何かを保管して、必要に応じて変異させることを歓迎します。
アドレスオブジェクトにはアクタータイプが必要ですが、メッセージを処理できるアクターに特定のメッセージを送信したい場合は、 Recipientインターフェイスを使用できます。 Recipientを使用する新しい俳優を作成しましょう。
use actix :: prelude :: * ;
use std :: time :: Duration ;
# [ derive ( Message ) ]
# [ rtype ( result = "()" ) ]
struct Ping {
pub id : usize ,
}
// Actor definition
struct Game {
counter : usize ,
name : String ,
recipient : Recipient < Ping > ,
}
impl Actor for Game {
type Context = Context < Game > ;
}
// simple message handler for Ping message
impl Handler < Ping > for Game {
type Result = ( ) ;
fn handle ( & mut self , msg : Ping , ctx : & mut Context < Self > ) {
self . counter += 1 ;
if self . counter > 10 {
System :: current ( ) . stop ( ) ;
} else {
println ! ( "[{0}] Ping received {1}" , self . name , msg . id ) ;
// wait 100 nanoseconds
ctx . run_later ( Duration :: new ( 0 , 100 ) , move |act , _| {
act . recipient . do_send ( Ping { id : msg . id + 1 } ) ;
} ) ;
}
}
}
fn main ( ) {
let system = System :: new ( ) ;
system . block_on ( async {
// To create a cyclic game link, we need to use a different constructor
// method to get access to its recipient before it starts.
let _game = Game :: create ( |ctx| {
// now we can get an address of the first actor and create the second actor
let addr = ctx . address ( ) ;
let addr2 = Game {
counter : 0 ,
name : String :: from ( "Game 2" ) ,
recipient : addr . recipient ( ) ,
}
. start ( ) ;
// let's start pings
addr2 . do_send ( Ping { id : 10 } ) ;
// now we can finally create first actor
Game {
counter : 0 ,
name : String :: from ( "Game 1" ) ,
recipient : addr2 . recipient ( ) ,
}
} ) ;
} ) ;
// let the actors all run until they've shut themselves down
system . run ( ) . unwrap ( ) ;
}ネットワーククライアント/サーバーサービスでより包括的な使用法を示すこのチャット例を参照してください。
機能リクエストがある場合は、問題を開くことをheしないでください。
このプロジェクトは、いずれかの下でライセンスされています
あなたのオプションで。
Actix Repoへの貢献は、貢献者契約の条件の下で編成されています。 Actixチームは、その行動規範を支持するために介入することを約束します。