]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net> | |
7 | * Portions Copyright (C) 2013 CohortFS, LLC | |
8 | * | |
9 | * This is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License version 2.1, as published by the Free Software | |
12 | * Foundation. See file COPYING. | |
13 | * | |
14 | */ | |
15 | ||
16 | #ifndef SIMPLE_POLICY_MESSENGER_H | |
17 | #define SIMPLE_POLICY_MESSENGER_H | |
18 | ||
19 | #include "Messenger.h" | |
20 | ||
21 | class SimplePolicyMessenger : public Messenger | |
22 | { | |
23 | private: | |
24 | /// lock protecting policy | |
25 | Mutex policy_lock; | |
26 | /// the default Policy we use for Pipes | |
27 | Policy default_policy; | |
28 | /// map specifying different Policies for specific peer types | |
29 | map<int, Policy> policy_map; // entity_name_t::type -> Policy | |
30 | ||
31 | public: | |
32 | ||
33 | SimplePolicyMessenger(CephContext *cct, entity_name_t name, | |
34 | string mname, uint64_t _nonce) | |
35 | : Messenger(cct, name), | |
36 | policy_lock("SimplePolicyMessenger::policy_lock") | |
37 | { | |
38 | } | |
39 | ||
40 | /** | |
41 | * Get the Policy associated with a type of peer. | |
42 | * @param t The peer type to get the default policy for. | |
43 | * | |
44 | * @return A const Policy reference. | |
45 | */ | |
46 | Policy get_policy(int t) override { | |
47 | Mutex::Locker l(policy_lock); | |
48 | map<int, Policy>::iterator iter = | |
49 | policy_map.find(t); | |
50 | if (iter != policy_map.end()) | |
51 | return iter->second; | |
52 | else | |
53 | return default_policy; | |
54 | } | |
55 | ||
56 | Policy get_default_policy() override { | |
57 | Mutex::Locker l(policy_lock); | |
58 | return default_policy; | |
59 | } | |
60 | ||
61 | /** | |
62 | * Set a policy which is applied to all peers who do not have a type-specific | |
63 | * Policy. | |
64 | * This is an init-time function and cannot be called after calling | |
65 | * start() or bind(). | |
66 | * | |
67 | * @param p The Policy to apply. | |
68 | */ | |
69 | void set_default_policy(Policy p) override { | |
70 | Mutex::Locker l(policy_lock); | |
71 | default_policy = p; | |
72 | } | |
73 | /** | |
74 | * Set a policy which is applied to all peers of the given type. | |
75 | * This is an init-time function and cannot be called after calling | |
76 | * start() or bind(). | |
77 | * | |
78 | * @param type The peer type this policy applies to. | |
79 | * @param p The policy to apply. | |
80 | */ | |
81 | void set_policy(int type, Policy p) override { | |
82 | Mutex::Locker l(policy_lock); | |
83 | policy_map[type] = p; | |
84 | } | |
85 | ||
86 | /** | |
87 | * Set a Throttler which is applied to all Messages from the given | |
88 | * type of peer. | |
89 | * This is an init-time function and cannot be called after calling | |
90 | * start() or bind(). | |
91 | * | |
92 | * @param type The peer type this Throttler will apply to. | |
93 | * @param t The Throttler to apply. SimpleMessenger does not take | |
94 | * ownership of this pointer, but you must not destroy it before | |
95 | * you destroy SimpleMessenger. | |
96 | */ | |
97 | void set_policy_throttlers(int type, | |
98 | Throttle *byte_throttle, | |
99 | Throttle *msg_throttle) override { | |
100 | Mutex::Locker l(policy_lock); | |
101 | map<int, Policy>::iterator iter = | |
102 | policy_map.find(type); | |
103 | if (iter != policy_map.end()) { | |
104 | iter->second.throttler_bytes = byte_throttle; | |
105 | iter->second.throttler_messages = msg_throttle; | |
106 | } else { | |
107 | default_policy.throttler_bytes = byte_throttle; | |
108 | default_policy.throttler_messages = msg_throttle; | |
109 | } | |
110 | } | |
111 | ||
112 | }; /* SimplePolicyMessenger */ | |
113 | ||
114 | #endif /* SIMPLE_POLICY_MESSENGER_H */ |