-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.cpp
119 lines (102 loc) · 2.46 KB
/
client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "caf/all.hpp"
#include "caf/io/all.hpp"
#include "string"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/tuple/tuple.hpp>
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using namespace caf;
using namespace std;
typedef unsigned short UInt16;
template<typename T>string Serialize(T obj);
template<typename T>T Derialize(string obj);
class nodeInfo{
public:
nodeInfo(string nodename,float ratio):nodename_(nodename),ratio_(ratio)
{
};
nodeInfo(){};
string nodename_;
float ratio_;
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & nodename_;
ar & ratio_;
}
};
void reciever(event_based_actor* self , UInt16 port)
{
auto p = io::publish(self, port);
self->become(
[self](const string& text) -> string {
// nodeInfo node1 = Derialize<nodeInfo>(text);
//aout(self)<<"name:"<<node1.nodename_ <<"ratio :"<< node1.ratio_<<endl;
return "a";
}
);
}
void sender(event_based_actor* self,const string& host, UInt16 port)
{
auto receiver = caf::io::remote_actor(host,port);
caf::scoped_actor scoped_self;
string info = "hello";
scoped_self ->sync_send(receiver,info).await(
[=](const std::string& response) {
cout<<"同步成功"<<response<<endl;
});
aout(self)<<"send success"<<endl;
}
int main(int argc, char** argv)
{
//string host;
//UInt16 port;
UInt16 port = 8081;
// auto res = message_builder(argv + 1, argv + argc).extract_opts({
// {"host,M", "set host", host},
// {"port,m", "set port", port}
// });
// if (! res.error.empty()) {
// cerr << res.error << endl;
// return 1;
// }
// if (res.opts.count("help") > 0) {
// cout << res.helptext << endl;
// return 0;
// }
// if (! res.remainder.empty()) {
// }
auto reciever_actor = spawn(reciever,8081);
auto send_actor = spawn(sender,"127.0.0.1",port);
await_all_actors_done();
//shutdown();
//cout<<host<<" "<<port;
return 0;
}
/**
* Transform T type c++ object to string
*/
template<typename T>
string Serialize(T obj) {
stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << obj;
return ss.str();
}
/**
* Transform string to T type c++ object
*/
template<typename T>
T Derialize(string obj) {
T ret;
stringstream ss(obj);
boost::archive::text_iarchive ia(ss);
ia >> ret;
return ret;
}