-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirConsumer.h
259 lines (211 loc) · 8.73 KB
/
DirConsumer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// ===========================================================================
// Copyright (c) 2004 by
// syscheme, Shanghai
// All Rights Reserved. Unpublished rights reserved under the copyright
// laws of the United States.
//
// The software contained on this media is proprietary to and embodies the
// confidential technology of syscheme Possession, use,
// duplication or dissemination of the software and media is authorized only
// pursuant to a valid written license from syscheme
//
// This software is furnished under a license and may be used and copied
// only in accordance with the terms of such license and with the inclusion
// of the above copyright notice. This software or any other copies thereof
// may not be provided or otherwise made available to any other person. No
// title to and ownership of the software is hereby transferred.
//
// The information in this software is subject to change without notice and
// should not be construed as a commitment by syscheme
//
// Ident : $Id: DirConsumer.h,v 1.0 2006/11/30 10:530:56 Chen Shuai Exp $
// Branch: $Name: $
// Author: Chen Shuai
// Desc : Define DirConsumer
//
// ===========================================================================
#ifndef __DIRCONSUMER_H__
#define __DIRCONSUMER_H__
#include "afx.h"
#include "NativeThread.h"
#include "Locks.h"
#include <list>
#include <queue>
#include "Log.h"
using namespace std;
#define OUT
namespace ZQ{
namespace common{
class ZQ_COMMON_API DirConsumer;
class ZQ_COMMON_API DirMonitor;
#define DIR_ADD 0x00000001
#define DIR_DEL 0x00000002
#define DIR_MODIFY 0x00000004
#define DIR_REN_OLD 0x00000008
#define DIR_REN_NEW 0x00000010
/************************************************************************/
/* DirConsumer 类定义 */
/************************************************************************/
class DirConsumer : public NativeThread
{
/************************************************************************/
/* DirItem 类定义 */
/************************************************************************/
class DirItem
{
public:
enum FileAction {UNKNOWN,
ADD,
REMOVE,
MODIFY,
RENAMEOLD,
RENAMENEW};
DirItem(const char* fileName, FileAction action);
~DirItem() {};
public:
char _fileName[MAX_PATH];
FileAction _fileAction;
private:
DirItem();
};
/************************************************************************/
/* typedef */
/************************************************************************/
typedef DirItem* LPDirItem;
typedef list<LPDirItem> DirItemList;
typedef list<LPDirItem>::iterator DirItemListIterator;
/************************************************************************/
/* DirConsumer 成员函数与变量定义 */
/************************************************************************/
friend class DirMonitor;
public:
DirConsumer(const char* monitorDirectory,
DWORD dirOperation = DIR_ADD | DIR_DEL | DIR_MODIFY | DIR_REN_OLD | DIR_REN_NEW,
bool bWatchSubdirectories = true,
bool bSnapshot = true,
bool bNotifyAsFileCome = false,
DWORD DirMonitorInterval= INFINITE);
DirConsumer(const WCHAR* monitorDirectory,
DWORD dirOperation = DIR_ADD | DIR_DEL | DIR_MODIFY | DIR_REN_OLD | DIR_REN_NEW,
bool bWatchSubdirectories = true,
bool bSnapshot = true,
bool bNotifyAsFileCome = false,
DWORD DirMonitorInterval = INFINITE);
virtual ~DirConsumer();
void stop() { SetEvent(_hStop); waitHandle(1000);};
/************************************************************************/
/* 针对不同的事件所对应的处理函数 */
/************************************************************************/
virtual bool processAdd(const char* fileName) {return true; };
virtual bool processDelete(const char* fileName) {return true; };
virtual bool processModify(const char* fileName) {return true; };
virtual bool processRenameOld(const char* fileName) {return true; };
virtual bool processRenameNew(const char* fileName) {return true; };
protected:
virtual bool init(void);
virtual int run(void);
virtual bool configure();
/************************************************************************/
/* 事件的响应 */
/************************************************************************/
virtual bool notify() { long count; return ReleaseSemaphore(_hNotify, 1, &count); };
virtual bool processNotify();
virtual bool processTimeout() { processNotify(); return true;};
void fileAdd(const char* fileName) {addTail(fileName, DirItem::ADD); notify(); };
private:
DirConsumer();
bool openHandles();
void closeHandles();
bool isFileAccess(const char* fileName);
void setDirOperation(DWORD dirOperation);
bool isEmpty() { return ( 0 == _dirItemList.size() ); };
void initMembers(DWORD dirOperation, bool bWatchSubdirectories,
bool bSnapshot, bool bNotifyAsFileCome);
/************************************************************************/
/* 对list的操作 */
/************************************************************************/
void fileRemove(const char* fileName) {addTail(fileName, DirItem::REMOVE); notify(); };
void fileModify(const char* fileName) {addTail(fileName, DirItem::MODIFY); notify(); };
void fileRenameOld(const char* fileName) {addTail(fileName, DirItem::RENAMEOLD); notify(); };
void fileRenameNew(const char* fileName) {addTail(fileName, DirItem::RENAMENEW); notify(); };
bool addTailExclusive(const char* fileName, DirItem::FileAction action = DirItem::ADD);
bool addHead(const char* fileName, DirItem::FileAction action = DirItem::ADD);
bool addTail(const char* fileName, DirItem::FileAction action = DirItem::ADD);
bool removeHead(OUT char* fileName, OUT DirItem::FileAction* action = NULL);
bool removeTail(OUT char* fileName, OUT DirItem::FileAction* action = NULL);
bool getHead(OUT char* fileName, OUT DirItem::FileAction* action = NULL);
bool getTail(OUT char* fileName, OUT DirItem::FileAction* action = NULL);
bool removeFile(const char* fileName);
void removeAll();
protected:
HANDLE _hStop;
HANDLE _hNotify;
DWORD _dirOperation;
DWORD _notifyFilter;
bool _bWatchSubdirectories;
bool _bSnapshot;
char _monitorDirectory[MAX_PATH];
bool _bNotifyAsFileCome;
DWORD _interval;
DWORD _DirMonitorInterval;
private:
DirItemList _dirItemList;
DirMonitor* _pDirMonitor;
Mutex _mutex;
};
/************************************************************************/
/* DirMonitor 类定义 */
/************************************************************************/
class DirMonitor : public NativeThread
{
typedef queue<string> StringQueue;
friend class DirConsumer;
protected:
DirMonitor(DirConsumer* dirConsumer,int interval);
virtual ~DirMonitor();
void stop() { SetEvent(_hStop); waitHandle(1000);};
virtual bool configure();
/************************************************************************/
/* NativeThread virtual函数 */
/************************************************************************/
virtual bool start();
virtual bool init(void);
virtual int run(void);
/************************************************************************/
/* 事件的响应 */
/************************************************************************/
virtual bool notify() { return (SetEvent(_hNotify)); };
virtual bool processTimeout() { processNotify(); return true;};
virtual bool processNotify();
/************************************************************************/
/* 对监视目录的操作 */
/************************************************************************/
virtual bool getDirectorySnapshot(const char* baseDirectory,
const char* additionalPath,
const char* filter);
virtual bool processDirectorySnapshot();
virtual int fileFilter(const char* str) { return 1; };
private:
DirMonitor();
bool setMonitorChanges(DWORD&);
bool processChangedFile(FILE_NOTIFY_INFORMATION* pFni);
static VOID WINAPI HandleDirChanges(DWORD, DWORD, LPOVERLAPPED);
bool openHandles();
void closeHandles();
protected:
HANDLE _hStop;
HANDLE _hNotify;
HANDLE _directoryEvent;
HANDLE _hSetCallback;
DWORD _interval;
private:
char _buffer[MAX_PATH];
OVERLAPPED _overlapped;
StringQueue _initialContentsQueue;
DirConsumer* _pDirConsumer;
bool _bSetCallbackSucess;
Mutex _mutex;
};
}
}
#endif