blob: a5afe5bce7938f86c387628959261c2d46274784 [file] [log] [blame]
pwyckoff99b000b2008-04-03 19:30:55 +00001// 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
19namespace facebook { namespace fb303 {
20
21using facebook::thrift::concurrency::Mutex;
22using facebook::thrift::concurrency::ReadWriteMutex;
23using facebook::thrift::server::TServer;
24
25struct ReadWriteInt : ReadWriteMutex {int64_t value;};
26struct ReadWriteCounterMap : ReadWriteMutex,
27 std::map<std::string, ReadWriteInt> {};
28
29typedef void (*get_static_limref_ptr)(facebook::thrift::reflection::limited::Service &);
30
31/**
32 * Base Facebook service implementation in C++.
33 *
34 * @author Mark Slee <mcslee@facebook.com>
35 */
36class FacebookBase : virtual public FacebookServiceIf {
37 protected:
38 FacebookBase(std::string name, get_static_limref_ptr reflect_lim = NULL);
39 virtual ~FacebookBase() {}
40
41 public:
42 void getName(std::string& _return);
43 virtual void getVersion(std::string& _return) { _return = ""; }
44
45 virtual fb_status getStatus() = 0;
46 virtual void getStatusDetails(std::string& _return) { _return = ""; }
47
48 void setOption(const std::string& key, const std::string& value);
49 void getOption(std::string& _return, const std::string& key);
50 void getOptions(std::map<std::string, std::string> & _return);
51
52 int64_t aliveSince();
53
54 void getLimitedReflection(facebook::thrift::reflection::limited::Service& _return) {
55 _return = reflection_limited_;
56 }
57
58 virtual void reinitialize() {}
59
60 virtual void shutdown() {
61 if (server_.get() != NULL) {
62 server_->stop();
63 }
64 }
65
66 int64_t incrementCounter(const std::string& key, int64_t amount = 1);
67 int64_t setCounter(const std::string& key, int64_t value);
68
69 void getCounters(std::map<std::string, int64_t>& _return);
70 int64_t getCounter(const std::string& key);
71
72 /**
73 * Set server handle for shutdown method
74 */
75 void setServer(boost::shared_ptr<TServer> server) {
76 server_ = server;
77 }
78
79 void getCpuProfile(std::string& _return, int32_t durSecs) { _return = ""; }
80
81 private:
82
83 std::string name_;
84 facebook::thrift::reflection::limited::Service reflection_limited_;
85 int64_t aliveSince_;
86
87 std::map<std::string, std::string> options_;
88 Mutex optionsLock_;
89
90 ReadWriteCounterMap counters_;
91
92 boost::shared_ptr<TServer> server_;
93
94};
95
96}} // facebook::tb303
97
98#endif // _FACEBOOK_TB303_FACEBOOKBASE_H_