blob: b62f4d0c7adab4b00b77d6d97e77c3fba014bc62 [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
David Reissf4335df2009-02-07 02:36:46 +000012#include "server/TServer.h"
13#include "concurrency/Mutex.h"
pwyckoff99b000b2008-04-03 19:30:55 +000014
15#include <time.h>
16#include <string>
17#include <map>
18
19namespace facebook { namespace fb303 {
20
David Reiss858519a2009-02-07 02:36:50 +000021using apache::thrift::concurrency::Mutex;
22using apache::thrift::concurrency::ReadWriteMutex;
23using apache::thrift::server::TServer;
pwyckoff99b000b2008-04-03 19:30:55 +000024
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 *
pwyckoff99b000b2008-04-03 19:30:55 +000032 */
33class FacebookBase : virtual public FacebookServiceIf {
34 protected:
David Reiss0b7d6fa2009-02-07 02:36:35 +000035 FacebookBase(std::string name);
pwyckoff99b000b2008-04-03 19:30:55 +000036 virtual ~FacebookBase() {}
37
38 public:
39 void getName(std::string& _return);
40 virtual void getVersion(std::string& _return) { _return = ""; }
41
42 virtual fb_status getStatus() = 0;
43 virtual void getStatusDetails(std::string& _return) { _return = ""; }
44
45 void setOption(const std::string& key, const std::string& value);
46 void getOption(std::string& _return, const std::string& key);
47 void getOptions(std::map<std::string, std::string> & _return);
48
49 int64_t aliveSince();
50
pwyckoff99b000b2008-04-03 19:30:55 +000051 virtual void reinitialize() {}
52
53 virtual void shutdown() {
54 if (server_.get() != NULL) {
55 server_->stop();
56 }
57 }
58
59 int64_t incrementCounter(const std::string& key, int64_t amount = 1);
60 int64_t setCounter(const std::string& key, int64_t value);
61
62 void getCounters(std::map<std::string, int64_t>& _return);
63 int64_t getCounter(const std::string& key);
64
65 /**
66 * Set server handle for shutdown method
67 */
68 void setServer(boost::shared_ptr<TServer> server) {
69 server_ = server;
70 }
71
72 void getCpuProfile(std::string& _return, int32_t durSecs) { _return = ""; }
73
74 private:
75
76 std::string name_;
pwyckoff99b000b2008-04-03 19:30:55 +000077 int64_t aliveSince_;
78
79 std::map<std::string, std::string> options_;
80 Mutex optionsLock_;
81
82 ReadWriteCounterMap counters_;
83
84 boost::shared_ptr<TServer> server_;
85
86};
87
88}} // facebook::tb303
89
90#endif // _FACEBOOK_TB303_FACEBOOKBASE_H_