blob: 174271b6195877bd089a307da2322d006976634a [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
pwyckoff99b000b2008-04-03 19:30:55 +000029/**
30 * Base Facebook service implementation in C++.
31 *
32 * @author Mark Slee <mcslee@facebook.com>
33 */
34class FacebookBase : virtual public FacebookServiceIf {
35 protected:
David Reiss0b7d6fa2009-02-07 02:36:35 +000036 FacebookBase(std::string name);
pwyckoff99b000b2008-04-03 19:30:55 +000037 virtual ~FacebookBase() {}
38
39 public:
40 void getName(std::string& _return);
41 virtual void getVersion(std::string& _return) { _return = ""; }
42
43 virtual fb_status getStatus() = 0;
44 virtual void getStatusDetails(std::string& _return) { _return = ""; }
45
46 void setOption(const std::string& key, const std::string& value);
47 void getOption(std::string& _return, const std::string& key);
48 void getOptions(std::map<std::string, std::string> & _return);
49
50 int64_t aliveSince();
51
pwyckoff99b000b2008-04-03 19:30:55 +000052 virtual void reinitialize() {}
53
54 virtual void shutdown() {
55 if (server_.get() != NULL) {
56 server_->stop();
57 }
58 }
59
60 int64_t incrementCounter(const std::string& key, int64_t amount = 1);
61 int64_t setCounter(const std::string& key, int64_t value);
62
63 void getCounters(std::map<std::string, int64_t>& _return);
64 int64_t getCounter(const std::string& key);
65
66 /**
67 * Set server handle for shutdown method
68 */
69 void setServer(boost::shared_ptr<TServer> server) {
70 server_ = server;
71 }
72
73 void getCpuProfile(std::string& _return, int32_t durSecs) { _return = ""; }
74
75 private:
76
77 std::string name_;
pwyckoff99b000b2008-04-03 19:30:55 +000078 int64_t aliveSince_;
79
80 std::map<std::string, std::string> options_;
81 Mutex optionsLock_;
82
83 ReadWriteCounterMap counters_;
84
85 boost::shared_ptr<TServer> server_;
86
87};
88
89}} // facebook::tb303
90
91#endif // _FACEBOOK_TB303_FACEBOOKBASE_H_