11 #include "stg/SMUX-PDUs.h"
12 #include "stg/ObjectSyntax.h"
14 #include "stg/plugin.h"
15 #include "stg/module_settings.h"
16 #include "stg/notifer.h"
17 #include "stg/noncopyable.h"
18 #include "stg/logger.h"
37 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
38 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
39 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
40 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
42 using UserPtr = STG::User*;
43 //-----------------------------------------------------------------------------
47 virtual ~SMUX_SETTINGS() {}
48 const std::string & GetStrError() const { return errorStr; }
49 int ParseSettings(const STG::ModuleSettings & s);
51 uint32_t GetIP() const { return ip; }
52 uint16_t GetPort() const { return port; }
53 const std::string GetPassword() const { return password; }
56 mutable std::string errorStr;
62 //-----------------------------------------------------------------------------
63 class CHG_AFTER_NOTIFIER : public STG::PropertyNotifierBase<std::string> {
65 CHG_AFTER_NOTIFIER(SMUX & s, const UserPtr & u)
66 : STG::PropertyNotifierBase<std::string>(),
67 smux(s), userPtr(u) {}
68 CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER & rvalue)
69 : STG::PropertyNotifierBase<std::string>(),
70 smux(rvalue.smux), userPtr(rvalue.userPtr) {}
71 void Notify(const std::string &, const std::string &);
73 UserPtr GetUserPtr() const { return userPtr; }
76 CHG_AFTER_NOTIFIER & operator=(const CHG_AFTER_NOTIFIER & rvalue);
80 //-----------------------------------------------------------------------------
81 class ADD_DEL_TARIFF_NOTIFIER : public STG::NotifierBase<STG::TariffData> {
83 explicit ADD_DEL_TARIFF_NOTIFIER(SMUX & s)
84 : STG::NotifierBase<STG::TariffData>(), smux(s) {}
85 void Notify(const STG::TariffData &);
90 //-----------------------------------------------------------------------------
91 class ADD_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
93 explicit ADD_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<STG::User*>(), smux(s) {}
94 void Notify(const UserPtr &);
99 //-----------------------------------------------------------------------------
100 class DEL_USER_NOTIFIER : public STG::NotifierBase<UserPtr> {
102 explicit DEL_USER_NOTIFIER(SMUX & s) : STG::NotifierBase<UserPtr>(), smux(s) {}
103 void Notify(const UserPtr &);
108 //-----------------------------------------------------------------------------
109 class SMUX : public STG::Plugin {
114 void SetUsers(STG::Users * u) { users = u; }
115 void SetTariffs(STG::Tariffs * t) { tariffs = t; }
116 void SetAdmins(STG::Admins * a) { admins = a; }
117 void SetServices(STG::Services * s) { services = s; }
118 void SetTraffcounter(STG::TraffCounter * tc) { traffcounter = tc; }
119 void SetCorporations(STG::Corporations * c) { corporations = c; }
120 void SetSettings(const STG::ModuleSettings & s) { settings = s; }
125 int Reload(const STG::ModuleSettings & ms);
126 bool IsRunning() { return running && !stopped; }
128 const std::string & GetStrError() const { return errorStr; }
129 std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
130 uint16_t GetStartPosition() const { return 10; }
131 uint16_t GetStopPosition() const { return 10; }
135 void SetNotifier(UserPtr userPtr);
136 void UnsetNotifier(UserPtr userPtr);
139 SMUX(const SMUX & rvalue);
140 SMUX & operator=(const SMUX & rvalue);
142 static void * Runner(void * d);
147 bool DispatchPDUs(const SMUX_PDUs_t * pdus);
149 bool CloseHandler(const SMUX_PDUs_t * pdus);
150 bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
151 bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
152 bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
154 bool GetRequestHandler(const PDUs_t * pdus);
155 bool GetNextRequestHandler(const PDUs_t * pdus);
156 bool SetRequestHandler(const PDUs_t * pdus);
159 void ResetNotifiers();
162 STG::Tariffs * tariffs;
163 STG::Admins * admins;
164 STG::Services * services;
165 STG::Corporations * corporations;
166 STG::TraffCounter * traffcounter;
168 mutable std::string errorStr;
169 SMUX_SETTINGS smuxSettings;
170 STG::ModuleSettings settings;
173 pthread_mutex_t mutex;
178 time_t lastReconnectTry;
179 unsigned reconnectTimeout;
183 SMUXHandlers smuxHandlers;
184 PDUsHandlers pdusHandlers;
188 std::list<CHG_AFTER_NOTIFIER> notifiers;
189 ADD_USER_NOTIFIER addUserNotifier;
190 DEL_USER_NOTIFIER delUserNotifier;
191 ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
193 STG::PluginLogger logger;
195 //-----------------------------------------------------------------------------
198 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
204 void ADD_DEL_TARIFF_NOTIFIER::Notify(const STG::TariffData &)
210 void ADD_USER_NOTIFIER::Notify(const UserPtr & userPtr)
212 smux.SetNotifier(userPtr);
217 void DEL_USER_NOTIFIER::Notify(const UserPtr & userPtr)
219 smux.UnsetNotifier(userPtr);