码迷,mamicode.com
首页 > 编程语言 > 详细

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)

时间:2016-03-06 15:35:08      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

e). 消息延迟发送(和前面没太大区别直接上代码)

#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include <string>
#include <chrono>
using namespace std;
using namespace caf;


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();
            }
    };
}

void fun1(event_based_actor* self, actor buddy){
    self->delayed_send(buddy, std::chrono::seconds(1), "hi!");
}

int main(){
    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;
}

结果为

技术分享

 f). 消息前转(消息转发)forward.

贴上代码

#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include <string>
#include <chrono>
using namespace std;
using namespace caf;

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, const actor &buddy){
    return {
        [=](const string& str){
            aout(self)<<"B get message forward to C"<<endl;
            self->forward_to(buddy);
            self->quit();
         }
    };
}

void fun(event_based_actor* self, const actor &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;
}

int main(){
    auto actorC = spawn(fun2);
    auto actorB = spawn(fun1,actorC);
    auto actorA = spawn(fun,actorB);
    caf::await_all_actors_done();
    shutdown();
    return 0;
}

结果为

技术分享

CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)

标签:

原文地址:http://www.cnblogs.com/zhejiangxiaomai/p/5247529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!