e). Message delayed send (not much different from the front directly on the code)
#include <iostream>#include"caf/all.hpp"#include"caf/io/all.hpp"#include<string>#include<chrono>using namespacestd;using namespacecaf;behavior Fun (Event_based_actor*Self ) { return{[Self] (Const string&str) {aout (self)<<str<<Endl; Auto T2=std::chrono::system_clock::to_time_t (Std::chrono::system_clock::now ()); cout<<"dalay Time:"<<t2<<Endl; Self-quit (); } };}voidFUN1 (event_based_actor*Self , actor Buddy) { Self->delayed_send (Buddy, Std::chrono::seconds (1),"hi!");}intMain () {Auto Actor1=spawn (fun); Auto T1=std::chrono::system_clock::to_time_t (Std::chrono::system_clock::now ()); cout<<"before Delayed_send:"<<t1<<Endl; Auto Actor2=spawn (Fun1,actor1); Caf::await_all_actors_done (); Shutdown (); return 0;}
Result is
f). Message forward (message forwarding) forward.
Put code on it.
#include <iostream>#include"caf/all.hpp"#include"caf/io/all.hpp"#include<string>#include<chrono>using namespacestd;using namespacecaf;behavior fun2 (event_based_actor*Self ) { return{[Self] (Const string&str) {aout (self)<<"C Get message return to A"<<Endl; Aout (self)<<"C ' s address is:"<<self->address () <<Endl; return "Hello, A"; Self-quit (); } };} Behavior Fun1 (Event_based_actor* Self,ConstActor &Buddy) { return { [=](Const string&str) {aout (self)<<"B Get message forward to C"<<Endl; Self-forward_to (Buddy); Self-quit (); } };}voidFun (event_based_actor* Self,ConstActor &Buddy) { Self->sync_send (Buddy,"hi!"). then ([=](Const string&str) {aout (self)<<str<<Endl; Aout (self)<<"A think Last_sender is:"<<self->last_sender () <<Endl; } ); Aout (self)<<"A Send to b!"<<Endl;}intMain () {Auto Actorc=spawn (FUN2); Auto Actorb=spawn (FUN1,ACTORC); Auto Actora=spawn (Fun,actorb); Caf::await_all_actors_done (); Shutdown (); return 0;}
Result is
CAF (c + + actor Framework) uses essays (deferred send, message forwarding, message priority) (iv)