File: miniftpclient.cpp

package info (click to toggle)
megaglest 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,904 kB
  • ctags: 18,215
  • sloc: cpp: 144,232; ansic: 11,860; sh: 2,949; perl: 1,899; python: 1,751; objc: 142; asm: 42; makefile: 24
file content (1244 lines) | stat: -rw-r--r-- 51,072 bytes parent folder | download | duplicates (5)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
// ==============================================================
//	This file is part of MegaGlest Shared Library (www.glest.org)
//
//	Copyright (C) 2009-2010 Titus Tscharntke (info@titusgames.de) and
//                          Mark Vejvoda (mark_vejvoda@hotmail.com)
//
//	You can redistribute this code and/or modify it under
//	the terms of the GNU General Public License as published
//	by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version
// ==============================================================

#include "miniftpclient.h"
#include "util.h"
#include "platform_common.h"

#include <curl/curl.h>
#include <curl/easy.h>
#include <algorithm>
#include "conversion.h"
#include "platform_util.h"

using namespace Shared::Util;
using namespace Shared::PlatformCommon;

namespace Shared { namespace PlatformCommon {

static const char *FTP_MAPS_CUSTOM_USERNAME        = "maps_custom";
static const char *FTP_MAPS_USERNAME               = "maps";
static const char *FTP_TILESETS_CUSTOM_USERNAME    = "tilesets_custom";
static const char *FTP_TILESETS_USERNAME           = "tilesets";
static const char *FTP_TECHTREES_CUSTOM_USERNAME   = "techtrees_custom";
static const char *FTP_TECHTREES_USERNAME          = "techtrees";

static const char *FTP_TEMPFILES_USERNAME          = "temp";

static const char *FTP_COMMON_PASSWORD             = "mg_ftp_server";

/*
 * This is an example showing how to get a single file from an FTP server.
 * It delays the actual destination file creation until the first write
 * callback so that it won't create an empty file in case the remote file
 * doesn't exist or something else fails.
 */

struct FtpFile {
  const char *itemName;
  const char *filename;
  const char *filepath;
  FILE *stream;
  FTPClientThread *ftpServer;
  string currentFilename;
  bool isValidXfer;
  FTP_Client_CallbackType downloadType;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) {
    struct FtpFile *out=(struct FtpFile *)stream;

    string fullFilePath = "";
    if(out != NULL && out->filepath != NULL) {
        fullFilePath = out->filepath;
    }
    if(out != NULL && out->filename != NULL) {
        fullFilePath += out->filename;
    }

    if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread writing to file [%s]\n",fullFilePath.c_str());
    //printf ("===> FTP Client thread writing to file [%s]\n",fullFilePath.c_str());

    // Abort file xfer and delete partial file
    if(out && out->ftpServer && out->ftpServer->getQuitStatus() == true) {
        if(out->stream) {
            fclose(out->stream);
            out->stream = NULL;
        }

        if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread CANCELLED, deleting file for writing [%s]\n",fullFilePath.c_str());
        if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread CANCELLED, deleting file for writing [%s]\n",fullFilePath.c_str());


        removeFile(fullFilePath);
        return 0;
    }

    if(out && out->stream == NULL) {
        if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread opening file for writing [%s]\n",fullFilePath.c_str());
        if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread opening file for writing [%s]\n",fullFilePath.c_str());

        /* open file for writing */
#ifdef WIN32
		out->stream= _wfopen(utf8_decode(fullFilePath).c_str(), L"wb");
#else
        out->stream = fopen(fullFilePath.c_str(), "wb");
#endif
        if(out->stream == NULL) {
          if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str());
          if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str());
          SystemFlags::OutputDebug(SystemFlags::debugError,"===> FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str());
          return 0; /* failure, can't open file to write */
        }

        out->isValidXfer = true;
    }
    else if(out == NULL) {
        if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> #2 FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str());
        if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> #2 FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str());
        SystemFlags::OutputDebug(SystemFlags::debugError,"===> #2 FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str());
        return 0; /* failure, can't open file to write */
    }

    size_t result = fwrite(buffer, size, nmemb, out->stream);
    if(result != nmemb) {
        if(SystemFlags::VERBOSE_MODE_ENABLED) printf("===> FTP Client thread FAILED to write data chunk to file [%s] nmemb = " MG_SIZE_T_SPECIFIER ", result = " MG_SIZE_T_SPECIFIER "\n",fullFilePath.c_str(),nmemb,result);
        if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread FAILED to write data chunk to file [%s] nmemb = " MG_SIZE_T_SPECIFIER ", result = " MG_SIZE_T_SPECIFIER "\n",fullFilePath.c_str(),nmemb,result);
        SystemFlags::OutputDebug(SystemFlags::debugError,"===> FTP Client thread FAILED to write data chunk to file [%s] nmemb = " MG_SIZE_T_SPECIFIER ", result = " MG_SIZE_T_SPECIFIER "\n",fullFilePath.c_str(),nmemb,result);
        //return -1; /* failure, can't open file to write */
    }
    return result;
}

/*
static long file_is_comming(struct curl_fileinfo *finfo,void *data,int remains) {
    struct FtpFile *out=(struct FtpFile *)data;

    string rootFilePath = "";
    string fullFilePath = "";
    if(out != NULL && out->filepath != NULL) {
        rootFilePath = out->filepath;
    }
    if(out != NULL && out->filename != NULL) {
        fullFilePath = rootFilePath + finfo->filename;
    }

    if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n===> FTP Client thread file_is_comming: remains: [%3d] filename: [%s] size: [%10luB] ", remains, finfo->filename,(unsigned long)finfo->size);
    SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread file_is_comming: remains: [%3d] filename: [%s] size: [%10luB] ", remains, finfo->filename,(unsigned long)finfo->size);

    if(out != NULL) {
        //out->currentFilename = finfo->filename;
        out->currentFilename = fullFilePath;

        if(SystemFlags::VERBOSE_MODE_ENABLED) printf(" current filename: [%s] ", fullFilePath.c_str());
        SystemFlags::OutputDebug(SystemFlags::debugNetwork,"current filename: [%s] ", fullFilePath.c_str());
    }

    switch(finfo->filetype) {
        case CURLFILETYPE_DIRECTORY:
            if(SystemFlags::VERBOSE_MODE_ENABLED) printf(" DIR (creating [%s%s])\n",rootFilePath.c_str(),finfo->filename);
            SystemFlags::OutputDebug(SystemFlags::debugNetwork," DIR (creating [%s%s])\n",rootFilePath.c_str(),finfo->filename);

            rootFilePath += finfo->filename;
            createDirectoryPaths(rootFilePath.c_str());
            break;
        case CURLFILETYPE_FILE:
            if(SystemFlags::VERBOSE_MODE_ENABLED) printf(" FILE ");
            SystemFlags::OutputDebug(SystemFlags::debugNetwork," FILE ");
            break;
        default:
            if(SystemFlags::VERBOSE_MODE_ENABLED) printf(" OTHER\n");
            SystemFlags::OutputDebug(SystemFlags::debugNetwork," OTHER\n");
            break;
    }

    if(finfo->filetype == CURLFILETYPE_FILE) {
        // do not transfer files >= 50B
        //if(finfo->size > 50) {
        //  printf("SKIPPED\n");
        //  return CURL_CHUNK_BGN_FUNC_SKIP;
        //}

        if(SystemFlags::VERBOSE_MODE_ENABLED) printf(" opening file [%s] ", fullFilePath.c_str());
        SystemFlags::OutputDebug(SystemFlags::debugNetwork," opening file [%s] ", fullFilePath.c_str());

        out->stream = fopen(fullFilePath.c_str(), "wb");
        if(out->stream == NULL) {
            if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str());
            SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread FAILED to open file for writing [%s]\n",fullFilePath.c_str());

            return CURL_CHUNK_BGN_FUNC_FAIL;
        }
    }

    out->isValidXfer = true;
    return CURL_CHUNK_BGN_FUNC_OK;
}

static long file_is_downloaded(void *data) {
    struct FtpFile *out=(struct FtpFile *)data;
    if(out->stream) {
        if(SystemFlags::VERBOSE_MODE_ENABLED) printf("DOWNLOAD COMPLETE!\n");
        SystemFlags::OutputDebug(SystemFlags::debugNetwork,"DOWNLOAD COMPLETE!\n");

        fclose(out->stream);
        out->stream = NULL;
    }
    return CURL_CHUNK_END_FUNC_OK;
}
*/

int file_progress(struct FtpFile *out,double download_total, double download_now, double upload_total,double upload_now) {
  //if(SystemFlags::VERBOSE_MODE_ENABLED) printf(" download progress [%f][%f][%f][%f] ",download_total,download_now,upload_total,upload_now);
	if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork," download progress [%f][%f][%f][%f] ",download_total,download_now,upload_total,upload_now);

  if(out != NULL &&
     out->ftpServer != NULL &&
     out->ftpServer->getCallBackObject() != NULL) {
         if(out->ftpServer->getQuitStatus() == true) {
             if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread CANCELLED\n");
             if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread CANCELLED\n");

             return -1;
         }
         FTPClientCallbackInterface::FtpProgressStats stats;
         stats.download_total   = download_total;
         stats.download_now     = download_now;
         stats.upload_total     = upload_total;
         stats.upload_now       = upload_now;
         stats.currentFilename  = out->currentFilename;
         stats.downloadType		= out->downloadType;

         static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
         MutexSafeWrapper safeMutex(out->ftpServer->getProgressMutex(),mutexOwnerId);
         out->ftpServer->getProgressMutex()->setOwnerId(mutexOwnerId);
         out->ftpServer->getCallBackObject()->FTPClient_CallbackEvent(
        		 out->itemName,
        		 ftp_cct_DownloadProgress,
        		 make_pair(ftp_crt_SUCCESS,""),
        		 &stats);
  }

  return 0;
}

FTPClientThread::FTPClientThread(int portNumber, string serverUrl,
		std::pair<string,string> mapsPath,
		std::pair<string,string> tilesetsPath,
		std::pair<string,string> techtreesPath,
		std::pair<string,string> scenariosPath,
		FTPClientCallbackInterface *pCBObject,
		string fileArchiveExtension,
		string fileArchiveExtractCommand,
		string fileArchiveExtractCommandParameters,
		int fileArchiveExtractCommandSuccessResult,
		string tempFilesPath) : BaseThread() {

	uniqueID = "FTPClientThread";
    this->portNumber    = portNumber;
    this->serverUrl     = serverUrl;
    this->mapsPath      = mapsPath;
    this->tilesetsPath  = tilesetsPath;
    this->techtreesPath = techtreesPath;
    this->scenariosPath	= scenariosPath;
    this->pCBObject     = pCBObject;
    this->shellCommandCallbackUserData = "";

    this->fileArchiveExtension = fileArchiveExtension;
    this->fileArchiveExtractCommand = fileArchiveExtractCommand;
    this->fileArchiveExtractCommandParameters = fileArchiveExtractCommandParameters;
    this->fileArchiveExtractCommandSuccessResult = fileArchiveExtractCommandSuccessResult;
    this->tempFilesPath = tempFilesPath;

    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d] Using FTP port #: %d, serverUrl [%s]\n",__FILE__,__FUNCTION__,__LINE__,portNumber,serverUrl.c_str());
}

void FTPClientThread::signalQuit() {
    if(SystemFlags::VERBOSE_MODE_ENABLED) printf("===> FTP Client: signalQuit\n");
    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client: signalQuit\n");
    BaseThread::signalQuit();
}

bool FTPClientThread::shutdownAndWait() {
    if(SystemFlags::VERBOSE_MODE_ENABLED) printf("===> FTP Client: shutdownAndWait\n");
    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client: shutdownAndWait\n");

    signalQuit();
    return BaseThread::shutdownAndWait();
}


pair<FTP_Client_ResultType,string> FTPClientThread::getMapFromServer(pair<string,string> mapFileName, string ftpUser, string ftpUserPassword) {
	pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");

    string destFileExt = "";
    string destFile = this->mapsPath.second;

	endPathWithSlash(destFile);
    destFile += mapFileName.first;

    if(mapFileName.second == "") {
		if(EndsWith(destFile,".mgm") == false && EndsWith(destFile,".gbm") == false) {
			destFileExt = ".mgm";
			destFile += destFileExt;
		}
    }

    if(SystemFlags::VERBOSE_MODE_ENABLED) printf("===> FTP Client thread about to try to RETR into [%s]\n",destFile.c_str());
    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread about to try to RETR into [%s]\n",destFile.c_str());

    struct FtpFile ftpfile = {
        NULL,
        destFile.c_str(), /* name to store the file as if succesful */
        NULL,
        NULL,
        this,
        "",
        false,
        ftp_cct_Map
    };

    CURL *curl = SystemFlags::initHTTP();
    if(curl) {
        ftpfile.stream = NULL;

        char szBuf[8096]="";
        if(mapFileName.second != "") {
        	snprintf(szBuf,8096,"%s",mapFileName.second.c_str());
        	curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
        }
        else {
        	snprintf(szBuf,8096,"ftp://%s:%s@%s:%d/%s%s",ftpUser.c_str(),ftpUserPassword.c_str(),serverUrl.c_str(),portNumber,mapFileName.first.c_str(),destFileExt.c_str());
        }

        curl_easy_setopt(curl, CURLOPT_URL,szBuf);
        curl_easy_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);

        /* Define our callback to get called when there's data to be written */
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
        /* Set a pointer to our struct to pass to the callback */
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);


        // Max 10 minutes to transfer
        //curl_easy_setopt(curl, CURLOPT_TIMEOUT, 600);
        // Max 60 minutes to transfer
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3600L);
        curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, 120L);

        /* Switch on full protocol/debug output */
        if(SystemFlags::VERBOSE_MODE_ENABLED) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        CURLcode res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
          result.second = curl_easy_strerror(res);
          // we failed
          printf("curl FAILED with: %d [%s] szBuf [%s]\n", res,curl_easy_strerror(res),szBuf);
          if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"curl FAILED with: %d [%s] szBuf [%s]\n", res,curl_easy_strerror(res),szBuf);

          if(res == CURLE_PARTIAL_FILE) {
        	  result.first = ftp_crt_PARTIALFAIL;
          }
          else if(res == CURLE_COULDNT_CONNECT) {
        	  result.first = ftp_crt_HOST_NOT_ACCEPTING;
          }
        }
        else {
            result.first = ftp_crt_SUCCESS;
        }

        SystemFlags::cleanupHTTP(&curl);
    }

    if(ftpfile.stream) {
        fclose(ftpfile.stream);
        ftpfile.stream = NULL;
    }
    if(result.first != ftp_crt_SUCCESS) {
    	removeFile(destFile);
    }

    return result;
}

void FTPClientThread::getMapFromServer(pair<string,string> mapFileName) {
	pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
	if(mapFileName.second != "") {
		result = getMapFromServer(mapFileName, "", "");
	}
	else {
		pair<string,string> findMapFileName = mapFileName;
		findMapFileName.first += + ".mgm";

		result = getMapFromServer(findMapFileName, FTP_MAPS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
		if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
			findMapFileName = mapFileName;
			findMapFileName.first += + ".gbm";
			result = getMapFromServer(findMapFileName, FTP_MAPS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
			if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
				findMapFileName = mapFileName;
				findMapFileName.first += + ".mgm";
				result = getMapFromServer(findMapFileName, FTP_MAPS_USERNAME, FTP_COMMON_PASSWORD);
				if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
					findMapFileName = mapFileName;
					findMapFileName.first += + ".gbm";
					result = getMapFromServer(findMapFileName, FTP_MAPS_USERNAME, FTP_COMMON_PASSWORD);
				}
			}
		}
	}

	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
    this->getProgressMutex()->setOwnerId(mutexOwnerId);
    if(this->pCBObject != NULL) {
        this->pCBObject->FTPClient_CallbackEvent(
        		mapFileName.first,
        		ftp_cct_Map,
        		result,
        		NULL);
    }
}

void FTPClientThread::addMapToRequests(string mapFilename,string URL) {
	std::pair<string,string> item = make_pair(mapFilename,URL);
	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(&mutexMapFileList,mutexOwnerId);
    mutexMapFileList.setOwnerId(mutexOwnerId);
    if(std::find(mapFileList.begin(),mapFileList.end(),item) == mapFileList.end()) {
        mapFileList.push_back(item);
    }
}

void FTPClientThread::addTilesetToRequests(string tileSetName,string URL) {
	std::pair<string,string> item = make_pair(tileSetName,URL);
	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(&mutexTilesetList,mutexOwnerId);
    mutexTilesetList.setOwnerId(mutexOwnerId);
    if(std::find(tilesetList.begin(),tilesetList.end(),item) == tilesetList.end()) {
        tilesetList.push_back(item);
    }
}

void FTPClientThread::addTechtreeToRequests(string techtreeName,string URL) {
	std::pair<string,string> item = make_pair(techtreeName,URL);
	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(&mutexTechtreeList,mutexOwnerId);
    mutexTechtreeList.setOwnerId(mutexOwnerId);
    if(std::find(techtreeList.begin(),techtreeList.end(),item) == techtreeList.end()) {
    	techtreeList.push_back(item);
    }
}

void FTPClientThread::addScenarioToRequests(string fileName,string URL) {
	std::pair<string,string> item = make_pair(fileName,URL);
	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(&mutexScenarioList,mutexOwnerId);
    mutexScenarioList.setOwnerId(mutexOwnerId);
    if(std::find(scenarioList.begin(),scenarioList.end(),item) == scenarioList.end()) {
    	scenarioList.push_back(item);
    }
}

void FTPClientThread::addFileToRequests(string fileName,string URL) {
	std::pair<string,string> item = make_pair(fileName,URL);
	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(&mutexFileList,mutexOwnerId);
    mutexFileList.setOwnerId(mutexOwnerId);
    if(std::find(fileList.begin(),fileList.end(),item) == fileList.end()) {
    	fileList.push_back(item);
    }
}

void FTPClientThread::addTempFileToRequests(string fileName,string URL) {
	std::pair<string,string> item = make_pair(fileName,URL);
	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(&mutexTempFileList,mutexOwnerId);
    mutexTempFileList.setOwnerId(mutexOwnerId);
    if(std::find(tempFileList.begin(),tempFileList.end(),item) == tempFileList.end()) {
    	tempFileList.push_back(item);
    }
}

void FTPClientThread::getTilesetFromServer(pair<string,string> tileSetName) {
	bool findArchive = executeShellCommand(
			this->fileArchiveExtractCommand,
			this->fileArchiveExtractCommandSuccessResult);

	pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
	if(findArchive == true) {
		if(tileSetName.second != "") {
			//result = getTilesetFromServer(tileSetName, "", "", "", findArchive);
			result = getTilesetFromServer(tileSetName, "", "", "", true);
		}
		else {
			//result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD, findArchive);
			result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD, true);
			if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
				//if(findArchive == true) {
					//result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD, false);
				result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_CUSTOM_USERNAME, FTP_COMMON_PASSWORD, true);
				//}
				if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
				//	result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_USERNAME, FTP_COMMON_PASSWORD, findArchive);
					result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_USERNAME, FTP_COMMON_PASSWORD, true);

				//	if(findArchive == true) {
				//		result = getTilesetFromServer(tileSetName, "", FTP_TILESETS_USERNAME, FTP_COMMON_PASSWORD, false);
				//	}
				}
			}
		}
	}

	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
    this->getProgressMutex()->setOwnerId(mutexOwnerId);
    if(this->pCBObject != NULL) {
        this->pCBObject->FTPClient_CallbackEvent(
        		tileSetName.first,
        		ftp_cct_Tileset,
        		result,
        		NULL);
    }
}

pair<FTP_Client_ResultType,string> FTPClientThread::getTilesetFromServer(
												pair<string,string> tileSetName,
												string tileSetNameSubfolder,
												string ftpUser,
												string ftpUserPassword,
												bool findArchive) {

	string destFileSaveAsNewFile = "";
	string destFileSaveAs = "";
	string remotePath = "";
	bool getFolderContents = false;
	vector<string> wantDirListOnly;

    if(tileSetNameSubfolder == "") {
        if(findArchive == true) {
        	destFileSaveAs = this->tilesetsPath.second;
            endPathWithSlash(destFileSaveAs);
            destFileSaveAs += tileSetName.first + this->fileArchiveExtension;

            if(tileSetName.second != "") {
            	remotePath = tileSetName.second;
            }
            else {
            	remotePath = tileSetName.first + this->fileArchiveExtension;
            }
        }
        else {
        	getFolderContents = true;
        	remotePath = tileSetName.first + "/";
        	destFileSaveAs = this->tilesetsPath.second;
            endPathWithSlash(destFileSaveAs);
            destFileSaveAs += tileSetName.first;
            destFileSaveAsNewFile = destFileSaveAs;
            endPathWithSlash(destFileSaveAsNewFile);
            destFileSaveAs += ".tmp";
        }
    }
    else {
    	getFolderContents = true;
    	remotePath = tileSetName.first + "/" + tileSetNameSubfolder + "/";
    	destFileSaveAs = this->tilesetsPath.second;
        endPathWithSlash(destFileSaveAs);
        destFileSaveAs += tileSetName.first;
        endPathWithSlash(destFileSaveAs);

        destFileSaveAs += tileSetNameSubfolder;
        destFileSaveAsNewFile = destFileSaveAs;
        endPathWithSlash(destFileSaveAsNewFile);
        destFileSaveAs += ".tmp";
    }

    vector <string> *pWantDirListOnly = NULL;
    if(getFolderContents == true) {
    	pWantDirListOnly = &wantDirListOnly;
    }

    if(SystemFlags::VERBOSE_MODE_ENABLED) printf("FTPClientThread::getTilesetFromServer [%s] remotePath [%s] destFileSaveAs [%s] getFolderContents = %d findArchive = %d\n",tileSetName.first.c_str(),remotePath.c_str(),destFileSaveAs.c_str(),getFolderContents,findArchive);

    pair<FTP_Client_ResultType,string> result = getFileFromServer(
    		ftp_cct_Tileset,
    		tileSetName,
    		remotePath,
    		destFileSaveAs,
    		ftpUser,
    		ftpUserPassword,
    		pWantDirListOnly);

    if(SystemFlags::VERBOSE_MODE_ENABLED) printf("FTPClientThread::getTilesetFromServer [%s] remotePath [%s] destFileSaveAs [%s] getFolderContents = %d result.first = %d [%s] findArchive = %d\n",tileSetName.first.c_str(),remotePath.c_str(),destFileSaveAs.c_str(),getFolderContents,result.first,result.second.c_str(),findArchive);

    // Extract the archive
    if(result.first == ftp_crt_SUCCESS) {
    	if(findArchive == true) {
    	    string destRootArchiveFolder = this->tilesetsPath.second;
   	        endPathWithSlash(destRootArchiveFolder);

			string extractCmd = getFullFileArchiveExtractCommand(
					this->fileArchiveExtractCommand,
					this->fileArchiveExtractCommandParameters,
					destRootArchiveFolder,
					destRootArchiveFolder + tileSetName.first + this->fileArchiveExtension);

			static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
		    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
		    this->getProgressMutex()->setOwnerId(mutexOwnerId);

		    if(this->pCBObject != NULL) {
		    	this->shellCommandCallbackUserData = tileSetName.first;
		        this->pCBObject->FTPClient_CallbackEvent(
		        		tileSetName.first,
		        		ftp_cct_ExtractProgress,
		        		make_pair(ftp_crt_SUCCESS,"extracting"),NULL);
		    }
		    safeMutex.ReleaseLock();

			if(executeShellCommand(extractCmd,this->fileArchiveExtractCommandSuccessResult,this) == false) {
				result.first = ftp_crt_FAIL;
				result.second = "failed to extract archive!";
			}

			return result;
    	}
    	else {
    		if(getFolderContents == true) {
    			removeFile(destFileSaveAs);

    			for(unsigned int i = 0; i < wantDirListOnly.size(); ++i) {
    				string fileFromList = wantDirListOnly[i];

    				if(SystemFlags::VERBOSE_MODE_ENABLED) printf("fileFromList [%s] i [%u]\n",fileFromList.c_str(),i);

    				if( fileFromList != "models" && fileFromList != "textures" &&
    					fileFromList != "sounds") {
						result = getFileFromServer(ftp_cct_Tileset,
								tileSetName,
								remotePath + fileFromList,
								destFileSaveAsNewFile + fileFromList,
								ftpUser, ftpUserPassword);
						if(result.first != ftp_crt_SUCCESS) {
							break;
						}
    				}
    				else {
    					result = getTilesetFromServer(tileSetName,
    							fileFromList, ftpUser, ftpUserPassword,
    							findArchive);
						if(result.first != ftp_crt_SUCCESS) {
							break;
						}
    				}
    			}
    		}
    	}
    }

    if(result.first != ftp_crt_SUCCESS && findArchive == false) {
        string destRootFolder = this->tilesetsPath.second;
        endPathWithSlash(destRootFolder);
        destRootFolder += tileSetName.first;
        endPathWithSlash(destRootFolder);

        removeFolder(destRootFolder);
    }

    return result;
}

void FTPClientThread::getTechtreeFromServer(pair<string,string> techtreeName) {
	pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
	bool findArchive = executeShellCommand(
			this->fileArchiveExtractCommand,
			this->fileArchiveExtractCommandSuccessResult);
	if(findArchive == true) {
		if(techtreeName.second != "") {
			result = getTechtreeFromServer(techtreeName, "", "");
		}
		else {
			result = getTechtreeFromServer(techtreeName, FTP_TECHTREES_CUSTOM_USERNAME, FTP_COMMON_PASSWORD);
			if(result.first == ftp_crt_FAIL && this->getQuitStatus() == false) {
				result = getTechtreeFromServer(techtreeName, FTP_TECHTREES_USERNAME, FTP_COMMON_PASSWORD);
			}
		}
	}

	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
    this->getProgressMutex()->setOwnerId(mutexOwnerId);
    if(this->pCBObject != NULL) {
        this->pCBObject->FTPClient_CallbackEvent(
        		techtreeName.first,
        		ftp_cct_Techtree,
        		result,
        		NULL);
    }
}

pair<FTP_Client_ResultType,string>  FTPClientThread::getTechtreeFromServer(pair<string,string> techtreeName,
		string ftpUser, string ftpUserPassword) {

    // Root folder for the techtree
    string destRootFolder = this->techtreesPath.second;
	endPathWithSlash(destRootFolder);
	string destRootArchiveFolder = destRootFolder;
	destRootFolder += techtreeName.first;
	endPathWithSlash(destRootFolder);

	string destFile = this->techtreesPath.second;
	endPathWithSlash(destFile);
    destFile += techtreeName.first;
    string destFileSaveAs = destFile + this->fileArchiveExtension;
    endPathWithSlash(destFile);

    string remotePath = techtreeName.first + this->fileArchiveExtension;
    if(techtreeName.second != "") {
    	remotePath = techtreeName.second;
    }

    pair<FTP_Client_ResultType,string> result = getFileFromServer(ftp_cct_Techtree,
    		techtreeName, remotePath, destFileSaveAs, ftpUser, ftpUserPassword);

    // Extract the archive
    if(result.first == ftp_crt_SUCCESS) {
        string extractCmd = getFullFileArchiveExtractCommand(
        		this->fileArchiveExtractCommand,
        		this->fileArchiveExtractCommandParameters,
        		destRootArchiveFolder,
        		destRootArchiveFolder + techtreeName.first + this->fileArchiveExtension);

		static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
	    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
	    this->getProgressMutex()->setOwnerId(mutexOwnerId);
	    if(this->pCBObject != NULL) {
	    	this->shellCommandCallbackUserData = techtreeName.first;
	        this->pCBObject->FTPClient_CallbackEvent(
	        		techtreeName.first,
	        		ftp_cct_ExtractProgress,
	        		make_pair(ftp_crt_SUCCESS,"extracting"),NULL);
	    }
	    safeMutex.ReleaseLock();

        if(executeShellCommand(extractCmd,this->fileArchiveExtractCommandSuccessResult,this) == false) {
        	result.first = ftp_crt_FAIL;
        	result.second = "failed to extract archive!";
        }
    }

    return result;

}

void FTPClientThread::getScenarioFromServer(pair<string,string> fileName) {
	pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
	bool findArchive = executeShellCommand(
			this->fileArchiveExtractCommand,
			this->fileArchiveExtractCommandSuccessResult);
	if(findArchive == true) {
		result = getScenarioInternalFromServer(fileName);
	}

	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
    this->getProgressMutex()->setOwnerId(mutexOwnerId);
    if(this->pCBObject != NULL) {
        this->pCBObject->FTPClient_CallbackEvent(
        		fileName.first,
        		ftp_cct_Scenario,
        		result,
        		NULL);
    }
}

pair<FTP_Client_ResultType,string>  FTPClientThread::getScenarioInternalFromServer(pair<string,string> fileName) {
    // Root folder for the techtree
    string destRootFolder = this->scenariosPath.second;
	endPathWithSlash(destRootFolder);
	string destRootArchiveFolder = destRootFolder;
	destRootFolder += fileName.first;
	endPathWithSlash(destRootFolder);

	string destFile = this->scenariosPath.second;
	endPathWithSlash(destFile);
    destFile += fileName.first;
    string destFileSaveAs = destFile + this->fileArchiveExtension;
    endPathWithSlash(destFile);

    string remotePath = fileName.first + this->fileArchiveExtension;
    if(fileName.second != "") {
    	remotePath = fileName.second;
    }

    pair<FTP_Client_ResultType,string> result = getFileFromServer(ftp_cct_Scenario,
    		fileName, remotePath, destFileSaveAs, "", "");

    // Extract the archive
    if(result.first == ftp_crt_SUCCESS) {
        string extractCmd = getFullFileArchiveExtractCommand(
        		this->fileArchiveExtractCommand,
        		this->fileArchiveExtractCommandParameters,
        		destRootArchiveFolder,
        		destRootArchiveFolder + fileName.first + this->fileArchiveExtension);

		static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
	    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
	    this->getProgressMutex()->setOwnerId(mutexOwnerId);
	    if(this->pCBObject != NULL) {
	    	this->shellCommandCallbackUserData = fileName.first;
	        this->pCBObject->FTPClient_CallbackEvent(
	        		fileName.first,
	        		ftp_cct_ExtractProgress,
	        		make_pair(ftp_crt_SUCCESS,"extracting"),NULL);
	    }
	    safeMutex.ReleaseLock();

        if(executeShellCommand(extractCmd,this->fileArchiveExtractCommandSuccessResult,this) == false) {
        	result.first = ftp_crt_FAIL;
        	result.second = "failed to extract archive!";
        }
    }

    return result;

}

void FTPClientThread::getFileFromServer(pair<string,string> fileName) {
	pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");

	bool findArchive = true;
	string ext = extractExtension(fileName.first);
	if(("." + ext) == this->fileArchiveExtension) {
		findArchive = executeShellCommand(
				this->fileArchiveExtractCommand,
				this->fileArchiveExtractCommandSuccessResult);
	}
	if(findArchive == true) {
		result = getFileInternalFromServer(fileName);
	}

	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
    this->getProgressMutex()->setOwnerId(mutexOwnerId);
    if(this->pCBObject != NULL) {
        this->pCBObject->FTPClient_CallbackEvent(fileName.first,ftp_cct_File,result,NULL);
    }
}

pair<FTP_Client_ResultType,string>  FTPClientThread::getFileInternalFromServer(pair<string,string> fileName) {
	string destFile = fileName.first;
	string destFileSaveAs = fileName.first;

	string remotePath = fileName.second;

    pair<FTP_Client_ResultType,string> result = getFileFromServer(ftp_cct_File,
    		fileName,remotePath, destFileSaveAs, "", "");

    //printf("Got file [%s] result.first = %d\n",destFileSaveAs.c_str(),result.first);

    // Extract the archive
    if(result.first == ftp_crt_SUCCESS) {
    	string ext = extractExtension(destFileSaveAs);
    	if(("." + ext) == fileArchiveExtension) {
    		string destRootArchiveFolder = extractDirectoryPathFromFile(destFileSaveAs);
			string extractCmd = getFullFileArchiveExtractCommand(
					this->fileArchiveExtractCommand,
					this->fileArchiveExtractCommandParameters,
					destRootArchiveFolder,
					destFileSaveAs);

			if(executeShellCommand(extractCmd,this->fileArchiveExtractCommandSuccessResult) == false) {
				result.first = ftp_crt_FAIL;
				result.second = "failed to extract archive!";
			}
    	}
    }

    return result;
}

void FTPClientThread::getTempFileFromServer(pair<string,string> fileName) {
	pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");

	bool findArchive = true;
	string ext = extractExtension(fileName.first);
	if(("." + ext) == this->fileArchiveExtension) {
		findArchive = executeShellCommand(
				this->fileArchiveExtractCommand,
				this->fileArchiveExtractCommandSuccessResult);
	}
	if(findArchive == true) {
		result = getTempFileInternalFromServer(fileName);
	}

	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
    this->getProgressMutex()->setOwnerId(mutexOwnerId);
    if(this->pCBObject != NULL) {
        this->pCBObject->FTPClient_CallbackEvent(fileName.first,ftp_cct_TempFile,result,NULL);
    }
}

pair<FTP_Client_ResultType,string>  FTPClientThread::getTempFileInternalFromServer(pair<string,string> fileName) {
	string destFile = fileName.first;
	//string destFileSaveAs = fileName.first;
    string destFileSaveAs = tempFilesPath;

	endPathWithSlash(destFileSaveAs);
	destFileSaveAs += fileName.first;

	string remotePath = fileName.second;

	//printf("First [%s] Second [%s]\n",fileName.first.c_str(),fileName.second.c_str());
	pair<FTP_Client_ResultType,string> result;
	if(StartsWith(remotePath,"http://")) {
		result = getFileFromServer(ftp_cct_TempFile, fileName,remotePath, destFileSaveAs, "", "");
	}
	else {
		fileName.second = "";
		result = getFileFromServer(ftp_cct_TempFile,fileName,remotePath, destFileSaveAs, FTP_TEMPFILES_USERNAME, FTP_COMMON_PASSWORD);
	}

    //printf("Got temp file [%s] result.first = %d\n",destFileSaveAs.c_str(),result.first);

    // Extract the archive
    if(result.first == ftp_crt_SUCCESS) {
    	string ext = extractExtension(destFileSaveAs);
    	if(("." + ext) == fileArchiveExtension) {
    		string destRootArchiveFolder = extractDirectoryPathFromFile(destFileSaveAs);
			string extractCmd = getFullFileArchiveExtractCommand(
					this->fileArchiveExtractCommand,
					this->fileArchiveExtractCommandParameters,
					destRootArchiveFolder,
					destFileSaveAs);

			if(executeShellCommand(extractCmd,this->fileArchiveExtractCommandSuccessResult) == false) {
				result.first = ftp_crt_FAIL;
				result.second = "failed to extract archive!";
			}
    	}
    }

    return result;
}

pair<FTP_Client_ResultType,string>  FTPClientThread::getFileFromServer(FTP_Client_CallbackType downloadType,
		pair<string,string> fileNameTitle,
		string remotePath, string destFileSaveAs,
		string ftpUser, string ftpUserPassword, vector <string> *wantDirListOnly) {
	pair<FTP_Client_ResultType,string> result = make_pair(ftp_crt_FAIL,"");
    if(wantDirListOnly) {
    	(*wantDirListOnly).clear();
    }
    string destRootFolder = extractDirectoryPathFromFile(destFileSaveAs);
    bool pathCreated = false;
    if(isdir(destRootFolder.c_str()) == false) {
    	createDirectoryPaths(destRootFolder);
    	pathCreated = true;
    }

    bool wantDirList = (wantDirListOnly != NULL);

    if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread about to try to RETR into [%s] wantDirList = %d\n",destFileSaveAs.c_str(),wantDirList);
    if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client thread about to try to RETR into [%s] wantDirList = %d\n",destFileSaveAs.c_str(),wantDirList);

    struct FtpFile ftpfile = {
    	fileNameTitle.first.c_str(),
    	destFileSaveAs.c_str(), // name to store the file as if successful
    	NULL,
        NULL,
        this,
        "",
        false,
        downloadType
    };

    CURL *curl = SystemFlags::initHTTP();
    if(curl) {
        ftpfile.stream = NULL;

        char szBuf[8096]="";
        if(fileNameTitle.second != "") {
        	snprintf(szBuf,8096,"%s",fileNameTitle.second.c_str());
        	curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
        }
        else {
        	snprintf(szBuf,8096,"ftp://%s:%s@%s:%d/%s",ftpUser.c_str(),ftpUserPassword.c_str(),serverUrl.c_str(),portNumber,remotePath.c_str());
        }

        //printf("===> Getting ftp file: %s\n",szBuf);

        curl_easy_setopt(curl, CURLOPT_URL,szBuf);
        curl_easy_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);

        // turn on wildcard matching
        //curl_easy_setopt(curl, CURLOPT_WILDCARDMATCH, 1L);

        // callback is called before download of concrete file started
        //curl_easy_setopt(curl, CURLOPT_CHUNK_BGN_FUNCTION, file_is_comming);
        // callback is called after data from the file have been transferred
        //curl_easy_setopt(curl, CURLOPT_CHUNK_END_FUNCTION, file_is_downloaded);

        //curl_easy_setopt(curl, CURLOPT_CHUNK_DATA, &ftpfile);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

        // Define our callback to get called when there's data to be written
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
        // Set a pointer to our struct to pass to the callback
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

        if(wantDirListOnly) {
        	curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1);
        }
        curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
        curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, file_progress);
        curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &ftpfile);

        // Max 10 minutes to transfer
        //curl_easy_setopt(curl, CURLOPT_TIMEOUT, 600);
        // Max 60 minutes to transfer
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3600L);
        curl_easy_setopt(curl, CURLOPT_FTP_RESPONSE_TIMEOUT, 120L);

        // Switch on full protocol/debug output
        if(SystemFlags::VERBOSE_MODE_ENABLED) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        CURLcode res = curl_easy_perform(curl);

        if(res != CURLE_OK) {
        	result.second = curl_easy_strerror(res);
            // we failed
            printf("curl FAILED with: %d [%s] attempting to remove folder contents [%s] szBuf [%s] ftpfile.isValidXfer = %d, pathCreated = %d\n", res,curl_easy_strerror(res),destRootFolder.c_str(),szBuf,ftpfile.isValidXfer,pathCreated);
            if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"curl FAILED with: %d [%s] attempting to remove folder contents [%s] szBuf [%s] ftpfile.isValidXfer = %d, pathCreated = %d\n", res,curl_easy_strerror(res),destRootFolder.c_str(),szBuf,ftpfile.isValidXfer,pathCreated);

            if(res == CURLE_PARTIAL_FILE || ftpfile.isValidXfer == true) {
        	    result.first = ftp_crt_PARTIALFAIL;
            }
            else if(res == CURLE_COULDNT_CONNECT) {
          	  result.first = ftp_crt_HOST_NOT_ACCEPTING;
            }


            if(destRootFolder != "") {
            	if(pathCreated == true) {
            		removeFolder(destRootFolder);
            	}
            	else {
            		removeFile(destFileSaveAs);
            	}
            }
        }
        else {
            result.first = ftp_crt_SUCCESS;

            if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] result.first = %d wantDirListOnly = %p\n",__FILE__,__FUNCTION__,__LINE__,result.first,wantDirListOnly);

            if(wantDirListOnly) {
                if(ftpfile.stream) {
                    fclose(ftpfile.stream);
                    ftpfile.stream = NULL;
                }

#ifdef WIN32
				FILE *fp = _wfopen(utf8_decode(destFileSaveAs).c_str(), L"rt");
#else
                FILE *fp = fopen(destFileSaveAs.c_str(), "rt");
#endif
                if(fp != NULL) {
                	char szBuf[4096]="";
                	while(feof(fp) == false) {
            			if(fgets( szBuf, 4095, fp) != NULL) {
            				string item = szBuf;
            				replaceAll(item,"\n","");
            				replaceAll(item,"\r","");
            				if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Got [%s]\n",item.c_str());
            				(*wantDirListOnly).push_back(item);
            			}
                	}
                	fclose(fp);
                }
            }
        }

        SystemFlags::cleanupHTTP(&curl);
    }

    if(ftpfile.stream) {
        fclose(ftpfile.stream);
        ftpfile.stream = NULL;
    }

    if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] result.first = %d\n",__FILE__,__FUNCTION__,__LINE__,result.first);

    return result;
}

FTPClientCallbackInterface * FTPClientThread::getCallBackObject() {
	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
    this->getProgressMutex()->setOwnerId(mutexOwnerId);
    return pCBObject;
}

void FTPClientThread::setCallBackObject(FTPClientCallbackInterface *value) {
	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
    MutexSafeWrapper safeMutex(this->getProgressMutex(),mutexOwnerId);
    this->getProgressMutex()->setOwnerId(mutexOwnerId);
    pCBObject = value;
}

void FTPClientThread::ShellCommandOutput_CallbackEvent(string cmd,char *output,void *userdata) {
    if(this->pCBObject != NULL) {

    	string &itemName = *static_cast<string *>(userdata);
        this->pCBObject->FTPClient_CallbackEvent(
        		itemName,
        		ftp_cct_ExtractProgress,
        		make_pair(ftp_crt_SUCCESS,"extracting"),
        		output);
    }
}

void * FTPClientThread::getShellCommandOutput_UserData(string cmd) {
	return &shellCommandCallbackUserData;
}

void FTPClientThread::execute() {
    {
        RunningStatusSafeWrapper runningStatus(this);
        if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

        if(getQuitStatus() == true) {
            return;
        }

        if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("===> FTP Client thread is running\n");
        if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"FTP Client thread is running\n");

        try	{
            while(this->getQuitStatus() == false) {
            	static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
                MutexSafeWrapper safeMutex(&mutexMapFileList,mutexOwnerId);
                mutexMapFileList.setOwnerId(mutexOwnerId);
                if(mapFileList.size() > 0) {
                    pair<string,string> mapFilename = mapFileList[0];
                    mapFileList.erase(mapFileList.begin() + 0);
                    safeMutex.ReleaseLock();

                    getMapFromServer(mapFilename);
                }
                else {
                    safeMutex.ReleaseLock();
                }

                if(this->getQuitStatus() == true) {
                    break;
                }

                static string mutexOwnerId2 = string(__FILE__) + string("_") + intToStr(__LINE__);
                MutexSafeWrapper safeMutex2(&mutexTilesetList,mutexOwnerId2);
                mutexTilesetList.setOwnerId(mutexOwnerId2);
                if(tilesetList.size() > 0) {
                	pair<string,string> tileset = tilesetList[0];
                    tilesetList.erase(tilesetList.begin() + 0);
                    safeMutex2.ReleaseLock();

                    getTilesetFromServer(tileset);
                }
                else {
                    safeMutex2.ReleaseLock();
                }

                static string mutexOwnerId3 = string(__FILE__) + string("_") + intToStr(__LINE__);
                MutexSafeWrapper safeMutex3(&mutexTechtreeList,mutexOwnerId3);
                mutexTechtreeList.setOwnerId(mutexOwnerId3);
                if(techtreeList.size() > 0) {
                	pair<string,string> techtree = techtreeList[0];
                    techtreeList.erase(techtreeList.begin() + 0);
                    safeMutex3.ReleaseLock();

                    getTechtreeFromServer(techtree);
                }
                else {
                    safeMutex3.ReleaseLock();
                }

                static string mutexOwnerId4 = string(__FILE__) + string("_") + intToStr(__LINE__);
                MutexSafeWrapper safeMutex4(&mutexScenarioList,mutexOwnerId4);
                mutexScenarioList.setOwnerId(mutexOwnerId4);
                if(scenarioList.size() > 0) {
                	pair<string,string> file = scenarioList[0];
                	scenarioList.erase(scenarioList.begin() + 0);
                    safeMutex4.ReleaseLock();

                    getScenarioFromServer(file);
                }
                else {
                    safeMutex4.ReleaseLock();
                }

                static string mutexOwnerId5 = string(__FILE__) + string("_") + intToStr(__LINE__);
                MutexSafeWrapper safeMutex5(&mutexFileList,mutexOwnerId5);
                mutexFileList.setOwnerId(mutexOwnerId5);
                if(fileList.size() > 0) {
                	pair<string,string> file = fileList[0];
                	fileList.erase(fileList.begin() + 0);
                    safeMutex5.ReleaseLock();

                    getFileFromServer(file);
                }
                else {
                    safeMutex5.ReleaseLock();
                }

                static string mutexOwnerId6 = string(__FILE__) + string("_") + intToStr(__LINE__);
                MutexSafeWrapper safeMutex6(&mutexTempFileList,mutexOwnerId6);
                mutexTempFileList.setOwnerId(mutexOwnerId6);
                if(tempFileList.size() > 0) {
                	pair<string,string> file = tempFileList[0];
                	tempFileList.erase(tempFileList.begin() + 0);
                    safeMutex6.ReleaseLock();

                    getTempFileFromServer(file);
                }
                else {
                    safeMutex6.ReleaseLock();
                }

                if(this->getQuitStatus() == false) {
                    sleep(25);
                }
            }

            if(SystemFlags::VERBOSE_MODE_ENABLED) printf("===> FTP Client exiting!\n");
            if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"===> FTP Client exiting!\n");
        }
        catch(const exception &ex) {
            SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
            if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
        }
        catch(...) {
            SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",__FILE__,__FUNCTION__,__LINE__);
            if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] unknown error\n",__FILE__,__FUNCTION__,__LINE__);
        }

        if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] FTP Client thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
    }

    // Delete ourself when the thread is done (no other actions can happen after this
    // such as the mutex which modifies the running status of this method
    deleteSelfIfRequired();
}

}}//end namespace