10 #include "stg/SMUX-PDUs.h"
11 #include "stg/ObjectSyntax.h"
13 #include "stg/os_int.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"
24 extern "C" PLUGIN * GetPlugin();
35 typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus);
36 typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus);
37 typedef std::map<SMUX_PDUs_PR, SMUXPacketHandler> SMUXHandlers;
38 typedef std::map<PDUs_PR, PDUsHandler> PDUsHandlers;
39 //-----------------------------------------------------------------------------
43 virtual ~SMUX_SETTINGS() {}
44 const std::string & GetStrError() const { return errorStr; }
45 int ParseSettings(const MODULE_SETTINGS & s);
47 uint32_t GetIP() const { return ip; }
48 uint16_t GetPort() const { return port; }
49 const std::string GetPassword() const { return password; }
52 mutable std::string errorStr;
58 //-----------------------------------------------------------------------------
59 class CHG_AFTER_NOTIFIER : public PROPERTY_NOTIFIER_BASE<std::string> {
61 CHG_AFTER_NOTIFIER(SMUX & s, const USER_PTR & u)
62 : PROPERTY_NOTIFIER_BASE<std::string>(),
63 smux(s), userPtr(u) {}
64 CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER & rvalue)
65 : PROPERTY_NOTIFIER_BASE<std::string>(),
66 smux(rvalue.smux), userPtr(rvalue.userPtr) {}
67 void Notify(const std::string &, const std::string &);
69 USER_PTR GetUserPtr() const { return userPtr; }
72 CHG_AFTER_NOTIFIER & operator=(const CHG_AFTER_NOTIFIER & rvalue);
76 //-----------------------------------------------------------------------------
77 class ADD_DEL_TARIFF_NOTIFIER : public NOTIFIER_BASE<TARIFF_DATA>, private NONCOPYABLE {
79 ADD_DEL_TARIFF_NOTIFIER(SMUX & s)
80 : NOTIFIER_BASE<TARIFF_DATA>(), smux(s) {}
81 void Notify(const TARIFF_DATA &);
86 //-----------------------------------------------------------------------------
87 class ADD_USER_NOTIFIER : public NOTIFIER_BASE<USER_PTR>, private NONCOPYABLE {
89 ADD_USER_NOTIFIER(SMUX & s) : NOTIFIER_BASE<USER_PTR>(), smux(s) {}
90 void Notify(const USER_PTR &);
95 //-----------------------------------------------------------------------------
96 class DEL_USER_NOTIFIER : public NOTIFIER_BASE<USER_PTR>, private NONCOPYABLE {
98 DEL_USER_NOTIFIER(SMUX & s) : NOTIFIER_BASE<USER_PTR>(), smux(s) {}
99 void Notify(const USER_PTR &);
104 //-----------------------------------------------------------------------------
105 class SMUX : public PLUGIN {
110 void SetUsers(USERS * u) { users = u; }
111 void SetTariffs(TARIFFS * t) { tariffs = t; }
112 void SetAdmins(ADMINS * a) { admins = a; }
113 void SetServices(SERVICES * s) { services = s; }
114 void SetTraffcounter(TRAFFCOUNTER * tc) { traffcounter = tc; }
115 void SetCorporations(CORPORATIONS * c) { corporations = c; }
116 void SetSettings(const MODULE_SETTINGS & s) { settings = s; }
122 bool IsRunning() { return running && !stopped; }
124 const std::string & GetStrError() const { return errorStr; }
125 const std::string GetVersion() const { return "Stg SMUX Plugin 1.1"; }
126 uint16_t GetStartPosition() const { return 10; }
127 uint16_t GetStopPosition() const { return 10; }
131 void SetNotifier(USER_PTR userPtr);
132 void UnsetNotifier(USER_PTR userPtr);
135 SMUX(const SMUX & rvalue);
136 SMUX & operator=(const SMUX & rvalue);
138 static void * Runner(void * d);
143 bool DispatchPDUs(const SMUX_PDUs_t * pdus);
145 bool CloseHandler(const SMUX_PDUs_t * pdus);
146 bool RegisterResponseHandler(const SMUX_PDUs_t * pdus);
147 bool PDUsRequestHandler(const SMUX_PDUs_t * pdus);
148 bool CommitOrRollbackHandler(const SMUX_PDUs_t * pdus);
150 bool GetRequestHandler(const PDUs_t * pdus);
151 bool GetNextRequestHandler(const PDUs_t * pdus);
152 bool SetRequestHandler(const PDUs_t * pdus);
155 void ResetNotifiers();
161 CORPORATIONS * corporations;
162 TRAFFCOUNTER * traffcounter;
164 mutable std::string errorStr;
165 SMUX_SETTINGS smuxSettings;
166 MODULE_SETTINGS settings;
169 pthread_mutex_t mutex;
174 time_t lastReconnectTry;
175 unsigned reconnectTimeout;
179 SMUXHandlers smuxHandlers;
180 PDUsHandlers pdusHandlers;
184 std::list<CHG_AFTER_NOTIFIER> notifiers;
185 ADD_USER_NOTIFIER addUserNotifier;
186 DEL_USER_NOTIFIER delUserNotifier;
187 ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
189 PLUGIN_LOGGER logger;
191 //-----------------------------------------------------------------------------
194 void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
200 void ADD_DEL_TARIFF_NOTIFIER::Notify(const TARIFF_DATA &)
206 void ADD_USER_NOTIFIER::Notify(const USER_PTR & userPtr)
208 smux.SetNotifier(userPtr);
213 void DEL_USER_NOTIFIER::Notify(const USER_PTR & userPtr)
215 smux.UnsetNotifier(userPtr);
219 extern "C" PLUGIN * GetPlugin();