Repository navigation
Expand file tree
/
Copy pathzzrpc_broker.cpp
More file actions
174 lines (128 loc) · 3.57 KB
/
Copy pathzzrpc_broker.cpp
File metadata and controls
174 lines (128 loc) · 3.57 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "rpc/zzrpc_broker.h"
#include "net/net_factory.h"
#include "rpc/zzrpc_ops.h"
#include <iostream>
using namespace std;
namespace zz {
zzrpc_broker_t::zzrpc_broker_t(string& host_, msg_handler_i* hook_handler_):
zzrpc_base_t(hook_handler_),
m_listen_host(host_),
m_acceptor(NULL)
{}
int zzrpc_broker_t::handle_msg(msg_t& msg_, socket_ptr_t sock_)
{
return zzrpc_base_t::handle_msg(msg_, sock_);
}
int zzrpc_broker_t::handle_broken(socket_ptr_t sock_)
{
zzrpc_base_t::handle_broken(sock_);
return 0;
}
int zzrpc_broker_t::handle_open(socket_ptr_t sock_)
{
zzrpc_base_t::handle_open(sock_);
return 0;
}
int zzrpc_broker_t::start()
{
m_acceptor = net_factory_t::listen(m_listen_host, this);
if (m_acceptor == NULL)
{
return -1 ;
}
bind_callback_with_cmd();
m_thread.create(task_binder::bind(&task_queue_t::consume,&m_tq),1);
return 0;
}
void zzrpc_broker_t::stop()
{
if (NULL != m_acceptor)
{
m_acceptor->close();
}
m_tq.stop();
m_thread.join_all();
}
/*
* zzrpc_master_broker
*/
zzrpc_master_broker_t::zzrpc_master_broker_t(string& host_, msg_handler_i* hook_handler_):
zzrpc_broker_t(host_, hook_handler_)
{
}
int zzrpc_master_broker_t::handle_rpc_svr_reg(msg_reg_svr_to_mb& msg_, socket_ptr_t sock_)
{
m_tq.produce(task_binder::bind(&zzrpc_master_broker_t::handle_rpc_svr_reg_impl,msg_,sock_,this));
return 0;
}
int zzrpc_master_broker_t::handle_rpc_svr_reg_impl(msg_reg_svr_to_mb& msg_, socket_ptr_t sock_)
{
#ifdef _DEBUG
cout<<"zzrpc_master_broker_t::handle_rpc_svr_reg_impl called!"<<endl;
cout<<msg_.m_service_name<<endl;
#endif
session_ctx_t* ctx = sock_->get_ctx<session_ctx_t>();
assert(NULL != ctx);
uint32_t new_node_id = m_id_alloctor.alloc_id();
m_svr_name_to_node_id[msg_.m_service_name] = new_node_id;
msg_reg_svr_to_mb_ret msg_ret_;
msg_ret_.m_slave_broker_host_vec = m_slave_broker_host;
msg_ret_.m_svr_node_id_allcated = new_node_id;
msg_ret_.m_bind_slave_broker_idx = -1;
if (false == m_slave_broker_host.empty() )
{
msg_ret_.m_bind_slave_broker_idx = new_node_id%m_slave_broker_host.size();
}
msg_send_tool_t::send(msg_ret_, k_rpc_reg_delegator_service_ret, sock_);
ctx->node_id = new_node_id;
ctx->node_type = k_rpc_node_delegator_service;
return 0;
}
int zzrpc_master_broker_t::handle_rpc_client_reg(msg_reg_client_to_mb& msg_, socket_ptr_t sock_)
{
#ifdef _DEBUG
cout<<"zzrpc_master_broker_t::handle_rpc_client_reg called!"<<endl;
cout<<msg_.m_client_desc<<endl;
#endif
return 0;
}
int zzrpc_master_broker_t::handle_broken(socket_ptr_t sock_)
{
zzrpc_broker_t::handle_broken(sock_);
return 0;
}
int zzrpc_master_broker_t::handle_open(socket_ptr_t sock_)
{
zzrpc_broker_t::handle_open(sock_);
return 0;
}
int zzrpc_master_broker_t::bind_callback_with_cmd()
{
m_slot_interface.add(k_rpc_reg_delegator_service,zzrpc_ops_t::gen_callback(&zzrpc_master_broker_t::handle_rpc_svr_reg, this));
m_slot_interface.add(k_rpc_reg_delegator_client,zzrpc_ops_t::gen_callback(&zzrpc_master_broker_t::handle_rpc_client_reg, this));
return 0;
}
/*
* zzrpc_slave_broker
*/
zzrpc_slave_broker_t::zzrpc_slave_broker_t(string& listen_host_, string& mb_host_, msg_handler_i* hook_handler_):
zzrpc_broker_t(listen_host_, hook_handler_),
m_master_broker_host(mb_host_)
{
}
//
//int handle_rpc_svr_reg(msg_reg_svr_to_mb&, socket_ptr_t);
//int handle_rpc_client_reg(msg_reg_client_to_mb&, socket_ptr_t);
int zzrpc_slave_broker_t::handle_broken(socket_ptr_t sock_)
{
return 0 ;
}
int zzrpc_slave_broker_t::handle_open(socket_ptr_t sock_)
{
return 0 ;
}
int zzrpc_slave_broker_t::bind_callback_with_cmd()
{
return 0 ;
}
}// namespace zz end