int main() { Server server { ServerId("s1"), Interfaces { Interface { InterfaceId("i1"), Ports { { PortId("p11"), PortName("Jolly") }, { PortId("p12"), PortName("Billy") } } }, Interface { InterfaceId("i2"), Ports { { PortId("p21"), PortName("Bimbo") }, { PortId("p22"), PortName("Luffy") } } } } }; auto interfaces = server.get<Interfaces>(); std::cout << "InterfaceId: " << interfaces.at(0).get<InterfaceId>() << std::endl; auto ports = interfaces.at(0).get<Ports>(); std::cout << "Port name: " << ports.at(0).get<PortName>() << std::endl; Handy<ServerId, InterfaceId, PortId> handy(ServerId("s1"), InterfaceId("i1"), PortId("p1")); handy.get<ServerId>() = ServerId{"s2"}; handy.get<PortId>() = PortId {"p2"}; auto record = MakeRecord(ServerId("s1"), PortId("p2")); std::cout << Get<ServerId>(record) << std::endl; std::cout << Get<ServerId>(handy) << std::endl; std::cout << handy.get<PortId>() << std::endl; std::cout << handy.get(PortId {}) << std::endl; handy.get(PortId()) = PortId("Port2"); std::cout << handy.get(PortId {}) << std::endl; }
void ChannelBalancer::RemoveAndDestroyChannel(SelectiveChannel::ChannelHandle handle) { if (!RemoveServer(ServerId(handle))) { return; } SocketUniquePtr ptr; const int rc = Socket::AddressFailedAsWell(handle, &ptr); if (rc >= 0) { SubChannel* sub = static_cast<SubChannel*>(ptr->user()); { BAIDU_SCOPED_LOCK(_mutex); CHECK_EQ(1UL, _chan_map.erase(sub->chan)); } { SocketUniquePtr ptr2(ptr.get()); // Dereference. } if (rc == 0) { ptr->ReleaseAdditionalReference(); } } }
int ChannelBalancer::AddChannel(ChannelBase* sub_channel, SelectiveChannel::ChannelHandle* handle) { if (NULL == sub_channel) { LOG(ERROR) << "Parameter[sub_channel] is NULL"; return -1; } BAIDU_SCOPED_LOCK(_mutex); if (_chan_map.find(sub_channel) != _chan_map.end()) { LOG(ERROR) << "Duplicated sub_channel=" << sub_channel; return -1; } SubChannel* sub_chan = new (std::nothrow) SubChannel; if (sub_chan == NULL) { LOG(FATAL) << "Fail to to new SubChannel"; return -1; } sub_chan->chan = sub_channel; SocketId sock_id; SocketOptions options; options.user = sub_chan; options.health_check_interval_s = FLAGS_channel_check_interval; if (Socket::Create(options, &sock_id) != 0) { delete sub_chan; LOG(ERROR) << "Fail to create fake socket for sub channel"; return -1; } SocketUniquePtr ptr; CHECK_EQ(0, Socket::Address(sock_id, &ptr)); if (!AddServer(ServerId(sock_id))) { LOG(ERROR) << "Duplicated sub_channel=" << sub_channel; // sub_chan will be deleted when the socket is recycled. ptr->SetFailed(); return -1; } _chan_map[sub_channel]= ptr.release(); // Add reference. if (handle) { *handle = sock_id; } return 0; }