pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | #ifndef _FACEBOOK_TB303_FACEBOOKBASE_H_ |
| 8 | #define _FACEBOOK_TB303_FACEBOOKBASE_H_ 1 |
| 9 | |
| 10 | #include "FacebookService.h" |
| 11 | |
| 12 | #include "thrift/server/TServer.h" |
| 13 | #include "thrift/concurrency/Mutex.h" |
| 14 | |
| 15 | #include <time.h> |
| 16 | #include <string> |
| 17 | #include <map> |
| 18 | |
| 19 | namespace facebook { namespace fb303 { |
| 20 | |
| 21 | using facebook::thrift::concurrency::Mutex; |
| 22 | using facebook::thrift::concurrency::ReadWriteMutex; |
| 23 | using facebook::thrift::server::TServer; |
| 24 | |
| 25 | struct ReadWriteInt : ReadWriteMutex {int64_t value;}; |
| 26 | struct ReadWriteCounterMap : ReadWriteMutex, |
| 27 | std::map<std::string, ReadWriteInt> {}; |
| 28 | |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Base Facebook service implementation in C++. |
| 31 | * |
| 32 | * @author Mark Slee <mcslee@facebook.com> |
| 33 | */ |
| 34 | class FacebookBase : virtual public FacebookServiceIf { |
| 35 | protected: |
David Reiss | 0b7d6fa | 2009-02-07 02:36:35 +0000 | [diff] [blame^] | 36 | FacebookBase(std::string name); |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 37 | virtual ~FacebookBase() {} |
| 38 | |
| 39 | public: |
| 40 | void getName(std::string& _return); |
| 41 | virtual void getVersion(std::string& _return) { _return = ""; } |
| 42 | |
| 43 | virtual fb_status getStatus() = 0; |
| 44 | virtual void getStatusDetails(std::string& _return) { _return = ""; } |
| 45 | |
| 46 | void setOption(const std::string& key, const std::string& value); |
| 47 | void getOption(std::string& _return, const std::string& key); |
| 48 | void getOptions(std::map<std::string, std::string> & _return); |
| 49 | |
| 50 | int64_t aliveSince(); |
| 51 | |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 52 | virtual void reinitialize() {} |
| 53 | |
| 54 | virtual void shutdown() { |
| 55 | if (server_.get() != NULL) { |
| 56 | server_->stop(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | int64_t incrementCounter(const std::string& key, int64_t amount = 1); |
| 61 | int64_t setCounter(const std::string& key, int64_t value); |
| 62 | |
| 63 | void getCounters(std::map<std::string, int64_t>& _return); |
| 64 | int64_t getCounter(const std::string& key); |
| 65 | |
| 66 | /** |
| 67 | * Set server handle for shutdown method |
| 68 | */ |
| 69 | void setServer(boost::shared_ptr<TServer> server) { |
| 70 | server_ = server; |
| 71 | } |
| 72 | |
| 73 | void getCpuProfile(std::string& _return, int32_t durSecs) { _return = ""; } |
| 74 | |
| 75 | private: |
| 76 | |
| 77 | std::string name_; |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 78 | int64_t aliveSince_; |
| 79 | |
| 80 | std::map<std::string, std::string> options_; |
| 81 | Mutex optionsLock_; |
| 82 | |
| 83 | ReadWriteCounterMap counters_; |
| 84 | |
| 85 | boost::shared_ptr<TServer> server_; |
| 86 | |
| 87 | }; |
| 88 | |
| 89 | }} // facebook::tb303 |
| 90 | |
| 91 | #endif // _FACEBOOK_TB303_FACEBOOKBASE_H_ |