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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
/*=============================================================================
                                client.cpp
===============================================================================
  This is the C++ XML-RPC client library for Xmlrpc-c.

  Note that unlike most of Xmlprc-c's C++ API, this is _not_ based on the
  C client library.  This code is independent of the C client library, and
  is based directly on the client XML transport libraries (with a little
  help from internal C utility libraries).
=============================================================================*/

#include <stdlib.h>
#include <cassert>
#include <string>
#include <vector>

#include "xmlrpc-c/girerr.hpp"
using girerr::error;
using girerr::throwf;
#include "xmlrpc-c/girmem.hpp"
using girmem::autoObjectPtr;
using girmem::autoObject;
#include "xmlrpc-c/env_wrap.hpp"
#include "xmlrpc-c/util_int.h"
#include "xmlrpc-c/base.h"
#include "xmlrpc-c/client.h"
#include "xmlrpc-c/transport.h"
#include "xmlrpc-c/base.hpp"
#include "xmlrpc-c/xml.hpp"
#include "xmlrpc-c/timeout.hpp"
#include "xmlrpc-c/client.hpp"
#include "transport_config.h"

using namespace std;
using namespace xmlrpc_c;


namespace {

void
throwIfError(env_wrap const& env) {

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



class memblockStringWrapper {

public:    
    memblockStringWrapper(string const value) {

        env_wrap env;

        this->memblockP = XMLRPC_MEMBLOCK_NEW(char, &env.env_c, 0);
        throwIfError(env);

        XMLRPC_MEMBLOCK_APPEND(char, &env.env_c, this->memblockP,
                               value.c_str(), value.size());
        throwIfError(env);
    }
    
    memblockStringWrapper(xmlrpc_mem_block * const memblockP) :
        memblockP(memblockP) {};

    ~memblockStringWrapper() {
        XMLRPC_MEMBLOCK_FREE(char, this->memblockP);
    }

    xmlrpc_mem_block * memblockP;
};

} // namespace

namespace xmlrpc_c {

struct client_xml_impl {
    /* We have both kinds of pointers to give the user flexibility -- we
       have constructors that take both.  But the simple pointer
       'transportP' is valid in both cases.
    */
    clientXmlTransport * transportP;
    clientXmlTransportPtr transportPtr;
    xmlrpc_dialect dialect;

    client_xml_impl(clientXmlTransport * const transportP,
                    xmlrpc_dialect       const dialect = xmlrpc_dialect_i8) :
        transportP(transportP),
        dialect(dialect) {}

    client_xml_impl(clientXmlTransportPtr const transportPtr,
                    clientXmlTransport *  const transportP,
                    xmlrpc_dialect        const dialect = xmlrpc_dialect_i8) :
        transportP(transportP),
        transportPtr(transportPtr),
        dialect(dialect) {}
};
     


carriageParm::carriageParm() {}



carriageParm::~carriageParm() {}



carriageParmPtr::carriageParmPtr() {
    // Base class constructor will construct pointer that points to nothing
}



carriageParmPtr::carriageParmPtr(
carriageParm * const carriageParmP) : autoObjectPtr(carriageParmP) {}



carriageParm *
carriageParmPtr::operator->() const {

    autoObject * const p(this->objectP);
    return dynamic_cast<carriageParm *>(p);
}



carriageParm *
carriageParmPtr::get() const {
    return dynamic_cast<carriageParm *>(this->objectP);
}



carriageParm_http0::carriageParm_http0() :
    c_serverInfoP(NULL) {}



carriageParm_http0::carriageParm_http0(string const serverUrl) {
    this->c_serverInfoP = NULL;

    this->instantiate(serverUrl);
}



carriageParm_http0::~carriageParm_http0() {

    if (this->c_serverInfoP)
        xmlrpc_server_info_free(this->c_serverInfoP);
}



void
carriageParm_http0::instantiate(string const serverUrl) {

    if (c_serverInfoP)
        throw(error("object already instantiated"));
    
    env_wrap env;

    this->c_serverInfoP =
        xmlrpc_server_info_new(&env.env_c, serverUrl.c_str());
    throwIfError(env);
}



void
carriageParm_http0::setUser(string const userid,
                            string const password) {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_set_user(
        &env.env_c, this->c_serverInfoP, userid.c_str(), password.c_str());

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::allowAuthBasic() {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_allow_auth_basic(&env.env_c, this->c_serverInfoP);

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::disallowAuthBasic() {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_disallow_auth_basic(&env.env_c, this->c_serverInfoP);

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::allowAuthDigest() {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_allow_auth_digest(&env.env_c, this->c_serverInfoP);

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::disallowAuthDigest() {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_disallow_auth_digest(&env.env_c, this->c_serverInfoP);

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::allowAuthNegotiate() {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_allow_auth_negotiate(&env.env_c, this->c_serverInfoP);

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::disallowAuthNegotiate() {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_disallow_auth_negotiate(
        &env.env_c, this->c_serverInfoP);

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::allowAuthNtlm() {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_allow_auth_ntlm(&env.env_c, this->c_serverInfoP);

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::disallowAuthNtlm() {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));

    env_wrap env;

    xmlrpc_server_info_disallow_auth_ntlm(&env.env_c, this->c_serverInfoP);

    if (env.env_c.fault_occurred)
        throw(error(env.env_c.fault_string));
}



void
carriageParm_http0::setBasicAuth(string const username,
                                 string const password) {

    if (!this->c_serverInfoP)
        throw(error("object not instantiated"));
    
    env_wrap env;

    xmlrpc_server_info_set_basic_auth(
        &env.env_c, this->c_serverInfoP, username.c_str(), password.c_str());
    throwIfError(env);
}



carriageParm_http0Ptr::carriageParm_http0Ptr() {
    // Base class constructor will construct pointer that points to nothing
}



carriageParm_http0Ptr::carriageParm_http0Ptr(
    carriageParm_http0 * const carriageParmP) :
    carriageParmPtr(carriageParmP) {}



carriageParm_http0 *
carriageParm_http0Ptr::operator->() const {

    autoObject * const p(this->objectP);
    return dynamic_cast<carriageParm_http0 *>(p);
}



xmlTransaction::xmlTransaction() {}



void
xmlTransaction::finish(string const& responseXml) const {

    xml::trace("XML-RPC RESPONSE", responseXml);
}



void
xmlTransaction::finishErr(error const&) const {

}



void
xmlTransaction::progress(struct xmlrpc_progress_data const&) const {

    // This is just the base class method.  A derived class may override
    // this with something substantial.
}



xmlTransactionPtr::xmlTransactionPtr() {}



xmlTransactionPtr::xmlTransactionPtr(xmlTransaction * xmlTransP) :
    autoObjectPtr(xmlTransP) {}
 


xmlTransaction *
xmlTransactionPtr::operator->() const {
    autoObject * const p(this->objectP);
    return dynamic_cast<xmlTransaction *>(p);
}



struct xmlTranCtl {
/*----------------------------------------------------------------------------
   This contains information needed to conduct a transaction.  You
   construct it as you start the transaction and destroy it after the
   work is done.  You need this only for an asynchronous one, because
   where the user starts and finishes the RPC in the same
   libxmlrpc_client call, you can just keep this information in
   various stack variables, and it's faster and easier to understand
   that way.

   The C transport is designed to take a xmlrpc_call_info argument for
   similar stuff needed by the the C client object.  But it's really
   opaque to the transport, so we just let xmlTranCtl masquerade as
   xmlprc_call_info in our call to the C transport.
-----------------------------------------------------------------------------*/
    xmlTranCtl(xmlTransactionPtr const& xmlTranP,
               string            const& callXml) :

        xmlTranP(xmlTranP) {

        env_wrap env;

        this->callXmlP = XMLRPC_MEMBLOCK_NEW(char, &env.env_c, 0);
        throwIfError(env);

        XMLRPC_MEMBLOCK_APPEND(char, &env.env_c, this->callXmlP,
                               callXml.c_str(), callXml.size());
        throwIfError(env);
    }
    
    ~xmlTranCtl() {
        XMLRPC_MEMBLOCK_FREE(char, this->callXmlP);
    }

    xmlTransactionPtr const xmlTranP;
        // The transaction we're controlling.  Most notable use of this is
        // that this object we inform when the transaction is done.  This
        // is where the response XML and other transaction results go.

    xmlrpc_mem_block * callXmlP;
        // The XML of the call.  This is what the transport transports.
};



clientXmlTransport::~clientXmlTransport() {}



void
clientXmlTransport::start(carriageParm *    const  carriageParmP,
                          string            const& callXml,
                          xmlTransactionPtr const& xmlTranP) {

    // Note: derived class clientXmlTransport_http overrides this,
    // so it doesn't normally get used.
    
    string responseXml;

    this->call(carriageParmP, callXml, &responseXml);

    xmlTranP->finish(responseXml);
}



void
clientXmlTransport::finishAsync(xmlrpc_c::timeout) {

    // Since our start() does the whole thing, there's nothing for
    // us to do.

    // A derived class that overrides start() with something properly
    // asynchronous had better also override finishAsync() with something
    // substantial.
}



void
clientXmlTransport::asyncComplete(
    struct xmlrpc_call_info * const callInfoP,
    xmlrpc_mem_block *        const responseXmlMP,
    xmlrpc_env                const transportEnv) {

    xmlTranCtl * const xmlTranCtlP = reinterpret_cast<xmlTranCtl *>(callInfoP);

    try {
        if (transportEnv.fault_occurred) {
            xmlTranCtlP->xmlTranP->finishErr(error(transportEnv.fault_string));
        } else {
            string const responseXml(
                XMLRPC_MEMBLOCK_CONTENTS(char, responseXmlMP),
                XMLRPC_MEMBLOCK_SIZE(char, responseXmlMP));
            xmlTranCtlP->xmlTranP->finish(responseXml);
        }
    } catch(exception const&) {
        /* We can't throw an error back to C code, and the async_complete
           interface does not provide for failure, so we define ->finish()
           as not being capable of throwing an error.
        */
        assert(false);
    }
    delete(xmlTranCtlP);

    /* Ordinarily, *xmlTranCtlP is the last reference to
       xmlTranCtlP->xmlTranP, so that will get destroyed too.  But
       ->finish() could conceivably create a new reference to
       xmlTranCtlP->xmlTranP, and then it would keep living.
    */
}



void
clientXmlTransport::progress(
    struct xmlrpc_call_info *   const callInfoP,
    struct xmlrpc_progress_data const progressData) {

    xmlTranCtl * const xmlTranCtlP = reinterpret_cast<xmlTranCtl *>(callInfoP);

    xmlTranCtlP->xmlTranP->progress(progressData);
}



void
clientXmlTransport::setInterrupt(int *) {

    throwf("The client XML transport is not interruptible");
}



clientXmlTransportPtr::clientXmlTransportPtr() {
    // Base class constructor will construct pointer that points to nothing
}



clientXmlTransportPtr::clientXmlTransportPtr(
    clientXmlTransport * const transportP) : autoObjectPtr(transportP) {}



clientXmlTransport *
clientXmlTransportPtr::get() const {
    return dynamic_cast<clientXmlTransport *>(objectP);
}



clientXmlTransport *
clientXmlTransportPtr::operator->() const {

    autoObject * const p(this->objectP);
    return dynamic_cast<clientXmlTransport *>(p);
}



clientXmlTransport_http::~clientXmlTransport_http() {}



void
clientXmlTransport_http::call(
    carriageParm * const  carriageParmP,
    string         const& callXml,
    string *       const  responseXmlP) {

    carriageParm_http0 * const carriageParmHttpP =
        dynamic_cast<carriageParm_http0 *>(carriageParmP);

    if (carriageParmHttpP == NULL)
        throw(error("HTTP client XML transport called with carriage "
                    "parameter object not of class carriageParm_http"));

    memblockStringWrapper callXmlM(callXml);

    xmlrpc_mem_block * responseXmlMP;

    env_wrap env;

    this->c_transportOpsP->call(&env.env_c,
                                this->c_transportP,
                                carriageParmHttpP->c_serverInfoP,
                                callXmlM.memblockP,
                                &responseXmlMP);

    throwIfError(env);

    memblockStringWrapper responseHolder(responseXmlMP);
        // Makes responseXmlMP get freed at end of scope
    
    *responseXmlP = string(XMLRPC_MEMBLOCK_CONTENTS(char, responseXmlMP),
                           XMLRPC_MEMBLOCK_SIZE(char, responseXmlMP));
}



void
clientXmlTransport_http::start(
    carriageParm *    const  carriageParmP,
    string            const& callXml,
    xmlTransactionPtr const& xmlTranP) {

    env_wrap env;

    carriageParm_http0 * const carriageParmHttpP =
        dynamic_cast<carriageParm_http0 *>(carriageParmP);

    if (carriageParmHttpP == NULL)
        throw(error("HTTP client XML transport called with carriage "
                    "parameter object not of type carriageParm_http"));

    xmlTranCtl * const tranCtlP(new xmlTranCtl(xmlTranP, callXml));

    try {
        this->c_transportOpsP->send_request(
            &env.env_c,
            this->c_transportP,
            carriageParmHttpP->c_serverInfoP,
            tranCtlP->callXmlP,
            &this->asyncComplete, &this->progress,
            reinterpret_cast<xmlrpc_call_info *>(tranCtlP));

        throwIfError(env);
    } catch (...) {
        delete tranCtlP;
        throw;
    }
}



void
clientXmlTransport_http::finishAsync(xmlrpc_c::timeout const timeout) {

    xmlrpc_timeoutType const c_timeoutType(
        timeout.finite ? timeout_yes : timeout_no);
    xmlrpc_timeout const c_timeout(timeout.duration);

    this->c_transportOpsP->finish_asynch(
        this->c_transportP, c_timeoutType, c_timeout);
}



void
clientXmlTransport_http::setInterrupt(int * const interruptP) {

    if (this->c_transportOpsP->set_interrupt)
        this->c_transportOpsP->set_interrupt(this->c_transportP, interruptP);
}



bool const haveCurl(
#if MUST_BUILD_CURL_CLIENT
true
#else
false
#endif
);

bool const haveLibwww(
#if MUST_BUILD_LIBWWW_CLIENT
true
#else
false
#endif
);

bool const haveWininet(
#if MUST_BUILD_WININET_CLIENT
true
#else
false
#endif
);



vector<string>
clientXmlTransport_http::availableTypes() {

    vector<string> retval;

    if (haveCurl)
        retval.push_back("curl");

    if (haveLibwww)
        retval.push_back("libwww");

    if (haveWininet)
        retval.push_back("wininet");

    return retval;
}



clientXmlTransportPtr
clientXmlTransport_http::create() {
/*----------------------------------------------------------------------------
  Make an HTTP Client XML transport of any kind (Caller doesn't care).

  Caller can find out what kind he got by trying dynamic casts.

  Caller can use a carriageParm_http0 with the transport.
-----------------------------------------------------------------------------*/
    if (haveCurl)
        return clientXmlTransportPtr(new clientXmlTransport_curl());
    else if (haveLibwww)
        return clientXmlTransportPtr(new clientXmlTransport_libwww());
    else if (haveWininet)
        return clientXmlTransportPtr(new clientXmlTransport_wininet());
    else
        throwf("This XML-RPC client library contains no HTTP XML transports");
}



clientTransaction::clientTransaction() {}



clientTransactionPtr::clientTransactionPtr() {}



clientTransactionPtr::clientTransactionPtr(
    clientTransaction * const transP) : autoObjectPtr(transP) {}



clientTransactionPtr::~clientTransactionPtr() {}



clientTransaction *
clientTransactionPtr::operator->() const {
    autoObject * const p(this->objectP);
    return dynamic_cast<clientTransaction *>(p);
}



client::~client() {}



void
client::start(carriageParm *       const  carriageParmP,
              string               const& methodName,
              paramList            const& paramList,
              clientTransactionPtr const& tranP) {
/*----------------------------------------------------------------------------
   Start an RPC, wait for it to complete, and finish it.

   Usually, a derived class overrides this with something that does
   not wait for the RPC to complete, but rather arranges for something
   to finish the RPC later when the RPC does complete.
-----------------------------------------------------------------------------*/
    rpcOutcome outcome;

    this->call(carriageParmP, methodName, paramList, &outcome);

    tranP->finish(outcome);
}



void
client::finishAsync(xmlrpc_c::timeout) {

    // Since our start() does the whole thing, there's nothing for
    // us to do.

    // A derived class that overrides start() with something properly
    // asynchronous had better also override finishAsync() with something
    // substantial.
}



void
client::setInterrupt(int *) {

    throwf("Clients of this type are not interruptible");
}



clientPtr::clientPtr() {
    // Base class constructor will construct pointer that points to nothing
}



clientPtr::clientPtr(
    client * const clientP) : autoObjectPtr(clientP) {}



client *
clientPtr::operator->() const {

    autoObject * const p(this->objectP);
    return dynamic_cast<client *>(p);
}



client *
clientPtr::get() const {
    return dynamic_cast<client *>(objectP);
}



client_xml::client_xml(clientXmlTransport * const transportP) {

    this->implP = new client_xml_impl(transportP);
}



client_xml::client_xml(clientXmlTransportPtr const transportPtr) {

    this->implP = new client_xml_impl(transportPtr, transportPtr.get());
}



client_xml::client_xml(clientXmlTransport * const transportP,
                       xmlrpc_dialect       const dialect) {

    this->implP = new client_xml_impl(transportP, dialect);
}



client_xml::client_xml(clientXmlTransportPtr const transportPtr,
                       xmlrpc_dialect        const dialect) {

    this->implP = new client_xml_impl(transportPtr, transportPtr.get(),
                                      dialect);
}



client_xml::~client_xml() {

    delete(this->implP);
}



void
client_xml::call(carriageParm * const  carriageParmP,
                 string         const& methodName,
                 paramList      const& paramList,
                 rpcOutcome *   const  outcomeP) {

    string callXml;
    string responseXml;

    xml::generateCall(methodName, paramList, this->implP->dialect, &callXml);
    
    xml::trace("XML-RPC CALL", callXml);

    try {
        this->implP->transportP->call(carriageParmP, callXml, &responseXml);
    } catch (exception const& e) {
        throwf("Unable to transport XML to server and "
               "get XML response back.  %s", e.what());
    }
    xml::trace("XML-RPC RESPONSE", responseXml);
        
    try {
        xml::parseResponse(responseXml, outcomeP);
    } catch (exception const& e) {
        throwf("Response XML from server is not valid XML-RPC response.  %s",
               e.what());
    }
}
 


void
client_xml::start(carriageParm *       const  carriageParmP,
                  string               const& methodName,
                  paramList            const& paramList,
                  clientTransactionPtr const& tranP) {

    string callXml;

    xml::generateCall(methodName, paramList, this->implP->dialect, &callXml);
    
    xml::trace("XML-RPC CALL", callXml);

    xmlTransaction_clientPtr const xmlTranP(tranP);

    this->implP->transportP->start(carriageParmP, callXml, xmlTranP);
}
 


void
client_xml::finishAsync(xmlrpc_c::timeout const timeout) {

    this->implP->transportP->finishAsync(timeout);
}



void
client_xml::setInterrupt(int * const interruptP) {

    this->implP->transportP->setInterrupt(interruptP);
}



serverAccessor::serverAccessor(clientPtr       const clientP,
                               carriageParmPtr const carriageParmP) :

    clientP(clientP), carriageParmP(carriageParmP) {};



void
serverAccessor::call(std::string            const& methodName,
                     xmlrpc_c::paramList    const& paramList,
                     xmlrpc_c::rpcOutcome * const  outcomeP) const {

    this->clientP->call(this->carriageParmP.get(),
                        methodName,
                        paramList,
                        outcomeP);
}



serverAccessorPtr::serverAccessorPtr() {
    // Base class constructor will construct pointer that points to nothing
}



serverAccessorPtr::serverAccessorPtr(
    serverAccessor * const serverAccessorParmP) :
    autoObjectPtr(serverAccessorParmP) {}



serverAccessor *
serverAccessorPtr::operator->() const {

    autoObject * const p(this->objectP);
    return dynamic_cast<serverAccessor *>(p);
}



serverAccessor *
serverAccessorPtr::get() const {
    return dynamic_cast<serverAccessor *>(objectP);
}



connection::connection(client *       const clientP,
                       carriageParm * const carriageParmP) :
    clientP(clientP), carriageParmP(carriageParmP) {}



connection::~connection() {}



struct rpc_impl {
    enum state {
        STATE_UNFINISHED,  // RPC is running or not started yet
        STATE_ERROR,       // We couldn't execute the RPC
        STATE_FAILED,      // RPC executed successfully, but failed per XML-RPC
        STATE_SUCCEEDED    // RPC is done, no exception
    };
    enum state state;
    girerr::error * errorP;     // Defined only in STATE_ERROR
    rpcOutcome outcome;
        // Defined only in STATE_FAILED and STATE_SUCCEEDED
    string methodName;
    xmlrpc_c::paramList paramList;

    rpc_impl(string              const& methodName,
             xmlrpc_c::paramList const& paramList) :
        state(STATE_UNFINISHED),
        methodName(methodName),
        paramList(paramList) {}
};



rpc::rpc(string    const  methodName,
         paramList const& paramList) {
    
    this->implP = new rpc_impl(methodName, paramList);
}



rpc::~rpc() {
    
    if (this->implP->state == rpc_impl::STATE_ERROR)
        delete(this->implP->errorP);

    delete(this->implP);
}



void
rpc::call(client       * const clientP,
          carriageParm * const carriageParmP) {

    if (this->implP->state != rpc_impl::STATE_UNFINISHED)
        throw(error("Attempt to execute an RPC that has already been "
                    "executed"));

    clientP->call(carriageParmP,
                  this->implP->methodName,
                  this->implP->paramList,
                  &this->implP->outcome);

    this->implP->state = this->implP->outcome.succeeded() ?
        rpc_impl::STATE_SUCCEEDED : rpc_impl::STATE_FAILED;
}



void
rpc::call(connection const& connection) {

    this->call(connection.clientP, connection.carriageParmP);

}


 
void
rpc::start(client       * const clientP,
           carriageParm * const carriageParmP) {
    
    if (this->implP->state != rpc_impl::STATE_UNFINISHED)
        throw(error("Attempt to execute an RPC that has already been "
                    "executed"));

    clientP->start(carriageParmP,
                   this->implP->methodName,
                   this->implP->paramList,
                   rpcPtr(this));
}


 
void
rpc::start(xmlrpc_c::connection const& connection) {
    
    this->start(connection.clientP, connection.carriageParmP);
}



void
rpc::finish(rpcOutcome const& outcome) {

    this->implP->state =
        outcome.succeeded() ?
        rpc_impl::STATE_SUCCEEDED : rpc_impl::STATE_FAILED;

    this->implP->outcome = outcome;

    this->notifyComplete();
}



void
rpc::finishErr(error const& error) {

    this->implP->state = rpc_impl::STATE_ERROR;
    this->implP->errorP = new girerr::error(error);
    this->notifyComplete();
}



void
rpc::notifyComplete() {
/*----------------------------------------------------------------------------
   Anyone who does RPCs asynchronously and doesn't use polling will
   want to make his own class derived from 'rpc' and override this
   with a notifyFinish() that does something.

   Typically, notifyFinish() will queue the RPC so some other thread
   will deal with the fact that the RPC is finished.


   In the absence of the aforementioned queueing, the RPC becomes
   unreferenced as soon as our Caller releases his reference, so the
   RPC gets destroyed when we return.
-----------------------------------------------------------------------------*/

}



void
rpc::progress(struct xmlrpc_progress_data const&) const {
/*----------------------------------------------------------------------------
  If the user is interested in tracking the progress of the RPC, he will
  derive a class from xmlrpc_c::rpc and override this with a progress()
  that does something, such as display a progress bar.
-----------------------------------------------------------------------------*/

}
    


value
rpc::getResult() const {

    switch (this->implP->state) {
    case rpc_impl::STATE_UNFINISHED:
        throw(error("Attempt to get result of RPC that is not finished."));
        break;
    case rpc_impl::STATE_ERROR:
        throw(*this->implP->errorP);
        break;
    case rpc_impl::STATE_FAILED:
        throw(error("RPC response indicates failure.  " +
                    this->implP->outcome.getFault().getDescription()));
        break;
    case rpc_impl::STATE_SUCCEEDED: {
        // All normal
    }
    }

    return this->implP->outcome.getResult();
}




fault
rpc::getFault() const {

    switch (this->implP->state) {
    case rpc_impl::STATE_UNFINISHED:
        throw(error("Attempt to get fault from RPC that is not finished"));
        break;
    case rpc_impl::STATE_ERROR:
        throw(*this->implP->errorP);
        break;
    case rpc_impl::STATE_SUCCEEDED:
        throw(error("Attempt to get fault from an RPC that succeeded"));
        break;
    case rpc_impl::STATE_FAILED: {
        // All normal
    }
    }

    return this->implP->outcome.getFault();
}



bool
rpc::isFinished() const {
    return (this->implP->state != rpc_impl::STATE_UNFINISHED);
}



bool
rpc::isSuccessful() const {
    return (this->implP->state == rpc_impl::STATE_SUCCEEDED);
}



rpcPtr::rpcPtr() {}



rpcPtr::rpcPtr(rpc * const rpcP) : clientTransactionPtr(rpcP) {}



rpcPtr::rpcPtr(string              const  methodName,
               xmlrpc_c::paramList const& paramList) :
                   clientTransactionPtr(new rpc(methodName, paramList)) {}



rpc *
rpcPtr::operator->() const {

    autoObject * const p(this->objectP);
    return dynamic_cast<rpc *>(p);
}



xmlTransaction_client::xmlTransaction_client(
    clientTransactionPtr const& tranP) :
    tranP(tranP) {}



void
xmlTransaction_client::finish(string const& responseXml) const {

    xml::trace("XML-RPC RESPONSE", responseXml);

    try {
        rpcOutcome outcome;
    
        xml::parseResponse(responseXml, &outcome);

        this->tranP->finish(outcome);
    } catch (error const& error) {
        this->tranP->finishErr(error);
    }
}



void
xmlTransaction_client::finishErr(error const& error) const {

    this->tranP->finishErr(error);
}



void
xmlTransaction_client::progress(
    struct xmlrpc_progress_data const& progressData) const {

    this->tranP->progress(progressData);
}



xmlTransaction_clientPtr::xmlTransaction_clientPtr() {}



xmlTransaction_clientPtr::xmlTransaction_clientPtr(
    clientTransactionPtr const& tranP) :
        xmlTransactionPtr(new xmlTransaction_client(tranP)) {}



xmlTransaction_client *
xmlTransaction_clientPtr::operator->() const {
    autoObject * const p(this->objectP);
    return dynamic_cast<xmlTransaction_client *>(p);
}



} // namespace