|
| 1 | +// Copyright (c) 2014 Baidu.com, Inc. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +// |
| 5 | +// Author: qinzuoyan01@baidu.com (Qin Zuoyan) |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <signal.h> |
| 9 | +#include <unistd.h> |
| 10 | +#include <fstream> |
| 11 | +#include <vector> |
| 12 | +#include <set> |
| 13 | + |
| 14 | +#include <sofa/pbrpc/pbrpc.h> |
| 15 | +#include "echo_service.pb.h" |
| 16 | + |
| 17 | +class ReloadableAddressProvider : public sofa::pbrpc::RpcChannel::AddressProvider |
| 18 | +{ |
| 19 | +public: |
| 20 | + typedef std::vector<sofa::pbrpc::RpcChannel::EventHandler*> EventHandlerVector; |
| 21 | +public: |
| 22 | + ReloadableAddressProvider(const std::string& addr_file) : _addr_file(addr_file) { |
| 23 | + Reload(); |
| 24 | + } |
| 25 | + |
| 26 | + virtual ~ReloadableAddressProvider() { |
| 27 | + sofa::pbrpc::ScopedLocker<sofa::pbrpc::MutexLock> _(_lock); |
| 28 | + for (EventHandlerVector::iterator it = _handler_list.begin(); |
| 29 | + it != _handler_list.end(); ++it) { |
| 30 | + delete *it; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + virtual void GetInitAddress(std::vector<std::string>* address_list) { |
| 35 | + sofa::pbrpc::ScopedLocker<sofa::pbrpc::MutexLock> _(_lock); |
| 36 | + address_list->assign(_addr_set.begin(), _addr_set.end()); |
| 37 | + } |
| 38 | + |
| 39 | + virtual bool RegisterEventHandler(sofa::pbrpc::RpcChannel::EventHandler* event_handler) { |
| 40 | + sofa::pbrpc::ScopedLocker<sofa::pbrpc::MutexLock> _(_lock); |
| 41 | + _handler_list.push_back(event_handler); |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + void Reload() { |
| 46 | + sofa::pbrpc::ScopedLocker<sofa::pbrpc::MutexLock> _(_lock); |
| 47 | + SLOG(NOTICE, "start reloading address list from file \"%s\"", _addr_file.c_str()); |
| 48 | + // open file |
| 49 | + std::ifstream ifs(_addr_file.c_str(), std::ifstream::in); |
| 50 | + if (!ifs.good()) { |
| 51 | + SLOG(ERROR, "open address list file \"%s\" fail", _addr_file.c_str()); |
| 52 | + return; |
| 53 | + } |
| 54 | + // read new addresses |
| 55 | + std::set<std::string> new_addr_set; |
| 56 | + std::string addr; |
| 57 | + while (std::getline(ifs, addr)) { |
| 58 | + if (!addr.empty()) { |
| 59 | + new_addr_set.insert(addr); |
| 60 | + } |
| 61 | + } |
| 62 | + // make diff |
| 63 | + std::vector<std::string> add_list; |
| 64 | + std::vector<std::string> remove_list; |
| 65 | + std::set<std::string>::iterator old_it = _addr_set.begin(); |
| 66 | + std::set<std::string>::iterator old_end = _addr_set.end(); |
| 67 | + std::set<std::string>::iterator new_it = new_addr_set.begin(); |
| 68 | + std::set<std::string>::iterator new_end = new_addr_set.end(); |
| 69 | + while (old_it != old_end && new_it != new_end) { |
| 70 | + if (*old_it == *new_it) { |
| 71 | + // keep |
| 72 | + ++old_it; |
| 73 | + ++new_it; |
| 74 | + } |
| 75 | + else if (*old_it < *new_it) { |
| 76 | + // remove |
| 77 | + remove_list.push_back(*old_it); |
| 78 | + ++old_it; |
| 79 | + } |
| 80 | + else { |
| 81 | + // add |
| 82 | + add_list.push_back(*new_it); |
| 83 | + ++new_it; |
| 84 | + } |
| 85 | + } |
| 86 | + if (old_it != old_end) { |
| 87 | + remove_list.insert(remove_list.end(), old_it, old_end); |
| 88 | + } |
| 89 | + if (new_it != new_end) { |
| 90 | + add_list.insert(add_list.end(), new_it, new_end); |
| 91 | + } |
| 92 | + // notice handler |
| 93 | + if (!add_list.empty() || !remove_list.empty()) { |
| 94 | + for (EventHandlerVector::iterator it = _handler_list.begin(); |
| 95 | + it != _handler_list.end(); ++it) { |
| 96 | + if (!add_list.empty()) { |
| 97 | + (*it)->OnAddressAdded(add_list); |
| 98 | + } |
| 99 | + if (!remove_list.empty()) { |
| 100 | + (*it)->OnAddressRemoved(remove_list); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + // update _addr_set |
| 105 | + _addr_set = new_addr_set; |
| 106 | + } |
| 107 | + |
| 108 | +private: |
| 109 | + std::string _addr_file; |
| 110 | + sofa::pbrpc::MutexLock _lock; |
| 111 | + std::set<std::string> _addr_set; |
| 112 | + EventHandlerVector _handler_list; |
| 113 | +}; |
| 114 | + |
| 115 | +static ReloadableAddressProvider* g_address_provider; |
| 116 | + |
| 117 | +static void sigcatcher(int sig) |
| 118 | +{ |
| 119 | + SLOG(NOTICE, "signal catched: %d", sig); |
| 120 | + if (g_address_provider) { |
| 121 | + g_address_provider->Reload(); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +int main(int argc, char** argv) |
| 126 | +{ |
| 127 | + SOFA_PBRPC_SET_LOG_LEVEL(NOTICE); |
| 128 | + |
| 129 | + if (argc < 2) { |
| 130 | + fprintf(stderr, "USAGE: %s <address-list-file>\n", argv[0]); |
| 131 | + return EXIT_FAILURE; |
| 132 | + } |
| 133 | + |
| 134 | + std::string addr_file = argv[1]; |
| 135 | + g_address_provider = new ReloadableAddressProvider(addr_file); |
| 136 | + |
| 137 | + signal(SIGTERM, &sigcatcher); |
| 138 | + |
| 139 | + // Define an rpc client. |
| 140 | + sofa::pbrpc::RpcClientOptions client_options; |
| 141 | + sofa::pbrpc::RpcClient* rpc_client = new sofa::pbrpc::RpcClient(client_options); |
| 142 | + |
| 143 | + // Define an rpc channel. |
| 144 | + sofa::pbrpc::RpcChannelOptions channel_options; |
| 145 | + sofa::pbrpc::RpcChannel* rpc_channel = |
| 146 | + new sofa::pbrpc::RpcChannel(rpc_client, g_address_provider, channel_options); |
| 147 | + |
| 148 | + // Define an rpc stub. |
| 149 | + sofa::pbrpc::test::EchoServer_Stub* stub = |
| 150 | + new sofa::pbrpc::test::EchoServer_Stub(rpc_channel); |
| 151 | + |
| 152 | + while (true) { |
| 153 | + // Prepare parameters. |
| 154 | + sofa::pbrpc::RpcController* cntl = new sofa::pbrpc::RpcController(); |
| 155 | + cntl->SetTimeout(3000); |
| 156 | + sofa::pbrpc::test::EchoRequest* request = new sofa::pbrpc::test::EchoRequest(); |
| 157 | + request->set_message("Hello from qinzuoyan01"); |
| 158 | + sofa::pbrpc::test::EchoResponse* response = new sofa::pbrpc::test::EchoResponse(); |
| 159 | + |
| 160 | + // Sync call. |
| 161 | + stub->Echo(cntl, request, response, NULL); |
| 162 | + |
| 163 | + // Check if the request has been sent. |
| 164 | + // If has been sent, then can get the sent bytes. |
| 165 | + SLOG(NOTICE, "RemoteAddress=%s", cntl->RemoteAddress().c_str()); |
| 166 | + SLOG(NOTICE, "IsRequestSent=%s", cntl->IsRequestSent() ? "true" : "false"); |
| 167 | + if (cntl->IsRequestSent()) { |
| 168 | + SLOG(NOTICE, "LocalAddress=%s", cntl->LocalAddress().c_str()); |
| 169 | + SLOG(NOTICE, "SentBytes=%ld", cntl->SentBytes()); |
| 170 | + } |
| 171 | + |
| 172 | + // Check if failed. |
| 173 | + if (cntl->Failed()) { |
| 174 | + SLOG(ERROR, "request failed: %s", cntl->ErrorText().c_str()); |
| 175 | + } |
| 176 | + else { |
| 177 | + SLOG(NOTICE, "request succeed: %s", response->message().c_str()); |
| 178 | + } |
| 179 | + |
| 180 | + // Destroy objects. |
| 181 | + delete cntl; |
| 182 | + delete request; |
| 183 | + delete response; |
| 184 | + |
| 185 | + sleep(1); |
| 186 | + } |
| 187 | + |
| 188 | + delete stub; |
| 189 | + delete rpc_channel; |
| 190 | + delete rpc_client; |
| 191 | + delete g_address_provider; |
| 192 | + g_address_provider = NULL; |
| 193 | + |
| 194 | + return EXIT_SUCCESS; |
| 195 | +} |
| 196 | + |
| 197 | +/* vim: set ts=4 sw=4 sts=4 tw=100 */ |
0 commit comments