blob: 2159c95fd9db5b1a67929234898b247c51732660 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 */
pwyckoff99b000b2008-04-03 19:30:55 +000019
20#ifndef _FACEBOOK_TB303_FACEBOOKBASE_H_
21#define _FACEBOOK_TB303_FACEBOOKBASE_H_ 1
22
23#include "FacebookService.h"
24
Roger Meier49ff8b12012-04-13 09:12:31 +000025#include <thrift/server/TServer.h>
26#include <thrift/concurrency/Mutex.h>
pwyckoff99b000b2008-04-03 19:30:55 +000027
28#include <time.h>
29#include <string>
30#include <map>
31
32namespace facebook { namespace fb303 {
33
David Reiss858519a2009-02-07 02:36:50 +000034using apache::thrift::concurrency::Mutex;
35using apache::thrift::concurrency::ReadWriteMutex;
36using apache::thrift::server::TServer;
pwyckoff99b000b2008-04-03 19:30:55 +000037
38struct ReadWriteInt : ReadWriteMutex {int64_t value;};
39struct ReadWriteCounterMap : ReadWriteMutex,
40 std::map<std::string, ReadWriteInt> {};
41
pwyckoff99b000b2008-04-03 19:30:55 +000042/**
43 * Base Facebook service implementation in C++.
44 *
pwyckoff99b000b2008-04-03 19:30:55 +000045 */
46class FacebookBase : virtual public FacebookServiceIf {
47 protected:
David Reiss0b7d6fa2009-02-07 02:36:35 +000048 FacebookBase(std::string name);
pwyckoff99b000b2008-04-03 19:30:55 +000049 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
pwyckoff99b000b2008-04-03 19:30:55 +000064 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_;
pwyckoff99b000b2008-04-03 19:30:55 +000090 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_