]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | */ | |
19 | ||
20 | #ifndef _FACEBOOK_TB303_FACEBOOKBASE_H_ | |
21 | #define _FACEBOOK_TB303_FACEBOOKBASE_H_ 1 | |
22 | ||
23 | #include "FacebookService.h" | |
24 | ||
25 | #include <boost/shared_ptr.hpp> | |
26 | #include <thrift/server/TServer.h> | |
27 | #include <thrift/concurrency/Mutex.h> | |
28 | ||
29 | #include <time.h> | |
30 | #include <string> | |
31 | #include <map> | |
32 | ||
33 | namespace facebook { namespace fb303 { | |
34 | ||
35 | using apache::thrift::concurrency::Mutex; | |
36 | using apache::thrift::concurrency::ReadWriteMutex; | |
37 | using apache::thrift::server::TServer; | |
38 | ||
39 | struct ReadWriteInt : ReadWriteMutex {int64_t value;}; | |
40 | struct ReadWriteCounterMap : ReadWriteMutex, | |
41 | std::map<std::string, ReadWriteInt> {}; | |
42 | ||
43 | /** | |
44 | * Base Facebook service implementation in C++. | |
45 | * | |
46 | */ | |
47 | class FacebookBase : virtual public FacebookServiceIf { | |
48 | protected: | |
49 | FacebookBase(std::string name); | |
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 | ||
65 | virtual void reinitialize() {} | |
66 | ||
67 | virtual void shutdown() { | |
68 | if (server_.get() != NULL) { | |
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_; | |
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_ |