forked from tungstenfabric/tf-third-party
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththrift_patch1.diff
More file actions
2446 lines (2414 loc) · 102 KB
/
thrift_patch1.diff
File metadata and controls
2446 lines (2414 loc) · 102 KB
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
diff --git a/third_party/thrift-0.8.0/compiler/cpp/src/generate/t_cpp_generator.cc b/third_party/thrift-0.8.0/compiler/cpp/src/generate/t_cpp_generator.cc
index 5291875..1f3376d 100755
--- a/third_party/thrift-0.8.0/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/third_party/thrift-0.8.0/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -69,6 +69,9 @@ class t_cpp_generator : public t_oop_generator {
iter = parsed_options.find("templates");
gen_templates_ = (iter != parsed_options.end());
+ iter = parsed_options.find("async");
+ gen_async_ = (iter != parsed_options.end());
+
out_dir_base_ = "gen-cpp";
}
@@ -100,7 +103,7 @@ class t_cpp_generator : public t_oop_generator {
void print_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
std::string render_const_value(std::ofstream& out, std::string name, t_type* type, t_const_value* value);
- void generate_struct_definition (std::ofstream& out, t_struct* tstruct, bool is_exception=false, bool pointers=false, bool read=true, bool write=true);
+ void generate_struct_definition (std::ofstream& out, t_struct* tstruct, bool is_exception=false, bool pointers=false, bool read=true, bool write=true, bool swap=false, bool typedefs=false);
void generate_struct_fingerprint (std::ofstream& out, t_struct* tstruct, bool is_definition);
void generate_struct_reader (std::ofstream& out, t_struct* tstruct, bool pointers=false);
void generate_struct_writer (std::ofstream& out, t_struct* tstruct, bool pointers=false);
@@ -124,6 +127,14 @@ class t_cpp_generator : public t_oop_generator {
void generate_service_async_skeleton (t_service* tservice);
/**
+ * Service-level async generation functions
+ */
+ void generate_async_interface(t_service* tservice);
+ void generate_async_client(t_service* tservice);
+ void generate_async_processor (t_service* tservice);
+ void generate_async_process_function (t_service* tservice, t_function* tfunction);
+
+ /**
* Serialization constructs
*/
@@ -199,6 +210,8 @@ class t_cpp_generator : public t_oop_generator {
std::string argument_list(t_struct* tstruct, bool name_params=true, bool start_comma=false);
std::string type_to_enum(t_type* ttype);
std::string local_reflection_name(const char*, t_type* ttype, bool external=false);
+ std::string async_function_signature(t_service* tservice, t_function* tfunction, std::string method_prefix="", std::string return_prefix="", bool name_params=true);
+ std::string async_recv_signature(t_service* tservice, t_function* tfunction, std::string prefix="", bool name_params=true);
void generate_enum_constant_list(std::ofstream& f,
const vector<t_enum_value*>& constants,
@@ -242,6 +255,11 @@ class t_cpp_generator : public t_oop_generator {
bool gen_dense_;
/**
+ * True if we should generate asynchronous callback code.
+ */
+ bool gen_async_;
+
+ /**
* True if we should generate templatized reader/writer methods.
*/
bool gen_templates_;
@@ -342,6 +360,15 @@ void t_cpp_generator::init_generator() {
"#include <protocol/TProtocol.h>" << endl <<
"#include <transport/TTransport.h>" << endl <<
endl;
+ if (gen_async_) {
+ f_types_ <<
+ "#include <boost/bind.hpp>" << endl <<
+ "#include <boost/function.hpp>" << endl <<
+ "#include <async/TAsync.h>" << endl <<
+ "#include <async/TFuture.h>" << endl <<
+ "#include <concurrency/Mutex.h>" << endl <<
+ endl;
+ }
// Include other Thrift includes
const vector<t_program*>& includes = program_->get_includes();
@@ -769,7 +796,9 @@ void t_cpp_generator::generate_struct_definition(ofstream& out,
bool is_exception,
bool pointers,
bool read,
- bool write) {
+ bool write,
+ bool swap,
+ bool typedefs) {
string extends = "";
if (is_exception) {
extends = " : public ::apache::thrift::TException";
@@ -889,6 +918,36 @@ void t_cpp_generator::generate_struct_definition(ofstream& out,
indent() << "virtual ~" << tstruct->get_name() << "() throw() {}" << endl << endl;
}
+ if (typedefs) {
+ // Add typedef so that we can use as types:
+ // - Calculator_add_result::success_t
+ // - Calculator_add_result::failure_t
+ // - Calculator_add_result::success_nonvoid_t (which replaces void with bool)
+ // - Calculator_add_result::success_constref_t (which changes to const& for complex types)
+ // This is useful when templatizing over result objects.
+
+ map<string, t_type*> typedef_types;
+
+ for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ t_type* t = get_true_type((*m_iter)->get_type());
+ typedef_types[(*m_iter)->get_name()] = t;
+ }
+
+ map<string, string> typedef_names;
+
+ typedef_names["success_t"] = typedef_types["success"] ? type_name(typedef_types["success"]) : "void";
+ typedef_names["failure_t"] = typedef_types["failure"] ? type_name(typedef_types["failure"]) : "void";
+ typedef_names["success_nonvoid_t"] = typedef_names["success_t"] != "void" ? typedef_names["success_t"] : "bool";
+ typedef_names["success_constref_t"] = (typedef_types["success"] && is_complex_type(typedef_types["success"]))
+ ? "const " + type_name(typedef_types["success"]) + "&"
+ : typedef_names["success_t"];
+ typedef_names["success_constref_nonvoid_t"] = typedef_names["success_constref_t"] != "void" ? typedef_names["success_constref_t"] : "bool";
+
+ for (map<string, string>::const_iterator i = typedef_names.begin(); i != typedef_names.end(); ++i) {
+ indent(out) << "typedef " << i->second << " " << i->first << ";" << endl;
+ }
+ }
+
// Pointer to this structure's reflection local typespec.
if (gen_dense_) {
indent(out) <<
@@ -942,6 +1001,8 @@ void t_cpp_generator::generate_struct_definition(ofstream& out,
(members.size() > 0 ? "rhs" : "/* rhs */") << ") const" << endl;
scope_up(out);
for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
+ if (gen_async_ && (*m_iter)->get_name() == "failure")
+ continue;
// Most existing Thrift code does not use isset or optional/required,
// so we treat "default" fields as required.
if ((*m_iter)->get_req() != t_field::T_OPTIONAL) {
@@ -1569,6 +1630,12 @@ void t_cpp_generator::generate_service(t_service* tservice) {
generate_service_multiface(tservice);
generate_service_skeleton(tservice);
+ if (gen_async_) {
+ generate_async_interface(tservice);
+ generate_async_client(tservice);
+ generate_async_processor(tservice);
+ }
+
// Generate all the cob components
if (gen_cob_style_) {
generate_service_interface(tservice, "CobCl");
@@ -1794,6 +1861,89 @@ void t_cpp_generator::generate_service_interface_factory(t_service* tservice,
}
/**
+ * Generates a service asynchronous callback interface definition.
+ *
+ * @param tservice The service to generate a header definition for
+ */
+void t_cpp_generator::generate_async_interface(t_service* tservice) {
+
+ string extends = "";
+ if (tservice->get_extends() != NULL) {
+ extends = " : virtual public " + type_name(tservice->get_extends()) + "AsyncIf";
+ }
+ f_header_ <<
+ "class " << service_name_ << "AsyncIf" << extends << " {" << endl <<
+ " public:" << endl;
+ indent_up();
+
+ // First, generate all the controller typedefs outside the class
+
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_function* tfunction = *f_iter;
+ if (!tfunction->is_oneway()) {
+ // t_type* ttype = tfunction->get_returntype();
+ f_header_ << indent() << "typedef apache::thrift::async::TSharedPromise<" + tservice->get_name() + "_" + tfunction->get_name() + "_result> " + tfunction->get_name() + "_shared_promise_t;" << endl;
+ f_header_ << indent() << "typedef apache::thrift::async::TSharedFuture<" + tservice->get_name() + "_" + tfunction->get_name() + "_result> " + tfunction->get_name() + "_shared_future_t;" << endl;
+ }
+ }
+
+ f_header_ << endl;
+
+ // Generate constructors and destructors
+
+ f_header_ <<
+ indent() << "virtual ~" << service_name_ << "AsyncIf() {}" << endl;
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ f_header_ <<
+ indent() << "virtual " << async_function_signature(tservice, *f_iter) << " = 0;" << endl;
+ }
+
+ // Callback result typedefs and convenience functions
+ f_header_ << endl;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ if (!(*f_iter)->is_oneway()) {
+ string resultname = (*f_iter)->get_name() + "_result";
+ indent(f_header_) <<
+ "typedef " << tservice->get_name() << "_" << resultname << " " << resultname << ";" << endl;
+ vector<t_field*>::iterator ff_iter;
+ vector<t_field*> results = (*f_iter)->get_xceptions()->get_members();
+
+ t_field success((*f_iter)->get_returntype(), "success", 0);
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ results.insert(results.begin(), &success);
+ }
+ t_struct exc(NULL, "apache::thrift::TApplicationException");
+ t_field failure(&exc, "failure", -1337); // FIXME: failure field id
+ results.insert(results.begin(), &failure);
+ for (ff_iter = results.begin(); ff_iter != results.end(); ++ff_iter) {
+ indent(f_header_) <<
+ "static " << resultname << " " << (*f_iter)->get_name() << "_" << (*ff_iter)->get_name() << "(" << type_name((*ff_iter)->get_type()) << " " << (*ff_iter)->get_name() << ");" << endl;
+ // Implementation
+ indent_down();
+ f_service_ <<
+ indent() << service_name_ << "AsyncIf::" << resultname << " " << service_name_ << "AsyncIf::" << (*f_iter)->get_name() << "_" << (*ff_iter)->get_name() << "(" << type_name((*ff_iter)->get_type()) << " " << (*ff_iter)->get_name() << ")" << endl;
+ scope_up(f_service_);
+ f_service_ <<
+ indent() << resultname << " result;" << endl <<
+ indent() << "result." << (*ff_iter)->get_name() << " = " << (*ff_iter)->get_name() << ";" << endl <<
+ indent() << "result.__isset." << (*ff_iter)->get_name() << " = true;" << endl <<
+ indent() << "return result;" << endl;
+ scope_down(f_service_);
+ f_service_ << endl;
+ indent_up();
+ }
+ }
+ }
+
+ indent_down();
+ f_header_ <<
+ "};" << endl << endl;
+}
+
+/**
* Generates a null implementation of the service.
*
* @param tservice The service to generate a header definition for
@@ -1830,13 +1980,13 @@ void t_cpp_generator::generate_service_null(t_service* tservice, string style) {
} else if (style == "CobSv") {
if (returntype->is_void()) {
f_header_ << indent() << "return cob();" << endl;
- } else {
- t_field returnfield(returntype, "_return");
- f_header_ <<
- indent() << declare_field(&returnfield, true) << endl <<
- indent() << "return cob(_return);" << endl;
- }
-
+ } else {
+ t_field returnfield(returntype, "_return");
+ f_header_ <<
+ indent() << declare_field(&returnfield, true) << endl <<
+ indent() << "return cob(_return);" << endl;
+ }
+
} else {
throw "UNKNOWN STYLE";
}
@@ -2080,6 +2230,316 @@ void t_cpp_generator::generate_service_multiface(t_service* tservice) {
}
/**
+ * Generates a service async client definition.
+ *
+ * @param tservice The service to generate an async client for.
+ */
+void t_cpp_generator::generate_async_client(t_service* tservice) {
+ string extends = "";
+ string extends_client = "";
+ string extends_processor = "";
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ extends_client = ", public " + extends + "AsyncClient";
+ }
+ else {
+ extends_processor = ", public apache::thrift::TProcessor";
+ }
+
+ // Generate the header portion
+ f_header_ <<
+ "class " << service_name_ << "AsyncClient : " <<
+ "virtual public " << service_name_ << "AsyncIf" <<
+ extends_client << extends_processor << " {" << endl <<
+ " public:" << endl;
+
+ indent_up();
+ f_header_ <<
+ indent() << service_name_ << "AsyncClient(boost::shared_ptr<apache::thrift::transport::TTransport> ot, boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> opf) :" << endl;
+ if (extends.empty()) {
+ f_header_ <<
+ indent() << " potransport_(ot)," << endl <<
+ indent() << " poprotfact_(opf)," << endl <<
+ indent() << " request_counter_(0) {" << endl <<
+ indent() << " otransport_ = ot.get();" << endl <<
+ indent() << " oprotfact_ = opf.get();" << endl <<
+ indent() << "}" << endl;
+ } else {
+ f_header_ <<
+ indent() << " " << extends << "AsyncClient(ot, opf) {}" << endl;
+ }
+
+ // Generate getters for the transport and protocol factory.
+ f_header_ <<
+ indent() << "boost::shared_ptr<apache::thrift::transport::TTransport> getOutputTransport() {" << endl <<
+ indent() << " return potransport_;" << endl <<
+ indent() << "}" << endl;
+ f_header_ <<
+ indent() << "boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> getOutputProtocolFactory() {" << endl <<
+ indent() << " return poprotfact_;" << endl <<
+ indent() << "}" << endl;
+
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::const_iterator f_iter;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ t_function send_function(g_type_void,
+ string("send_") + (*f_iter)->get_name(),
+ (*f_iter)->get_arglist());
+ indent(f_header_) << async_function_signature(tservice, *f_iter) << ";" << endl;
+ indent(f_header_) << function_signature(&send_function, "") << ";" << endl;
+ if (!(*f_iter)->is_oneway()) {
+ //t_struct noargs(program_);
+ //t_function recv_function((*f_iter)->get_returntype(),
+ // string("recv_") + (*f_iter)->get_name(),
+ // &noargs);
+ //indent(f_header_) << function_signature(&recv_function, "") << ";" << endl;
+ //indent(f_header_) << "void " << "recv_" << (*f_iter)->get_name() << "(apache::thrift::protocol::TProtocol* iprot, std::string fname, apache::thrift::protocol::TMessageType mtype);" << endl;
+ indent(f_header_) << async_recv_signature(tservice, *f_iter) << ";" << endl;
+ }
+ }
+ if (extends.empty()) {
+ f_header_ <<
+ indent() << "bool abort(int32_t request_id) {" << endl <<
+ indent() << "apache::thrift::concurrency::Guard g(mutex_);" << endl <<
+ indent() << " return requests_.erase(request_id) != 0;" << endl <<
+ indent() << "}" << endl;
+ }
+ indent(f_header_) << "bool process(boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, void* connectionContext);" << endl;
+
+ indent_down();
+
+ if (extends.empty()) {
+ f_header_ <<
+ " protected:" << endl;
+ indent_up();
+ f_header_ <<
+ indent() << "boost::shared_ptr<apache::thrift::transport::TTransport> potransport_;" << endl <<
+ indent() << "boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> poprotfact_;" << endl <<
+ indent() << "apache::thrift::transport::TTransport* otransport_;" << endl <<
+ indent() << "apache::thrift::protocol::TProtocolFactory* oprotfact_;" << endl <<
+ indent() << "typedef std::map<int32_t, boost::function<void (boost::shared_ptr<apache::thrift::protocol::TProtocol> prot, std::string fname, apache::thrift::protocol::TMessageType mtype)> > request_map_t;" << endl <<
+ indent() << "request_map_t requests_;" << endl <<
+ indent() << "int32_t request_counter_;" << endl <<
+ indent() << "apache::thrift::concurrency::Mutex mutex_;";
+ indent_down();
+ }
+
+ f_header_ <<
+ "};" << endl <<
+ endl;
+
+ string scope = service_name_ + "AsyncClient::";
+ string typedef_scope = service_name_ + "AsyncIf::";
+
+ // Generate async client method implementations
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ string funname = (*f_iter)->get_name();
+
+ // Open function
+ indent(f_service_) <<
+ async_function_signature(tservice, *f_iter, scope, typedef_scope) << endl;
+ scope_up(f_service_);
+ indent(f_service_) <<
+ "send_" << funname << "(";
+
+ // Get the struct of function call params
+ t_struct* arg_struct = (*f_iter)->get_arglist();
+
+ // Declare the function arguments
+ const vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator fld_iter;
+ bool first = true;
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ if (first) {
+ first = false;
+ } else {
+ f_service_ << ", ";
+ }
+ f_service_ << (*fld_iter)->get_name();
+ }
+ f_service_ << ");" << endl;
+
+ if (!(*f_iter)->is_oneway()) {
+ string promise_type = (*f_iter)->get_name() + "_shared_promise_t";
+ f_service_ << indent() << promise_type << " promise;" << endl;
+ f_service_ << indent() << "apache::thrift::concurrency::Guard g(mutex_);" << endl;
+ f_service_ << indent() << "requests_[request_counter_] = boost::bind(&" << scope << "recv_" << funname << ", this, _1 /* piprot */, _2 /* fname */, _3 /* mtype */, promise);" << endl;
+ f_service_ << indent() << "return promise;" << endl;
+ /*
+ f_service_ << indent();
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ if (is_complex_type((*f_iter)->get_returntype())) {
+ f_service_ << "recv_" << funname << "(_return);" << endl;
+ } else {
+ f_service_ << "return recv_" << funname << "();" << endl;
+ }
+ } else {
+ f_service_ <<
+ "recv_" << funname << "();" << endl;
+ }
+ */
+ }
+ else {
+ f_service_ << indent() << "return; // one-way" << endl;
+ }
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ // Function for sending
+ t_function send_function(g_type_void,
+ string("send_") + (*f_iter)->get_name(),
+ (*f_iter)->get_arglist());
+
+ // Open the send function
+ indent(f_service_) <<
+ function_signature(&send_function, "", scope) << endl;
+ scope_up(f_service_);
+
+ // Function arguments and results
+ string argsname = tservice->get_name() + "_" + (*f_iter)->get_name() + "_pargs";
+ string resultname = tservice->get_name() + "_" + (*f_iter)->get_name() + "_result";
+
+ // Serialize the request
+ f_service_ <<
+ indent() << "boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot = oprotfact_->getProtocol(potransport_);" << endl <<
+ indent() << "apache::thrift::protocol::TProtocol* oprot = poprot.get();" << endl <<
+ indent() << "apache::thrift::concurrency::Guard g(mutex_); // for oprot and request_counter_" << endl <<
+ indent() << "if (++request_counter_ < 0)" << endl <<
+ indent() << " request_counter_ = 1;" << endl <<
+ indent() << "int32_t cseqid = request_counter_;" << endl <<
+ indent() << "oprot->writeMessageBegin(\"" << (*f_iter)->get_name() << "\", apache::thrift::protocol::T_CALL, cseqid);" << endl <<
+ endl <<
+ indent() << argsname << " args;" << endl;
+
+ for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
+ f_service_ <<
+ indent() << "args." << (*fld_iter)->get_name() << " = &" << (*fld_iter)->get_name() << ";" << endl;
+ }
+
+ f_service_ <<
+ indent() << "args.write(oprot);" << endl <<
+ endl <<
+ indent() << "oprot->writeMessageEnd();" << endl <<
+ indent() << "oprot->getTransport()->flush();" << endl <<
+ indent() << "oprot->getTransport()->writeEnd();" << endl;
+
+ scope_down(f_service_);
+ f_service_ << endl;
+
+ // Generate recv function only if not an oneway function
+ if (!(*f_iter)->is_oneway()) {
+ t_struct noargs(program_);
+ t_function recv_function((*f_iter)->get_returntype(),
+ string("recv_") + (*f_iter)->get_name(),
+ &noargs);
+
+ // Open function
+ indent(f_service_) << async_recv_signature(tservice, *f_iter, scope) << endl;
+ scope_up(f_service_);
+
+ f_service_ <<
+ endl <<
+ indent() << "if (mtype == apache::thrift::protocol::T_EXCEPTION) {" << endl <<
+ indent() << " apache::thrift::TApplicationException x;" << endl <<
+ indent() << " x.read(iprot.get());" << endl <<
+ indent() << " iprot->readMessageEnd();" << endl <<
+ indent() << " iprot->getTransport()->readEnd();" << endl <<
+ indent() << " promise.errback(" << (*f_iter)->get_name() << "_failure(x));" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if (mtype != apache::thrift::protocol::T_REPLY) {" << endl <<
+ indent() << " iprot->skip(apache::thrift::protocol::T_STRUCT);" << endl <<
+ indent() << " iprot->readMessageEnd();" << endl <<
+ indent() << " iprot->getTransport()->readEnd();" << endl <<
+ indent() << " promise.errback(" << (*f_iter)->get_name() << "_failure(apache::thrift::TApplicationException(apache::thrift::TApplicationException::INVALID_MESSAGE_TYPE)));" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl <<
+ indent() << "if (fname.compare(\"" << (*f_iter)->get_name() << "\") != 0) {" << endl <<
+ indent() << " iprot->skip(apache::thrift::protocol::T_STRUCT);" << endl <<
+ indent() << " iprot->readMessageEnd();" << endl <<
+ indent() << " iprot->getTransport()->readEnd();" << endl <<
+ indent() << " promise.errback(" << (*f_iter)->get_name() << "_failure(apache::thrift::TApplicationException(apache::thrift::TApplicationException::WRONG_METHOD_NAME)));" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl;
+
+ if (!(*f_iter)->get_returntype()->is_void() &&
+ !is_complex_type((*f_iter)->get_returntype())) {
+ t_field returnfield((*f_iter)->get_returntype(), "_return");
+ f_service_ <<
+ indent() << declare_field(&returnfield) << endl;
+ }
+
+ f_service_ <<
+ indent() << resultname << " result;" << endl;
+
+ f_service_ <<
+ indent() << "result.read(iprot.get());" << endl <<
+ indent() << "iprot->readMessageEnd();" << endl <<
+ indent() << "iprot->getTransport()->readEnd();" << endl <<
+ endl;
+
+ // Careful, only look for _result if not a void function
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ f_service_ <<
+ indent() << "if (result.__isset.success) {" << endl <<
+ indent() << " promise.callback(result.success);" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl;
+ }
+
+ // Handle failure
+ // FIXME: deprecated! failure is sent as exception
+ f_service_ <<
+ indent() << "if (result.__isset.failure) {" << endl <<
+ indent() << " promise.errback(result);" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl;
+
+ // Handle exception
+ t_struct* xs = (*f_iter)->get_xceptions();
+ const std::vector<t_field*>& xceptions = xs->get_members();
+ vector<t_field*>::const_iterator x_iter;
+ for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
+ f_service_ <<
+ indent() << "if (result.__isset." << (*x_iter)->get_name() << ") {" << endl <<
+ indent() << " promise.errback(result);" << endl <<
+ indent() << " return;" << endl <<
+ indent() << "}" << endl;
+ }
+
+ // We only get here if we are a void function
+ if ((*f_iter)->get_returntype()->is_void()) {
+ indent(f_service_) << "promise.callback();" << endl;
+ } else {
+ f_service_ <<
+ indent() << " promise.errback(" << (*f_iter)->get_name() << "_failure(apache::thrift::TApplicationException(apache::thrift::TApplicationException::MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\")));" <<
+ indent() << " return;" << endl;
+ }
+
+ // Close function
+ scope_down(f_service_);
+ f_service_ << endl;
+ }
+ }
+ indent(f_service_) << "bool " << scope << "process(boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, void* connectionContext)" << endl;
+ scope_up(f_service_);
+ f_service_ <<
+ indent() << "std::string fname;" << endl <<
+ indent() << "apache::thrift::protocol::TMessageType mtype;" << endl <<
+ indent() << "int32_t rseqid;" << endl <<
+ endl <<
+ indent() << "piprot->readMessageBegin(fname, mtype, rseqid);" << endl <<
+ indent() << "apache::thrift::concurrency::Guard g(mutex_);" << endl <<
+ indent() << "request_map_t::iterator it = requests_.find(rseqid);" << endl <<
+ indent() << "if (it == requests_.end()) return false;" << endl <<
+ indent() << "it->second(piprot, fname, mtype); /* Invoke user method */" << endl <<
+ indent() << "requests_.erase(it);" << endl <<
+ indent() << "return true;" << endl;
+
+ scope_down(f_service_);
+}
+
+/**
* Generates a service client definition.
*
* @param tservice The service to generate a server for.
@@ -2696,7 +3156,7 @@ void ProcessorGenerator::generate_class_definition() {
f_header_ <<
indent() << "boost::shared_ptr<" << if_name_ << "> iface_;" << endl;
f_header_ <<
- indent() << "virtual " << ret_type_ << "process_fn(" << finish_cob_ << "apache::thrift::protocol::TProtocol* iprot, apache::thrift::protocol::TProtocol* oprot, std::string& fname, int32_t seqid" << call_context_ << ");" << endl;
+ indent() << "virtual " << ret_type_ << "process_fn(" << finish_cob_ << " ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot, std::string& fname, int32_t seqid" << call_context_ << ");" << endl;
indent_down();
// Process function declarations
@@ -2706,12 +3166,12 @@ void ProcessorGenerator::generate_class_definition() {
f_header_ <<
indent() << "std::map<std::string, void (" <<
class_name_ << "::*)(" << finish_cob_decl_ <<
- "int32_t, apache::thrift::protocol::TProtocol*, " <<
- "apache::thrift::protocol::TProtocol*" << call_context_decl_ <<
+ "int32_t, ::apache::thrift::protocol::TProtocol*, " <<
+ " ::apache::thrift::protocol::TProtocol*" << call_context_decl_ <<
")> processMap_;" << endl;
for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
indent(f_header_) <<
- "void process_" << (*f_iter)->get_name() << "(" << finish_cob_ << "int32_t seqid, apache::thrift::protocol::TProtocol* iprot, apache::thrift::protocol::TProtocol* oprot" << call_context_ << ");" << endl;
+ "void process_" << (*f_iter)->get_name() << "(" << finish_cob_ << "int32_t seqid, ::apache::thrift::protocol::TProtocol* iprot, ::apache::thrift::protocol::TProtocol* oprot" << call_context_ << ");" << endl;
if (generator_->gen_templates_) {
indent(f_header_) <<
"void process_" << (*f_iter)->get_name() << "(" << finish_cob_ <<
@@ -2738,8 +3198,8 @@ void ProcessorGenerator::generate_class_definition() {
f_header_ <<
indent() << "void throw_" << (*f_iter)->get_name() <<
"(std::tr1::function<void(bool ok)> cob, int32_t seqid, " <<
- "apache::thrift::protocol::TProtocol* oprot, void* ctx, " <<
- "apache::thrift::TDelayedException* _throw);" << endl;
+ " ::apache::thrift::protocol::TProtocol* oprot, void* ctx, " <<
+ " ::apache::thrift::TDelayedException* _throw);" << endl;
if (generator_->gen_templates_) {
f_header_ <<
indent() << "void throw_" << (*f_iter)->get_name() <<
@@ -2783,7 +3243,7 @@ void ProcessorGenerator::generate_class_definition() {
declare_map <<
indent() << "}" << endl <<
endl <<
- indent() << "virtual " << ret_type_ << "process(" << finish_cob_ << "boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot" << call_context_ << ");" << endl <<
+ indent() << "virtual " << ret_type_ << "process(" << finish_cob_ << "boost::shared_ptr< ::apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr< ::apache::thrift::protocol::TProtocol> poprot" << call_context_ << ");" << endl <<
indent() << "virtual ~" << class_name_ <<
"() {}" << endl;
indent_down();
@@ -2802,7 +3262,7 @@ void ProcessorGenerator::generate_class_definition() {
// template parameter here.
f_header_ <<
"typedef " << class_name_ <<
- "<apache::thrift::protocol::TDummyProtocol> " <<
+ "< ::apache::thrift::protocol::TDummyProtocol> " <<
service_name_ << pstyle_ << "Processor;" << endl << endl;
}
}
@@ -2812,8 +3272,8 @@ void ProcessorGenerator::generate_process() {
template_header_ <<
ret_type_ << class_name_ <<
template_suffix_ << "::process(" << finish_cob_ <<
- "boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, " <<
- "boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot" <<
+ "boost::shared_ptr< ::apache::thrift::protocol::TProtocol> piprot, " <<
+ "boost::shared_ptr< ::apache::thrift::protocol::TProtocol> poprot" <<
call_context_ << ") {" << endl;
indent_up();
@@ -2855,8 +3315,8 @@ void ProcessorGenerator::generate_process_fn() {
template_header_ <<
ret_type_ << class_name_ <<
template_suffix_ << "::process_fn(" << finish_cob_ <<
- "apache::thrift::protocol::TProtocol* iprot, " <<
- "apache::thrift::protocol::TProtocol* oprot, " <<
+ " ::apache::thrift::protocol::TProtocol* iprot, " <<
+ " ::apache::thrift::protocol::TProtocol* oprot, " <<
"std::string& fname, int32_t seqid" << call_context_ << ") {" << endl;
indent_up();
@@ -2864,13 +3324,13 @@ void ProcessorGenerator::generate_process_fn() {
f_out_ <<
indent() << typename_str_ << "std::map<std::string, void (" <<
class_name_ << "::*)(" << finish_cob_decl_ <<
- "int32_t, apache::thrift::protocol::TProtocol*, " <<
- "apache::thrift::protocol::TProtocol*" << call_context_decl_ << ")>::iterator pfn;" << endl <<
+ "int32_t, ::apache::thrift::protocol::TProtocol*, " <<
+ " ::apache::thrift::protocol::TProtocol*" << call_context_decl_ << ")>::iterator pfn;" << endl <<
indent() << "pfn = processMap_.find(fname);" << endl <<
indent() << "if (pfn == processMap_.end()) {" << endl;
if (extends_.empty()) {
f_out_ <<
- indent() << " iprot->skip(apache::thrift::protocol::T_STRUCT);" << endl <<
+ indent() << " iprot->skip(::apache::thrift::protocol::T_STRUCT);" << endl <<
indent() << " iprot->readMessageEnd();" << endl <<
indent() << " iprot->getTransport()->readEnd();" << endl <<
indent() << " ::apache::thrift::TApplicationException x(::apache::thrift::TApplicationException::UNKNOWN_METHOD, \"Invalid method name: '\"+fname+\"'\");" << endl <<
@@ -2998,6 +3458,177 @@ void t_cpp_generator::generate_service_processor(t_service* tservice,
}
/**
+ * Generates a service server definition.
+ *
+ * @param tservice The service to generate a server for.
+ */
+void t_cpp_generator::generate_async_processor(t_service* tservice) {
+ // Generate the dispatch methods
+ vector<t_function*> functions = tservice->get_functions();
+ vector<t_function*>::iterator f_iter;
+
+ string extends = "";
+ string extends_processor = "";
+ if (tservice->get_extends() != NULL) {
+ extends = type_name(tservice->get_extends());
+ extends_processor = ", public " + extends + "AsyncProcessor";
+ }
+
+ // Generate the header portion
+ f_header_ <<
+ "class " << service_name_ << "AsyncProcessor : " <<
+ "virtual public apache::thrift::TProcessor" <<
+ extends_processor << " {" << endl;
+
+ // Protected data members
+ f_header_ <<
+ " protected:" << endl;
+ indent_up();
+ f_header_ <<
+ indent() << "boost::shared_ptr<" << service_name_ << "AsyncIf> iface_;" << endl;
+ f_header_ <<
+ indent() << "virtual bool process_fn(boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, std::string& fname, int32_t seqid);" << endl;
+
+ indent_down();
+
+ // Process function declarations
+ f_header_ <<
+ " private:" << endl;
+ indent_up();
+ f_header_ <<
+ indent() << "std::map<std::string, void (" << service_name_ << "AsyncProcessor::*)(int32_t, boost::shared_ptr<apache::thrift::protocol::TProtocol>, boost::shared_ptr<apache::thrift::protocol::TProtocol>)> processMap_;" << endl;
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ indent(f_header_) <<
+ "void process_" << (*f_iter)->get_name() << "(int32_t seqid, boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot);" << endl;
+ if (!(*f_iter)->is_oneway()) {
+ if (!(*f_iter)->get_returntype()->is_void()) {
+ indent(f_header_) <<
+ "void success_" << (*f_iter)->get_name() << "(int32_t seqid, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, " << type_name((*f_iter)->get_returntype()) << " value);" << endl;
+ }
+ else {
+ indent(f_header_) <<
+ "void success_" << (*f_iter)->get_name() << "(int32_t seqid, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot);" << endl;
+ }
+ indent(f_header_) <<
+ "void reply_" << (*f_iter)->get_name() << "(int32_t seqid, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, " << service_name_ << "_" << (*f_iter)->get_name() << "_result result);" << endl;
+ }
+ }
+ indent_down();
+
+ indent_up();
+ string declare_map = "";
+ indent_up();
+
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ declare_map += indent();
+ declare_map += "processMap_[\"";
+ declare_map += (*f_iter)->get_name();
+ declare_map += "\"] = &";
+ declare_map += service_name_;
+ declare_map += "AsyncProcessor::process_";
+ declare_map += (*f_iter)->get_name();
+ declare_map += ";\n";
+ }
+ indent_down();
+
+ f_header_ <<
+ " public:" << endl <<
+ // Constructor
+ indent() << service_name_ << "AsyncProcessor(boost::shared_ptr<" << service_name_ << "AsyncIf> iface) :" << endl;
+ if (extends.empty()) {
+ f_header_ <<
+ indent() << " iface_(iface) {" << endl;
+ } else {
+ f_header_ <<
+ indent() << " " << extends << "AsyncProcessor(iface)," << endl <<
+ indent() << " iface_(iface) {" << endl;
+ }
+ f_header_ <<
+ declare_map <<
+ indent() << "}" << endl <<
+ endl <<
+ indent() << "virtual bool process(boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, void* connectionContext);" << endl <<
+ indent() << "virtual ~" << service_name_ << "AsyncProcessor() {}" << endl;
+ indent_down();
+ f_header_ <<
+ "};" << endl << endl;
+
+ // Generate the server implementation
+ f_service_ <<
+ "bool " << service_name_ << "AsyncProcessor::process(boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, void* connectionContext) {" << endl;
+ indent_up();
+
+ f_service_ <<
+ endl <<
+ indent() << "std::string fname;" << endl <<
+ indent() << "apache::thrift::protocol::TMessageType mtype;" << endl <<
+ indent() << "int32_t seqid;" << endl <<
+ endl <<
+ indent() << "piprot->readMessageBegin(fname, mtype, seqid);" << endl <<
+ endl <<
+ indent() << "if (mtype != apache::thrift::protocol::T_CALL && mtype != apache::thrift::protocol::T_ONEWAY) {" << endl <<
+ indent() << " piprot->skip(apache::thrift::protocol::T_STRUCT);" << endl <<
+ indent() << " piprot->readMessageEnd();" << endl <<
+ indent() << " piprot->getTransport()->readEnd();" << endl <<
+ indent() << " apache::thrift::TApplicationException x(apache::thrift::TApplicationException::INVALID_MESSAGE_TYPE);" << endl <<
+ indent() << " poprot->writeMessageBegin(fname, apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
+ indent() << " x.write(poprot.get());" << endl <<
+ indent() << " poprot->writeMessageEnd();" << endl <<
+ indent() << " poprot->getTransport()->flush();" << endl <<
+ indent() << " poprot->getTransport()->writeEnd();" << endl <<
+ indent() << " return true;" << endl <<
+ indent() << "}" << endl <<
+ endl <<
+ indent() << "return process_fn(piprot, poprot, fname, seqid);" <<
+ endl;
+
+ indent_down();
+ f_service_ <<
+ indent() << "}" << endl <<
+ endl;
+
+ f_service_ <<
+ "bool " << service_name_ << "AsyncProcessor::process_fn(boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot, std::string& fname, int32_t seqid) {" << endl;
+ indent_up();
+
+ // HOT: member function pointer map
+ f_service_ <<
+ indent() << "std::map<std::string, void (" << service_name_ << "AsyncProcessor::*)(int32_t, boost::shared_ptr<apache::thrift::protocol::TProtocol>, boost::shared_ptr<apache::thrift::protocol::TProtocol>)>::iterator pfn;" << endl <<
+ indent() << "pfn = processMap_.find(fname);" << endl <<
+ indent() << "if (pfn == processMap_.end()) {" << endl;
+ if (extends.empty()) {
+ f_service_ <<
+ indent() << " piprot->skip(apache::thrift::protocol::T_STRUCT);" << endl <<
+ indent() << " piprot->readMessageEnd();" << endl <<
+ indent() << " piprot->getTransport()->readEnd();" << endl <<
+ indent() << " apache::thrift::TApplicationException x(apache::thrift::TApplicationException::UNKNOWN_METHOD, \"Invalid method name: '\"+fname+\"'\");" << endl <<
+ indent() << " poprot->writeMessageBegin(fname, apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
+ indent() << " x.write(poprot.get());" << endl <<
+ indent() << " poprot->writeMessageEnd();" << endl <<
+ indent() << " poprot->getTransport()->flush();" << endl <<
+ indent() << " poprot->getTransport()->writeEnd();" << endl <<
+ indent() << " return true;" << endl;
+ } else {
+ f_service_ <<
+ indent() << " return " << extends << "AsyncProcessor::process_fn(piprot, poprot, fname, seqid);" << endl;
+ }
+ f_service_ <<
+ indent() << "}" << endl <<
+ indent() << "(this->*(pfn->second))(seqid, piprot, poprot);" << endl <<
+ indent() << "return true;" << endl;
+
+ indent_down();
+ f_service_ <<
+ "}" << endl <<
+ endl;
+
+ // Generate the process subfunctions
+ for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
+ generate_async_process_function(tservice, *f_iter);
+ }
+}
+
+/**
* Generates a struct and helpers for a function.
*
* @param tfunction The function
@@ -3016,6 +3647,12 @@ void t_cpp_generator::generate_function_helpers(t_service* tservice,
result.append(&success);
}
+ t_struct exc(NULL, "apache::thrift::TApplicationException");
+ t_field failure(&exc, "failure", -1337); // FIXME: failure field id
+ if (gen_async_) {
+ result.append(&failure);
+ }
+
t_struct* xs = tfunction->get_xceptions();
const vector<t_field*>& fields = xs->get_members();
vector<t_field*>::const_iterator f_iter;
@@ -3023,7 +3660,7 @@ void t_cpp_generator::generate_function_helpers(t_service* tservice,
result.append(*f_iter);
}
- generate_struct_definition(f_header_, &result, false);
+ generate_struct_definition(f_header_, &result, false, false, true, true, false, gen_async_); //if typedefs is true -> compilation error in tests
generate_struct_reader(out, &result);
generate_struct_result_writer(out, &result);
@@ -3109,7 +3746,7 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
indent() << " ctx = this->eventHandler_->getContext(\"" <<
service_func_name << "\", callContext);" << endl <<
indent() << "}" << endl <<
- indent() << "apache::thrift::TProcessorContextFreer freer(" <<
+ indent() << " ::apache::thrift::TProcessorContextFreer freer(" <<
"this->eventHandler_.get(), ctx, \"" << service_func_name << "\");" <<
endl << endl <<
indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
@@ -3202,10 +3839,10 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
if (!tfunction->is_oneway()) {
out <<
endl <<
- indent() << "apache::thrift::TApplicationException x(e.what());" <<
+ indent() << " ::apache::thrift::TApplicationException x(e.what());" <<
endl <<
indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
- "\", apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
+ "\", ::apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
indent() << "x.write(oprot);" << endl <<
indent() << "oprot->writeMessageEnd();" << endl <<
indent() << "oprot->getTransport()->writeEnd();" << endl <<
@@ -3236,7 +3873,7 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
service_func_name << "\");" << endl <<
indent() << "}" << endl << endl <<
indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
- "\", apache::thrift::protocol::T_REPLY, seqid);" << endl <<
+ "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl <<
indent() << "result.write(oprot);" << endl <<
indent() << "oprot->writeMessageEnd();" << endl <<
indent() << "bytes = oprot->getTransport()->writeEnd();" << endl <<
@@ -3298,7 +3935,7 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
indent() << " ctx = this->eventHandler_->getContext(\"" <<
service_func_name << "\", NULL);" << endl <<
indent() << "}" << endl <<
- indent() << "apache::thrift::TProcessorContextFreer freer(" <<
+ indent() << " ::apache::thrift::TProcessorContextFreer freer(" <<
"this->eventHandler_.get(), ctx, \"" << service_func_name << "\");" <<
endl << endl <<
indent() << "try {" << endl;
@@ -3452,7 +4089,7 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
indent() << " ctx = this->eventHandler_->getContext(\"" <<
service_func_name << "\", NULL);" << endl <<
indent() << "}" << endl <<
- indent() << "apache::thrift::TProcessorContextFreer freer(" <<
+ indent() << " ::apache::thrift::TProcessorContextFreer freer(" <<
"this->eventHandler_.get(), ctx, \"" << service_func_name <<
"\");" << endl << endl <<
indent() << "if (this->eventHandler_.get() != NULL) {" << endl <<
@@ -3460,7 +4097,7 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
service_func_name << "\");" << endl <<
indent() << "}" << endl << endl <<
indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
- "\", apache::thrift::protocol::T_REPLY, seqid);" << endl <<
+ "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl <<
indent() << "result.write(oprot);" << endl <<
indent() << "oprot->writeMessageEnd();" << endl <<
indent() << "uint32_t bytes = oprot->getTransport()->writeEnd();" <<
@@ -3510,7 +4147,7 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
indent() << " ctx = this->eventHandler_->getContext(\"" <<
service_func_name << "\", NULL);" << endl <<
indent() << "}" << endl <<
- indent() << "apache::thrift::TProcessorContextFreer freer(" <<
+ indent() << " ::apache::thrift::TProcessorContextFreer freer(" <<
"this->eventHandler_.get(), ctx, \"" << service_func_name << "\");" <<
endl << endl;
@@ -3547,10 +4184,10 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
service_func_name << "\");" << endl <<
indent() << "}" << endl <<
endl <<
- indent() << "apache::thrift::TApplicationException x(e.what());" <<
+ indent() << " ::apache::thrift::TApplicationException x(e.what());" <<
endl <<
indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
- "\", apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
+ "\", ::apache::thrift::protocol::T_EXCEPTION, seqid);" << endl <<
indent() << "x.write(oprot);" << endl <<
indent() << "oprot->writeMessageEnd();" << endl <<
indent() << "oprot->getTransport()->writeEnd();" << endl <<
@@ -3568,7 +4205,7 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
service_func_name << "\");" << endl <<
indent() << "}" << endl << endl <<
indent() << "oprot->writeMessageBegin(\"" << tfunction->get_name() <<
- "\", apache::thrift::protocol::T_REPLY, seqid);" << endl <<
+ "\", ::apache::thrift::protocol::T_REPLY, seqid);" << endl <<
indent() << "result.write(oprot);" << endl <<
indent() << "oprot->writeMessageEnd();" << endl <<
indent() << "uint32_t bytes = oprot->getTransport()->writeEnd();" <<
@@ -3586,6 +4223,129 @@ void t_cpp_generator::generate_process_function(t_service* tservice,
}
/**
+ * Generates async process function definitions.
+ *
+ * @param tfunction The function to write a dispatcher for
+ */
+void t_cpp_generator::generate_async_process_function(t_service* tservice,
+ t_function* tfunction) {
+ // Open function
+ f_service_ <<
+ "void " << tservice->get_name() << "AsyncProcessor::" <<
+ "process_" << tfunction->get_name() <<
+ "(int32_t seqid, boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot)" << endl;
+ scope_up(f_service_);
+
+ string argsname = tservice->get_name() + "_" + tfunction->get_name() + "_args";
+ string resultname = tservice->get_name() + "_" + tfunction->get_name() + "_result";
+ string sharedfuturename = tservice->get_name() + "AsyncIf::" + tfunction->get_name() + "_shared_future_t";
+
+ f_service_ <<
+ indent() << argsname << " args;" << endl <<
+ indent() << "args.read(piprot.get());" << endl <<
+ indent() << "piprot->readMessageEnd();" << endl <<
+ indent() << "piprot->getTransport()->readEnd();" << endl <<
+ endl;
+
+ //t_struct* xs = tfunction->get_xceptions();
+ //const std::vector<t_field*>& xceptions = xs->get_members();
+ //vector<t_field*>::const_iterator x_iter;
+
+ // Generate the function call
+ t_struct* arg_struct = tfunction->get_arglist();
+ const std::vector<t_field*>& fields = arg_struct->get_members();
+ vector<t_field*>::const_iterator f_iter;
+
+ if (!tfunction->is_oneway()) {
+ f_service_ << indent() << sharedfuturename << " future = ";
+ } else {
+ f_service_ << indent();
+ }
+
+ bool first = true;
+ f_service_ <<