fintp_base
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AbstractWatcher.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 ABSTRACTWATCHER_H
22 #define ABSTRACTWATCHER_H
23 
24 #include "DllMain.h"
25 #include "WorkItemPool.h"
26 
27 namespace FinTP
28 {
30  {
31  public : // inner class definitions
33  {
34  public :
36  {
38  TYPE_CHAR
39  };
40 
41  private :
42  string m_ObjectId, m_ObjectGroupId;
43  unsigned long m_ObjectSize;
44  void* m_Object;
46 
47  public :
48 
49  explicit NotificationObject( const string& objectId, const string& objectGroupId = "",
50  const unsigned long objectSize = 0, NotificationType objectType = NotificationObject::TYPE_XMLDOM ) :
51  m_ObjectId( objectId ), m_ObjectGroupId( objectGroupId ), m_ObjectSize( objectSize ),
52  m_Object( NULL ), m_ObjectType( objectType )
53  {}
54 
55  NotificationObject( const string& objectId, void* object, const string& objectGroupId = "",
56  const unsigned long objectSize = 0, NotificationType objectType = NotificationObject::TYPE_XMLDOM ) :
57  m_ObjectId( objectId ), m_ObjectGroupId( objectGroupId ), m_ObjectSize( objectSize ),
58  m_Object( object ), m_ObjectType( objectType )
59  {}
60 
62  m_ObjectId( source.m_ObjectId ), m_ObjectGroupId( source.m_ObjectGroupId ), m_ObjectSize( source.m_ObjectSize ),
63  m_Object( source.m_Object ), m_ObjectType( source.m_ObjectType )
64  {
65  }
66 
67  NotificationObject& operator=( const NotificationObject& source )
68  {
69  if ( this == &source )
70  return *this;
71 
72  m_ObjectId = source.m_ObjectId;
73  m_ObjectGroupId = source.m_ObjectGroupId;
74  m_ObjectSize = source.m_ObjectSize;
75  m_Object = source.m_Object;
76  m_ObjectType = source.m_ObjectType;
77 
78  return *this;
79  }
80 
81  string getObjectId() const { return m_ObjectId; }
82 
83  void* getObject() const { return m_Object; }
84  template< typename T >
85  T* getObject() const { return dynamic_cast< T* >( m_Object ); }
86 
87  NotificationType getObjectType() const { return m_ObjectType; }
88  string getObjectGroupId() const { return m_ObjectGroupId; }
89  unsigned long getObjectSize() const { return m_ObjectSize; }
90  };
91 
92  public :
93  typedef WorkItemPool< NotificationObject > NotificationPool;
94 
95  void setCallback( void ( *callback )( const NotificationObject* ) )
96  {
97  m_Callback = callback;
98  }
99 
100  void setNotificationPool( NotificationPool* notificationPool )
101  {
102  m_NotificationPool = notificationPool;
103  }
104 
105  void setIdleCallback( void ( *callback )( void ), unsigned int seconds )
106  {
107  m_IdleTimeout = seconds;
108  m_IdleCallback = callback;
109  }
110 
111  protected :
112 
113  explicit AbstractWatcher( NotificationPool* notificationPool, void *( *prepareCallback )( void *object ) = NULL ) :
114  m_Callback( NULL ), m_PrepareCallback( prepareCallback ), m_IdleCallback( NULL ),
115  m_ScanThreadId( 0 ), m_Enabled( false ), m_NotificationType( NotificationObject::TYPE_XMLDOM ),
116  m_IdleTimeout( 0 ), m_NotificationPool( notificationPool )
117  {
118  }
119 
120  explicit AbstractWatcher( void ( *callback )( const NotificationObject* ), void *( *prepareCallback )( void *object ) = NULL ) :
121  m_Callback( callback ), m_PrepareCallback( prepareCallback ), m_IdleCallback( NULL ),
122  m_ScanThreadId( 0 ), m_Enabled( false ), m_NotificationType( NotificationObject::TYPE_XMLDOM ),
123  m_IdleTimeout( 0 ), m_NotificationPool( NULL )
124  {
125  }
126 
127  virtual void internalScan() = 0;
128 
129  void ( *m_Callback )( const NotificationObject *notification );
130  void *( *m_PrepareCallback )( void *object );
131  void ( *m_IdleCallback )( void );
132 
133  pthread_t m_ScanThreadId;
134  bool m_Enabled;
136  unsigned int m_IdleTimeout;
137 
139 
140  public:
141 
142  virtual ~AbstractWatcher();
143 
144  void setEnableRaisingEvents( bool val );
145  void setObjectPrepareMethod( void *( *prepareCallback )( void* ) );
146 
147  void setNotificationType( const NotificationObject::NotificationType notifType )
148  {
149  m_NotificationType = notifType;
150  }
151 
152  pthread_t getThreadId() { return m_ScanThreadId; }
153 
154  void waitForExit();
155 
156  private :
157 
158  static void* ScanInNewThread( void *pThis );
159  };
160 }
161 
162 #endif //ABSTRACTWATCHER_H