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 | |
David Reiss | f4335df | 2009-02-07 02:36:46 +0000 | [diff] [blame] | 12 | #include "server/TServer.h" |
| 13 | #include "concurrency/Mutex.h" |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 14 | |
| 15 | #include <time.h> |
| 16 | #include <string> |
| 17 | #include <map> |
| 18 | |
| 19 | namespace facebook { namespace fb303 { |
| 20 | |
David Reiss | 858519a | 2009-02-07 02:36:50 +0000 | [diff] [blame] | 21 | using apache::thrift::concurrency::Mutex; |
| 22 | using apache::thrift::concurrency::ReadWriteMutex; |
| 23 | using apache::thrift::server::TServer; |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 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 | * |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 32 | */ |
| 33 | class FacebookBase : virtual public FacebookServiceIf { |
| 34 | protected: |
David Reiss | 0b7d6fa | 2009-02-07 02:36:35 +0000 | [diff] [blame] | 35 | FacebookBase(std::string name); |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 36 | virtual ~FacebookBase() {} |
| 37 | |
| 38 | public: |
| 39 | void getName(std::string& _return); |
| 40 | virtual void getVersion(std::string& _return) { _return = ""; } |
| 41 | |
| 42 | virtual fb_status getStatus() = 0; |
| 43 | virtual void getStatusDetails(std::string& _return) { _return = ""; } |
| 44 | |
| 45 | void setOption(const std::string& key, const std::string& value); |
| 46 | void getOption(std::string& _return, const std::string& key); |
| 47 | void getOptions(std::map<std::string, std::string> & _return); |
| 48 | |
| 49 | int64_t aliveSince(); |
| 50 | |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 51 | virtual void reinitialize() {} |
| 52 | |
| 53 | virtual void shutdown() { |
| 54 | if (server_.get() != NULL) { |
| 55 | server_->stop(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | int64_t incrementCounter(const std::string& key, int64_t amount = 1); |
| 60 | int64_t setCounter(const std::string& key, int64_t value); |
| 61 | |
| 62 | void getCounters(std::map<std::string, int64_t>& _return); |
| 63 | int64_t getCounter(const std::string& key); |
| 64 | |
| 65 | /** |
| 66 | * Set server handle for shutdown method |
| 67 | */ |
| 68 | void setServer(boost::shared_ptr<TServer> server) { |
| 69 | server_ = server; |
| 70 | } |
| 71 | |
| 72 | void getCpuProfile(std::string& _return, int32_t durSecs) { _return = ""; } |
| 73 | |
| 74 | private: |
| 75 | |
| 76 | std::string name_; |
pwyckoff | 99b000b | 2008-04-03 19:30:55 +0000 | [diff] [blame] | 77 | int64_t aliveSince_; |
| 78 | |
| 79 | std::map<std::string, std::string> options_; |
| 80 | Mutex optionsLock_; |
| 81 | |
| 82 | ReadWriteCounterMap counters_; |
| 83 | |
| 84 | boost::shared_ptr<TServer> server_; |
| 85 | |
| 86 | }; |
| 87 | |
| 88 | }} // facebook::tb303 |
| 89 | |
| 90 | #endif // _FACEBOOK_TB303_FACEBOOKBASE_H_ |