David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 19 | |
| 20 | #ifndef _FACEBOOK_TB303_FACEBOOKBASE_H_ |
| 21 | #define _FACEBOOK_TB303_FACEBOOKBASE_H_ 1 |
| 22 | |
| 23 | #include "FacebookService.h" |
| 24 | |
James E. King, III | 2590988 | 2017-12-09 12:21:37 -0500 | [diff] [blame] | 25 | #include <boost/shared_ptr.hpp> |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 26 | #include <thrift/server/TServer.h> |
| 27 | #include <thrift/concurrency/Mutex.h> |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 28 | |
| 29 | #include <time.h> |
| 30 | #include <string> |
| 31 | #include <map> |
| 32 | |
| 33 | namespace facebook { namespace fb303 { |
| 34 | |
David Reiss | 858519a | 2009-02-07 02:36:50 +0000 | [diff] [blame] | 35 | using apache::thrift::concurrency::Mutex; |
| 36 | using apache::thrift::concurrency::ReadWriteMutex; |
| 37 | using apache::thrift::server::TServer; |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 38 | |
| 39 | struct ReadWriteInt : ReadWriteMutex {int64_t value;}; |
| 40 | struct ReadWriteCounterMap : ReadWriteMutex, |
| 41 | std::map<std::string, ReadWriteInt> {}; |
| 42 | |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Base Facebook service implementation in C++. |
| 45 | * |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 46 | */ |
| 47 | class FacebookBase : virtual public FacebookServiceIf { |
| 48 | protected: |
David Reiss | 0b7d6fa | 2009-02-07 02:36:35 +0000 | [diff] [blame] | 49 | FacebookBase(std::string name); |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 50 | virtual ~FacebookBase() {} |
| 51 | |
| 52 | public: |
| 53 | void getName(std::string& _return); |
| 54 | virtual void getVersion(std::string& _return) { _return = ""; } |
| 55 | |
| 56 | virtual fb_status getStatus() = 0; |
| 57 | virtual void getStatusDetails(std::string& _return) { _return = ""; } |
| 58 | |
| 59 | void setOption(const std::string& key, const std::string& value); |
| 60 | void getOption(std::string& _return, const std::string& key); |
| 61 | void getOptions(std::map<std::string, std::string> & _return); |
| 62 | |
| 63 | int64_t aliveSince(); |
| 64 | |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 65 | virtual void reinitialize() {} |
| 66 | |
| 67 | virtual void shutdown() { |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 68 | if (server_.get() != nullptr) { |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 69 | server_->stop(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int64_t incrementCounter(const std::string& key, int64_t amount = 1); |
| 74 | int64_t setCounter(const std::string& key, int64_t value); |
| 75 | |
| 76 | void getCounters(std::map<std::string, int64_t>& _return); |
| 77 | int64_t getCounter(const std::string& key); |
| 78 | |
| 79 | /** |
| 80 | * Set server handle for shutdown method |
| 81 | */ |
| 82 | void setServer(boost::shared_ptr<TServer> server) { |
| 83 | server_ = server; |
| 84 | } |
| 85 | |
| 86 | void getCpuProfile(std::string& _return, int32_t durSecs) { _return = ""; } |
| 87 | |
| 88 | private: |
| 89 | |
| 90 | std::string name_; |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 91 | int64_t aliveSince_; |
| 92 | |
| 93 | std::map<std::string, std::string> options_; |
| 94 | Mutex optionsLock_; |
| 95 | |
| 96 | ReadWriteCounterMap counters_; |
| 97 | |
| 98 | boost::shared_ptr<TServer> server_; |
| 99 | |
| 100 | }; |
| 101 | |
| 102 | }} // facebook::tb303 |
| 103 | |
| 104 | #endif // _FACEBOOK_TB303_FACEBOOKBASE_H_ |