Qore FtpPoller Module Reference 2.0
Loading...
Searching...
No Matches
FtpPoller.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* FtpPoller.qm Copyright 2019 - 2023 Qore Technologies, s.r.o.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23*/
24
25
26// make sure we have the required qore version
27
28
29}
30
118namespace FtpPoller {
120const EVENT_FTP_FILE = "ftp-file-event";
121
124
125public:
127 const OrderAsc = 0;
129 const OrderDesc = 1;
130
132 const SortNone = 0;
134 const SortName = 1;
135 const SortDate = 2;
136
138 const RequiredKeys = ...;
139
140
142 const RequiredKeysWithHost = RequiredKeys + "host";
143
145 const Defaults = {
146 "protocol": "ftp",
147 "port": 21,
148 "mask": "*",
149 "poll_interval": 10,
150 "reopts": 0,
151 "tempfile_template": ".tmp.%s.part",
152 "atomic_transfer": False,
153 };
154
156 const OptionalKeys = ...;
157
158
160 const AllKeys = RequiredKeysWithHost + keys Defaults + OptionalKeys;
161
163 const ErrorDelay = 1m;
164
165protected:
167 string protocol;
168
170 string host;
171
173 int port;
174
176 string user;
177
179 string url;
180
182 hash<UrlInfo> urlh;
183
185 *string pass;
186
188 softlist<string> path = ".";
189
192
194 *string mask;
195
198
200 string local_dir;
201
204
207
209 bool runflag = False;
210
213
215 bool fatal = False;
216
218 int pollcnt = 0;
219
221 Mutex m();
222
224 int tid;
225
227 timeout timeout;
228
230 Counter sc();
231
234
236 int reopts = 0;
237
239 *softint minage;
240
242 *code log_info;
243
246
249
252
254 *code sleep;
255
256public:
257
259
296 constructor(Qore::FtpClient n_ftp, hash<auto> nconf);
297
298
300
339 constructor(hash<auto> nconf);
340
341
344
345
348
349
351protected:
352 logInfo(string fmt);
353public:
354
355
357protected:
358 logDetail(string fmt);
359public:
360
361
363protected:
364 logDebug(string fmt);
365public:
366
367
369protected:
371public:
372
373
375
378 getStoreFile(string remote_path, string local_path);
379
380
382 string getTextFile(string path);
383
384
386 binary getFile(string path);
387
388
390 rename(string old, string nnew);
391
392
394 removeFile(string fn);
395
396
398
408 list<hash<FtpPollerFileEventInfo>> getFiles(string subdir, int sort = FtpPoller::SortNone,
409 int order = FtpPoller::OrderAsc) {
410 if (subdir.val());
411
412 list<string> fl;
413 {
414 *string nlst = ftp.nlst();
415 if (!exists nlst) {
416 throw "FTP-ERROR", sprintf("no data returned from NLST command");
417 }
418 fl = nlst.split("\r\n");
419 }
420
421 // remove all files that don't fit the mask
422 if (mask);
423
424
425 // now get a list of info about file sizes and last modified timestamps
426 list<hash<FtpPollerFileEventInfo>> l = ();
427 foreach string fn in (fl);
428
429
430 // return list of files
431 switch (sort);
432
433
434 logDetail("%s: polled %d matching file%s", url, l.size(), l.size() == 1 ? "" : "s");
435 return l;
436 }
437
439
442 int start();
443
444
446
449
450
452
460
461
463
469
470
472
476
477
479 bool runOnce();
480
481
483 hash<FtpPollerFileEventInfo> getRemoteFileData(hash<FtpPollerFileEventInfo> event);
484
485
487protected:
488 hash<FtpPollerFileEventInfo> retrieveTempFile(hash<FtpPollerFileEventInfo> event);
489public:
490
491
493protected:
494 hash<FtpPollerFileEventInfo> retrieveFile(hash<FtpPollerFileEventInfo> event);
495public:
496
497
499protected:
500 ftpSleep(softint secs);
501public:
502
503
505protected:
507public:
508
509
511
513 *bool fileEvent(list<hash<FtpPollerFileEventInfo>> l);
514
515
517
521 abstract singleFileEvent(hash<FtpPollerFileEventInfo> event);
522
524
533 abstract postSingleFileEvent(hash<FtpPollerFileEventInfo> event);
534
536
540 static checkPath(string path, string type, bool write = False);
541};
542
544
547
548public:
551 "atomic_transfer": <DataProviderOptionInfo>{
552 "type": AbstractDataProviderType::get(BoolType),
553 "desc": "Use an atomic transfer mechanism with `local_dir` where remote files are first moved to a "
554 "temporary location and then moved to the final location when they have been fully transferred",
555 "default_value": True,
556 },
557
558 "local_dir": <DataProviderOptionInfo>{
559 "type": AbstractDataProviderType::get(StringOrNothingType),
560 "desc": "A local directory that will be used to retrieve files",
561 },
562
563 "mask": <DataProviderOptionInfo>{
564 "type": AbstractDataProviderType::get(StringType),
565 "desc": "The glob mask to use; will be treated as a regex if `regex` is `true`",
566 "default_value": "*",
567 },
568
569 "minage": <DataProviderOptionInfo>{
570 "type": AbstractDataProviderType::get(IntType),
571 "desc": "An integer giving the minimum file age in seconds before the file will be polled; this is "
572 "meant to work around non-atomic file transfer operations",
573 },
574
575 "poll_interval": <DataProviderOptionInfo>{
576 "type": AbstractDataProviderType::get(IntType),
577 "desc": "The interval in seconds between polling for files",
578 "default_value": 10,
579 },
580
581 "regex": <DataProviderOptionInfo>{
582 "type": AbstractDataProviderType::get(BoolType),
583 "desc": "If `true` then `mask` is treated as a regular expression instead of a glob pattern",
584 },
585
586 "reopt": <DataProviderOptionInfo>{
587 "type": AbstractDataProviderType::get(IntType),
588 "desc": "A bitfield of regular expression options (`1` = ignore case, `2` = treat EOL as a regular "
589 "character); ignored if `regex` is not `true`",
590 },
591
592 "sort_desc": <DataProviderOptionInfo>{
593 "type": AbstractDataProviderType::get(BoolType),
594 "desc": "Sort descending; if not given then an ascending sort is assumed if a `sort_type` is given",
595 },
596
597 "sort_type": <DataProviderOptionInfo>{
598 "type": AbstractDataProviderType::get(StringType),
599 "desc": "Either `name` or `date` for the data to use for sorting",
600 },
601
602 "url": <DataProviderOptionInfo>{
603 "type": AbstractDataProviderType::get(StringType),
604 "desc": "A URL for an FTP connection",
605 "required": True,
606 },
607 };
608
609protected:
611 EmbeddedFtpPoller poller;
612
613public:
614
616 constructor(*hash<auto> options);
617
618
619 destructor();
620
621
622 string getName();
623
624
626 *string getDesc();
627
628
630
634protected:
635 hash<string, hash<DataProviderMessageInfo>> getEventTypesImpl();
636public:
637
638};
639
641
646
647public:
649 const ProviderInfo = <DataProviderInfo>{
650 "type": "FtpPollerDataProvider",
651 "supports_observable": True,
652 "constructor_options": ConstructorOptions,
653 };
654
656 constructor(*hash<auto> options) ;
657
658
659protected:
660 hash<DataProviderInfo> getStaticInfoImpl();
661public:
662
663};
664
666
671
672public:
674 const ProviderInfo = <DataProviderInfo>{
675 "type": "FtpDelayedPollerDataProvider",
676 "supports_observable": True,
677 "constructor_options": ConstructorOptions,
678 };
679
681 constructor(*hash<auto> options) ;
682
683
685
688
689
690protected:
691 hash<DataProviderInfo> getStaticInfoImpl();
692public:
693
694};
695
697class FtpPollerDataProviderFactory : public AbstractDataProviderFactory {
698
699public:
700protected:
702 static Class cls = new Class("FtpPollerDataProvider");
703
705 const FactoryInfo = <DataProviderFactoryInfo>{
706 "name": "ftppoller",
707 "desc": "FTP poller data provider factory",
708 "children_can_support_observers": True,
709 };
710
711public:
712
714
716protected:
717 hash<DataProviderFactoryInfo> getInfoImpl();
718public:
719
720
722
724protected:
725 hash<DataProviderInfo> getProviderInfoImpl();
726public:
727
728
730protected:
732public:
733
734};
735};
736
737// private namespace; not exported
738namespace Priv {
739class EmbeddedFtpPoller : public FtpPoller {
740
741public:
742protected:
744 Observable observable;
745
746public:
747
748 constructor(FtpPollerDataProviderBase provider, hash<auto> options)
749 ;
750
751
752 singleFileEvent(hash<FtpPollerFileEventInfo> event);
753
754
755 postSingleFileEvent(hash<FtpPollerFileEventInfo> event);
756
757};
758};
Event-based data provider for FTP polling events.
Definition: FtpPoller.qm.dox.h:670
const ProviderInfo
Provider info.
Definition: FtpPoller.qm.dox.h:674
constructor(*hash< auto > options)
Creates the object from constructor options.
observersReady()
Called when all observers have been added to the object.
Event-based data provider for FTP polling events.
Definition: FtpPoller.qm.dox.h:546
*string getDesc()
Returns the data provider description.
hash< string, hash< DataProviderMessageInfo > > getEventTypesImpl()
Returns a hash of all supported event types.
constructor(*hash< auto > options)
Creates the object from constructor options.
const ConstructorOptions
Constructor options.
Definition: FtpPoller.qm.dox.h:550
EmbeddedFtpPoller poller
The file poller itself.
Definition: FtpPoller.qm.dox.h:611
The FTP poller data provider factory.
Definition: FtpPoller.qm.dox.h:697
const FactoryInfo
Factory info.
Definition: FtpPoller.qm.dox.h:705
Class getClassImpl()
Returns the class for the data provider object.
static Class cls
Data provider type info.
Definition: FtpPoller.qm.dox.h:702
hash< DataProviderFactoryInfo > getInfoImpl()
Returns static factory information without provider_info.
hash< DataProviderInfo > getProviderInfoImpl()
Returns static provider information.
Event-based data provider for FTP polling events.
Definition: FtpPoller.qm.dox.h:645
const ProviderInfo
Provider info.
Definition: FtpPoller.qm.dox.h:649
constructor(*hash< auto > options)
Creates the object from constructor options.
static checkPath(string path, string type, bool write=False)
checks a path on the local file system
string local_dir
Local directory to transfer file.
Definition: FtpPoller.qm.dox.h:200
const SortNone
no sorting
Definition: FtpPoller.qm.dox.h:132
logInfo(string fmt)
calls the "log_info" closure or call reference with important information
hash< FtpPollerFileEventInfo > getRemoteFileData(hash< FtpPollerFileEventInfo > event)
Retrieves remote file data and adds it to the event data.
int port
port
Definition: FtpPoller.qm.dox.h:173
getStoreFile(string remote_path, string local_path)
retrieves a remote file and stores it to a local path
*code sleep
optional sleep closure
Definition: FtpPoller.qm.dox.h:254
list< hash< FtpPollerFileEventInfo > > getFiles(string subdir, int sort=FtpPoller::SortNone, int order=FtpPoller::OrderAsc)
returns a list of regular file hashes matching any file name mask set for the object
Definition: FtpPoller.qm.dox.h:408
int start()
starts polling in the background; returns the thread ID of the polling thread
*code log_debug
optional debug log closure
Definition: FtpPoller.qm.dox.h:248
stopNoWait()
stops the polling operation, returns immediately
logDetail(string fmt)
calls the "log_detail" closure or call reference with detail information
destructor()
stops the polling operation if in progress and destroys the object
*string mask
file glob name mask (ignored if "regex_mask" also set)
Definition: FtpPoller.qm.dox.h:194
rename(string old, string nnew)
renames a file on the server
int tid
polling tid
Definition: FtpPoller.qm.dox.h:224
*string pass
password
Definition: FtpPoller.qm.dox.h:185
*code start_thread
optional start thread closure
Definition: FtpPoller.qm.dox.h:251
hash< FtpPollerFileEventInfo > retrieveTempFile(hash< FtpPollerFileEventInfo > event)
Retrieves the remote file to local_dir using a temporary file.
constructor(hash< auto > nconf)
creates the FtpPoller object from the configuration hash argument passed
binary getFile(string path)
retrieves a binary file and returns the file's contents
string host
host or address name
Definition: FtpPoller.qm.dox.h:170
*code log_detail
optional detail log closure
Definition: FtpPoller.qm.dox.h:245
*bool fileEvent(list< hash< FtpPollerFileEventInfo > > l)
called for each poll with a list of all files matched before transfer
Counter sc()
stop counter
bool atomic_transfer
Atomic transfer flag for use with local_dir.
Definition: FtpPoller.qm.dox.h:206
abstract postSingleFileEvent(hash< FtpPollerFileEventInfo > event)
called after singleFileEvent() for each matching file individually
string url
url
Definition: FtpPoller.qm.dox.h:179
*code log_info
optional info log closure
Definition: FtpPoller.qm.dox.h:242
*softint minage
minimum file age
Definition: FtpPoller.qm.dox.h:239
hash< FtpPollerFileEventInfo > retrieveFile(hash< FtpPollerFileEventInfo > event)
Retrieves the remote file to local_dir directly.
string tempfile_template
The temporary filename template when local_dir is set.
Definition: FtpPoller.qm.dox.h:203
stop()
stops the polling operation, returns when the polling operation has been stopped
bool runOnce()
runs a single poll (useful for checking for errors inline before starting a background thread)
ftpSleep(softint secs)
sleeps for the specificed number of seconds
Mutex m()
start mutex
run()
starts the polling operation
string getTextFile(string path)
retrieves a text file and returns the file's contents
logDebug(string fmt)
calls the "log_debug" closure or call reference with verbose debugging information
int poll_interval
poll interval in seconds
Definition: FtpPoller.qm.dox.h:197
constructor(Qore::FtpClient n_ftp, hash< auto > nconf)
creates the FtpPoller object from the FtpClient argument and configuration hash argument passed
bool get_files
internal "get files" flag
Definition: FtpPoller.qm.dox.h:212
timeout timeout
timeout in ms
Definition: FtpPoller.qm.dox.h:227
waitStop()
waits indefinitely for the polling operation to stop
int getPollCount()
returns the current poll count
removeFile(string fn)
deletes a file on the server
setMask()
converts a glob mask into a regex
Qore::FtpClient ftp
FtpClient object.
Definition: FtpPoller.qm.dox.h:233
abstract singleFileEvent(hash< FtpPollerFileEventInfo > event)
called for each matching file individually whenever matching files are polled
const OrderAsc
ascending sort order
Definition: FtpPoller.qm.dox.h:127
string user
user
Definition: FtpPoller.qm.dox.h:176
hash< UrlInfo > urlh
url hash
Definition: FtpPoller.qm.dox.h:182
string protocol
Protocol ("ftp" or "ftps")
Definition: FtpPoller.qm.dox.h:167
startInline()
starts the polling operation inline (not in a background thread)
string rootFtpPath
path after connecting to Ftp server
Definition: FtpPoller.qm.dox.h:191
*string nlst()
main FtpPoller namespace
const EVENT_FTP_FILE
FTP file event constant.
Definition: FtpPoller.qm.dox.h:120