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