fintp_eventswatcher
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
EventsWatcher.h
Go to the documentation of this file.
1 /*
2 * FinTP - Financial Transactions Processing Application
3 * Copyright (C) 2013 Business Information Systems (Allevo) S.R.L.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 * or contact Allevo at : 031281 Bucuresti, 23C Calea Vitan, Romania,
18 * phone +40212554577, office@allevo.ro <mailto:office@allevo.ro>, www.allevo.ro.
19 */
20 
21 #ifndef EVENTSWATCHER_H
22 #define EVENTSWATCHER_H
23 
24 #include <string>
25 #include <time.h>
26 
27 #include "EventsWatcherMain.h"
28 
29 #include "AbstractWatcher.h"
30 #include "MQ/MqWatcher.h"
31 #include "TransportHelper.h"
32 #include "AppExceptions.h"
33 #include "InstrumentedObject.h"
34 
35 using namespace std;
36 
37 class Service
38 {
39  public :
40 
41  typedef enum
42  {
43  RUNNING = 3,
44  STOPPED = 1
45  } SERVICE_STATE;
46 
47  static string ToString( const SERVICE_STATE value );
48 
49  string Report() const;
50 
51  // ctor
52  Service() : m_State( Service::STOPPED ), m_LastHeartbeat( 0 ), m_ServiceName( "qPIUnk00" ), m_SessionId( "" ),
53  m_ServiceId( -1 ), m_HeartbeatInterval( 30 ) {};
54 
55  Service( const long serviceId, const long state, const time_t lastHeartbeat,
56  const string& serviceName="qPIUnk00", const string& sessionId="", const long heartbeatInterval = 30 ) :
57  m_State( ( SERVICE_STATE )state ), m_LastHeartbeat( lastHeartbeat ), m_ServiceName( serviceName ),
58  m_SessionId( sessionId ), m_ServiceId( serviceId ), m_HeartbeatInterval( heartbeatInterval ) {}
59 
60  // session id
61  string getSession() const { return m_SessionId; }
62  void setSessionId( const string& sessionId ) { m_SessionId = sessionId; }
63 
64  // service id
65  long getServiceId() const { return m_ServiceId; }
66 
67  // name
68  string getName() const { return m_ServiceName; }
69 
70  // state
71  Service::SERVICE_STATE getState() const { return m_State; }
72  void setState( const Service::SERVICE_STATE newState );
73 
74  // heartbeat
75  void setLastHeartbeat( const time_t newtime ){ m_LastHeartbeat = newtime; }
76  double getTimeSinceLastHeartbeat() const;
77  long getHeartbeatInterval() const { return m_HeartbeatInterval; }
78 
79  private :
80 
83  string m_ServiceName;
84  string m_SessionId;
87 };
88 
89 class ExportedTestObject EventsWatcher : public InstrumentedObject
90 {
91 #if defined( TESTDLL_EXPORT ) || defined ( TESTDLL_IMPORT )
92  friend class EventsTest;
93 #endif
94 
95  public:
96 
97  typedef map< string, Service > ServiceMap;
98 
99  ~EventsWatcher();
100 
101  //public methods
102  void Start();
103  void Stop();
104 
105  static EventsWatcher* getInstance( const string& configFile = "" );
106 
107  void Process( const string& messageId, const unsigned long messageLength );
108 
109  static void UpdateServiceState( const string& sessionId, const Service::SERVICE_STATE newState, const string& serviceName = "" );
110  static void* HeartbeatMonitor( void* data );
111 
112  static ServiceMap* getConnectorStateMap() { return &( getInstance()->m_ConnectorState ); }
113 
114  static pthread_mutex_t StatusSyncMutex;
115 
116  pthread_t getPublisherThreadId() const { return m_SelfThreadId; }
117  pthread_t getHBThreadId() const{ return m_MonitorThreadId; }
118 
119  bool isRunning() const { return true; }
120 
121  private:
122 
123  EventsWatcher( const string& configFile );
124 
125  static void* StartInNewThread( void* pThis );
126  void internalStart( void );
127 
128  void UploadMessage( AppException &ex );
129 
130  long getServiceId( const string& sessionId ) const;
131 
133  static bool ShouldStop;
134  pthread_t m_SelfThreadId;
135 
136  //int m_BackoutCount;
137  //int m_CurrentStage;
138 
139  MqWatcher m_Watcher;
140  AbstractWatcher::NotificationPool m_NotificationPool;
141 
142  string m_SessionId;
143  TransportHelper* m_CurrentHelper;
144  TransportHelper::TRANSPORT_HELPER_TYPE m_HelperType;
145  pthread_t m_MonitorThreadId;
146 
147  string m_WatchQueue;
150 
151 
152  // collection of connector-state pairs
154 
155 #if defined( WIN32 ) && defined( _DEBUG )
156  _CrtMemState m_NextMemoryState, m_OldMemoryState;
157  bool m_OldStateAvailable;
158 #endif
159 };
160 
161 #endif //EVENTSWATCHER_H