36 #include <boost/tuple/tuple.hpp>
41 using boost::tuples::make_tuple;
51 const std::map<std::string, std::string>& parsed_options,
52 const std::string& option_string)
56 std::map<std::string, std::string>::const_iterator iter;
58 iter = parsed_options.find(
"pure_enums");
59 gen_pure_enums_ = (iter != parsed_options.end());
61 iter = parsed_options.find(
"dense");
62 gen_dense_ = (iter != parsed_options.end());
64 iter = parsed_options.find(
"include_prefix");
65 use_include_prefix_ = (iter != parsed_options.end());
67 iter = parsed_options.find(
"cob_style");
68 gen_cob_style_ = (iter != parsed_options.end());
70 iter = parsed_options.find(
"no_client_completion");
71 gen_no_client_completion_ = (iter != parsed_options.end());
73 iter = parsed_options.find(
"templates");
74 gen_templates_ = (iter != parsed_options.end());
76 out_dir_base_ =
"gen-cpp";
83 void init_generator();
84 void close_generator();
86 void generate_consts(std::vector<t_const*> consts);
92 void generate_typedef(
t_typedef* ttypedef);
93 void generate_enum(
t_enum* tenum);
95 generate_cpp_struct(tstruct,
false);
98 generate_cpp_struct(txception,
true);
101 void generate_sandesh(
t_sandesh* tsandesh) {
102 generate_cpp_sandesh(tsandesh);
104 void generate_cpp_sandesh(
t_sandesh* tsandesh);
105 void generate_sandesh_definition (std::ofstream& out,
t_sandesh* tsandesh);
106 void generate_sandesh_fingerprint (std::ofstream& out,
t_sandesh* tsandesh,
bool is_definition);
107 void generate_sandesh_http_reader (std::ofstream& out,
t_sandesh* tsandesh);
108 void generate_sandesh_reader (std::ofstream& out,
t_sandesh* tsandesh);
109 void generate_sandesh_writer (std::ofstream& out,
t_sandesh* tsandesh);
110 void generate_sandesh_creator (std::ofstream& out,
t_sandesh* tsandesh);
111 void generate_sandesh_member_init_list(std::ofstream& out,
t_sandesh* tsandesh,
bool init_dval =
false);
112 void generate_sandesh_base_init (ofstream& out,
t_sandesh* tsandesh,
bool init_dval);
113 void generate_sandesh_hints (std::ofstream& out,
t_sandesh* tsandesh);
114 void generate_sandesh_request (std::ofstream& out,
t_sandesh* tsandesh);
115 void generate_sandesh_default_ctor (std::ofstream& out,
t_sandesh* tsandesh,
bool is_request);
116 void generate_sandesh_loggers (std::ofstream& out,
t_sandesh* tsandesh);
117 void generate_logger_field (std::ofstream& out,
t_field *tfield,
string prefix,
bool log_value_only,
bool no_name_log,
bool for_sandesh =
false);
118 void generate_logger_struct (std::ofstream& out,
t_struct *tstruct,
string prefix,
string name);
119 void generate_logger_container (std::ofstream& out,
t_type* ttype,
string name,
bool log_value_only);
120 void generate_logger_map_element (std::ofstream& out,
t_map* tmap,
string iter,
bool log_value_only);
121 void generate_logger_set_element (std::ofstream& out,
t_set* tset,
string iter,
bool log_value_only);
122 void generate_logger_list_element (std::ofstream& out,
t_list* tlist,
string iter,
bool log_value_only);
123 void generate_sandesh_get_size (std::ofstream& out,
t_sandesh* tsandesh);
124 void generate_get_size_field (std::ofstream& out,
t_field *tfield);
125 void generate_get_size_struct (std::ofstream& out,
t_struct *tstruct,
string name);
126 void generate_get_size_container (std::ofstream& out,
t_type* ttype,
string name);
127 void generate_get_size_map_element (std::ofstream& out,
t_map* tmap,
string name);
128 void generate_get_size_list_element(std::ofstream& out,
t_list* ttype,
string name);
129 void generate_get_size_set_element (std::ofstream& out,
t_set* ttype,
string name);
130 void generate_sandesh_trace (std::ofstream& out,
t_sandesh* tsandesh);
131 void generate_sandesh_context (std::ofstream& out,
t_sandesh* tsandesh,
string val);
132 void generate_sandesh_seqnum(std::ofstream& out,
t_sandesh* tsandesh);
133 void generate_sandesh_versionsig(std::ofstream& out,
t_sandesh* tsandesh);
134 void generate_sandesh_static_seqnum_def(std::ofstream& out,
t_sandesh* tsandesh);
135 void generate_sandesh_static_versionsig_def(std::ofstream& out,
t_sandesh* tsandesh);
136 void generate_sandesh_trace_seqnum_ctor(std::ofstream& out,
t_sandesh* tsandesh);
137 void generate_static_const_string_definition(std::ofstream& out,
t_sandesh* tsandesh);
138 std::string generate_sandesh_no_static_const_string_function(
t_sandesh *tsandesh,
bool signature,
bool autogen_darg,
bool trace =
false,
bool request =
false,
bool ctorcall =
false);
139 void generate_static_const_string_definition(std::ofstream& out, std::string name,
140 const vector<t_field*>& fields);
141 std::string generate_sandesh_async_creator(
t_sandesh *tsandesh,
bool signature,
bool expand_autogen,
bool skip_autogen,
142 std::string prefix, std::string suffix,
bool category_level_file_line_first,
143 bool autogen_category_level,
bool drop_log_reason,
bool use_sandesh_object =
false);
144 void generate_sandesh_rate_limit_fn(ofstream &out,
t_sandesh *tsandesh);
145 void generate_sandesh_async_send_fn(ofstream &out,
t_sandesh *tsandesh,
146 bool generate_sandesh_object,
bool generate_rate_limit,
bool generate_system_log);
147 void generate_sandesh_async_send_macros(ofstream &out,
t_sandesh *tsandesh,
148 bool generate_sandesh_object);
149 void generate_sandesh_static_log_fn(ofstream &out,
t_sandesh *tsandesh,
150 bool generate_sandesh_object);
151 void generate_sandesh_async_create_fn(ofstream &out,
t_sandesh *tsandesh);
152 void generate_sandesh_async_create_macro(ofstream &out,
t_sandesh *tsandesh);
153 void generate_sandesh_session_log_unrolled_fn(ofstream &out,
t_sandesh *tsandesh);
154 void generate_sandesh_session_adjust_session_end_point_objects_fn(ofstream &out,
156 void generate_sandesh_flow_send_fn(ofstream &out,
t_sandesh *tsandesh);
157 void generate_sandesh_systemlog_creators(ofstream &out,
t_sandesh *tsandesh);
158 void generate_sandesh_objectlog_creators(ofstream &out,
t_sandesh *tsandesh);
159 void generate_sandesh_flow_creators(ofstream &out,
t_sandesh *tsandesh);
160 void generate_sandesh_uve_creator(std::ofstream& out,
t_sandesh* tsandesh);
161 std::string generate_sandesh_trace_creator(
t_sandesh *tsandesh,
bool signature,
bool expand_autogen,
bool skip_autogen,
162 std::string prefix, std::string suffix);
163 void generate_sandesh_updater(ofstream& out,
t_sandesh* tsandesh);
164 void generate_isRatelimitPass(ofstream& out,
t_sandesh* tsandesh);
165 void generate_sandesh_static_rate_limit_log_def(ofstream& out,
t_sandesh* tsandesh);
166 void generate_sandesh_static_rate_limit_mutex_def(ofstream& out,
t_sandesh* tsandesh);
167 void generate_sandesh_static_rate_limit_buffer_def(ofstream& out,
t_sandesh* tsandesh);
186 DSInfo(
bool is_map,
size_t period, CacheAttribute cat,
187 string rawtype, RawMetric rmtype,
188 string resulttype,
string algo,
string annotation,
189 string compattr,
string subcompattr,
string prealgo) :
190 is_map_(is_map), period_(period), cat_(cat),
191 rawtype_(rawtype), rmtype_(rmtype),
192 resulttype_(resulttype), algo_(algo),
193 annotation_(annotation),
194 compattr_(compattr) , subcompattr_(subcompattr), prealgo_(prealgo) {}
207 void derived_stats_info(
t_struct* tstruct,
208 map<string,DSInfo>& dsmap,
210 map<
string,set<string> >& rawmap);
212 void cache_attr_info(
t_struct* tstruct, std::map<string, CacheAttribute>& attrs);
215 set<string>& inl, set<string>& never, set<string>& periodic);
218 void generate_cpp_struct(
t_struct* tstruct,
bool is_exception);
220 void generate_service(
t_service* tservice);
225 void generate_struct_definition (std::ofstream& out,
t_struct* tstruct,
bool is_exception=
false,
bool pointers=
false,
bool read=
true,
bool write=
true);
226 void generate_struct_fingerprint (std::ofstream& out,
t_struct* tstruct,
bool is_definition);
227 void generate_struct_reader (std::ofstream& out,
t_struct* tstruct,
bool pointers=
false);
228 void generate_struct_writer (std::ofstream& out,
t_struct* tstruct,
bool pointers=
false);
229 void generate_struct_result_writer (std::ofstream& out,
t_struct* tstruct,
bool pointers=
false);
231 void generate_static_const_string_definition(std::ofstream& out,
t_struct* tstruct);
232 void generate_struct_logger (ofstream& out,
const string& name,
233 const vector<t_field*>& fields);
234 void generate_struct_get_size (ofstream& out,
const string& name,
235 const vector<t_field*>& fields);
242 void generate_service_interface (
t_service* tservice,
string style);
243 void generate_service_interface_factory (
t_service* tservice,
string style);
244 void generate_service_null (
t_service* tservice,
string style);
245 void generate_service_multiface (
t_service* tservice);
246 void generate_service_helpers (
t_service* tservice);
247 void generate_service_client (
t_service* tservice,
string style);
248 void generate_service_processor (
t_service* tservice,
string style);
249 void generate_service_skeleton (
t_service* tservice);
251 string style,
bool specialized=
false);
253 void generate_service_async_skeleton (
t_service* tservice);
259 void generate_deserialize_field (std::ofstream& out,
261 std::string prefix=
"",
262 std::string suffix=
"");
264 void generate_deserialize_struct (std::ofstream& out,
266 std::string prefix=
"");
268 void generate_deserialize_container (std::ofstream& out,
270 std::string prefix=
"");
272 void generate_deserialize_set_element (std::ofstream& out,
274 std::string prefix=
"");
276 void generate_deserialize_map_element (std::ofstream& out,
278 std::string prefix=
"");
280 void generate_deserialize_list_element (std::ofstream& out,
286 void generate_serialize_field (std::ofstream& out,
288 std::string prefix=
"",
289 std::string suffix=
"");
291 void generate_serialize_struct (std::ofstream& out,
293 std::string prefix=
"");
295 void generate_serialize_container (std::ofstream& out,
297 std::string prefix=
"");
299 void generate_serialize_map_element (std::ofstream& out,
303 void generate_serialize_set_element (std::ofstream& out,
307 void generate_serialize_list_element (std::ofstream& out,
311 void generate_function_call (ostream& out,
318 void generate_serialize_sandesh (std::ofstream& out,
320 std::string prefix=
"");
328 std::string namespace_open(std::string ns);
329 std::string namespace_close(std::string ns);
330 std::string type_name(
t_type* ttype,
bool in_typedef=
false,
bool arg=
false);
333 std::string declare_field(
t_field* tfield,
bool init=
false,
bool pointer=
false,
bool constant=
false,
bool reference=
false,
bool constructor=
false);
335 std::string declare_field(
t_field* tfield,
bool init=
false,
bool pointer=
false,
bool constant=
false,
bool reference=
false);
337 std::string function_signature(
t_function* tfunction, std::string style, std::string prefix=
"",
bool name_params=
true);
339 std::string argument_list(
t_struct* tstruct,
bool name_params=
true,
bool start_comma=
false);
340 std::string type_to_enum(
t_type* ttype);
341 std::string local_reflection_name(
const char*,
t_type* ttype,
bool external=
false);
343 void generate_enum_constant_list(std::ofstream& f,
344 const vector<t_enum_value*>& constants,
347 bool include_values);
350 void generate_local_reflection(std::ofstream& out,
t_type* ttype,
bool is_definition);
351 void generate_local_reflection_pointer(std::ofstream& out,
t_type* ttype);
354 ttype = get_true_type(ttype);
361 ttype->is_sandesh() ||
368 use_include_prefix_ = use_include_prefix;
376 std::string get_include_prefix(
const t_program& program)
const;
382 void generate_common_struct_reader_body(std::ofstream& out,
384 bool pointers =
false);
390 void generate_common_struct_writer_body(std::ofstream& out,
392 bool pointers =
false);
444 std::ofstream f_request_impl_;
445 std::ofstream f_html_template_;
446 struct sandesh_logger {
453 void generate_sandesh_logger(std::ofstream& out,
t_sandesh* tsandesh,
455 void generate_sandesh_static_logger(ofstream &out,
t_sandesh *tsandesh,
456 bool generate_sandesh_object);
457 void generate_sandesh_static_drop_logger(ofstream &out,
t_sandesh *tsandesh,
458 bool generate_sandesh_object);
481 MKDIR(get_out_dir().c_str());
484 string f_types_name = get_out_dir()+program_name_+
"_types.h";
485 f_types_.open(f_types_name.c_str());
487 string f_types_impl_name = get_out_dir()+program_name_+
"_types.cpp";
488 f_types_impl_.open(f_types_impl_name.c_str());
491 string f_request_impl_name = get_out_dir()+program_name_+
"_request_skeleton.cpp";
492 f_request_impl_.open(f_request_impl_name.c_str());
494 string f_html_template_name = get_out_dir()+program_name_+
"_html_template.cpp";
495 f_html_template_.open(f_html_template_name.c_str());
498 if (gen_templates_) {
501 string f_types_tcc_name = get_out_dir()+program_name_+
"_types.tcc";
502 f_types_tcc_.open(f_types_tcc_name.c_str());
521 "#ifndef " << program_name_ <<
"_TYPES_H" << endl <<
522 "#define " << program_name_ <<
"_TYPES_H" << endl <<
525 "#ifndef " << program_name_ <<
"_TYPES_TCC" << endl <<
526 "#define " << program_name_ <<
"_TYPES_TCC" << endl <<
531 "#include <sandesh/Thrift.h>" << endl <<
533 "#include <TApplicationException.h>" << endl <<
535 "#include <tbb/atomic.h>" << endl <<
536 "#include <boost/shared_ptr.hpp>" << endl <<
537 "#include <sandesh/derived_stats.h>" << endl <<
538 "#include <sandesh/derived_stats_algo.h>" << endl <<
539 "#include <boost/pointer_cast.hpp>" << endl <<
541 "#include <base/trace.h>" << endl;
542 if (program_name_ !=
"sandesh") {
543 f_types_ <<
"#include <sandesh/sandesh_types.h>" << endl;
544 f_types_ <<
"#include <sandesh/sandesh_constants.h>" << endl;
545 if (program_name_ !=
"derived_stats_results" && program_name_ !=
"vns") {
546 f_types_ <<
"#include <sandesh/sandesh.h>" << endl;
550 "#include <sandesh/protocol/TProtocol.h>" << endl <<
551 "#include <sandesh/transport/TTransport.h>" << endl <<
555 const vector<t_program*>& includes = program_->get_includes();
556 for (
size_t i = 0; i < includes.size(); ++i) {
558 "#include \"" << get_include_prefix(*(includes[i])) <<
559 includes[i]->get_name() <<
"_types.h\"" << endl;
564 "#include \"" << get_include_prefix(*(includes[i])) <<
565 includes[i]->get_name() <<
"_types.tcc\"" << endl;
570 const vector<string>& cpp_includes = program_->get_cpp_includes();
571 for (
size_t i = 0; i < cpp_includes.size(); ++i) {
572 if (cpp_includes[i][0] ==
'<') {
574 "#include " << cpp_includes[i] << endl;
577 "#include \"" << cpp_includes[i] <<
"\"" << endl;
586 "#include <boost/date_time/posix_time/posix_time.hpp>" << endl << endl <<
587 "#include <base/logging.h>" << endl << endl <<
588 "#include <sandesh/sandesh_uve.h>" << endl <<
589 "#include <sandesh/sandesh_http.h>" << endl <<
590 "#include <sandesh/sandesh_trace.h>" << endl <<
591 "#include <curl/curl.h>" << endl <<
592 "#include <boost/foreach.hpp>" << endl <<
593 "#include <boost/assign/list_of.hpp>" << endl <<
594 "#include <boost/make_shared.hpp>" << endl << endl;
597 "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
598 "_types.h\"" << endl <<
601 "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
602 "_types.h\"" << endl <<
606 "#include <sandesh/sandesh_types.h>" << endl <<
607 "#include <sandesh/sandesh_constants.h>" << endl <<
608 "#include <sandesh/sandesh.h>" << endl <<
609 "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
610 "_types.h\"" << endl <<
613 "#include <sandesh/sandesh_http.h>" << endl;
615 "static SandeshHttp::HtmlInfo h_info(" <<
616 program_name_ <<
"_xml," << program_name_ <<
"_xml_len);" << endl;
618 "static SandeshHttp sh(\"" << program_name_ <<
".xml\", h_info);" << endl;
619 f_html_template_ <<
"int " << program_name_ <<
"_marker = 0;" << endl;
628 "#include <TReflectionLocal.h>" << endl <<
633 ns_open_ = namespace_open(program_->get_namespace(
"cpp"));
634 ns_close_ = namespace_close(program_->get_namespace(
"cpp"));
676 if (gen_templates_) {
678 "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
679 "_types.tcc\"" << endl <<
691 f_types_impl_.close();
692 f_types_tcc_.close();
694 f_request_impl_.close();
695 f_html_template_.close();
712 const vector<t_enum_value*>& constants,
715 bool include_values) {
719 vector<t_enum_value*>::const_iterator c_iter;
721 for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
728 << prefix << (*c_iter)->get_name() << suffix;
729 if (include_values && (*c_iter)->has_value()) {
730 f <<
" = " << (*c_iter)->get_value();
736 indent(f) <<
"};" << endl;
748 std::string enum_name = tenum->
get_name();
749 if (!gen_pure_enums_) {
756 indent() <<
"enum " << enum_name;
758 generate_enum_constant_list(f_types_, constants,
"",
"",
true);
760 if (!gen_pure_enums_) {
762 f_types_ <<
"};" << endl;
770 std::string prefix =
"";
771 if (!gen_pure_enums_) {
777 generate_enum_constant_list(f_types_impl_, constants, prefix.c_str(),
"",
false);
781 generate_enum_constant_list(f_types_impl_, constants,
"\"",
"\"",
false);
784 indent() <<
"extern const std::map<int, const char*> _" <<
785 tenum->
get_name() <<
"_VALUES_TO_NAMES;" << endl << endl;
788 indent() <<
"const std::map<int, const char*> _" << tenum->
get_name() <<
789 "_VALUES_TO_NAMES(::contrail::sandesh::TEnumIterator(" << constants.size() <<
790 ", _k" << tenum->
get_name() <<
"Values" <<
791 ", _k" << tenum->
get_name() <<
"Names), " <<
792 "::contrail::sandesh::TEnumIterator(-1, NULL, NULL));" << endl << endl;
794 generate_local_reflection(f_types_, tenum,
false);
795 generate_local_reflection(f_types_impl_, tenum,
true);
802 string f_consts_name = get_out_dir()+program_name_+
"_constants.h";
804 f_consts.open(f_consts_name.c_str());
806 string f_consts_impl_name = get_out_dir()+program_name_+
"_constants.cpp";
807 ofstream f_consts_impl;
808 f_consts_impl.open(f_consts_impl_name.c_str());
818 "#ifndef " << program_name_ <<
"_CONSTANTS_H" << endl <<
819 "#define " << program_name_ <<
"_CONSTANTS_H" << endl <<
821 "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
822 "_types.h\"" << endl <<
829 "#include <base/trace.h>" << endl <<
830 "#include <sandesh/sandesh_types.h>" << endl <<
831 "#include <sandesh/sandesh_constants.h>" << endl <<
832 "#include <sandesh/sandesh.h>" << endl <<
833 "#include <sandesh/sandesh_trace.h>" << endl << endl;
836 "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
837 "_constants.h\"" << endl <<
843 "class " << program_name_ <<
"Constants {" << endl <<
844 " public:" << endl <<
845 " " << program_name_ <<
"Constants();" << endl <<
848 vector<t_const*>::iterator c_iter;
849 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
850 string name = (*c_iter)->get_name();
860 "const " << program_name_ <<
"Constants g_" << program_name_ <<
"_constants;" << endl <<
862 program_name_ <<
"Constants::" << program_name_ <<
"Constants() {" << endl;
864 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
865 print_const_value(f_consts_impl,
866 (*c_iter)->get_name(),
867 (*c_iter)->get_type(),
868 (*c_iter)->get_value());
876 "extern const " << program_name_ <<
"Constants g_" << program_name_ <<
"_constants;" << endl <<
896 if (
type->is_base_type()) {
897 string v2 = render_const_value(out, name,
type, value);
898 indent(out) << name <<
" = " << v2 <<
";" << endl <<
900 }
else if (
type->is_enum()) {
903 }
else if (
type->is_struct() ||
type->is_xception()) {
904 const vector<t_field*>& fields = ((
t_struct*)
type)->get_members();
905 vector<t_field*>::const_iterator f_iter;
906 const map<t_const_value*, t_const_value*>& val = value->
get_map();
907 map<t_const_value*, t_const_value*>::const_iterator v_iter;
908 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
909 t_type* field_type = NULL;
910 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
911 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
912 field_type = (*f_iter)->get_type();
915 if (field_type == NULL) {
916 throw "type error: " +
type->get_name() +
" has no field " + v_iter->first->get_string();
918 string val = render_const_value(out, name, field_type, v_iter->second);
919 indent(out) << name <<
"." << v_iter->first->get_string() <<
" = " << val <<
";" << endl;
920 indent(out) << name <<
".__isset." << v_iter->first->get_string() <<
" = true;" << endl;
923 }
else if (
type->is_map()) {
926 const map<t_const_value*, t_const_value*>& val = value->
get_map();
927 map<t_const_value*, t_const_value*>::const_iterator v_iter;
928 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
929 string key = render_const_value(out, name, ktype, v_iter->first);
930 string val = render_const_value(out, name, vtype, v_iter->second);
931 indent(out) << name <<
".insert(std::make_pair(" << key <<
", " << val <<
"));" << endl;
934 }
else if (
type->is_list()) {
936 const vector<t_const_value*>& val = value->
get_list();
937 vector<t_const_value*>::const_iterator v_iter;
938 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
939 string val = render_const_value(out, name, etype, *v_iter);
940 indent(out) << name <<
".push_back(" << val <<
");" << endl;
943 }
else if (
type->is_set()) {
945 const vector<t_const_value*>& val = value->
get_list();
946 vector<t_const_value*>::const_iterator v_iter;
947 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
948 string val = render_const_value(out, name, etype, *v_iter);
949 indent(out) << name <<
".insert(" << val <<
");" << endl;
953 throw "INVALID TYPE IN print_const_value: " +
type->get_name();
962 std::ostringstream render;
964 if (
type->is_base_type()) {
969 case t_base_type::TYPE_STATIC_CONST_STRING:
970 case t_base_type::TYPE_XML:
972 render <<
'"' << get_escaped_string(value) <<
'"';
975 render << ((value->
get_integer() > 0) ?
"true" :
"false");
981 case t_base_type::TYPE_U16:
982 case t_base_type::TYPE_U32:
990 case t_base_type::TYPE_U64:
993 case t_base_type::TYPE_UUID:
994 render <<
"boost::uuids::string_generator()(\"" << value->
get_uuid() <<
"\")";
1005 case t_base_type::TYPE_IPV4:
1008 case t_base_type::TYPE_IPADDR:
1009 render <<
"boost::asio::ip::address::from_string(\"" <<
1016 }
else if (
type->is_enum()) {
1019 string t = tmp(
"tmp");
1021 print_const_value(out, t,
type, value);
1025 return render.str();
1036 generate_struct_definition(f_types_, tstruct, is_exception);
1037 generate_struct_fingerprint(f_types_impl_, tstruct,
true);
1038 generate_local_reflection(f_types_, tstruct,
false);
1039 generate_local_reflection(f_types_impl_, tstruct,
true);
1040 generate_local_reflection_pointer(f_types_impl_, tstruct);
1042 generate_static_const_string_definition(f_types_impl_, tstruct);
1045 std::ofstream& out = (gen_templates_ ? f_types_tcc_ : f_types_impl_);
1046 generate_struct_reader(out, tstruct);
1047 generate_struct_writer(out, tstruct);
1062 void t_cpp_generator::generate_cpp_sandesh(
t_sandesh* tsandesh) {
1075 generate_sandesh_definition(f_types_, tsandesh);
1076 generate_sandesh_fingerprint(f_types_impl_, tsandesh,
true);
1077 std::ofstream& out = f_types_impl_;
1078 generate_static_const_string_definition(out, tsandesh);
1079 generate_sandesh_static_versionsig_def(out, tsandesh);
1080 generate_sandesh_creator(out, tsandesh);
1081 generate_sandesh_reader(out, tsandesh);
1082 generate_sandesh_writer(out, tsandesh);
1083 generate_sandesh_loggers(out, tsandesh);
1084 generate_sandesh_get_size(out, tsandesh);
1087 generate_sandesh_static_seqnum_def(out, tsandesh);
1090 generate_sandesh_http_reader(out, tsandesh);
1092 generate_sandesh_request(f_request_impl_, tsandesh);
1094 if (is_uve || is_alarm) {
1095 generate_sandesh_updater(out,tsandesh);
1098 generate_sandesh_static_rate_limit_log_def(out, tsandesh);
1099 generate_sandesh_static_rate_limit_mutex_def(out, tsandesh);
1100 generate_sandesh_static_rate_limit_buffer_def(out, tsandesh);
1111 void t_cpp_generator::generate_sandesh_request(ofstream& out,
1113 out <<
"void " << tsandesh->
get_name() <<
"::" <<
1114 "HandleRequest() const {" << endl;
1117 indent(out) <<
"// This autogenerated skeleton file illustrates the " <<
1118 "function needed to" << endl;
1119 indent(out) <<
"// implement " << tsandesh->
get_name() <<
"." << endl;
1120 indent(out) <<
"// You should copy it to another filename to avoid " <<
1121 "overwriting it. Add the" << endl;
1122 indent(out) <<
"// HandleRequest() implementation to it, and include it " <<
1123 "in your module's" << endl;
1124 indent(out) <<
"// SConscript. Alternatively, add the implementation of " <<
1125 "HandleRequest()" << endl;
1126 indent(out) <<
"// in your module's sources." << endl;
1136 std::string t_cpp_generator::generate_sandesh_no_static_const_string_function(
1137 t_sandesh* tsandesh,
bool signature,
bool autogen_darg,
1138 bool trace,
bool request,
bool ctorcall) {
1142 vector<t_field*>::const_iterator m_iter;
1143 const vector<t_field*>& members = tsandesh->
get_members();
1145 bool init_function =
false;
1149 init_function =
true;
1151 result +=
"SandeshTraceBufferPtr trace_buf, std::string file, int32_t line";
1153 result +=
"trace_buf, file, line";
1159 init_function =
true;
1160 result +=
"uint32_t seqno";
1164 init_function =
true;
1168 result +=
"lseqnum_++";
1172 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1173 t_type* t = get_true_type((*m_iter)->get_type());
1174 if (t->is_static_const_string()) {
1177 if ((*m_iter)->get_auto_generated() && trace) {
1184 if (!init_function) {
1186 init_function =
true;
1193 if (autogen_darg && (*m_iter)->get_auto_generated()) {
1194 result += declare_field(*m_iter,
true,
false,
false,
false,
true);
1197 result += declare_field(*m_iter,
false,
false, use_const, !t->
is_base_type() || t->
is_string(),
true);
1200 result += (*m_iter)->get_name();
1203 if (!init_function) {
1209 if (init_function) {
1212 result +=
"const std::string& context, SandeshConnection * sconn = NULL";
1214 if (init_function) {
1217 result +=
"context, sconn";
1225 std::string t_cpp_generator::generate_sandesh_async_creator(
t_sandesh* tsandesh,
bool signature,
1226 bool expand_autogen,
bool skip_autogen, std::string prefix, std::string suffix,
1227 bool category_level_file_line_first,
bool autogen_category_level,
bool
1228 drop_log_reason,
bool use_sandesh_object)
1232 string category_def = prefix +
"category" + suffix;
1233 string level_def = prefix +
"level" + suffix;
1234 string category_dec =
"std::string " + category_def;
1235 string level_dec =
"SandeshLevel::type " + level_def;
1236 string module_name =
"\"\"";
1237 string drop_reason_dec;
1238 string drop_reason_def;
1241 vector<t_field*>::const_iterator m_iter;
1242 const vector<t_field*>& members = tsandesh->
get_members();
1243 bool init_function =
false;
1244 if (drop_log_reason) {
1245 drop_reason_def = prefix +
"drop_reason" + suffix +
", ";
1246 drop_reason_dec =
"const std::string& " + drop_reason_def;
1249 if (category_level_file_line_first) {
1251 result +=
"(" + drop_reason_dec + category_dec +
", " + level_dec;
1252 if (!is_flow) result +=
", std::string file, int32_t line";
1254 if (!autogen_category_level) {
1255 result +=
"(" + drop_reason_dec + category_def +
", " + level_def;
1256 if (!is_flow) result +=
", __FILE__, __LINE__";
1258 result +=
"(" + drop_reason_dec + module_name +
", SandeshLevel::SYS_INFO";
1259 if (!is_flow) result +=
", __FILE__, __LINE__";
1262 init_function =
true;
1265 if (!autogen_category_level) {
1266 result +=
"(" + drop_reason_dec + category_dec +
", " + level_dec;
1267 init_function =
true;
1270 if (!autogen_category_level) {
1271 result +=
"(" + drop_reason_def + category_def +
", " + level_def;
1272 init_function =
true;
1276 if(use_sandesh_object) {
1277 if (!init_function) {
1279 init_function =
true;
1282 result +=
", " + tsandesh->
get_name() +
" *snh";
1288 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1290 t_type* t = get_true_type((*m_iter)->get_type());
1291 if (t->is_static_const_string()) {
1298 bool skip = (*m_iter)->get_auto_generated() &&
1299 ((*m_iter)->get_name() ==
"file" || (*m_iter)->get_name() ==
"line") &&
1300 category_level_file_line_first;
1301 if (!init_function) {
1303 init_function =
true;
1312 if ((*m_iter)->get_auto_generated()) {
1314 result += declare_field(*m_iter,
true,
false,
false, !t->
is_base_type(),
true);
1318 result += declare_field(*m_iter,
false,
false, use_const,
1326 if ((*m_iter)->get_auto_generated()) {
1332 if (expand_autogen) {
1333 if ((*m_iter)->get_name() ==
"file") {
1335 result +=
"__FILE__";
1336 }
else if ((*m_iter)->get_name() ==
"line") {
1338 result +=
"__LINE__";
1342 result += prefix + (*m_iter)->get_name() + suffix;
1346 result += prefix + (*m_iter)->get_name() + suffix;
1351 if (!init_function) {
1358 void t_cpp_generator::generate_sandesh_async_create_fn(ofstream &out,
1360 std::string creator_func_name =
"Create";
1362 << creator_func_name <<
"(std::string file = \"\", int32_t line = 0) {"
1366 " * snh = new " << tsandesh->
get_name() <<
"(lseqnum_++, file, line);"
1368 out <<
indent() <<
"return snh;" << endl;
1370 indent(out) <<
"}" << endl << endl;
1373 void t_cpp_generator::generate_sandesh_async_create_macro(ofstream &out,
1375 std::string creator_func_name =
"Create";
1377 string creator_name = tsandesh->
get_name() + creator_func_name;
1378 string creator_name_usc = underscore(creator_name);
1379 string creator_name_uc = uppercase(creator_name_usc);
1380 out <<
indent() <<
"#define " << creator_name_uc <<
1383 out <<
indent() << tsandesh->
get_name() <<
"::" << creator_func_name <<
1384 "(__FILE__, __LINE__)" << endl;
1386 indent(out) << endl << endl;
1389 void t_cpp_generator::generate_sandesh_async_send_fn(
1390 ofstream &out,
t_sandesh *tsandesh,
bool generate_sandesh_object,
1391 bool generate_rate_limit,
bool generate_system_log) {
1392 std::string sender_func_name =
"Send";
1394 out <<
indent() <<
"static void " << sender_func_name;
1395 out << generate_sandesh_async_creator(tsandesh,
true,
false,
false,
"",
"",
1396 true,
false,
false, generate_sandesh_object);
1397 out <<
" {" << endl;
1399 if (generate_sandesh_object) {
1400 out <<
indent() <<
"snh->set_level(level);" << endl;
1401 out <<
indent() <<
"snh->set_category(category);" << endl;
1403 out <<
indent() <<
"if (HandleTest(level, category)) {" << endl;
1405 out <<
indent() <<
"Log";
1406 if (generate_sandesh_object) {
1407 out <<
"(category, level, snh);" << endl;
1408 out <<
indent() <<
"snh->Release();" << endl;
1410 out << generate_sandesh_async_creator(tsandesh,
false,
false,
false,
"",
1411 "",
false,
false,
false) <<
"; " << endl;
1413 out <<
indent() <<
"return;" << endl;
1415 if (generate_system_log) {
1416 out <<
indent() <<
"if (IsSendingAllMessagesDisabled() || " <<
1417 "IsSendingSystemLogsDisabled()) {" << endl;
1419 out <<
indent() <<
"if (IsSendingAllMessagesDisabled() || " <<
1420 "IsSendingObjectLogsDisabled()) {" << endl;
1423 out <<
indent() <<
"UpdateTxMsgFailStats(\"" << tsandesh->
get_name() <<
1424 "\", 0, SandeshTxDropReason::SendingDisabled);" << endl;
1425 out <<
indent() <<
"Log";
1426 if (generate_sandesh_object) {
1427 out <<
"(category, level, snh);" << endl;
1428 out <<
indent() <<
"snh->Release();" << endl;
1430 out << generate_sandesh_async_creator(tsandesh,
false,
false,
false,
"",
1431 "",
false,
false,
false) <<
"; " << endl;
1433 out <<
indent() <<
"return;" << endl;
1435 if (generate_rate_limit) {
1436 out <<
indent() <<
"if (!IsRatelimitPass()) {" << endl;
1438 out <<
indent() <<
"UpdateTxMsgFailStats(\"" << tsandesh->
get_name() <<
1439 "\", 0, SandeshTxDropReason::RatelimitDrop);" << endl;
1440 out <<
indent() <<
"if (do_rate_limit_drop_log_) {" << endl;
1442 out <<
indent() <<
"std::stringstream ratelimit_val;" << endl;
1443 out <<
indent() <<
" ratelimit_val << Sandesh::get_send_rate_limit();"
1445 out <<
indent() <<
"std::string drop_reason = \"SANDESH: Ratelimit"
1446 " Drop (\" + ratelimit_val.str() + std::string(\" messages"
1447 "/second): \") ;" << endl;
1448 out <<
indent() <<
"DropLog";
1449 if (generate_sandesh_object) {
1450 out <<
"(drop_reason, category, level, snh);" << endl;
1451 out <<
indent() <<
"snh->Release();" << endl;
1453 out << generate_sandesh_async_creator(tsandesh,
false,
false,
1454 false,
"",
"",
false,
false,
true) <<
"; " << endl;
1456 out <<
indent() <<
"do_rate_limit_drop_log_ = false;" << endl;
1458 out <<
indent() <<
"return;" << endl;
1461 out <<
indent() <<
"if (level >= SendingLevel()) {" << endl;
1463 out <<
indent() <<
"UpdateTxMsgFailStats(\"" << tsandesh->
get_name() <<
1464 "\", 0, SandeshTxDropReason::QueueLevel);" << endl;
1465 out <<
indent() <<
"std::string drop_reason = \"SANDESH: Queue Drop:"
1467 out <<
indent() <<
"DropLog";
1468 if (generate_sandesh_object) {
1469 out <<
"(drop_reason, category, level, snh);" << endl;
1470 out <<
indent() <<
"snh->Release();" << endl;
1472 out << generate_sandesh_async_creator(tsandesh,
false,
false,
false,
"",
1473 "",
false,
false,
true) <<
"; " << endl;
1475 out <<
indent() <<
"return;" << endl;
1477 if (!generate_sandesh_object) {
1479 " * snh = new " << tsandesh->
get_name() <<
1480 generate_sandesh_no_static_const_string_function(tsandesh,
1481 false,
false,
false,
false) <<
";" << endl;
1482 out <<
indent() <<
"snh->set_level(level);" << endl;
1483 out <<
indent() <<
"snh->set_category(category);" << endl;
1485 out <<
indent() <<
"snh->Dispatch();" << endl;
1487 indent(out) <<
"}" << endl << endl;
1490 void t_cpp_generator::generate_sandesh_async_send_macros(ofstream &out,
1491 t_sandesh *tsandesh,
bool generate_sandesh_object) {
1492 std::string sender_func_name =
"Send";
1493 std::string logger_func_name =
"Log";
1495 string creator_name = tsandesh->
get_name() + logger_func_name;
1496 if (generate_sandesh_object) {
1497 creator_name +=
"Sandesh";
1499 string creator_name_usc = underscore(creator_name);
1500 string creator_name_uc = uppercase(creator_name_usc);
1501 out <<
indent() <<
"#define " << creator_name_uc;
1502 out << generate_sandesh_async_creator(tsandesh,
false,
false,
true,
"_",
1503 "",
false,
false,
false, generate_sandesh_object);
1504 out <<
"\\" << endl;
1506 out <<
indent() << tsandesh->
get_name() <<
"::" << sender_func_name;
1507 out << generate_sandesh_async_creator(tsandesh,
false,
true,
false,
1508 "(_",
")",
true,
false,
false, generate_sandesh_object) << endl;
1510 indent(out) << endl << endl;
1513 creator_name = tsandesh->
get_name() + sender_func_name;
1514 if (generate_sandesh_object) {
1515 creator_name +=
"Sandesh";
1517 creator_name_usc = underscore(creator_name);
1518 creator_name_uc = uppercase(creator_name_usc);
1519 out <<
indent() <<
"#define " << creator_name_uc;
1520 out << generate_sandesh_async_creator(tsandesh,
false,
false,
true,
"_",
1521 "",
false,
true,
false, generate_sandesh_object);
1522 out <<
"\\" << endl;
1524 out <<
indent() << tsandesh->
get_name() <<
"::" << sender_func_name;
1525 out << generate_sandesh_async_creator(tsandesh,
false,
true,
false,
1526 "(_",
")",
true,
true,
false, generate_sandesh_object) << endl;
1528 indent(out) << endl << endl;
1531 void t_cpp_generator::generate_sandesh_rate_limit_fn(ofstream &out,
1533 out <<
indent() <<
"static bool IsRatelimitPass() {" << endl;
1535 generate_isRatelimitPass(out, tsandesh);
1537 indent(out) <<
"}" << endl << endl;
1540 void t_cpp_generator::generate_sandesh_systemlog_creators(ofstream &out,
1542 bool generate_sandesh_object =
false;
1543 bool generate_rate_limit =
true;
1544 bool generate_system_log =
true;
1546 generate_sandesh_async_send_fn(out, tsandesh, generate_sandesh_object,
1547 generate_rate_limit, generate_system_log);
1548 generate_sandesh_async_send_macros(out, tsandesh, generate_sandesh_object);
1550 generate_sandesh_static_drop_logger(out, tsandesh,
1551 generate_sandesh_object);
1553 generate_sandesh_static_logger(out, tsandesh, generate_sandesh_object);
1555 generate_sandesh_rate_limit_fn(out, tsandesh);
1558 void t_cpp_generator::generate_sandesh_objectlog_creators(ofstream &out,
1561 bool generate_sandesh_object =
false;
1562 bool generate_rate_limit =
false;
1563 bool generate_system_log =
false;
1565 generate_sandesh_async_send_fn(out, tsandesh, generate_sandesh_object,
1566 generate_rate_limit, generate_system_log);
1567 generate_sandesh_async_send_macros(out, tsandesh, generate_sandesh_object);
1569 generate_sandesh_static_drop_logger(out, tsandesh,
1570 generate_sandesh_object);
1572 generate_sandesh_static_logger(out, tsandesh, generate_sandesh_object);
1574 generate_sandesh_object =
true;
1576 generate_sandesh_async_create_fn(out, tsandesh);
1577 generate_sandesh_async_create_macro(out, tsandesh);
1579 generate_sandesh_async_send_fn(out, tsandesh, generate_sandesh_object,
1580 generate_rate_limit, generate_system_log);
1581 generate_sandesh_async_send_macros(out, tsandesh, generate_sandesh_object);
1583 generate_sandesh_static_drop_logger(out, tsandesh,
1584 generate_sandesh_object);
1586 generate_sandesh_static_logger(out, tsandesh, generate_sandesh_object);
1589 void t_cpp_generator::generate_sandesh_session_log_unrolled_fn(ofstream &out,
1592 out <<
indent() <<
"static void LogUnrolled";
1593 out << generate_sandesh_async_creator(tsandesh,
true,
false,
false,
"",
"",
1594 true,
false,
false,
false);
1598 void t_cpp_generator::generate_sandesh_session_adjust_session_end_point_objects_fn(ofstream &out,
1600 out <<
"static void adjust_session_end_point_objects(std::vector"
1601 " <SessionEndpoint> & session_data);" << endl;
1605 void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out,
1607 std::string sender_func_name =
"Send";
1608 std::string logger_func_name =
"Log";
1610 out <<
indent() <<
"static void " << sender_func_name;
1611 out << generate_sandesh_async_creator(tsandesh,
true,
false,
false,
"",
"",
1612 true,
false,
false,
false);
1613 out <<
" {" << endl;
1615 out <<
indent() <<
"if (HandleTest(level, category)) {" << endl;
1617 out <<
indent() <<
"return;" << endl;
1619 out <<
indent() <<
"if (IsSendingAllMessagesDisabled() ||" <<
1620 " IsSendingFlowsDisabled()) {" << endl;
1622 out <<
indent() <<
"UpdateTxMsgFailStats(\"" << tsandesh->
get_name() <<
1623 "\", 0, SandeshTxDropReason::SendingDisabled);" << endl;
1624 out <<
indent() <<
"if (IsLoggingDroppedAllowed(SandeshType::FLOW))" <<
1627 out <<
indent() <<
"Log" <<
1628 generate_sandesh_async_creator(tsandesh,
false,
1629 false,
false,
"",
"",
false,
false,
false) <<
";" << endl;
1631 out <<
indent() <<
"return;" << endl;
1635 "if (is_send_slo_to_logger_enabled() || is_send_sampled_to_logger_enabled()) { " <<
1640 out <<
indent() <<
"LogUnrolled(category, level, session_data);" << endl;
1643 out <<
indent() <<
"if (level >= SendingLevel()) {" << endl;
1645 out <<
indent() <<
"UpdateTxMsgFailStats(\"" << tsandesh->
get_name() <<
1646 "\", 0, SandeshTxDropReason::QueueLevel);" << endl;
1647 out <<
indent() <<
"if (IsLoggingDroppedAllowed(SandeshType::FLOW))" <<
1650 out <<
indent() <<
"std::string drop_reason = \"SANDESH: Queue Drop:"
1652 out <<
indent() <<
"DropLog" <<
1653 generate_sandesh_async_creator(tsandesh,
false,
1654 false,
false,
"",
"",
false,
false,
true) <<
";" << endl;
1656 out <<
indent() <<
"return;" << endl;
1659 " * snh = new " << tsandesh->
get_name() <<
1660 generate_sandesh_no_static_const_string_function(tsandesh,
1661 false,
false,
false,
false) <<
";" << endl;
1663 out <<
indent() <<
"if (!is_send_sampled_to_collector_enabled() && !is_send_slo_to_collector_enabled()) {" << endl;
1665 out <<
indent() <<
"return;" << endl;
1667 out <<
indent() <<
" if (is_send_sampled_to_collector_enabled() != is_send_slo_to_collector_enabled()) {" << endl;
1669 out <<
indent() <<
"adjust_session_end_point_objects(snh->session_data);";
1673 out <<
indent() <<
"snh->set_level(level);" << endl;
1674 out <<
indent() <<
"snh->set_category(category);" << endl;
1675 out <<
indent() <<
"snh->Dispatch();" << endl;
1677 indent(out) <<
"}" << endl << endl;
1680 void t_cpp_generator::generate_sandesh_flow_creators(ofstream &out,
1682 bool generate_sandesh_object =
false;
1686 generate_sandesh_session_log_unrolled_fn(out, tsandesh);
1687 generate_sandesh_session_adjust_session_end_point_objects_fn(out, tsandesh);
1690 generate_sandesh_flow_send_fn(out, tsandesh);
1691 generate_sandesh_async_send_macros(out, tsandesh, generate_sandesh_object);
1693 generate_sandesh_static_drop_logger(out, tsandesh,
1694 generate_sandesh_object);
1696 generate_sandesh_static_logger(out, tsandesh, generate_sandesh_object);
1699 std::string t_cpp_generator::generate_sandesh_trace_creator(
t_sandesh *tsandesh,
1700 bool signature,
bool expand_autogen,
bool skip_autogen,
1701 std::string prefix, std::string suffix) {
1705 vector<t_field*>::const_iterator m_iter;
1706 const vector<t_field*>& members = tsandesh->
get_members();
1710 result +=
"SandeshTraceBufferPtr tracebuf";
1711 if (!skip_autogen) {
1712 result +=
", std::string file, int32_t line";
1715 result += prefix +
"trace_buf" + suffix;
1716 if (!skip_autogen) {
1717 if (expand_autogen) {
1718 result +=
", __FILE__, __LINE__";
1720 result +=
", " + prefix +
"file" + suffix +
", " +
1721 prefix +
"line" + suffix;
1726 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1728 t_type* t = get_true_type((*m_iter)->get_type());
1729 if (t->is_static_const_string()) {
1732 if ((*m_iter)->get_auto_generated()) {
1739 result += declare_field(*m_iter,
false,
false, use_const,
1743 result += prefix + (*m_iter)->get_name() + suffix;
1757 void t_cpp_generator::generate_sandesh_member_init_list(ofstream& out,
1760 vector<t_field*>::const_iterator m_iter;
1761 const vector<t_field*>& members = tsandesh->
get_members();
1764 generate_sandesh_base_init(out, tsandesh, init_dval);
1766 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1767 t_type* t = get_true_type((*m_iter)->get_type());
1772 if (t->is_static_const_string()) {
1785 }
else if (t->is_uuid()) {
1786 dval +=
"boost::uuids::nil_uuid()";
1787 }
else if (t->is_ipaddr()) {
1794 dval = render_const_value(out, (*m_iter)->get_name(), t, cv);
1797 dval = (*m_iter)->get_name();
1800 out <<
", " << (*m_iter)->get_name() <<
"(" << dval <<
")";
1811 void t_cpp_generator::generate_sandesh_seqnum(ofstream& out,
1815 indent(out) <<
"static tbb::atomic<uint32_t> lseqnum_;" << endl;
1817 indent(out) <<
"static uint32_t lseqnum_;" << endl;
1822 string generate_sandesh_base_name(
t_sandesh* tsandesh,
bool type) {
1826 return "SandeshType::REQUEST";
1828 return "SandeshRequest";
1830 }
else if (((
t_base_type *)t)->is_sandesh_response()) {
1832 return "SandeshType::RESPONSE";
1834 return "SandeshResponse";
1836 }
else if (((
t_base_type *)t)->is_sandesh_uve()) {
1838 return "SandeshType::UVE";
1840 return "SandeshUVE";
1842 }
else if (((
t_base_type *)t)->is_sandesh_alarm()) {
1844 return "SandeshType::ALARM";
1846 return "SandeshAlarm";
1848 }
else if (((
t_base_type *)t)->is_sandesh_system()) {
1850 return "SandeshType::SYSTEM";
1852 return "SandeshSystem";
1854 }
else if (((
t_base_type *)t)->is_sandesh_buffer()) {
1856 return "SandeshType::BUFFER";
1858 return "SandeshBuffer";
1860 }
else if (((
t_base_type *)t)->is_sandesh_trace() ||
1863 return "SandeshType::TRACE";
1865 return "SandeshTrace";
1867 }
else if (((
t_base_type *)t)->is_sandesh_object()) {
1869 return "SandeshType::OBJECT";
1871 return "SandeshObject";
1873 }
else if (((
t_base_type *)t)->is_sandesh_flow()) {
1876 return "SandeshType::SESSION";
1878 return "SandeshFlowSession";
1883 return "SandeshType::FLOW";
1885 return "SandeshFlow";
1899 void t_cpp_generator::generate_sandesh_versionsig(ofstream& out,
1901 indent(out) <<
"static uint32_t versionsig_;" << endl;
1904 void t_cpp_generator::generate_sandesh_base_init(
1905 ofstream& out,
t_sandesh* tsandesh,
bool init_dval) {
1906 out << generate_sandesh_base_name(tsandesh,
false);
1909 out <<
"(\"" << tsandesh->
get_name() <<
"\",lseqnum_++)";
1911 out <<
"(\"" << tsandesh->
get_name() <<
"\",seqno)";
1915 void t_cpp_generator::generate_sandesh_trace_seqnum_ctor(ofstream& out,
1917 indent(out) <<
"set_seqnum(0);" << endl;
1926 void t_cpp_generator::generate_sandesh_context(ofstream& out,
1929 indent(out) <<
"set_context(" << val <<
");" << endl;
1939 void t_cpp_generator::generate_sandesh_hints(ofstream& out,
1942 string shints =
"0";
1944 if (has_key_annotation) {
1945 shints +=
" | g_sandesh_constants.SANDESH_KEY_HINT";
1947 indent(out) <<
"set_hints(" << shints <<
");" << endl;
1956 void t_cpp_generator::generate_sandesh_default_ctor(ofstream& out,
1959 vector<t_field*>::const_iterator m_iter;
1960 const vector<t_field*>& members = tsandesh->
get_members();
1963 generate_sandesh_member_init_list(out, tsandesh,
true);
1964 out <<
" {" << endl;
1968 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
1969 t_type* t = get_true_type((*m_iter)->get_type());
1973 print_const_value(out, (*m_iter)->get_name(), t, cv);
1977 generate_sandesh_hints(out, tsandesh);
1979 generate_sandesh_context(out, tsandesh,
"\"\"");
1991 void t_cpp_generator::generate_sandesh_definition(ofstream& out,
1995 bool is_trace = ((
t_base_type *)t)->is_sandesh_trace() ||
1997 bool is_request = ((
t_base_type *)t)->is_sandesh_request();
1998 bool is_response = ((
t_base_type *)t)->is_sandesh_response();
1999 bool is_uve = ((
t_base_type *)t)->is_sandesh_uve();
2000 bool is_alarm = ((
t_base_type *)t)->is_sandesh_alarm();
2001 bool is_buffer = ((
t_base_type *)t)->is_sandesh_buffer();
2002 bool is_system = ((
t_base_type *)t)->is_sandesh_system();
2003 bool is_object = ((
t_base_type *)t)->is_sandesh_object();
2004 bool is_flow = ((
t_base_type *)t)->is_sandesh_flow();
2005 bool is_session = ((
t_base_type *)t)->is_sandesh_session();
2008 extends =
" : public SandeshRequest";
2009 }
else if (is_response) {
2010 extends =
" : public SandeshResponse";
2011 }
else if (is_uve) {
2012 extends =
" : public SandeshUVE";
2013 }
else if (is_alarm) {
2014 extends =
" : public SandeshAlarm";
2015 }
else if (is_system) {
2016 extends =
" : public SandeshSystem";
2017 }
else if (is_buffer) {
2018 extends =
" : public SandeshBuffer";
2019 }
else if (is_trace) {
2020 extends =
" : public SandeshTrace";
2021 }
else if (is_object) {
2022 extends =
" : public SandeshObject";
2023 }
else if (is_flow) {
2025 extends =
" : public SandeshFlowSession";
2027 extends =
" : public SandeshFlow";
2032 vector<t_field*>::const_iterator m_iter;
2033 const vector<t_field*>& members = tsandesh->
get_members();
2040 bool has_nonrequired_fields =
false;
2041 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2043 has_nonrequired_fields =
true;
2046 if (has_nonrequired_fields) {
2049 indent() <<
"typedef struct _" << tsandesh->
get_name() <<
"__isset {" << endl;
2053 "_" << tsandesh->
get_name() <<
"__isset() ";
2055 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2062 ": " << (*m_iter)->get_name() <<
"(false)";
2065 ", " << (*m_iter)->get_name() <<
"(false)";
2068 out <<
" {}" << endl;
2070 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2073 "bool " << (*m_iter)->get_name() <<
";" << endl;
2079 "} _" << tsandesh->
get_name() <<
"__isset;" << endl;
2085 out <<
indent() <<
"class " << tsandesh->
get_name() << extends <<
" {" << endl;
2088 out <<
indent() <<
" public:" << endl;
2090 generate_sandesh_fingerprint(out, tsandesh,
false);
2094 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2095 if (((*m_iter)->get_type())->is_static_const_string()) {
2098 out << endl <<
indent() <<
2099 "void set_" << (*m_iter)->get_name() <<
2100 "(" <<
type_name((*m_iter)->get_type(),
false,
true);
2101 out <<
" val) {" << endl <<
indent() <<
2102 indent() << (*m_iter)->get_name() <<
" = val;" << endl;
2109 (*m_iter)->get_name() <<
" = true;" << endl;
2111 out <<
indent() <<
"}" << endl;
2113 out << endl <<
indent() <<
2114 type_name((*m_iter)->get_type(),
false,
true)
2115 <<
" get_" << (*m_iter)->get_name() <<
2116 "() const {" << endl;
2118 (*m_iter)->get_name() <<
";" << endl;
2119 out <<
indent() <<
"}" << endl;
2126 indent() <<
"bool operator == (const " << tsandesh->
get_name() <<
" & " <<
2127 (members.size() > 0 ?
"rhs" :
"/* rhs */") <<
") const" << endl;
2129 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2134 indent() <<
"if (!(" << (*m_iter)->get_name()
2135 <<
" == rhs." << (*m_iter)->get_name() <<
"))" << endl <<
2136 indent() <<
" return false;" << endl;
2139 indent() <<
"if (__isset." << (*m_iter)->get_name()
2140 <<
" != rhs.__isset." << (*m_iter)->get_name() <<
")" << endl <<
2141 indent() <<
" return false;" << endl <<
2142 indent() <<
"else if (__isset." << (*m_iter)->get_name() <<
" && !("
2143 << (*m_iter)->get_name() <<
" == rhs." << (*m_iter)->get_name()
2145 indent() <<
" return false;" << endl;
2148 indent(out) <<
"return true;" << endl;
2151 indent() <<
"bool operator != (const " << tsandesh->
get_name() <<
" &rhs) const {" << endl <<
2152 indent() <<
" return !(*this == rhs);" << endl <<
2153 indent() <<
"}" << endl << endl;
2157 out << endl <<
indent() <<
"virtual ~" <<
2158 tsandesh->
get_name() <<
"() {}" << endl << endl;
2162 if (has_nonrequired_fields) {
2164 "__isset __isset;" << endl;
2167 out <<
indent() <<
"virtual std::string ModuleName() const { " <<
2168 "return \"" << program_name_ <<
"\"; }" << endl << endl;
2173 generate_sandesh_default_ctor(out, tsandesh,
true);
2175 indent(out) <<
"SANDESH_REGISTER_DEC_TYPE(" << tsandesh->
get_name() <<
2177 out <<
indent() <<
"virtual void HandleRequest() const;" << endl;
2178 indent(out) <<
"virtual bool RequestFromHttp(" <<
2179 "const std::string& ctx, const std::string& snh_query);" << endl;
2182 out <<
indent() <<
"static void Request" <<
2183 generate_sandesh_no_static_const_string_function(tsandesh,
true,
true,
false, is_request,
true) <<
2187 " * snh = new " << tsandesh->
get_name() <<
2188 generate_sandesh_no_static_const_string_function(tsandesh,
false,
false,
false, is_request) <<
2190 out <<
indent() <<
"snh->Dispatch(sconn);" << endl;
2192 indent(out) <<
"}" << endl << endl;
2193 }
else if (is_response) {
2196 generate_sandesh_default_ctor(out, tsandesh,
false);
2198 string creator_func_name =
"Response";
2199 out <<
indent() <<
"void " << creator_func_name <<
2200 "() { Dispatch(); }" << endl;
2201 }
else if (is_uve || is_alarm) {
2202 const vector<t_field*>& fields = tsandesh->
get_members();
2203 vector<t_field*>::const_iterator f_iter = fields.begin();
2204 assert((*f_iter)->get_name() ==
"data");
2206 bool is_proxy =
false;
2207 std::map<std::string, std::string>::iterator ait;
2208 ait = ((*f_iter)->get_type())->annotations_.find(
"timeout");
2209 if (ait != ((*f_iter)->get_type())->annotations_.end()) {
2214 indent(out) <<
"static void Send(const " <<
type_name((*f_iter)->get_type()) <<
2215 "& data, std::string table = \"\", uint64_t mono_usec=0, int partition=-1);" << endl;
2216 indent(out) <<
"static void Send(const " <<
type_name((*f_iter)->get_type()) <<
2217 "& data, SandeshLevel::type Xlevel = SandeshLevel::SYS_NOTICE, " <<
2218 "std::string table = \"\", uint64_t mono_usec=0, int partition=-1);" << endl;
2219 std::map<std::string, std::string>::iterator pit;
2220 pit = ((*f_iter)->get_type())->annotations_.find(
"period");
2221 indent(out) <<
"static const uint64_t kProxyPeriod_us = " <<
2222 pit->second.c_str() <<
"000000;" << endl;
2225 indent(out) <<
"static void Send(const " <<
type_name((*f_iter)->get_type()) <<
2226 "& data, std::string table = \"\", uint64_t mono_usec=0);" << endl;
2227 indent(out) <<
"static void Send(const " <<
type_name((*f_iter)->get_type()) <<
2228 "& data, SandeshLevel::type Xlevel, " <<
2229 "std::string table = \"\", uint64_t mono_usec=0);" << endl;
2232 indent(out) <<
"static void Send(const " <<
type_name((*f_iter)->get_type()) <<
2233 "& cdata, SandeshUVE::SendType stype, uint32_t seqno," <<
2234 " uint32_t cycle, std::string ctx = \"\");" << endl;
2235 indent(out) <<
"static void Send(const " <<
type_name((*f_iter)->get_type()) <<
2236 "& cdata, SandeshLevel::type Xlevel, SandeshUVE::SendType stype, " <<
2237 "uint32_t seqno, uint32_t cycle, std::string ctx = \"\");" << endl;
2238 }
else if (is_system) {
2239 generate_sandesh_systemlog_creators(out, tsandesh);
2240 }
else if (is_flow) {
2241 generate_sandesh_flow_creators(out, tsandesh);
2242 }
else if (is_object) {
2243 generate_sandesh_objectlog_creators(out, tsandesh);
2244 }
else if (is_trace) {
2246 out <<
indent() <<
"virtual void SendTrace(" <<
2247 "const std::string& tcontext, bool more) {" << endl;
2249 indent(out) <<
"set_context(tcontext);" << endl;
2250 indent(out) <<
"set_more(more);" << endl;
2252 " * snh = new " << tsandesh->
get_name() <<
"(*this);" << endl;
2253 indent(out) <<
"snh->Dispatch();" << endl;
2255 indent(out) <<
"}" << endl << endl;
2257 string creator_func_name =
"TraceMsg";
2258 out << endl <<
indent() <<
"static void " << creator_func_name <<
2259 generate_sandesh_trace_creator(tsandesh,
true,
false,
false,
"",
"") <<
2260 ";" << endl << endl;
2261 string creator_macro_name = tsandesh->
get_name() +
"Trace";
2262 string creator_name_usc = underscore(creator_macro_name);
2263 string creator_name_uc = uppercase(creator_name_usc);
2264 out <<
indent() <<
"#define " << creator_name_uc <<
2265 generate_sandesh_trace_creator(tsandesh,
false,
false,
true,
"_",
"") <<
2268 out <<
indent() << tsandesh->
get_name() <<
"::" << creator_func_name <<
2269 generate_sandesh_trace_creator(tsandesh,
false,
true,
false,
"(_",
")") <<
2272 }
else if (is_buffer) {
2275 generate_sandesh_default_ctor(out, tsandesh,
false);
2277 indent(out) <<
"SANDESH_REGISTER_DEC_TYPE(" << tsandesh->
get_name() <<
2278 ");" << endl << endl;
2282 out <<
indent() <<
"virtual const int32_t versionsig() const { return versionsig_;}" << endl;
2283 out <<
indent() <<
"static const int32_t sversionsig() { return versionsig_;}" << endl;
2286 out <<
indent() <<
"static int32_t lseqnum() { return lseqnum_;}" << endl;
2289 if (is_uve || is_alarm) {
2290 const vector<t_field*>& fields = tsandesh->
get_members();
2291 vector<t_field*>::const_iterator f_iter = fields.begin();
2292 assert((*f_iter)->get_name() ==
"data");
2293 string dtype =
type_name((*f_iter)->get_type());
2295 "static std::map<std::string, std::string> _DSConf(void);" << endl;
2296 out <<
indent() <<
"static void _InitDerivedStats(" << dtype <<
2297 " & _data, const map<string,string> & _dsconf);" << endl;
2298 out <<
indent() <<
"static bool UpdateUVE(" << dtype <<
2299 " & _data, " << dtype <<
2300 " & tdata, uint64_t mono_usec, SandeshLevel::type Xlevel);" << endl;
2301 out <<
indent() <<
"bool LoadUVE(SendType stype, uint32_t cycle);" << endl;
2304 out <<
indent() <<
"std::string ToString() const;" << endl;
2306 out <<
indent() <<
"size_t GetSize() const;" << endl;
2310 out <<
"private:" << endl << endl;
2312 indent(out) <<
"virtual void Process(SandeshContext *context);" << endl << endl;
2313 }
else if (!((
t_base_type *)t)->is_sandesh_response()) {
2316 out << generate_sandesh_no_static_const_string_function(tsandesh,
true,
false,
false, is_request);
2317 generate_sandesh_member_init_list(out, tsandesh,
false);
2318 out <<
" {" << endl;
2322 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2323 t_type* t = get_true_type((*m_iter)->get_type());
2327 print_const_value(out, (*m_iter)->get_name(), t, cv);
2332 generate_sandesh_trace_seqnum_ctor(out, tsandesh);
2335 generate_sandesh_context(out, tsandesh,
"context");
2336 out <<
indent() <<
"if (context == \"ctrl\") " <<
2337 "set_hints(g_sandesh_constants.SANDESH_CONTROL_HINT);" << endl;
2338 out <<
indent() <<
"else set_hints(0);" << endl;
2339 out <<
indent() <<
"(void)sconn;" << endl;
2342 generate_sandesh_hints(out, tsandesh);
2350 <<
"(uint32_t seqno," " std::string file, int32_t line) : ";
2351 generate_sandesh_base_init(out, tsandesh,
false);
2352 out <<
", file(file), line(line) {" << endl;
2354 generate_sandesh_hints(out, tsandesh);
2359 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2360 indent(out) << declare_field(*m_iter,
false,
false,
false) << endl;
2364 generate_sandesh_seqnum(out, tsandesh);
2367 generate_sandesh_versionsig(out, tsandesh);
2369 out <<
indent() <<
"static const char *name_;" << endl;
2371 out <<
indent() <<
"void Log() const;" << endl;
2373 out <<
indent() <<
"void ForcedLog() const;" << endl;
2375 out <<
indent() <<
"int32_t Read(" <<
2376 "boost::shared_ptr<contrail::sandesh::protocol::TProtocol> iprot);" << endl;
2378 out <<
indent() <<
"int32_t Write(" <<
2379 "boost::shared_ptr<contrail::sandesh::protocol::TProtocol> oprot) const;" << endl;
2382 out <<
indent() <<
"static bool do_rate_limit_drop_log_;" << endl;
2384 out <<
indent() <<
"static boost::circular_buffer<time_t>"
2385 " rate_limit_buffer_;" << endl;
2387 out <<
indent() <<
"static tbb::mutex rate_limit_mutex_;" << endl;
2392 indent(out) <<
"};" << endl << endl;
2395 void t_cpp_generator::generate_sandesh_fingerprint(ofstream& out,
2397 bool is_definition) {
2398 string stat, nspace, comment;
2399 if (is_definition) {
2401 nspace = tsandesh->
get_name() +
"::";
2411 indent() << stat <<
"const char* " << nspace
2412 <<
"ascii_fingerprint" << comment <<
"= \"" <<
2414 indent() << stat <<
"const uint8_t " << nspace <<
2416 const char* comma =
"";
2421 out <<
"};" << endl << endl;
2425 void t_cpp_generator::cache_attr_info(
t_struct* tstruct,
2426 std::map<string, CacheAttribute> &attrs) {
2428 const vector<t_field*>& members = tstruct->
get_members();
2429 vector<t_field*>::const_iterator m_iter;
2430 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2431 std::map<std::string, std::string>::iterator jt;
2432 jt = (*m_iter)->annotations_.find(
"hidden");
2436 assert(jt == (*m_iter)->annotations_.end());
2437 assert((*m_iter)->annotations_.find(
"stats") == (*m_iter)->annotations_.end());
2438 assert((*m_iter)->annotations_.find(
"mstats") == (*m_iter)->annotations_.end());
2439 attrs.insert(std::make_pair((*m_iter)->get_name(),
2444 if (((*m_iter)->annotations_.find(
"stats") != (*m_iter)->annotations_.end()) ||
2445 ((*m_iter)->annotations_.find(
"mstats") != (*m_iter)->annotations_.end())) {
2449 if (jt!=(*m_iter)->annotations_.end()) {
2450 attrs.insert(std::make_pair((*m_iter)->get_name(), HIDDEN));
2452 attrs.insert(std::make_pair((*m_iter)->get_name(), INLINE));
2457 void t_cpp_generator::derived_stats_info(
t_struct* tstruct,
2459 map<string,DSInfo> & dsmap,
2461 map<
string,set<string> >& rawmap) {
2462 const vector<t_field*>& members = tstruct->
get_members();
2463 vector<t_field*>::const_iterator m_iter;
2464 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2465 std::map<std::string, std::string>::iterator jt,mt;
2466 jt = (*m_iter)->annotations_.find(
"stats");
2467 mt = (*m_iter)->annotations_.find(
"mstats");
2468 if ((jt != (*m_iter)->annotations_.end()) ||
2469 (mt != (*m_iter)->annotations_.end())) {
2472 t_type* retype = get_true_type((*m_iter)->get_type());
2475 bool is_ds_map =
false;
2477 if (mt!= (*m_iter)->annotations_.end()) {
2479 assert(jt == (*m_iter)->annotations_.end());
2481 assert(retype->
is_map());
2483 restype =
type_name(get_true_type(vtype));
2485 restype =
type_name(get_true_type((*m_iter)->get_type()));
2488 const string &tstr = ((jt == (*m_iter)->annotations_.end()) ?
2489 mt->second : jt->second);
2490 size_t pos = tstr.find(
':');
2492 string residual = tstr.substr(pos+1, string::npos);
2493 size_t rpos = residual.find(
':');
2495 if (rpos == string::npos) {
2500 algo = residual.substr(0,rpos);
2501 anno = residual.substr(rpos+1, string::npos);
2505 string rawperiodattr = tstr.substr(0,pos);
2506 size_t ppos = rawperiodattr.find(
"-");
2509 if (ppos != string::npos) {
2510 string periodpre = rawperiodattr.substr(0,ppos);
2511 size_t prepos = periodpre.find(
".");
2512 if (prepos != string::npos) {
2513 period = atoi(periodpre.substr(0,prepos).c_str());
2514 prealgo = periodpre.substr(prepos+1, string::npos);
2516 period = atoi(rawperiodattr.substr(0,ppos).c_str());
2519 rawfullattr = rawperiodattr.substr(ppos+1, string::npos);
2521 rawfullattr = rawperiodattr;
2524 size_t cpos = rawfullattr.find(
'.');
2526 string compattr(
"");
2527 string subcompattr(
"");
2530 if (cpos != string::npos) {
2531 rawattr = rawfullattr.substr(0,cpos);
2532 string rescomp = rawfullattr.substr(cpos+1, string::npos);
2533 size_t upos = rescomp.find(
'.');
2534 if (upos != string::npos) {
2535 compattr = rescomp.substr(0,upos);
2536 subcompattr = rescomp.substr(upos+1, string::npos);
2541 rawattr = rawfullattr;
2545 RawMetric rmt = RM_DIAL;
2546 vector<t_field*>::const_iterator s_iter;
2547 for (s_iter = members.begin(); s_iter != members.end(); ++s_iter) {
2548 if (rawattr.compare((*s_iter)->get_name())==0) {
2549 map<string,string>::const_iterator cit =
2550 (*s_iter)->annotations_.find(
"metric");
2551 if (cit != (*s_iter)->annotations_.end()) {
2552 if (cit->second.compare(
"agg") == 0) rmt = RM_AGG;
2553 else if (cit->second.compare(
"diff") == 0) rmt = RM_DIFF;
2555 t_type* ratype = get_true_type((*s_iter)->get_type());
2558 assert(ratype->
is_map());
2559 vtype = ((
t_map*)ratype)->get_val_type();
2562 if (!compattr.empty()) {
2566 rawtype = string(
"");
2567 const vector<t_field*>& cmembers = ((
t_struct*)vtype)->get_members();
2568 vector<t_field*>::const_iterator c_iter;
2569 for (c_iter = cmembers.begin(); c_iter != cmembers.end(); ++c_iter) {
2570 if (compattr.compare((*c_iter)->get_name())==0) {
2571 t_type* catype = get_true_type((*c_iter)->get_type());
2572 if (!subcompattr.empty()) {
2575 const vector<t_field*>& umem =
2576 ((
t_struct*)catype)->get_members();
2577 vector<t_field*>::const_iterator u_iter;
2578 for (u_iter = umem.begin(); u_iter != umem.end(); ++u_iter) {
2579 if (subcompattr.compare((*u_iter)->get_name())==0) {
2580 t_type* uatype = get_true_type((*u_iter)->get_type());
2596 assert(!rawtype.empty());
2599 (*m_iter)->annotations_.find(
"hidden") != (*m_iter)->annotations_.end();
2601 CacheAttribute cat = INLINE;
2604 if (period==-1) period=1;
2606 if (period!=0) cat = HIDDEN_PER;
2609 if (period!=0) cat = PERIODIC;
2614 if (period==-1) period=0;
2622 DSInfo dsi(is_ds_map, period, cat, rawtype, rmt,
2623 restype, algo, anno, compattr, subcompattr, prealgo);
2626 dsmap.insert(make_pair((*m_iter)->get_name(), dsi));
2629 map<string,set<string> >::iterator r_iter = rawmap.find(rawattr);
2630 if (r_iter != rawmap.end()) {
2631 r_iter->second.insert((*m_iter)->get_name());
2634 dss.insert((*m_iter)->get_name());
2635 rawmap.insert(make_pair(rawattr,dss));
2653 string extends =
"";
2655 extends =
" : public ::contrail::sandesh::TException";
2659 vector<t_field*>::const_iterator m_iter;
2660 const vector<t_field*>& members = tstruct->
get_members();
2667 bool has_nonrequired_fields =
false;
2668 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2670 has_nonrequired_fields =
true;
2673 bool del_support =
false;
2674 bool proxy_support =
false;
2675 if (has_nonrequired_fields && (!pointers || read)) {
2678 indent() <<
"typedef struct _" << tstruct->
get_name() <<
"__isset {" << endl;
2682 "_" << tstruct->
get_name() <<
"__isset() ";
2684 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2688 t_type* t = get_true_type((*m_iter)->get_type());
2690 ((*m_iter)->get_name().compare(
"deleted") == 0)) {
2695 ((*m_iter)->get_name().compare(
"proxy") == 0)) {
2703 ": " << (*m_iter)->get_name() <<
"(false)";
2706 ", " << (*m_iter)->get_name() <<
"(false)";
2709 out <<
" {}" << endl;
2711 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2714 "bool " << (*m_iter)->get_name() <<
";" << endl;
2720 "} _" << tstruct->
get_name() <<
"__isset;" << endl << endl;
2728 indent() <<
"class " << tstruct->
get_name() << extends <<
" {" << endl <<
2729 indent() <<
" public:" << endl <<
2734 generate_struct_fingerprint(out, tstruct,
false);
2737 bool is_table =
false;
2739 std::map<string, CacheAttribute> cache_attrs;
2740 cache_attr_info(tstruct, cache_attrs);
2742 map<string,DSInfo> dsinfo;
2743 map<string,set<string> > rawmap;
2744 derived_stats_info(tstruct, dsinfo, rawmap);
2745 map<string,DSInfo >::iterator ds_iter;
2751 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2752 if ((*m_iter)->get_name() ==
"name") {
2753 std::map<std::string, std::string>::iterator it;
2754 it = (*m_iter)->annotations_.find(
"key");
2755 if (it != (*m_iter)->annotations_.end()) {
2757 indent(out) << tstruct->
get_name() <<
"(std::string __tbl = \"" <<
2758 it->second <<
"\")";
2772 bool init_ctor =
false;
2774 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2775 t_type* t = get_true_type((*m_iter)->get_type());
2777 if (t->is_static_const_string()) {
2788 }
else if (t->is_uuid()) {
2789 dval +=
"boost::uuids::nil_uuid()";
2790 }
else if (t->is_ipaddr()) {
2797 dval = render_const_value(out, (*m_iter)->get_name(), t, cv);
2802 out << (*m_iter)->get_name() <<
"(" << dval <<
")";
2804 out <<
", " << (*m_iter)->get_name() <<
"(" << dval <<
")";
2811 out <<
", table_(__tbl)";
2814 out <<
" {" << endl;
2818 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2819 t_type* t = get_true_type((*m_iter)->get_type());
2824 print_const_value(out, (*m_iter)->get_name(), t, cv);
2834 indent() <<
"virtual ~" << tstruct->
get_name() <<
"() throw() {}" << endl << endl;
2840 "static ::contrail::sandesh::reflection::local::TypeSpec* local_reflection;" <<
2845 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2847 declare_field(*m_iter,
false, pointers && !(*m_iter)->get_type()->is_xception(), !read) << endl;
2850 for (ds_iter = dsinfo.begin(); ds_iter != dsinfo.end(); ++ds_iter) {
2853 if ((ds_iter->second.cat_ == PERIODIC) ||
2854 (ds_iter->second.cat_ == HIDDEN_PER)) {
2855 if (ds_iter->second.prealgo_.empty()) {
2856 indent(out) <<
"boost::shared_ptr< ::contrail::sandesh::DerivedStatsPeriodicIf< ::contrail::sandesh::" <<
2857 ds_iter->second.algo_ <<
", " <<
2858 ds_iter->second.rawtype_ <<
", " <<
2859 ds_iter->second.resulttype_.substr(0, ds_iter->second.resulttype_.size() - 3) <<
", " <<
2860 ds_iter->second.resulttype_ <<
2861 "> > __dsobj_" << ds_iter->first <<
";" << endl;
2863 indent(out) <<
"boost::shared_ptr< ::contrail::sandesh::DerivedStatsPeriodicAnomalyIf< ::contrail::sandesh::" <<
2864 ds_iter->second.algo_ <<
", " <<
2865 ds_iter->second.rawtype_ <<
", ::contrail::sandesh::" <<
2866 ds_iter->second.prealgo_ <<
", " <<
2867 ds_iter->second.resulttype_ <<
2868 "> > __dsobj_" << ds_iter->first <<
";" << endl;
2871 indent(out) <<
"boost::shared_ptr< ::contrail::sandesh::DerivedStatsIf< ::contrail::sandesh::" <<
2872 ds_iter->second.algo_ <<
", " <<
2873 ds_iter->second.rawtype_ <<
", " << ds_iter->second.resulttype_ <<
2874 "> > __dsobj_" << ds_iter->first <<
";" << endl;
2879 indent(out) <<
"std::string table_;" << endl;
2885 if (has_nonrequired_fields && (!pointers || read)) {
2888 indent() <<
"_" << tstruct->
get_name() <<
"__isset __isset;" << endl;
2893 vector<string> dskeys;
2894 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2899 if (((*m_iter)->get_type())->is_static_const_string()) {
2902 std::map<std::string, std::string>::iterator jt;
2903 jt = (*m_iter)->annotations_.find(
"aggtype");
2904 if (jt != (*m_iter)->annotations_.end()) {
2905 if (jt->second.compare(
"listkey")==0) {
2906 dskeys.push_back((*m_iter)->get_name());
2913 indent() << (dsinfo.find((*m_iter)->get_name()) == dsinfo.end() ?
2914 "void set_" :
"void __set_") << (*m_iter)->get_name() <<
2916 indent() <<
"void __set_" << (*m_iter)->get_name() <<
2918 "(" <<
type_name((*m_iter)->get_type(),
false,
true);
2919 out <<
" val) {" << endl <<
indent() <<
2920 indent() << (*m_iter)->get_name() <<
" = val;" << endl;
2928 indent() <<
"__isset." << (*m_iter)->get_name() <<
" = true;" << endl;
2934 out << endl <<
indent() <<
type_name((*m_iter)->get_type(),
false,
true)
2936 out << (*m_iter)->get_name() <<
"() const {" << endl;
2937 out <<
indent() <<
indent() <<
"return " << (*m_iter)->get_name() <<
";" << endl;
2938 out <<
indent() <<
"}" << endl;
2945 indent() <<
"bool operator == (long " <<
2946 (members.size() > 0 ?
"rhs" :
"/* rhs */") <<
") const" << endl;
2948 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2951 t_type*
type = get_true_type((*m_iter)->get_type());
2952 if (
type->is_base_type()) {
2959 case t_base_type::TYPE_U16:
2960 case t_base_type::TYPE_U32:
2961 case t_base_type::TYPE_U64:
2965 indent() <<
"if (!((long)" << (*m_iter)->get_name()
2966 <<
" == rhs))" << endl <<
2967 indent() <<
" return false;" << endl;
2970 indent() <<
"if (__isset." << (*m_iter)->get_name()
2972 << (*m_iter)->get_name() <<
" == rhs))" << endl <<
2973 indent() <<
" return false;" << endl;
2977 out <<
indent() <<
"return false;" << endl;
2982 indent(out) <<
"return true;" << endl;
2987 indent() <<
"bool operator == (const " << tstruct->
get_name() <<
" & " <<
2988 (members.size() > 0 ?
"rhs" :
"/* rhs */") <<
") const" << endl;
2990 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
2995 indent() <<
"if (!(" << (*m_iter)->get_name()
2996 <<
" == rhs." << (*m_iter)->get_name() <<
"))" << endl <<
2997 indent() <<
" return false;" << endl;
3000 indent() <<
"if (__isset." << (*m_iter)->get_name()
3001 <<
" != rhs.__isset." << (*m_iter)->get_name() <<
")" << endl <<
3002 indent() <<
" return false;" << endl <<
3003 indent() <<
"else if (__isset." << (*m_iter)->get_name() <<
" && !("
3004 << (*m_iter)->get_name() <<
" == rhs." << (*m_iter)->get_name()
3006 indent() <<
" return false;" << endl;
3009 indent(out) <<
"return true;" << endl;
3012 indent() <<
"bool operator != (const " << tstruct->
get_name() <<
" &rhs) const {" << endl <<
3013 indent() <<
" return !(*this == rhs);" << endl <<
3014 indent() <<
"}" << endl << endl;
3020 indent() <<
"bool operator < (const "
3021 << tstruct->
get_name() <<
" & ) const;" << endl << endl;
3025 if (gen_templates_) {
3027 indent() <<
"template <class Protocol_>" << endl <<
3028 indent() <<
"uint32_t read(Protocol_* iprot);" << endl;
3031 indent() <<
"int32_t read(" <<
3032 "boost::shared_ptr<contrail::sandesh::protocol::TProtocol> iprot);" << endl;
3036 if (gen_templates_) {
3038 indent() <<
"template <class Protocol_>" << endl <<
3039 indent() <<
"uint32_t write(Protocol_* oprot) const;" << endl;
3042 indent() <<
"int32_t write(" <<
3043 "boost::shared_ptr<contrail::sandesh::protocol::TProtocol> oprot) const;" << endl;
3047 out <<
indent() <<
"std::string log() const;" << endl;
3051 "& right) const" << endl;
3054 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3055 std::string nn = (*m_iter)->get_name();
3056 ds_iter = dsinfo.find((*m_iter)->get_name());
3058 if (ds_iter == dsinfo.end()) sn = std::string(
"set_");
3059 else sn = std::string(
"__set_");
3061 t_type*
type = get_true_type((*m_iter)->get_type());
3062 if (
type->is_base_type()) {
3070 case t_base_type::TYPE_U16:
3071 case t_base_type::TYPE_U32:
3072 case t_base_type::TYPE_U64:
3075 indent() <<
"result." << sn << nn <<
"(get_" << nn <<
3076 "() + right.get_" << nn <<
"());" << endl << endl;
3079 indent() <<
"if ((__isset." << nn <<
") && (right.__isset." <<
3080 nn <<
")) result." << sn << nn <<
"(get_" << nn <<
3081 "() + right.get_" << nn <<
"());" << endl <<
3082 indent() <<
"else if (__isset." << nn <<
") result." << sn <<
3083 nn <<
"(get_" << nn <<
"());" << endl <<
3084 indent() <<
"else if (right.__isset." << nn <<
") result." << sn <<
3085 nn <<
"(right.get_" << nn <<
"());" << endl << endl;
3092 indent(out) <<
"return result;" << endl;
3096 tstruct->
get_name() <<
" operator/(int div) const" << endl;
3099 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3100 std::string nn = (*m_iter)->get_name();
3101 ds_iter = dsinfo.find((*m_iter)->get_name());
3103 if (ds_iter == dsinfo.end()) sn = std::string(
"set_");
3104 else sn = std::string(
"__set_");
3106 t_type*
type = get_true_type((*m_iter)->get_type());
3107 if (
type->is_base_type()) {
3115 case t_base_type::TYPE_U16:
3116 case t_base_type::TYPE_U32:
3117 case t_base_type::TYPE_U64:
3120 indent() <<
"result." << sn << nn <<
"(get_" << nn <<
3121 "() / div);" << endl << endl;
3124 indent() <<
"if (__isset." << nn <<
") result." << sn << nn <<
"(get_" <<
3125 nn <<
"() / div);" << endl << endl;
3132 indent(out) <<
"return result;" << endl;
3137 "& right) const" << endl;
3140 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3141 std::string nn = (*m_iter)->get_name();
3142 ds_iter = dsinfo.find((*m_iter)->get_name());
3144 if (ds_iter == dsinfo.end()) sn = std::string(
"set_");
3145 else sn = std::string(
"__set_");
3146 t_type*
type = get_true_type((*m_iter)->get_type());
3147 if (
type->is_base_type()) {
3155 case t_base_type::TYPE_U16:
3156 case t_base_type::TYPE_U32:
3157 case t_base_type::TYPE_U64:
3160 indent() <<
"result." << sn << nn <<
"(get_" << nn <<
3161 "() - right.get_" << nn <<
"());" << endl << endl;
3164 indent() <<
"if ((__isset." << nn <<
") && (right.__isset." <<
3165 nn <<
")) {" << endl <<
3166 indent() <<
" result." << sn << nn <<
"(get_" << nn <<
3167 "() - right.get_" << nn <<
"()); }" << endl <<
3168 indent() <<
"else if (__isset." << nn <<
") result." << sn <<
3169 nn <<
"(get_" << nn <<
"());" << endl;
3176 indent(out) <<
"return result;" << endl;
3179 out <<
indent() <<
"size_t GetSize() const;" << endl;
3180 out <<
indent() <<
"std::string __listkey(void) {" << endl;
3181 out <<
indent() <<
" std::string __result;" << endl;
3183 vector<string>::iterator kit;
3184 for (kit = dskeys.begin(); kit != dskeys.end(); kit++) {
3186 out <<
indent() <<
" __result.append(\":\");" << endl;
3188 out <<
indent() <<
" __result += " << *kit <<
";" << endl;
3191 out <<
indent() <<
" return __result;" << endl;
3192 out <<
indent() <<
"}" << endl;
3203 indent() <<
"template <>" << endl <<
3204 indent() <<
"struct SandeshStructDeleteTrait<" <<
3205 tstruct->
get_name() <<
"> {" << endl <<
3206 indent() <<
" static bool get(const " <<
3207 tstruct->
get_name() <<
"& s) { return s.get_deleted(); }" <<
3209 indent() <<
"};" << endl;
3211 if (proxy_support) {
3213 indent() <<
"template <>" << endl <<
3214 indent() <<
"struct SandeshStructProxyTrait<" <<
3215 tstruct->
get_name() <<
"> {" << endl <<
3216 indent() <<
" static std::string get(const " <<
3217 tstruct->
get_name() <<
"& s) { return s.get_proxy(); }" <<
3219 indent() <<
"};" << endl;
3231 bool is_definition) {
3232 string stat, nspace, comment;
3233 if (is_definition) {
3235 nspace = tstruct->
get_name() +
"::";
3245 indent() << stat <<
"const char* " << nspace
3246 <<
"ascii_fingerprint" << comment <<
"= \"" <<
3248 indent() << stat <<
"const uint8_t " << nspace <<
3250 const char* comma =
"";
3255 out <<
"};" << endl << endl;
3264 bool is_definition) {
3268 ttype = get_true_type(ttype);
3272 if (!reflected_fingerprints_.insert(key).second) {
3282 generate_local_reflection(out, ((
t_list*)ttype)->get_elem_type(), is_definition);
3283 }
else if (ttype->
is_set()) {
3284 generate_local_reflection(out, ((
t_set*)ttype)->get_elem_type(), is_definition);
3285 }
else if (ttype->
is_map()) {
3286 generate_local_reflection(out, ((
t_map*)ttype)->get_key_type(), is_definition);
3287 generate_local_reflection(out, ((
t_map*)ttype)->get_val_type(), is_definition);
3294 const vector<t_field*>& members = ((
t_struct*)ttype)->get_sorted_members();
3295 vector<t_field*>::const_iterator m_iter;
3296 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3297 generate_local_reflection(out, (**m_iter).get_type(), is_definition);
3299 generate_local_reflection(out,
g_type_void, is_definition);
3302 if (is_definition) {
3304 indent() <<
"::contrail::sandesh::reflection::local::FieldMeta" << endl <<
3305 indent() << local_reflection_name(
"metas", ttype) <<
"[] = {" << endl;
3307 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3308 indent(out) <<
"{ " << (*m_iter)->get_key() <<
", " <<
3313 indent(out) <<
"{ 0, false }" << endl <<
"};" << endl;
3317 indent() <<
"::contrail::sandesh::reflection::local::TypeSpec*" << endl <<
3318 indent() << local_reflection_name(
"specs", ttype) <<
"[] = {" << endl;
3320 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
3322 local_reflection_name(
"typespec", (*m_iter)->get_type(),
true) <<
"," << endl;
3325 local_reflection_name(
"typespec",
g_type_void) <<
"," << endl;
3327 indent(out) <<
"};" << endl;
3333 indent() << (is_definition ?
"" :
"extern ") <<
3334 "::contrail::sandesh::reflection::local::TypeSpec" << endl <<
3335 local_reflection_name(
"typespec", ttype) <<
3336 (is_definition ?
"(" :
";") << endl;
3338 if (!is_definition) {
3346 indent(out) <<
"::contrail::sandesh::protocol::T_STOP";
3348 indent(out) << type_to_enum(ttype);
3352 out <<
"," << endl <<
3354 indent() << local_reflection_name(
"metas", ttype) <<
"," << endl <<
3355 indent() << local_reflection_name(
"specs", ttype);
3356 }
else if (ttype->
is_list()) {
3357 out <<
"," << endl <<
3358 indent() <<
"&" << local_reflection_name(
"typespec", ((
t_list*)ttype)->get_elem_type(),
true) <<
"," << endl <<
3360 }
else if (ttype->
is_set()) {
3361 out <<
"," << endl <<
3362 indent() <<
"&" << local_reflection_name(
"typespec", ((
t_set*)ttype)->get_elem_type(),
true) <<
"," << endl <<
3364 }
else if (ttype->
is_map()) {
3365 out <<
"," << endl <<
3366 indent() <<
"&" << local_reflection_name(
"typespec", ((
t_map*)ttype)->get_key_type(),
true) <<
"," << endl <<
3367 indent() <<
"&" << local_reflection_name(
"typespec", ((
t_map*)ttype)->get_val_type(),
true);
3370 out <<
");" << endl << endl;
3385 "::contrail::sandesh::reflection::local::TypeSpec* " <<
3386 ttype->
get_name() <<
"::local_reflection = " << endl <<
3387 indent() <<
" &" << local_reflection_name(
"typespec", ttype) <<
";" <<
3400 if (gen_templates_) {
3402 indent() <<
"template <class Protocol_>" << endl <<
3404 "::read(Protocol_* iprot) {" << endl;
3407 "int32_t " << tstruct->
get_name() <<
3408 "::read(boost::shared_ptr<contrail::sandesh::protocol::TProtocol> iprot) {" << endl;
3411 generate_common_struct_reader_body(out, tstruct, pointers);
3414 "}" << endl << endl;
3426 if (gen_templates_) {
3428 indent() <<
"template <class Protocol_>" << endl <<
3430 "::write(Protocol_* oprot) const {" << endl;
3433 "int32_t " << tstruct->
get_name() <<
3434 "::write(boost::shared_ptr<contrail::sandesh::protocol::TProtocol> oprot) const {" << endl;
3437 generate_common_struct_writer_body(out, tstruct, pointers);
3451 void t_cpp_generator::generate_struct_get_size(ofstream& out,
const string& name,
3452 const vector<t_field*>& fields) {
3454 indent(out) <<
"size_t " << name <<
3455 "::GetSize() const {" << endl;
3457 indent(out) <<
"size_t size = 0;" << endl;
3458 vector<t_field*>::const_iterator f_iter;
3459 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
3460 generate_get_size_field(out, *f_iter);
3462 indent(out) <<
"return size;" << endl;
3464 indent(out) <<
"}" << endl << endl;
3473 void t_cpp_generator::generate_struct_logger(ofstream& out,
const string& name,
3474 const vector<t_field*>& fields) {
3475 indent(out) <<
"std::string " << name <<
3476 "::log() const {" << endl;
3478 out <<
indent() <<
"std::stringstream Xbuf;" << endl;
3479 vector<t_field*>::const_iterator f_iter;
3481 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
3482 string prefix =
"\" \" << ";
3487 generate_logger_field(out, *f_iter, prefix,
false,
false);
3490 out <<
indent() <<
"return std::string();" << endl;
3492 out <<
indent() <<
"return Xbuf.str();" << endl;
3495 indent(out) <<
"}" << endl << endl;
3504 void t_cpp_generator::generate_static_const_string_definition(ofstream& out,
3506 const vector<t_field*>& fields) {
3507 vector<t_field*>::const_iterator f_iter;
3508 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
3509 if (!((*f_iter)->get_type())->is_static_const_string()) {
3516 print_const_value(out, (*f_iter)->get_name(), (*f_iter)->get_type(), cv);
3526 void t_cpp_generator::generate_static_const_string_definition(ofstream& out,
3528 generate_static_const_string_definition(out, tsandesh->
get_name(),
3537 void t_cpp_generator::generate_static_const_string_definition(ofstream& out,
3539 generate_static_const_string_definition(out, tstruct->
get_name(),
3543 void t_cpp_generator::generate_sandesh_static_seqnum_def(ofstream& out,
3547 out <<
"tbb::atomic<uint32_t> " << tsandesh->
get_name() <<
"::lseqnum_;" << endl;
3549 out <<
"uint32_t " << tsandesh->
get_name() <<
"::lseqnum_ = 1;" << endl;
3553 void t_cpp_generator::generate_sandesh_static_versionsig_def(ofstream& out,
3555 out <<
"uint32_t " << tsandesh->
get_name() <<
"::versionsig_ = "
3559 void t_cpp_generator::generate_sandesh_static_rate_limit_buffer_def(
3561 out <<
"boost::circular_buffer<time_t> " << tsandesh->
get_name() <<
3562 "::rate_limit_buffer_(Sandesh::get_send_rate_limit());" << endl << endl;
3565 void t_cpp_generator::generate_sandesh_static_rate_limit_mutex_def(
3567 out <<
"tbb::mutex " << tsandesh->
get_name() <<
"::rate_limit_mutex_; "
3571 void t_cpp_generator::generate_sandesh_static_rate_limit_log_def(
3573 out <<
"bool " << tsandesh->
get_name() <<
"::do_rate_limit_drop_log_ = true;"
3584 void t_cpp_generator::generate_sandesh_http_reader(ofstream& out,
3586 indent(out) <<
"#include <boost/tokenizer.hpp>" << endl;
3587 indent(out) <<
"#include <base/util.h>" << endl << endl;
3588 indent(out) <<
"bool " << tsandesh->
get_name() <<
"::RequestFromHttp" <<
3589 "(const std::string& ctx, const std::string& snh_query) {" << endl;
3593 const vector<t_field*>& fields = tsandesh->
get_members();
3594 vector<t_field*>::const_iterator f_iter;
3596 indent(out) << program_name_ <<
"_marker = 0;" << endl;
3599 indent() <<
"using std::string;" << endl <<
3600 indent() <<
"boost::char_separator<char> sep(\"&\");" << endl <<
3601 indent() <<
"boost::char_separator<char> varsep(\"=\");" << endl <<
3603 indent() <<
"CURL * cr = curl_easy_init();" <<
3604 indent() <<
"boost::tokenizer<boost::char_separator<char> >" <<
3605 "tokens(snh_query, sep);" << endl <<
3606 indent() <<
"boost::tokenizer<boost::char_separator<char> >" <<
3607 "::iterator it1 = tokens.begin();" << endl << endl;
3609 indent(out) <<
"string entity_name;" << endl;
3610 indent(out) <<
"int fields_vector_size =" << fields.size() <<
";" << endl;
3614 indent(out) <<
"for(int i =0; i < fields_vector_size; i++)" << endl;
3616 indent(out) <<
"if (it1 != tokens.end())" << endl;
3618 indent(out) <<
"string tok = (*it1++).c_str();" << endl ;
3619 indent(out) <<
"boost::tokenizer<boost::char_separator<char> >" <<
3620 " var(tok, varsep);" << endl;
3621 indent(out) <<
"boost::tokenizer<boost::char_separator<char> >" <<
3622 "::iterator it2 = var.begin();" << endl << endl;
3624 indent(out) <<
"string tok_new = (*it2).c_str();" << endl;
3625 indent(out) <<
"if (it2 != var.end()&& ++it2 != var.end()) " << endl;
3629 indent(out) <<
"char *unescaped = NULL;" << endl;
3630 indent(out) <<
"unescaped = (char*) (malloc(sizeof(char)*1024));" <<endl;
3632 indent(out) <<
"std::string tmpstr;" << endl;
3634 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter)
3636 t_type *ftype = (*f_iter)->get_type();
3640 if (btype->
is_string() || btype->is_xml())
3642 indent(out) <<
"entity_name=\"" << (*f_iter)->get_name() <<
"\";" << endl;
3643 indent(out) <<
"if((tok_new.substr(0,(entity_name.length()))).compare(entity_name) == 0)" << endl;
3645 indent(out) <<
"unescaped = curl_easy_unescape(cr, (*it2).c_str(), 0, NULL);" << endl;
3646 indent(out) <<
"tmpstr=unescaped;" << endl;
3647 indent(out) <<
"set_" << (*f_iter)->get_name() <<
"(boost::lexical_cast<std::string>(tmpstr));" << endl;
3648 indent(out) <<
"curl_free(unescaped);" << endl;
3650 indent(out) <<
"continue;" << endl;
3653 else if (btype->is_uuid())
3655 indent(out) <<
"entity_name=\"" << (*f_iter)->get_name() <<
"\";" << endl;
3656 indent(out) <<
"if((tok_new.substr(0,(entity_name.length()))).compare(entity_name) == 0)" << endl;
3658 indent(out) <<
"std::stringstream ss;" << endl;
3659 indent(out) <<
"ss << *it2;" << endl;
3660 indent(out) <<
type_name((*f_iter)->get_type()) <<
" tmpval;" << endl;
3661 indent(out) <<
"ss >> tmpval;" << endl;
3662 indent(out) <<
"set_" << (*f_iter)->get_name() <<
"(tmpval);" << endl;
3663 indent(out) <<
"continue;" << endl;
3666 else if (btype->is_ipaddr())
3668 indent(out) <<
"entity_name=\"" << (*f_iter)->get_name() <<
"\";" << endl;
3669 indent(out) <<
"if((tok_new.substr(0,(entity_name.length()))).compare(entity_name) == 0)" << endl;
3671 indent(out) <<
"boost::system::error_code ec;" << endl;
3672 indent(out) <<
type_name((*f_iter)->get_type()) <<
" tmpval;" << endl;
3673 indent(out) <<
"tmpval = boost::asio::ip::address::from_string((*it2), ec);" << endl;
3674 indent(out) <<
"set_" << (*f_iter)->get_name() <<
"(tmpval);" << endl;
3675 indent(out) <<
"continue;" << endl;
3680 indent(out) <<
"entity_name=\"" << (*f_iter)->get_name() <<
"\";" << endl;
3681 indent(out) <<
"if((tok_new.substr(0,(entity_name.length()))).compare(entity_name) == 0)" << endl;
3683 assert(btype->is_integer());
3684 indent(out) <<
type_name((*f_iter)->get_type()) <<
" tmpval;" << endl;
3685 indent(out) <<
"stringToInteger((*it2), tmpval);" << endl;
3686 indent(out) <<
"set_" << (*f_iter)->get_name() <<
"(tmpval);" << endl;
3687 indent(out) <<
"continue;" << endl;
3694 indent(out) <<
"++it2;" << endl;
3697 vector<t_field*>::const_iterator f_iter2 = fields.begin();
3698 if (f_iter2 != fields.end())
3700 t_type *ftype = (*f_iter2)->get_type();
3704 if (btype->
is_string() || btype->is_xml())
3706 indent(out) <<
"entity_name=\"" << (*f_iter2)->get_name() <<
"\";" << endl;
3707 indent(out) <<
"unescaped = curl_easy_unescape(cr, (*it2).c_str(), 0, NULL);" << endl;
3708 indent(out) <<
"tmpstr=unescaped;" << endl;
3709 indent(out) <<
"set_" << (*f_iter2)->get_name() <<
"(boost::lexical_cast<std::string>(tmpstr));" << endl;
3710 indent(out) <<
"curl_free(unescaped);" << endl;
3711 indent(out) <<
"continue;" << endl;
3714 else if (btype->is_uuid())
3716 indent(out) <<
"entity_name=\"" << (*f_iter2)->get_name() <<
"\";" << endl;
3717 indent(out) <<
"std::stringstream ss;" << endl;
3718 indent(out) <<
"ss << *it2;" << endl;
3719 indent(out) <<
type_name((*f_iter2)->get_type()) <<
" tmpval;" << endl;
3720 indent(out) <<
"ss >> tmpval;" << endl;
3721 indent(out) <<
"set_" << (*f_iter2)->get_name() <<
"(tmpval);" << endl;
3722 indent(out) <<
"continue;" << endl;
3724 else if (btype->is_ipaddr())
3726 indent(out) <<
"entity_name=\"" << (*f_iter2)->get_name() <<
"\";" << endl;
3727 indent(out) <<
"boost::system::error_code ec;" << endl;
3728 indent(out) <<
type_name((*f_iter2)->get_type()) <<
" tmpval;" << endl;
3729 indent(out) <<
"tmpval = boost::asio::ip::address::from_string((*it2), ec);" << endl;
3730 indent(out) <<
"set_" << (*f_iter2)->get_name() <<
"(tmpval);" << endl;
3731 indent(out) <<
"continue;" << endl;
3735 indent(out) <<
"entity_name=\"" << (*f_iter2)->get_name() <<
"\";" << endl;
3736 assert(btype->is_integer());
3737 indent(out) <<
type_name((*f_iter2)->get_type()) <<
" tmpval;" << endl;
3738 indent(out) <<
"stringToInteger((*it2), tmpval);" << endl;
3739 indent(out) <<
"set_" << (*f_iter2)->get_name() <<
"(tmpval);" << endl;
3740 indent(out) <<
"continue;" << endl;
3744 indent(out) <<
"free(unescaped);" << endl;
3750 indent(out) <<
"set_context(ctx);" << endl;
3751 indent(out) <<
"curl_easy_cleanup(cr);" << endl;
3752 indent(out) <<
"return true;" << endl;
3754 indent(out) <<
"}" << endl << endl;
3764 void t_cpp_generator::generate_sandesh_updater(ofstream& out,
3767 const vector<t_field*>& fields = tsandesh->
get_members();
3768 vector<t_field*>::const_iterator f_iter = fields.begin();
3769 assert((*f_iter)->get_name() ==
"data");
3771 bool periodic_ustruct =
false;
3773 std::map<std::string, std::string>::iterator ait;
3774 ait = ((*f_iter)->get_type())->annotations_.find(
"period");
3775 if (ait != ((*f_iter)->get_type())->annotations_.end()) periodic_ustruct =
true;
3777 string dtype =
type_name((*f_iter)->get_type());
3778 t_type* t = get_true_type((*f_iter)->get_type());
3783 map<string,DSInfo> dsinfo;
3784 map<string,set<string> > rawmap;
3785 derived_stats_info(ts, dsinfo, rawmap);
3787 std::map<string, CacheAttribute> cache_attrs;
3788 cache_attr_info(ts, cache_attrs);
3790 const vector<t_field*>& sfields = ts->
get_members();
3791 vector<t_field*>::const_iterator s_iter;
3794 "::LoadUVE(SendType stype, uint32_t cycle) {" << endl;
3796 indent(out) <<
"(void)cycle;" << endl;
3797 indent(out) <<
"bool is_periodic_attr = false;" << endl << endl;
3799 for (s_iter = sfields.begin(); s_iter != sfields.end(); ++s_iter) {
3800 string snm = (*s_iter)->get_name();
3801 if (snm ==
"deleted")
continue;
3805 map<string,DSInfo>::const_iterator ds_iter = dsinfo.find(snm);
3806 if (ds_iter == dsinfo.end()) {
3808 CacheAttribute cat = (cache_attrs.find(snm))->second;
3809 if (cat == MANDATORY)
continue;
3811 indent(out) <<
"if (data.__isset." << snm <<
") {" << endl;
3813 if (cat == HIDDEN) {
3814 indent(out) <<
"if (stype != ST_INTROSPECT) data.__isset." <<
3815 snm <<
" = false;" << endl;
3816 }
else if (cat == INLINE) {
3817 indent(out) <<
"if (stype == ST_PERIODIC) " <<
3818 "data.__isset." << snm <<
" = false;" << endl;
3821 indent(out) <<
"}" << endl << endl;
3823 indent(out) <<
"if (data.__dsobj_" << snm <<
"->IsResult()) {" << endl;
3825 CacheAttribute cat = ds_iter->second.cat_;
3826 if (cat == HIDDEN) {
3827 indent(out) <<
"if (stype != ST_INTROSPECT) data.__isset." <<
3828 snm <<
" = false;" << endl;
3829 }
else if (cat == HIDDEN_PER) {
3830 indent(out) <<
"if (stype == ST_PERIODIC) {" << endl;
3832 indent(out) <<
"if (cycle % " << ds_iter->second.period_ <<
3833 " == 0) data.__dsobj_" << snm <<
"->Flush(data." <<
3834 snm <<
");" << endl;
3835 indent(out) <<
"data.__isset." << snm <<
" = false;" << endl;
3837 indent(out) <<
"} else if (stype == ST_SYNC) data.__isset." <<
3838 snm <<
" = false;" << endl;
3839 }
else if (cat == INLINE) {
3840 indent(out) <<
"if (stype == ST_PERIODIC) " <<
3841 "data.__isset." << snm <<
" = false;" << endl;
3842 }
else if (cat == PERIODIC) {
3843 indent(out) <<
"if (stype == ST_PERIODIC) {" << endl;
3846 indent(out) <<
"if (cycle % " << ds_iter->second.period_ <<
3850 indent(out) <<
"data.__dsobj_" << snm <<
"->Flush(data." <<
3851 snm <<
");" << endl;
3852 indent(out) <<
"data.__dsobj_" << snm <<
"->FillResult(data." <<
3853 snm <<
", data.__isset." << snm <<
", true);" << endl;
3856 indent(out) <<
"} else data.__isset." << snm <<
" = false;" << endl;
3859 indent(out) <<
"} else if (stype == ST_SYNC) data.__isset." <<
3860 snm <<
" = false;" << endl;
3863 indent(out) <<
"else data.__dsobj_" << snm <<
"->FillResult(data." <<
3864 snm <<
", data.__isset." << snm <<
", true);" <<endl;
3866 if (cat == PERIODIC) {
3867 indent(out) <<
"is_periodic_attr |= data.__isset." << snm <<
";" << endl;
3870 indent(out) <<
"}" << endl << endl;
3873 indent(out) <<
"if (stype == ST_PERIODIC) return ((is_periodic_attr) || (data.get_deleted()));" << endl;
3874 indent(out) <<
"else return true;" << endl;
3876 indent(out) <<
"}" << endl << endl;
3878 indent(out) <<
"std::map<std::string, std::string> " << tsandesh->
get_name() <<
3879 "::_DSConf(void) {" << endl;
3882 bool is_dsconf =
false;
3883 for (map<string,DSInfo>::const_iterator ds_iter = dsinfo.begin();
3884 ds_iter != dsinfo.end(); ++ds_iter) {
3886 indent(out) <<
"std::map<std::string, std::string> _dsconf = " <<
3887 "boost::assign::map_list_of" << endl;
3888 indent(out) <<
"(\"" << ds_iter->first <<
"\", \"" <<
3889 ds_iter->second.annotation_ <<
"\")" << endl;
3892 if (is_dsconf)
indent(out) <<
";" << endl;
3893 else indent(out) <<
"std::map<std::string, std::string> _dsconf;" << endl;
3895 indent(out) <<
"return _dsconf;" << endl;
3897 indent(out) <<
"}" << endl << endl;
3900 "::_InitDerivedStats(" << dtype <<
3901 " & _data, const map<string,string> & _dsconf) {" << endl;
3904 for (map<string,DSInfo>::const_iterator ds_iter = dsinfo.begin();
3905 ds_iter != dsinfo.end(); ++ds_iter) {
3907 indent(out) <<
"{" << endl;
3909 indent(out) <<
"map<string,string>::const_iterator _dci = _dsconf.find(\"" <<
3910 ds_iter->first <<
"\");" << endl;
3911 if (ds_iter->second.rmtype_ == RM_AGG) {
3912 indent(out) <<
"bool is_agg = true;" << endl;
3914 indent(out) <<
"bool is_agg = false;" << endl;
3916 indent(out) <<
"assert(_dci != _dsconf.end());" << endl;
3918 indent(out) <<
"_data.__dsobj_" << ds_iter->first <<
".reset();" << endl;
3920 if ((ds_iter->second.cat_ == PERIODIC) ||
3921 (ds_iter->second.cat_ == HIDDEN_PER)) {
3923 if (ds_iter->second.prealgo_.empty()) {
3924 indent(out) <<
"_data.__dsobj_" << ds_iter->first <<
" = boost::make_shared<" <<
3925 " ::contrail::sandesh::DerivedStatsPeriodicIf< ::contrail::sandesh::" <<
3926 ds_iter->second.algo_ <<
", " <<
3927 ds_iter->second.rawtype_ <<
", " <<
3928 ds_iter->second.resulttype_.substr(0, ds_iter->second.resulttype_.size() - 3) <<
", " <<
3929 ds_iter->second.resulttype_ <<
"> >(_dci->second, is_agg);" << endl;
3931 indent(out) <<
"_data.__dsobj_" << ds_iter->first <<
" = boost::make_shared<" <<
3932 " ::contrail::sandesh::DerivedStatsPeriodicAnomalyIf< ::contrail::sandesh::" <<
3933 ds_iter->second.algo_ <<
", " <<
3934 ds_iter->second.rawtype_ <<
", ::contrail::sandesh::" <<
3935 ds_iter->second.prealgo_ <<
", " <<
3936 ds_iter->second.resulttype_ <<
"> >(_dci->second, is_agg);" << endl;
3939 indent(out) <<
"_data.__dsobj_" << ds_iter->first <<
" = boost::make_shared<" <<
3940 " ::contrail::sandesh::DerivedStatsIf< ::contrail::sandesh::" <<
3941 ds_iter->second.algo_ <<
", " <<
3942 ds_iter->second.rawtype_ <<
", " << ds_iter->second.resulttype_ <<
3943 "> >(_dci->second, is_agg);" << endl;
3946 indent(out) <<
"}" << endl;
3949 indent(out) <<
"}" << endl << endl;
3952 "::UpdateUVE(" << dtype <<
3953 " & _data, " << dtype <<
3954 " & tdata, uint64_t mono_usec, SandeshLevel::type Xlevel) {" << endl;
3958 indent(out) <<
"bool send = false;" << endl;
3960 for (s_iter = sfields.begin(); s_iter != sfields.end(); ++s_iter) {
3961 string snm = (*s_iter)->get_name();
3964 if (dsinfo.find(snm) == dsinfo.end()) {
3965 CacheAttribute cat = (cache_attrs.find(snm))->second;
3968 if (cat != INLINE) {
3969 indent(out) <<
"if (_data.__isset." << snm <<
") { _data.__isset." <<
3970 snm <<
" = false;" << endl;
3972 indent(out) <<
"if (_data.__isset." << snm <<
") { send = true;" << endl;
3975 if ((snm.compare(
"name") == 0) || (snm.compare(
"proxy") == 0)) {
3976 indent(out) <<
"{" << endl;
3978 indent(out) <<
"{ send = true;" << endl;
3982 indent(out) <<
"tdata.set_" << snm <<
"(_data.get_" <<
3983 snm <<
"());" << endl;
3984 map<string,set<string> >::const_iterator r_iter = rawmap.find(snm);
3986 if (r_iter != rawmap.end()) {
3987 t_type* ratype = (*s_iter)->get_type();
3991 " _T_" << snm <<
";" << endl;
3992 indent(out) <<
"std::map<string,bool> _delmap_" << snm
3994 indent(out) <<
"BOOST_FOREACH(const _T_" << snm <<
3995 "::value_type& _tp, _data.get_" << snm <<
"())" << endl;
3996 indent(out) <<
" _delmap_" << snm <<
3997 ".insert(make_pair(_tp.first, SandeshStructDeleteTrait<" <<
3998 type_name(vtype) <<
">::get(_tp.second)));" << endl;
4000 set<string>::const_iterator d_iter;
4001 for (d_iter = r_iter->second.begin(); d_iter != r_iter->second.end(); ++d_iter) {
4002 map<string,DSInfo>::const_iterator c_iter = dsinfo.find(*d_iter);
4003 CacheAttribute dat = c_iter->second.cat_;
4005 if (c_iter->second.compattr_.empty()) {
4006 if (!c_iter->second.is_map_) {
4007 indent(out) <<
"tdata.__dsobj_" << *d_iter <<
4008 "->Update(_data.get_" << snm <<
"(), mono_usec);" << endl;
4010 indent(out) <<
"tdata.__dsobj_" << *d_iter <<
4011 "->Update(_data.get_" << snm <<
"(), _delmap_" <<
4012 snm <<
", mono_usec);" << endl;
4015 string getexpr = string(
"get_") + c_iter->second.compattr_ +
4017 if (!c_iter->second.subcompattr_.empty()) {
4018 getexpr += string(
".get_") + c_iter->second.subcompattr_ +
4021 if (!c_iter->second.is_map_) {
4022 indent(out) <<
"tdata.__dsobj_" << *d_iter <<
"->Update(_data.get_" <<
4023 snm <<
"()." << getexpr <<
", mono_usec);" << endl;
4025 indent(out) <<
"std::map<string," << c_iter->second.rawtype_ <<
"> temp_" <<
4026 *d_iter <<
";" << endl;
4027 indent(out) <<
"BOOST_FOREACH(const _T_" << snm <<
4028 "::value_type& _tp, _data.get_" << snm <<
"()) {" << endl;
4030 indent(out) <<
"temp_" << *d_iter <<
4031 ".insert(make_pair(_tp.first, _tp.second." <<
4032 getexpr <<
"));" << endl;
4034 indent(out) <<
"}" << endl;
4035 indent(out) <<
"tdata.__dsobj_" << *d_iter <<
"->Update(temp_" <<
4036 *d_iter <<
", _delmap_" << snm <<
", mono_usec);" << endl;
4037 indent(out) <<
"temp_" << *d_iter <<
".clear();" << endl;
4041 if (dat == INLINE) {
4042 indent(out) <<
"tdata.__dsobj_" << *d_iter <<
"->FillResult(_data." <<
4043 *d_iter <<
", _data.__isset." << *d_iter <<
");" << endl;
4044 indent(out) <<
"if (_data.__isset." << *d_iter <<
") send = true;" << endl;
4050 indent(out) <<
"}" << endl << endl;
4053 if (!periodic_ustruct) {
4054 indent(out) <<
"send = true;" << endl;
4056 indent(out) <<
"return send;" << endl;
4058 indent(out) <<
"}" << endl << endl;
4061 assert(f_iter == fields.end());
4070 void t_cpp_generator::generate_sandesh_reader(ofstream& out,
4073 "int32_t " << tsandesh->
get_name() <<
4074 "::Read(boost::shared_ptr<contrail::sandesh::protocol::TProtocol> iprot) {"
4077 generate_common_struct_reader_body(out, tsandesh,
false);
4080 "}" << endl << endl;
4089 void t_cpp_generator::generate_sandesh_writer(ofstream& out,
4092 "int32_t " << tsandesh->
get_name() <<
4093 "::Write(boost::shared_ptr<contrail::sandesh::protocol::TProtocol> oprot) const {" <<
4096 generate_common_struct_writer_body(out, tsandesh,
false);
4110 void t_cpp_generator::generate_sandesh_uve_creator(
4111 std::ofstream& out,
t_sandesh* tsandesh) {
4112 const vector<t_field*>& fields = tsandesh->
get_members();
4113 vector<t_field*>::const_iterator f_iter = fields.begin();
4114 assert((*f_iter)->get_name() ==
"data");
4116 bool is_proxy =
false;
4117 std::map<std::string, std::string>::iterator ait;
4118 ait = ((*f_iter)->get_type())->annotations_.find(
"period");
4120 std::string sname = tsandesh->
get_name();
4121 indent(out) <<
"SANDESH_UVE_DEF(" << sname <<
"," <<
4124 if (ait == ((*f_iter)->get_type())->annotations_.end()) {
4125 indent(out) <<
", 0, 0);" << endl;
4127 std::map<std::string, std::string>::iterator tmit;
4128 tmit = ((*f_iter)->get_type())->annotations_.find(
"timeout");
4129 if (tmit == ((*f_iter)->get_type())->annotations_.end()) {
4130 indent(out) <<
", " << atoi(ait->second.c_str()) <<
", 0);" << endl;
4132 indent(out) <<
", " << atoi(ait->second.c_str()) <<
", " <<
4133 atoi(tmit->second.c_str()) <<
");" << endl;
4138 indent(out) <<
"void " << sname <<
4139 "::Send(const " <<
type_name((*f_iter)->get_type()) <<
4140 "& cdata, SandeshLevel::type Xlevel, SandeshUVE::SendType stype," <<
4141 " uint32_t seqno, uint32_t cycle, std::string ctx) {" << endl;
4143 indent(out) << sname <<
" *snh = new " << sname <<
"(seqno, cdata);" << endl;
4144 indent(out) <<
"snh->set_level(Xlevel);" << endl;
4145 indent(out) <<
"if (snh->LoadUVE(stype, cycle)) {" << endl;
4147 indent(out) <<
"snh->set_context(ctx); snh->set_more(!ctx.empty());" << endl;
4148 indent(out) <<
"if (stype == SandeshUVE::ST_SYNC) snh->set_hints(snh->hints() | " <<
4149 "g_sandesh_constants.SANDESH_SYNC_HINT);" << endl;
4150 indent(out) <<
"snh->Dispatch();" << endl;
4152 indent(out) <<
"} else snh->Release();" << endl;
4154 indent(out) <<
"}" << endl << endl;
4156 indent(out) <<
"void " << sname <<
4157 "::Send(const " <<
type_name((*f_iter)->get_type()) <<
4158 "& cdata, SandeshUVE::SendType stype, uint32_t seqno," <<
4159 " uint32_t cycle, std::string ctx) {" << endl;
4161 indent(out) << sname <<
" *snh = new " << sname <<
"(seqno, cdata);" << endl;
4162 indent(out) <<
"snh->set_level(SandeshLevel::SYS_NOTICE);" << endl;
4163 indent(out) <<
"if (snh->LoadUVE(stype, cycle)) {" << endl;
4165 indent(out) <<
"snh->set_context(ctx); snh->set_more(!ctx.empty());" << endl;
4166 indent(out) <<
"if (stype == SandeshUVE::ST_SYNC) snh->set_hints(snh->hints() | " <<
4167 "g_sandesh_constants.SANDESH_SYNC_HINT);" << endl;
4168 indent(out) <<
"snh->Dispatch();" << endl;
4170 indent(out) <<
"} else snh->Release();" << endl;
4172 indent(out) <<
"}" << endl << endl;
4175 indent(out) <<
"void " << sname <<
4176 "::Send(const " <<
type_name((*f_iter)->get_type()) <<
4177 "& data, SandeshLevel::type Xlevel, std::string table, " <<
4178 "uint64_t mono_usec, int partition) {" << endl;
4181 indent(out) <<
"void " << sname <<
4182 "::Send(const " <<
type_name((*f_iter)->get_type()) <<
4183 "& data, SandeshLevel::type Xlevel, std::string table, " <<
4184 "uint64_t mono_usec) {" << endl;
4186 indent(out) <<
"int partition = -1;" << endl;
4190 " & cdata = const_cast<" <<
type_name((*f_iter)->get_type()) <<
4191 " &>(data);" << endl;
4192 indent(out) <<
"uint32_t msg_seqno = lseqnum_.fetch_and_increment() + 1;" << endl;
4193 indent(out) <<
"if (!table.empty()) cdata.table_ = table;" << endl;
4194 indent(out) <<
"if (uvemap" << sname <<
4195 ".UpdateUVE(cdata, msg_seqno, mono_usec, partition, Xlevel)) {" << endl;
4197 indent(out) << sname <<
" *snh = new " << sname <<
"(msg_seqno, cdata);" << endl;
4198 indent(out) <<
"snh->set_level(Xlevel);" << endl;
4199 indent(out) <<
"snh->Dispatch();" << endl;
4201 indent(out) <<
"}" << endl;
4204 indent(out) <<
"}" << endl << endl;
4207 indent(out) <<
"void " << sname <<
4208 "::Send(const " <<
type_name((*f_iter)->get_type()) <<
4209 "& data, std::string table, " <<
4210 "uint64_t mono_usec, int partition) {" << endl;
4213 indent(out) <<
"void " << sname <<
4214 "::Send(const " <<
type_name((*f_iter)->get_type()) <<
4215 "& data, std::string table, " <<
4216 "uint64_t mono_usec) {" << endl;
4218 indent(out) <<
"int partition = -1;" << endl;
4222 " & cdata = const_cast<" <<
type_name((*f_iter)->get_type()) <<
4223 " &>(data);" << endl;
4224 indent(out) <<
"uint32_t msg_seqno = lseqnum_.fetch_and_increment() + 1;" << endl;
4225 indent(out) <<
"if (!table.empty()) cdata.table_ = table;" << endl;
4226 indent(out) <<
"if (uvemap" << sname <<
4227 ".UpdateUVE(cdata, msg_seqno, mono_usec, partition, SandeshLevel::SYS_NOTICE)) {" << endl;
4229 indent(out) << sname <<
" *snh = new " << sname <<
"(msg_seqno, cdata);" << endl;
4230 indent(out) <<
"snh->set_level(SandeshLevel::SYS_NOTICE);" << endl;
4231 indent(out) <<
"snh->Dispatch();" << endl;
4233 indent(out) <<
"}" << endl;
4236 indent(out) <<
"}" << endl << endl;
4245 void t_cpp_generator::generate_sandesh_creator(ofstream& out,
4252 indent(out) <<
"extern int " << program_name_ <<
"_marker;" << endl;
4254 indent(out) <<
"SANDESH_REGISTER_DEF_TYPE(" << tsandesh->
get_name() <<
4255 ");" << endl << endl;
4257 }
else if (((
t_base_type *)t)->is_sandesh_trace() ||
4259 generate_sandesh_trace(out, tsandesh);
4261 }
else if (((
t_base_type *)t)->is_sandesh_buffer()) {
4263 indent(out) <<
"SANDESH_REGISTER_DEF_TYPE(" << tsandesh->
get_name() <<
4264 ");" << endl << endl;
4266 }
else if (((
t_base_type *)t)->is_sandesh_uve() ||
4268 generate_sandesh_uve_creator(out, tsandesh);
4277 void t_cpp_generator::generate_get_size_field(ofstream& out,
t_field *tfield) {
4282 out <<
indent() <<
"if (__isset." << name <<
") {" <<
4286 if (
type->is_struct()) {
4288 }
else if (
type->is_container()) {
4289 generate_get_size_container(out,
type, name);
4290 }
else if (
type->is_string() ||
type->is_xml() ||
4291 type->is_static_const_string()) {
4292 out <<
indent() <<
"size += " << name <<
".length();" << endl;
4294 out <<
indent() <<
"size += sizeof(" + name +
");" << endl;
4299 out <<
indent() <<
"}" << endl;
4306 void t_cpp_generator::generate_logger_field(ofstream& out,
4309 bool log_value_only,
4319 out <<
indent() <<
"if (__isset." << name <<
") {" <<
4323 if (
type->is_struct()) {
4325 out <<
indent() <<
"Xbuf << " << prefix <<
"\"[ \";" << endl;
4327 out <<
indent() <<
"Xbuf << " << prefix <<
"\"" << name <<
"\"" <<
" << \"= [ \";" << endl;
4329 generate_logger_struct(out, (
t_struct *)
type, prefix, name);
4330 out <<
indent() <<
"Xbuf << " <<
"\" ]\";" << endl;
4331 }
else if (
type->is_container()) {
4333 out <<
indent() <<
"Xbuf << " << prefix <<
"\"[ \";" << endl;
4335 out <<
indent() <<
"Xbuf << " << prefix <<
"\"" << name <<
"\"" <<
" << \"= [ \";" << endl;
4337 generate_logger_container(out,
type, name, log_value_only);
4338 out <<
indent() <<
"Xbuf << " <<
"\" ]\";" << endl;
4339 }
else if (
type->is_base_type() ||
type->is_enum()) {
4341 if (log_value_only) {
4342 if (tbase->is_integer()) {
4343 out <<
indent() <<
"Xbuf << " << prefix <<
4344 "integerToString(" << name <<
4347 out <<
indent() <<
"Xbuf << " << prefix <<
4348 name <<
";" << endl;
4351 if (tbase->is_integer()) {
4352 out <<
indent() <<
"Xbuf << " << prefix <<
4353 "\"" << name <<
"\"" <<
4354 " << \" = \" << integerToString(" << name <<
4359 out <<
indent() <<
"struct in_addr ip_addr_"<< name <<
";" << endl;
4360 out <<
indent() <<
"ip_addr_" << name <<
".s_addr = htonl(" << name <<
");" << endl;
4361 out <<
indent() <<
"Xbuf << " << prefix <<
4362 "\"" << name <<
"\"" <<
4363 " << \" = \" << inet_ntoa(ip_addr_" << name <<
");" << endl;
4367 out <<
indent() <<
"Xbuf << " << prefix <<
4368 "\"" << name <<
"\"" <<
4369 " << \" = \" << " << name <<
4378 out <<
indent() <<
"}" << endl;
4385 void t_cpp_generator::generate_get_size_container(ofstream& out,
4389 string iter = tmp(
"_iter");
4392 out <<
indent() <<
"for (" << iter <<
" = " << name <<
".begin(); "
4393 << iter <<
" != " << name <<
".end(); ++" << iter <<
")" << endl;
4396 generate_get_size_map_element(out, (
t_map *)ttype, iter);
4397 }
else if(ttype->
is_set()) {
4398 generate_get_size_set_element(out, (
t_set *)ttype, iter);
4400 generate_get_size_list_element(out, (
t_list *)ttype, iter);
4409 void t_cpp_generator::generate_get_size_map_element(ofstream& out,
4413 generate_get_size_field(out, &kfield);
4415 generate_get_size_field(out, &vfield);
4421 void t_cpp_generator::generate_get_size_list_element(ofstream& out,
4425 generate_get_size_field(out, &efield);
4431 void t_cpp_generator::generate_get_size_set_element(ofstream& out,
4435 generate_get_size_field(out, &efield);
4441 void t_cpp_generator::generate_logger_container(ofstream& out,
4444 bool log_value_only) {
4446 string iter = tmp(
"_iter");
4447 string prefix =
"\" \" << ";
4448 out <<
indent() <<
"Xbuf << " << prefix <<
"\"[\";" << endl;
4450 indent() <<
type_name(ttype) <<
"::const_iterator " << iter <<
";" << endl <<
4451 indent() <<
"for (" << iter <<
" = " << name <<
".begin(); " << iter <<
" != " << name <<
".end(); ++" << iter <<
")" << endl;
4454 generate_logger_map_element(out, (
t_map*)ttype, iter, log_value_only);
4455 }
else if (ttype->
is_set()) {
4456 generate_logger_set_element(out, (
t_set*)ttype, iter, log_value_only);
4457 }
else if (ttype->
is_list()) {
4458 generate_logger_list_element(out, (
t_list*)ttype, iter, log_value_only);
4460 out <<
indent() <<
"Xbuf << " <<
"\", \";" << endl;
4462 out <<
indent() <<
"Xbuf << " << prefix <<
"\"]\";" << endl;
4469 void t_cpp_generator::generate_logger_map_element(ofstream& out,
4472 bool log_value_only) {
4473 string prefix =
"\" \" << ";
4475 generate_logger_field(out, &kfield, prefix, log_value_only,
true);
4477 generate_logger_field(out, &vfield, prefix, log_value_only,
true);
4483 void t_cpp_generator::generate_logger_set_element(ofstream& out,
4486 bool log_value_only) {
4487 string prefix =
"\" \" << ";
4489 generate_logger_field(out, &efield, prefix, log_value_only,
true);
4495 void t_cpp_generator::generate_logger_list_element(ofstream& out,
4498 bool log_value_only) {
4499 string prefix =
"\" \" << ";
4501 generate_logger_field(out, &efield, prefix, log_value_only,
true);
4507 void t_cpp_generator::generate_get_size_struct(ofstream& out,
4511 out <<
indent() <<
"size += " << name <<
".GetSize();" << endl;
4517 void t_cpp_generator::generate_logger_struct(ofstream& out,
4522 out <<
indent() <<
"Xbuf << " << prefix << name <<
".log();" << endl;
4531 void t_cpp_generator::generate_sandesh_static_drop_logger(ofstream &out,
4532 t_sandesh *tsandesh,
bool generate_sandesh_object) {
4534 if (generate_sandesh_object) {
4535 out <<
indent() <<
"static void DropLog(const std::string& " <<
4536 "drop_reason, std::string category, SandeshLevel::type level, " <<
4537 tsandesh->
get_name() <<
" *snh) {" << endl;
4539 out <<
indent() <<
"static void DropLog" <<
4540 generate_sandesh_async_creator(tsandesh,
true,
false,
false,
"",
"",
4541 false,
false,
true) <<
" {" << endl;
4544 out <<
indent() <<
"log4cplus::LogLevel Xlog4level(" <<
4545 "SandeshLevelTolog4Level(SandeshLevel::SYS_ERR));" << endl;
4547 "log4cplus::Logger Xlogger = Sandesh::logger();" << endl;
4549 "if (!Xlogger.isEnabledFor(Xlog4level)) {" << endl;
4551 out <<
indent() <<
"return;" << endl;
4553 out <<
indent() <<
"log4cplus::tostringstream Xbuf;" << endl;
4557 if (generate_sandesh_object) {
4558 out <<
indent() <<
"Xbuf << drop_reason << snh->ToString();"
4561 out <<
indent() <<
"Xbuf << drop_reason << category << " <<
4562 "\" [\" << LevelToString(level) << \"]: " <<
4563 tsandesh->
get_name() <<
": \";" << endl;
4566 assert(!generate_sandesh_object);
4567 out <<
indent() <<
"Xbuf << drop_reason << \"" <<
4568 tsandesh->
get_name() <<
": \";" << endl;
4575 bool log_value_only = is_system || is_trace;
4578 const vector<t_field*>& fields = tsandesh->
get_members();
4579 vector<t_field*>::const_iterator f_iter;
4580 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
4582 prefix =
"\" \" << ";
4586 if (!generate_sandesh_object) {
4587 generate_logger_field(out, *f_iter, prefix, log_value_only,
false,
4591 out <<
indent() <<
"Xlogger.forcedLog(Xlog4level, Xbuf.str());" <<
4594 indent(out) <<
"}" << endl << endl;
4597 void t_cpp_generator::generate_sandesh_static_logger(ofstream &out,
4598 t_sandesh *tsandesh,
bool generate_sandesh_object) {
4600 if (generate_sandesh_object) {
4601 out <<
indent() <<
"static void Log(std::string category, " <<
4602 "SandeshLevel::type level, " << tsandesh->
get_name() <<
4605 out <<
indent() <<
"static void Log" <<
4606 generate_sandesh_async_creator(tsandesh,
true,
false,
false,
"",
"",
4607 false,
false,
false) <<
" {" << endl;
4611 "if (!IsLevelCategoryLoggingAllowed(" <<
4612 generate_sandesh_base_name(tsandesh,
true) <<
4613 ", level, category)) {" << endl;
4615 out <<
indent() <<
"return;" << endl;
4617 out <<
indent() <<
"log4cplus::LogLevel Xlog4level(" <<
4618 "SandeshLevelTolog4Level(level));" << endl;
4620 "log4cplus::Logger Xlogger = Sandesh::logger();" << endl;
4622 "if (!Xlogger.isEnabledFor(Xlog4level)) {" << endl;
4624 out <<
indent() <<
"return;" << endl;
4626 out <<
indent() <<
"log4cplus::tostringstream Xbuf;" << endl;
4630 if (generate_sandesh_object) {
4631 out <<
indent() <<
"Xbuf << snh->ToString();" << endl;
4633 out <<
indent() <<
"Xbuf << category << " <<
4634 "\" [\" << LevelToString(level) << \"]: " <<
4635 tsandesh->
get_name() <<
": \";" << endl;
4638 assert(!generate_sandesh_object);
4639 out <<
indent() <<
"Xbuf << \"" <<
4640 tsandesh->
get_name() <<
": \";" << endl;
4647 bool log_value_only = is_system || is_trace;
4650 const vector<t_field*>& fields = tsandesh->
get_members();
4651 vector<t_field*>::const_iterator f_iter;
4652 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
4654 prefix =
"\" \" << ";
4658 if (!generate_sandesh_object) {
4659 generate_logger_field(out, *f_iter, prefix, log_value_only,
false,
4663 out <<
indent() <<
"Xlogger.forcedLog(Xlog4level, Xbuf.str());" <<
4666 indent(out) <<
"}" << endl << endl;
4676 void t_cpp_generator::generate_sandesh_logger(ofstream &out,
4679 if (ltype == sandesh_logger::BUFFER) {
4681 "::ToString() const {" << endl;
4685 "::Log() const {" << endl;
4687 }
else if (ltype == sandesh_logger::FORCED_LOG) {
4689 "::ForcedLog() const {" << endl;
4694 const vector<t_field*>& fields = tsandesh->
get_members();
4695 vector<t_field*>::const_iterator f_iter;
4697 bool log_value_only =
4700 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
4701 string prefix =
"\" \" << ";
4705 string category_str =
"Sandesh::category()";
4706 string level_str =
"Sandesh::level()";
4708 out <<
indent() <<
"if (!IsLevelCategoryLoggingAllowed(" <<
4709 generate_sandesh_base_name(tsandesh,
true) <<
", " <<
4710 level_str <<
", " << category_str <<
")) {" << endl;
4712 out <<
indent() <<
"return;" << endl;
4714 out <<
indent() <<
"log4cplus::LogLevel Xlog4level(" <<
4715 "SandeshLevelTolog4Level(" << level_str <<
"));" << endl;
4717 "log4cplus::Logger Xlogger = Sandesh::logger();" << endl;
4719 "if (!Xlogger.isEnabledFor(Xlog4level)) {" << endl;
4721 out <<
indent() <<
"return;" << endl;
4723 out <<
indent() <<
"log4cplus::tostringstream Xbuf;" << endl;
4724 }
else if (ltype == sandesh_logger::FORCED_LOG) {
4725 out <<
indent() <<
"log4cplus::LogLevel Xlog4level(" <<
4726 "SandeshLevelTolog4Level(" << level_str <<
"));" << endl;
4728 "log4cplus::Logger Xlogger = Sandesh::logger();" << endl;
4729 out <<
indent() <<
"log4cplus::tostringstream Xbuf;" << endl;
4730 }
else if (ltype == sandesh_logger::BUFFER) {
4731 out <<
indent() <<
"std::stringstream Xbuf;" << endl;
4734 "Xbuf << integerToString(timestamp()) << \" \";" <<
4740 out <<
indent() <<
"Xbuf << " << category_str <<
4741 " << \" [\" << LevelToString(" << level_str <<
") << \"]: "
4742 << tsandesh->
get_name() <<
": \";" << endl;
4748 generate_logger_field(out, *f_iter, prefix, log_value_only,
false);
4751 if (ltype == sandesh_logger::BUFFER) {
4752 out <<
indent() <<
"return Xbuf.str();" << endl;
4754 out <<
indent() <<
"Xlogger.forcedLog(Xlog4level, Xbuf.str());" <<
4758 if (ltype == sandesh_logger::BUFFER) {
4759 out <<
indent() <<
"return std::string();" << endl;
4763 indent(out) <<
"}" << endl << endl;
4772 void t_cpp_generator::generate_sandesh_get_size(ofstream& out,
4776 "::GetSize() const {" << endl;
4778 indent(out) <<
"size_t size = 0;" << endl;
4779 const vector<t_field*>& fields = tsandesh->
get_members();
4780 vector<t_field*>::const_iterator f_iter;
4781 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
4782 generate_get_size_field(out, *f_iter);
4784 indent(out) <<
"return size;" << endl;
4786 indent(out) <<
"}" << endl << endl;
4795 void t_cpp_generator::generate_sandesh_loggers(ofstream& out,
4800 generate_sandesh_logger(out, tsandesh, sandesh_logger::FORCED_LOG);
4802 generate_sandesh_logger(out, tsandesh, sandesh_logger::BUFFER);
4811 void t_cpp_generator::generate_sandesh_trace(ofstream& out,
4813 std::string creator_name =
"trace_sandesh";
4814 indent(out) <<
"void " << tsandesh->
get_name() <<
"::TraceMsg" <<
4815 generate_sandesh_no_static_const_string_function(tsandesh,
true,
false,
true) <<
4818 out <<
indent() <<
"TraceSandeshType *trace = " <<
4819 "TraceSandeshType::GetInstance();" << endl;
4820 out <<
indent() <<
"if (trace != NULL && trace->IsTraceOn() && trace_buf->IsTraceOn()) {" << endl;
4822 out <<
indent() << tsandesh->
get_name() <<
" *" << creator_name <<
" = " <<
4824 generate_sandesh_no_static_const_string_function(tsandesh,
false,
false,
false,
false,
true) <<
4826 out <<
indent() << creator_name <<
"->set_category(trace_buf->Name());" << endl;
4827 out <<
indent() <<
"uint32_t seqnum(trace_buf->GetNextSeqNum());" << endl;
4828 out <<
indent() << creator_name <<
"->set_seqnum(seqnum);" << endl;
4829 out <<
indent() <<
"trace_buf->TraceWrite(" << creator_name <<
");" << endl;
4830 out <<
indent() <<
"if ((IsLocalLoggingEnabled() && IsTracePrintEnabled()) || IsUnitTest()) " << creator_name <<
"->Log();" << endl;
4832 out <<
indent() <<
"}" <<endl;
4835 "}" << endl << endl;
4842 void t_cpp_generator::generate_isRatelimitPass(ofstream& out,
4844 out <<
indent() <<
"uint32_t send_rate_limit = Sandesh::get_send_rate_limit();" << endl;
4845 out <<
indent() <<
"if (send_rate_limit == 0) {" << endl;
4847 out <<
indent() <<
"return false;" << endl;
4849 out <<
indent() <<
"}" << endl;
4850 out <<
indent() <<
"tbb::mutex::scoped_lock lock(rate_limit_mutex_);" << endl;
4851 out <<
indent() <<
"if (rate_limit_buffer_.capacity() !="
4852 " send_rate_limit) {" << endl;
4854 out <<
indent() <<
"//Resize the buffer to the "
4855 "buffer_threshold_" << endl;
4856 out <<
indent() <<
"rate_limit_buffer_.rresize(send_rate_limit);"
4858 out <<
indent() <<
"rate_limit_buffer_.set_capacity("
4859 "send_rate_limit);" << endl;
4861 out <<
indent() <<
"}" << endl;
4862 out <<
indent() <<
"time_t current_time = time(0);" << endl;
4863 out <<
indent() <<
"if (rate_limit_buffer_.capacity() == rate_limit_buffer_"
4864 ".size()) {" << endl;
4866 out <<
indent() <<
"if (*rate_limit_buffer_.begin() == current_time) {" << endl;
4868 out <<
indent() <<
"//update tx and call droplog" << endl;
4869 out <<
indent() <<
"//Dont have to log more than once" << endl;
4870 out <<
indent() <<
"return false;" << endl;
4872 out <<
indent() <<
"}" << endl;
4874 out <<
indent() <<
"}" << endl;
4875 out <<
indent() <<
"//Should log failure after a sucessful write" << endl;
4876 out <<
indent() <<
"do_rate_limit_drop_log_ = true;" << endl;
4877 out <<
indent() <<
"rate_limit_buffer_.push_back(current_time);" << endl;
4878 out <<
indent() <<
"return true;" << endl;
4897 vector<t_field*>::const_iterator f_iter;
4899 if (gen_templates_) {
4901 indent() <<
"template <class Protocol_>" << endl <<
4903 "::write(Protocol_* oprot) const {" << endl;
4906 "uint32_t " << tstruct->
get_name() <<
4907 "::write(::contrail::sandesh::protocol::TProtocol* oprot) const {" << endl;
4913 indent() <<
"uint32_t xfer = 0;" << endl <<
4917 "xfer += oprot->writeStructBegin(\"" << name <<
"\");" << endl;
4920 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
4931 out <<
"(this->__isset." << (*f_iter)->get_name() <<
") {" << endl;
4937 indent() <<
"xfer += oprot->writeFieldBegin(" <<
4938 "\"" << (*f_iter)->get_name() <<
"\", " <<
4939 type_to_enum((*f_iter)->get_type()) <<
", " <<
4940 (*f_iter)->get_key() <<
");" << endl;
4943 generate_serialize_field(out, *f_iter,
"(*(this->",
"))");
4945 generate_serialize_field(out, *f_iter,
"this->");
4948 indent(out) <<
"xfer += oprot->writeFieldEnd();" << endl;
4957 indent() <<
"xfer += oprot->writeFieldStop();" << endl <<
4958 indent() <<
"xfer += oprot->writeStructEnd();" << endl <<
4959 indent() <<
"return xfer;" << endl;
4976 string svcname = tservice->
get_name();
4979 string f_header_name = get_out_dir()+svcname+
".h";
4986 "#ifndef " << svcname <<
"_H" << endl <<
4987 "#define " << svcname <<
"_H" << endl <<
4989 if (gen_cob_style_) {
4991 "#include <transport/TBufferTransports.h>" << endl <<
4992 "#include <tr1/functional>" << endl <<
4993 "namespace apache { namespace thrift { namespace async {" << endl <<
4994 "class TAsyncChannel;" << endl <<
4998 "#include <TProcessor.h>" << endl;
4999 if (gen_cob_style_) {
5001 "#include <async/TAsyncProcessor.h>" << endl;
5004 "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
5005 "_types.h\"" << endl;
5008 if (extends_service != NULL) {
5010 "#include \"" << get_include_prefix(*(extends_service->
get_program())) <<
5011 extends_service->
get_name() <<
".h\"" << endl;
5020 string f_service_name = get_out_dir()+svcname+
".cpp";
5021 f_service_.open(f_service_name.c_str());
5025 "#include \"" << get_include_prefix(*get_program()) << svcname <<
".h\"" << endl;
5026 if (gen_cob_style_) {
5028 "#include \"async/TAsyncChannel.h\"" << endl;
5030 if (gen_templates_) {
5032 "#include \"" << get_include_prefix(*get_program()) << svcname <<
5035 string f_service_tcc_name = get_out_dir()+svcname+
".tcc";
5036 f_service_tcc_.open(f_service_tcc_name.c_str());
5040 "#include \"" << get_include_prefix(*get_program()) << svcname <<
5044 "#ifndef " << svcname <<
"_TCC" << endl <<
5045 "#define " << svcname <<
"_TCC" << endl <<
5048 if (gen_cob_style_) {
5050 "#include \"async/TAsyncChannel.h\"" << endl;
5055 endl << ns_open_ << endl << endl;
5057 endl << ns_open_ << endl << endl;
5060 generate_service_interface(tservice,
"");
5061 generate_service_interface_factory(tservice,
"");
5062 generate_service_null(tservice,
"");
5063 generate_service_helpers(tservice);
5064 generate_service_client(tservice,
"");
5065 generate_service_processor(tservice,
"");
5066 generate_service_multiface(tservice);
5067 generate_service_skeleton(tservice);
5070 if (gen_cob_style_) {
5071 generate_service_interface(tservice,
"CobCl");
5072 generate_service_interface(tservice,
"CobSv");
5073 generate_service_interface_factory(tservice,
"CobSv");
5074 generate_service_null(tservice,
"CobSv");
5075 generate_service_client(tservice,
"Cob");
5076 generate_service_processor(tservice,
"Cob");
5077 generate_service_async_skeleton(tservice);
5082 ns_close_ << endl <<
5085 ns_close_ << endl <<
5088 ns_close_ << endl <<
5092 if (gen_templates_) {
5094 "#include \"" << get_include_prefix(*get_program()) << svcname <<
5096 "#include \"" << get_include_prefix(*get_program()) << program_name_ <<
5097 "_types.tcc\"" << endl <<
5107 f_service_tcc_.close();
5120 vector<t_function*>::iterator f_iter;
5121 std::ofstream& out = (gen_templates_ ? f_service_tcc_ : f_service_);
5123 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
5124 t_struct* ts = (*f_iter)->get_arglist();
5128 ts->
set_name(tservice->
get_name() +
"_" + (*f_iter)->get_name() +
"_args");
5129 generate_struct_definition(
f_header_, ts,
false);
5130 generate_struct_reader(out, ts);
5131 generate_struct_writer(out, ts);
5132 ts->
set_name(tservice->
get_name() +
"_" + (*f_iter)->get_name() +
"_pargs");
5133 generate_struct_definition(
f_header_, ts,
false,
true,
false,
true);
5134 generate_struct_writer(out, ts,
true);
5137 generate_function_helpers(tservice, *f_iter);
5149 if (style ==
"CobCl") {
5152 if (gen_templates_) {
5154 service_if_name +=
"T";
5156 "template <class Protocol_>" << endl;
5162 string extends =
"";
5166 if (style ==
"CobCl" && gen_templates_) {
5169 extends +=
"T<Protocol_>";
5173 if (style ==
"CobCl" && gen_templates_) {
5174 f_header_ <<
"template <class Protocol_>" << endl;
5177 "class " << service_if_name << extends <<
" {" << endl <<
5181 indent() <<
"virtual ~" << service_if_name <<
"() {}" << endl;
5184 vector<t_function*>::iterator f_iter;
5185 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
5187 indent() <<
"virtual " << function_signature(*f_iter, style) <<
" = 0;" << endl;
5191 "};" << endl << endl;
5193 if (style ==
"CobCl" && gen_templates_) {
5197 "typedef " << service_if_name <<
5198 "< ::contrail::sandesh::protocol::TProtocol> " <<
5235 string base_if_name =
type_name(base_service) + style +
"If";
5238 string factory_name = service_if_name +
"Factory";
5242 style +
"IfFactory";
5246 "class " << factory_name << extends <<
" {" << endl <<
5250 indent() <<
"typedef " << service_if_name <<
" Handler;" << endl <<
5252 indent() <<
"virtual ~" << factory_name <<
"() {}" << endl <<
5254 indent() <<
"virtual " << service_if_name <<
"* getHandler(" <<
5255 "const ::contrail::sandesh::TConnectionInfo& connInfo) = 0;" <<
5257 indent() <<
"virtual void releaseHandler(" << base_if_name <<
5258 "* /* handler */) = 0;" << endl;
5262 "};" << endl << endl;
5265 string singleton_factory_name = service_if_name +
"SingletonFactory";
5267 "class " << singleton_factory_name <<
5268 " : virtual public " << factory_name <<
" {" << endl <<
5272 indent() << singleton_factory_name <<
"(const boost::shared_ptr<" <<
5273 service_if_name <<
">& iface) : iface_(iface) {}" << endl <<
5274 indent() <<
"virtual ~" << singleton_factory_name <<
"() {}" << endl <<
5276 indent() <<
"virtual " << service_if_name <<
"* getHandler(" <<
5277 "const ::contrail::sandesh::TConnectionInfo&) {" << endl <<
5278 indent() <<
" return iface_.get();" << endl <<
5279 indent() <<
"}" << endl <<
5280 indent() <<
"virtual void releaseHandler(" << base_if_name <<
5281 "* /* handler */) {}" << endl;
5285 " protected:" << endl <<
5286 indent() <<
"boost::shared_ptr<" << service_if_name <<
"> iface_;" << endl;
5290 "};" << endl << endl;
5299 string extends =
"";
5304 "class " <<
service_name_ << style <<
"Null : virtual public " <<
service_name_ << style <<
"If" << extends <<
" {" << endl <<
5310 vector<t_function*>::iterator f_iter;
5311 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
5313 indent() << function_signature(*f_iter, style,
"",
false) <<
" {" << endl;
5316 t_type* returntype = (*f_iter)->get_returntype();
5317 t_field returnfield(returntype,
"_return");
5320 if (returntype->
is_void() || is_complex_type(returntype)) {
5324 indent() << declare_field(&returnfield,
true) << endl <<
5325 indent() <<
"return _return;" << endl;
5327 }
else if (style ==
"CobSv") {
5331 t_field returnfield(returntype,
"_return");
5333 indent() << declare_field(&returnfield,
true) << endl <<
5334 indent() <<
"return cob(_return);" << endl;
5338 throw "UNKNOWN STYLE";
5347 "};" << endl << endl;
5355 if (is_complex_type(ret_type)) {
5357 out << iface <<
"->" << tfunction->
get_name() <<
"(" << target;
5359 out << target <<
" = " << iface <<
"->" << tfunction->
get_name() <<
"(";
5362 out << iface <<
"->" << tfunction->
get_name() <<
"(";
5365 vector<t_field*>::const_iterator f_iter;
5366 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
5372 out << arg_prefix << (*f_iter)->get_name();
5374 out <<
");" << endl;
5378 string svcname = tservice->
get_name();
5381 string f_skeleton_name = get_out_dir()+svcname+
"_async_server.skeleton.cpp";
5385 ofstream f_skeleton;
5386 f_skeleton.open(f_skeleton_name.c_str());
5388 "// This autogenerated skeleton file illustrates one way to adapt a synchronous" << endl <<
5389 "// interface into an asynchronous interface. You should copy it to another" << endl <<
5390 "// filename to avoid overwriting it and rewrite as asynchronous any functions" << endl <<
5391 "// that would otherwise introduce unwanted latency." << endl <<
5393 "#include \"" << get_include_prefix(*get_program()) << svcname <<
".h\"" << endl <<
5394 "#include <protocol/TBinaryProtocol.h>" << endl <<
5395 "#include <async/TEventServer.h>" << endl <<
5397 "using namespace ::contrail::sandesh;" << endl <<
5398 "using namespace ::contrail::sandesh::protocol;" << endl <<
5399 "using namespace ::contrail::sandesh::transport;" << endl <<
5400 "using namespace ::contrail::sandesh::async;" << endl <<
5402 "using boost::shared_ptr;" << endl <<
5407 "using namespace " << string(ns, 0, ns.size()-2) <<
";" << endl <<
5412 "class " << svcname <<
"AsyncHandler : " <<
5413 "public " << svcname <<
"CobSvIf {" << endl <<
5417 indent() << svcname <<
"AsyncHandler() {" << endl <<
5418 indent() <<
" syncHandler_ = std::unique_ptr<" << svcname <<
5419 "Handler>(new " << svcname <<
"Handler);" << endl <<
5420 indent() <<
" // Your initialization goes here" << endl <<
5426 vector<t_function*>::iterator f_iter;
5427 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
5430 indent() << function_signature(*f_iter,
"CobSv",
"",
true) <<
" {" << endl;
5433 t_type* returntype = (*f_iter)->get_returntype();
5434 t_field returnfield(returntype,
"_return");
5436 string target = returntype->
is_void() ?
"" :
"_return";
5439 indent() << declare_field(&returnfield,
true) << endl;
5441 generate_function_call(f_skeleton, *f_iter, target,
"syncHandler_",
"");
5442 f_skeleton <<
indent() <<
"return cob(" << target <<
");" << endl;
5444 scope_down(f_skeleton);
5446 f_skeleton << endl <<
5447 " protected:" << endl <<
5448 indent() <<
"std::unique_ptr<" << svcname <<
"Handler> syncHandler_;" << endl;
5451 "};" << endl << endl;
5464 vector<t_function*>::iterator f_iter;
5466 string extends =
"";
5467 string extends_multiface =
"";
5470 extends_multiface =
", public " + extends +
"Multiface";
5473 string list_type = string(
"std::vector<boost::shared_ptr<") +
service_name_ +
"If> >";
5479 extends_multiface <<
" {" << endl <<
5483 indent() <<
service_name_ <<
"Multiface(" << list_type <<
"& ifaces) : ifaces_(ifaces) {" << endl;
5484 if (!extends.empty()) {
5486 indent() <<
" std::vector<boost::shared_ptr<" +
service_name_ +
"If> >::iterator iter;" << endl <<
5487 indent() <<
" for (iter = ifaces.begin(); iter != ifaces.end(); ++iter) {" << endl <<
5488 indent() <<
" " << extends <<
"Multiface::add(*iter);" << endl <<
5489 indent() <<
" }" << endl;
5492 indent() <<
"}" << endl <<
5498 " protected:" << endl;
5501 indent() << list_type <<
" ifaces_;" << endl <<
5504 if (!extends.empty()) {
5506 indent() <<
" " << extends <<
"Multiface::add(iface);" << endl;
5509 indent() <<
" ifaces_.push_back(iface);" << endl <<
5514 indent() <<
" public:" << endl;
5517 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
5518 t_struct* arglist = (*f_iter)->get_arglist();
5519 const vector<t_field*>& args = arglist->
get_members();
5520 vector<t_field*>::const_iterator a_iter;
5522 string call = string(
"ifaces_[i]->") + (*f_iter)->get_name() +
"(";
5524 if (is_complex_type((*f_iter)->get_returntype())) {
5528 for (a_iter = args.begin(); a_iter != args.end(); ++a_iter) {
5534 call += (*a_iter)->get_name();
5539 indent() << function_signature(*f_iter,
"") <<
" {" << endl;
5542 indent() <<
"size_t sz = ifaces_.size();" << endl <<
5543 indent() <<
"for (size_t i = 0; i < sz; ++i) {" << endl;
5544 if (!(*f_iter)->get_returntype()->is_void()) {
5546 indent() <<
" if (i == sz - 1) {" << endl;
5547 if (is_complex_type((*f_iter)->get_returntype())) {
5549 indent() <<
" " << call <<
";" << endl <<
5550 indent() <<
" return;" << endl;
5553 indent() <<
" return " << call <<
";" << endl;
5556 indent() <<
" } else {" << endl <<
5557 indent() <<
" " << call <<
";" << endl <<
5558 indent() <<
" }" << endl;
5561 indent() <<
" " << call <<
";" << endl;
5569 indent() <<
"}" << endl <<
5575 indent() <<
"};" << endl <<
5586 if (style ==
"Cob") {
5590 std::ofstream& out = (gen_templates_ ? f_service_tcc_ : f_service_);
5591 string template_header, template_suffix, short_suffix, protocol_type, _this;
5592 string const prot_factory_type =
5593 "::contrail::sandesh::protocol::TProtocolFactory";
5594 if (gen_templates_) {
5595 template_header =
"template <class Protocol_>\n";
5597 template_suffix =
"T<Protocol_>";
5598 protocol_type =
"Protocol_";
5601 protocol_type =
"::contrail::sandesh::protocol::TProtocol";
5603 string prot_ptr =
"boost::shared_ptr< " + protocol_type +
">";
5604 string client_suffix =
"Client" + template_suffix;
5605 string if_suffix =
"If";
5606 if (style ==
"Cob") {
5607 if_suffix += template_suffix;
5610 string extends =
"";
5611 string extends_client =
"";
5616 extends_client =
", public " + extends + style + client_suffix;
5622 "class " <<
service_name_ << style <<
"Client" << short_suffix <<
" : " <<
5623 "virtual public " <<
service_name_ << ifstyle << if_suffix <<
5624 extends_client <<
" {" << endl <<
5628 if (style !=
"Cob") {
5631 "(" << prot_ptr <<
" prot) :" <<
5633 if (extends.empty()) {
5635 indent() <<
" piprot_(prot)," << endl <<
5636 indent() <<
" poprot_(prot) {" << endl <<
5637 indent() <<
" iprot_ = prot.get();" << endl <<
5638 indent() <<
" oprot_ = prot.get();" << endl <<
5642 indent() <<
" " << extends << style << client_suffix <<
5643 "(prot, prot) {}" << endl;
5648 "(" << prot_ptr <<
" iprot, " << prot_ptr <<
" oprot) :" << endl;
5649 if (extends.empty()) {
5651 indent() <<
" piprot_(iprot)," << endl <<
5652 indent() <<
" poprot_(oprot) {" << endl <<
5653 indent() <<
" iprot_ = iprot.get();" << endl <<
5654 indent() <<
" oprot_ = oprot.get();" << endl <<
5658 indent() <<
" " << extends << style << client_suffix <<
5659 "(iprot, oprot) {}" << endl;
5666 indent() <<
"boost::shared_ptr< ::contrail::sandesh::protocol::TProtocol> getInputProtocol() {" << endl <<
5667 indent() <<
" return " << _this <<
"piprot_;" << endl <<
5671 indent() <<
"boost::shared_ptr< ::contrail::sandesh::protocol::TProtocol> getOutputProtocol() {" << endl <<
5672 indent() <<
" return " << _this <<
"poprot_;" << endl <<
5678 "boost::shared_ptr< ::contrail::sandesh::async::TAsyncChannel> channel, " <<
5679 "::contrail::sandesh::protocol::TProtocolFactory* protocolFactory) :" <<
5681 if (extends.empty()) {
5683 indent() <<
" channel_(channel)," << endl <<
5684 indent() <<
" itrans_(new ::contrail::sandesh::transport::TMemoryBuffer())," << endl <<
5685 indent() <<
" otrans_(new ::contrail::sandesh::transport::TMemoryBuffer())," << endl;
5686 if (gen_templates_) {
5690 indent() <<
" piprot_(boost::dynamic_pointer_cast<Protocol_>(" <<
5691 "protocolFactory->getProtocol(itrans_)))," << endl <<
5692 indent() <<
" poprot_(boost::dynamic_pointer_cast<Protocol_>(" <<
5693 "protocolFactory->getProtocol(otrans_))) {" << endl;
5696 indent() <<
" if (!piprot_ || !poprot_) {" << endl <<
5697 indent() <<
" throw ::contrail::sandesh::TException(\"" <<
5698 "TProtocolFactory returned unexpected protocol type in " <<
5700 " constructor\");" << endl <<
5701 indent() <<
" }" << endl;
5704 indent() <<
" piprot_(protocolFactory->getProtocol(itrans_))," <<
5706 indent() <<
" poprot_(protocolFactory->getProtocol(otrans_)) {" <<
5710 indent() <<
" iprot_ = piprot_.get();" << endl <<
5711 indent() <<
" oprot_ = poprot_.get();" << endl <<
5715 indent() <<
" " << extends << style << client_suffix <<
5716 "(channel, protocolFactory) {}" << endl;
5720 if (style ==
"Cob") {
5722 indent() <<
"boost::shared_ptr< ::contrail::sandesh::async::TAsyncChannel> getChannel() {" << endl <<
5723 indent() <<
" return " << _this <<
"channel_;" << endl <<
5725 if (!gen_no_client_completion_) {
5727 indent() <<
"virtual void completed__(bool /* success */) {}" << endl;
5732 vector<t_function*>::const_iterator f_iter;
5733 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
5734 indent(
f_header_) << function_signature(*f_iter, ifstyle) <<
";" << endl;
5737 string(
"send_") + (*f_iter)->
get_name(),
5738 (*f_iter)->get_arglist());
5739 indent(
f_header_) << function_signature(&send_function,
"") <<
";" << endl;
5740 if (!(*f_iter)->is_oneway()) {
5742 t_function recv_function((*f_iter)->get_returntype(),
5743 string(
"recv_") + (*f_iter)->get_name(),
5745 indent(
f_header_) << function_signature(&recv_function,
"") <<
";" << endl;
5750 if (extends.empty()) {
5752 " protected:" << endl;
5755 if (style ==
"Cob") {
5757 indent() <<
"boost::shared_ptr< ::contrail::sandesh::async::TAsyncChannel> channel_;" << endl <<
5758 indent() <<
"boost::shared_ptr< ::contrail::sandesh::transport::TMemoryBuffer> itrans_;" << endl <<
5759 indent() <<
"boost::shared_ptr< ::contrail::sandesh::transport::TMemoryBuffer> otrans_;" << endl;
5762 indent() << prot_ptr <<
" piprot_;" << endl <<
5763 indent() << prot_ptr <<
" poprot_;" << endl <<
5764 indent() << protocol_type <<
"* iprot_;" << endl <<
5765 indent() << protocol_type <<
"* oprot_;" << endl;
5774 if (gen_templates_) {
5779 "ClientT< ::contrail::sandesh::protocol::TProtocol> " <<
5784 string scope =
service_name_ + style + client_suffix +
"::";
5787 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
5788 string funname = (*f_iter)->get_name();
5791 if (gen_templates_) {
5792 indent(out) << template_header;
5795 function_signature(*f_iter, ifstyle, scope) << endl;
5798 "send_" << funname <<
"(";
5801 t_struct* arg_struct = (*f_iter)->get_arglist();
5804 const vector<t_field*>& fields = arg_struct->
get_members();
5805 vector<t_field*>::const_iterator fld_iter;
5807 for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
5813 out << (*fld_iter)->get_name();
5815 out <<
");" << endl;
5817 if (style !=
"Cob") {
5818 if (!(*f_iter)->is_oneway()) {
5820 if (!(*f_iter)->get_returntype()->is_void()) {
5821 if (is_complex_type((*f_iter)->get_returntype())) {
5822 out <<
"recv_" << funname <<
"(_return);" << endl;
5824 out <<
"return recv_" << funname <<
"();" << endl;
5828 "recv_" << funname <<
"();" << endl;
5832 if (!(*f_iter)->is_oneway()) {
5834 indent() << _this <<
"channel_->sendAndRecvMessage(" <<
5835 "std::tr1::bind(cob, this), " << _this <<
"otrans_.get(), " <<
5836 _this <<
"itrans_.get());" << endl;
5839 indent() << _this <<
"channel_->sendMessage(" <<
5840 "std::tr1::bind(cob, this), " << _this <<
"otrans_.get());" << endl;
5850 string(
"send_") + (*f_iter)->
get_name(),
5851 (*f_iter)->get_arglist());
5854 if (gen_templates_) {
5855 indent(out) << template_header;
5858 function_signature(&send_function,
"", scope) << endl;
5862 string argsname = tservice->
get_name() +
"_" + (*f_iter)->get_name() +
"_pargs";
5863 string resultname = tservice->
get_name() +
"_" + (*f_iter)->get_name() +
"_presult";
5867 indent() <<
"int32_t cseqid = 0;" << endl <<
5868 indent() << _this <<
"oprot_->writeMessageBegin(\"" <<
5869 (*f_iter)->get_name() <<
5870 "\", ::contrail::sandesh::protocol::T_CALL, cseqid);" << endl <<
5872 indent() << argsname <<
" args;" << endl;
5874 for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
5876 indent() <<
"args." << (*fld_iter)->get_name() <<
" = &" << (*fld_iter)->get_name() <<
";" << endl;
5880 indent() <<
"args.write(" << _this <<
"oprot_);" << endl <<
5882 indent() << _this <<
"oprot_->writeMessageEnd();" << endl <<
5883 indent() << _this <<
"oprot_->getTransport()->writeEnd();" << endl <<
5884 indent() << _this <<
"oprot_->getTransport()->flush();" << endl;
5890 if (!(*f_iter)->is_oneway()) {
5892 t_function recv_function((*f_iter)->get_returntype(),
5893 string(
"recv_") + (*f_iter)->get_name(),
5896 if (gen_templates_) {
5897 indent(out) << template_header;
5900 function_signature(&recv_function,
"", scope) << endl;
5905 indent() <<
"int32_t rseqid = 0;" << endl <<
5906 indent() <<
"std::string fname;" << endl <<
5907 indent() <<
"::contrail::sandesh::protocol::TMessageType mtype;" << endl;
5908 if (style ==
"Cob" && !gen_no_client_completion_) {
5910 indent() <<
"bool completed = false;" << endl << endl <<
5915 indent() << _this <<
"iprot_->readMessageBegin(fname, mtype, rseqid);" << endl <<
5916 indent() <<
"if (mtype == ::contrail::sandesh::protocol::T_EXCEPTION) {" << endl <<
5917 indent() <<
" ::contrail::sandesh::TApplicationException x;" << endl <<
5918 indent() <<
" x.read(" << _this <<
"iprot_);" << endl <<
5919 indent() <<
" " << _this <<
"iprot_->readMessageEnd();" << endl <<
5920 indent() <<
" " << _this <<
"iprot_->getTransport()->readEnd();" <<
5922 if (style ==
"Cob" && !gen_no_client_completion_) {
5924 indent() <<
" completed = true;" << endl <<
5925 indent() <<
" completed__(true);" << endl;
5928 indent() <<
" throw x;" << endl <<
5929 indent() <<
"}" << endl <<
5930 indent() <<
"if (mtype != ::contrail::sandesh::protocol::T_REPLY) {" << endl <<
5931 indent() <<
" " << _this <<
"iprot_->skip(" <<
5932 "::contrail::sandesh::protocol::T_STRUCT);" << endl <<
5933 indent() <<
" " << _this <<
"iprot_->readMessageEnd();" << endl <<
5934 indent() <<
" " << _this <<
"iprot_->getTransport()->readEnd();" <<
5936 if (style ==
"Cob" && !gen_no_client_completion_) {
5938 indent() <<
" completed = true;" << endl <<
5939 indent() <<
" completed__(false);" << endl;
5942 indent() <<
"}" << endl <<
5943 indent() <<
"if (fname.compare(\"" << (*f_iter)->get_name() <<
"\") != 0) {" << endl <<
5944 indent() <<
" " << _this <<
"iprot_->skip(" <<
5945 "::contrail::sandesh::protocol::T_STRUCT);" << endl <<
5946 indent() <<
" " << _this <<
"iprot_->readMessageEnd();" << endl <<
5947 indent() <<
" " << _this <<
"iprot_->getTransport()->readEnd();" <<
5949 if (style ==
"Cob" && !gen_no_client_completion_) {
5951 indent() <<
" completed = true;" << endl <<
5952 indent() <<
" completed__(false);" << endl;
5957 if (!(*f_iter)->get_returntype()->is_void() &&
5958 !is_complex_type((*f_iter)->get_returntype())) {
5959 t_field returnfield((*f_iter)->get_returntype(),
"_return");
5961 indent() << declare_field(&returnfield) << endl;
5965 indent() << resultname <<
" result;" << endl;
5967 if (!(*f_iter)->get_returntype()->is_void()) {
5969 indent() <<
"result.success = &_return;" << endl;
5973 indent() <<
"result.read(" << _this <<
"iprot_);" << endl <<
5974 indent() << _this <<
"iprot_->readMessageEnd();" << endl <<
5975 indent() << _this <<
"iprot_->getTransport()->readEnd();" << endl <<
5979 if (!(*f_iter)->get_returntype()->is_void()) {
5980 if (is_complex_type((*f_iter)->get_returntype())) {
5982 indent() <<
"if (result.__isset.success) {" << endl <<
5983 indent() <<
" // _return pointer has now been filled" << endl;
5984 if (style ==
"Cob" && !gen_no_client_completion_) {
5986 indent() <<
" completed = true;" << endl <<
5987 indent() <<
" completed__(true);" << endl;
5990 indent() <<
" return;" << endl <<
5994 indent() <<
"if (result.__isset.success) {" << endl;
5995 if (style ==
"Cob" && !gen_no_client_completion_) {
5997 indent() <<
" completed = true;" << endl <<
5998 indent() <<
" completed__(true);" << endl;
6001 indent() <<
" return _return;" << endl <<
6006 t_struct* xs = (*f_iter)->get_xceptions();
6007 const std::vector<t_field*>& xceptions = xs->
get_members();
6008 vector<t_field*>::const_iterator x_iter;
6009 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
6011 indent() <<
"if (result.__isset." << (*x_iter)->get_name() <<
") {" << endl;
6012 if (style ==
"Cob" && !gen_no_client_completion_) {
6014 indent() <<
" completed = true;" << endl <<
6015 indent() <<
" completed__(true);" << endl;
6018 indent() <<
" throw result." << (*x_iter)->get_name() <<
";" << endl <<
6023 if ((*f_iter)->get_returntype()->is_void()) {
6024 if (style ==
"Cob" && !gen_no_client_completion_) {
6026 indent() <<
"completed = true;" << endl <<
6027 indent() <<
"completed__(true);" << endl;
6032 if (style ==
"Cob" && !gen_no_client_completion_) {
6034 indent() <<
"completed = true;" << endl <<
6035 indent() <<
"completed__(true);" << endl;
6038 indent() <<
"throw ::contrail::sandesh::TApplicationException(::contrail::sandesh::TApplicationException::MISSING_RESULT, \"" << (*f_iter)->get_name() <<
" failed: unknown result\");" << endl;
6040 if (style ==
"Cob" && !gen_no_client_completion_) {
6043 indent() <<
"} catch (...) {" << endl <<
6044 indent() <<
" if (!completed) {" << endl <<
6045 indent() <<
" completed__(false);" << endl <<
6046 indent() <<
" }" << endl <<
6047 indent() <<
" throw;" << endl <<
6061 const string& style);
6064 generate_class_definition();
6068 generate_process_fn();
6070 generate_process_functions();
6075 void generate_class_definition();
6076 void generate_process();
6077 void generate_process_fn();
6078 void generate_process_functions();
6079 void generate_factory();
6083 return generator_->type_name(ttype, in_typedef, arg);
6087 return generator_->indent();
6090 return generator_->indent(os);
6094 generator_->indent_up();
6097 generator_->indent_down();
6127 const string& style)
6128 : generator_(generator),
6130 f_header_(generator->f_header_),
6131 f_out_(generator->gen_templates_ ?
6132 generator->f_service_tcc_ : generator->f_service_),
6133 service_name_(generator->service_name_),
6140 finish_cob_ =
"std::tr1::function<void(bool ok)> cob, ";
6181 vector<t_function*>::iterator f_iter;
6191 " protected:" << endl;
6194 indent() <<
"boost::shared_ptr<" <<
if_name_ <<
"> iface_;" << endl;
6196 indent() <<
"virtual " <<
ret_type_ <<
"process_fn(" <<
finish_cob_ <<
"contrail::sandesh::protocol::TProtocol* iprot, contrail::sandesh::protocol::TProtocol* oprot, std::string& fname, int32_t seqid" <<
call_context_ <<
");" << endl;
6201 " private:" << endl;
6204 indent() <<
"std::map<std::string, void (" <<
6206 "int32_t, contrail::sandesh::protocol::TProtocol*, " <<
6208 ")> processMap_;" << endl;
6209 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
6211 "void process_" << (*f_iter)->get_name() <<
"(" <<
finish_cob_ <<
"int32_t seqid, contrail::sandesh::protocol::TProtocol* iprot, contrail::sandesh::protocol::TProtocol* oprot" <<
call_context_ <<
");" << endl;
6214 "void process_" << (*f_iter)->get_name() <<
"(" <<
finish_cob_ <<
6215 "int32_t seqid, Protocol_* iprot, Protocol_* oprot" <<
6220 string ret_arg = ((*f_iter)->get_returntype()->is_void()
6222 :
", const " +
type_name((*f_iter)->get_returntype()) +
"& _return");
6224 indent() <<
"void return_" << (*f_iter)->get_name() <<
6225 "(std::tr1::function<void(bool ok)> cob, int32_t seqid, " <<
6226 "::contrail::sandesh::protocol::TProtocol* oprot, " <<
6227 "void* ctx" << ret_arg <<
");" << endl;
6230 indent() <<
"void return_" << (*f_iter)->get_name() <<
6231 "(std::tr1::function<void(bool ok)> cob, int32_t seqid, " <<
6232 "Protocol_* oprot, void* ctx" << ret_arg <<
");" << endl;
6236 indent() <<
"void throw_" << (*f_iter)->get_name() <<
6237 "(std::tr1::function<void(bool ok)> cob, int32_t seqid, " <<
6238 "contrail::sandesh::protocol::TProtocol* oprot, void* ctx, " <<
6239 "contrail::sandesh::TDelayedException* _throw);" << endl;
6242 indent() <<
"void throw_" << (*f_iter)->get_name() <<
6243 "(std::tr1::function<void(bool ok)> cob, int32_t seqid, " <<
6244 "Protocol_* oprot, void* ctx, " <<
6245 "::contrail::sandesh::TDelayedException* _throw);" << endl;
6252 string declare_map =
"";
6255 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
6257 declare_map +=
"processMap_[\"";
6258 declare_map += (*f_iter)->get_name();
6259 declare_map +=
"\"] = &";
6261 declare_map +=
"::process_";
6262 declare_map += (*f_iter)->get_name();
6263 declare_map +=
";\n";
6268 " public:" << endl <<
6270 "(boost::shared_ptr<" <<
if_name_ <<
"> iface) :" << endl;
6273 indent() <<
" iface_(iface) {" << endl;
6277 indent() <<
" iface_(iface) {" << endl;
6281 indent() <<
"}" << endl <<
6283 indent() <<
"virtual " <<
ret_type_ <<
"process(" <<
finish_cob_ <<
"boost::shared_ptr<contrail::sandesh::protocol::TProtocol> piprot, boost::shared_ptr<contrail::sandesh::protocol::TProtocol> poprot" <<
call_context_ <<
");" << endl <<
6288 "};" << endl << endl;
6302 "<contrail::sandesh::protocol::TDummyProtocol> " <<
6312 "boost::shared_ptr<contrail::sandesh::protocol::TProtocol> piprot, " <<
6313 "boost::shared_ptr<contrail::sandesh::protocol::TProtocol> poprot" <<
6319 indent() <<
"::contrail::sandesh::protocol::TProtocol* iprot = piprot.get();" << endl <<
6320 indent() <<
"::contrail::sandesh::protocol::TProtocol* oprot = poprot.get();" << endl <<
6321 indent() <<
"std::string fname;" << endl <<
6322 indent() <<
"::contrail::sandesh::protocol::TMessageType mtype;" << endl <<
6323 indent() <<
"int32_t seqid;" << endl <<
6325 indent() <<
"iprot->readMessageBegin(fname, mtype, seqid);" << endl <<
6327 indent() <<
"if (mtype != ::contrail::sandesh::protocol::T_CALL && mtype != ::contrail::sandesh::protocol::T_ONEWAY) {" << endl <<
6328 indent() <<
" iprot->skip(::contrail::sandesh::protocol::T_STRUCT);" << endl <<
6329 indent() <<
" iprot->readMessageEnd();" << endl <<
6330 indent() <<
" iprot->getTransport()->readEnd();" << endl <<
6331 indent() <<
" ::contrail::sandesh::TApplicationException x(::contrail::sandesh::TApplicationException::INVALID_MESSAGE_TYPE);" << endl <<
6332 indent() <<
" oprot->writeMessageBegin(fname, ::contrail::sandesh::protocol::T_EXCEPTION, seqid);" << endl <<
6333 indent() <<
" x.write(oprot);" << endl <<
6334 indent() <<
" oprot->writeMessageEnd();" << endl <<
6335 indent() <<
" oprot->getTransport()->writeEnd();" << endl <<
6336 indent() <<
" oprot->getTransport()->flush();" << endl <<
6337 indent() << (
style_ ==
"Cob" ?
" return cob(true);" :
" return true;") << endl <<
6338 indent() <<
"}" << endl <<
6340 indent() <<
"return process_fn(" << (
style_ ==
"Cob" ?
"cob, " :
"")
6346 indent() <<
"}" << endl <<
6355 "contrail::sandesh::protocol::TProtocol* iprot, " <<
6356 "contrail::sandesh::protocol::TProtocol* oprot, " <<
6357 "std::string& fname, int32_t seqid" <<
call_context_ <<
") {" << endl;
6364 "int32_t, contrail::sandesh::protocol::TProtocol*, " <<
6365 "contrail::sandesh::protocol::TProtocol*" <<
call_context_decl_ <<
")>::iterator pfn;" << endl <<
6366 indent() <<
"pfn = processMap_.find(fname);" << endl <<
6367 indent() <<
"if (pfn == processMap_.end()) {" << endl;
6370 indent() <<
" iprot->skip(contrail::sandesh::protocol::T_STRUCT);" << endl <<
6371 indent() <<
" iprot->readMessageEnd();" << endl <<
6372 indent() <<
" iprot->getTransport()->readEnd();" << endl <<
6373 indent() <<
" ::contrail::sandesh::TApplicationException x(::contrail::sandesh::TApplicationException::UNKNOWN_METHOD, \"Invalid method name: '\"+fname+\"'\");" << endl <<
6374 indent() <<
" oprot->writeMessageBegin(fname, ::contrail::sandesh::protocol::T_EXCEPTION, seqid);" << endl <<
6375 indent() <<
" x.write(oprot);" << endl <<
6376 indent() <<
" oprot->writeMessageEnd();" << endl <<
6377 indent() <<
" oprot->getTransport()->writeEnd();" << endl <<
6378 indent() <<
" oprot->getTransport()->flush();" << endl <<
6379 indent() << (
style_ ==
"Cob" ?
" return cob(true);" :
" return true;") << endl;
6384 << (
style_ ==
"Cob" ?
"cob, " :
"")
6388 indent() <<
"}" << endl <<
6394 indent() <<
"return;" << endl;
6397 indent() <<
"return true;" << endl;
6408 vector<t_function*>::iterator f_iter;
6409 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
6420 string if_factory_name =
if_name_ +
"Factory";
6426 " : public ::contrail::sandesh::" <<
6427 (
style_ ==
"Cob" ?
"async::TAsyncProcessorFactory" :
"TProcessorFactory") <<
6434 if_factory_name <<
" >& handlerFactory) :" << endl <<
6435 indent() <<
" handlerFactory_(handlerFactory) {}" << endl <<
6437 indent() <<
"::boost::shared_ptr< ::contrail::sandesh::" <<
6438 (
style_ ==
"Cob" ?
"async::TAsyncProcessor" :
"TProcessor") <<
" > " <<
6439 "getProcessor(const ::contrail::sandesh::TConnectionInfo& connInfo);" <<
6444 " protected:" << endl <<
6445 indent() <<
"::boost::shared_ptr< " << if_factory_name <<
6446 " > handlerFactory_;" << endl;
6450 "};" << endl << endl;
6457 "< ::contrail::sandesh::protocol::TDummyProtocol > " <<
6464 indent() <<
"::boost::shared_ptr< ::contrail::sandesh::" <<
6465 (
style_ ==
"Cob" ?
"async::TAsyncProcessor" :
"TProcessor") <<
" > " <<
6467 "const ::contrail::sandesh::TConnectionInfo& connInfo) {" << endl;
6471 indent() <<
"::contrail::sandesh::ReleaseHandler< " << if_factory_name <<
6472 " > cleanup(handlerFactory_);" << endl <<
6473 indent() <<
"::boost::shared_ptr< " <<
if_name_ <<
" > handler(" <<
6474 "handlerFactory_->getHandler(connInfo), cleanup);" << endl <<
6475 indent() <<
"::boost::shared_ptr< ::contrail::sandesh::" <<
6476 (
style_ ==
"Cob" ?
"async::TAsyncProcessor" :
"TProcessor") <<
" > " <<
6478 "(handler));" << endl <<
6479 indent() <<
"return processor;" << endl;
6517 const vector<t_field*>& fields = xs->
get_members();
6518 vector<t_field*>::const_iterator f_iter;
6519 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
6546 const std::vector<t_field*>& fields = arg_struct->
get_members();
6547 vector<t_field*>::const_iterator f_iter;
6550 const std::vector<t_field*>& xceptions = xs->
get_members();
6551 vector<t_field*>::const_iterator x_iter;
6552 string service_func_name = tservice->
get_name() +
"." +
6558 (specialized ?
"Protocol_" :
"::contrail::sandesh::protocol::TProtocol");
6559 string class_suffix;
6561 class_suffix =
"T<Protocol_>";
6565 if (style !=
"Cob") {
6569 indent() <<
"template <class Protocol_>" << endl;
6571 const bool unnamed_oprot_seqid = tfunction->
is_oneway() &&
6574 "void " << tservice->
get_name() <<
"Processor" << class_suffix <<
"::" <<
6575 "process_" << tfunction->
get_name() <<
"(" <<
6576 "int32_t" << (unnamed_oprot_seqid ?
", " :
" seqid, ") <<
6577 prot_type <<
"* iprot, " <<
6578 prot_type <<
"*" << (unnamed_oprot_seqid ?
", " :
" oprot, ") <<
6579 "void* callContext)" << endl;
6586 indent() <<
"Protocol_* _iprot = dynamic_cast<Protocol_*>(iprot);" <<
6588 indent() <<
"Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" <<
6590 indent() <<
"if (_iprot && _oprot) {" << endl <<
6592 "(seqid, _iprot, _oprot, callContext);" << endl <<
6593 indent() <<
"}" << endl <<
6594 indent() <<
"T_GENERIC_PROTOCOL(this, iprot, _iprot);" << endl <<
6595 indent() <<
"T_GENERIC_PROTOCOL(this, oprot, _oprot);" << endl << endl;
6604 indent() <<
"void* ctx = NULL;" << endl <<
6605 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6606 indent() <<
" ctx = this->eventHandler_->getContext(\"" <<
6607 service_func_name <<
"\", callContext);" << endl <<
6608 indent() <<
"}" << endl <<
6609 indent() <<
"contrail::sandesh::TProcessorContextFreer freer(" <<
6610 "this->eventHandler_.get(), ctx, \"" << service_func_name <<
"\");" <<
6612 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6613 indent() <<
" this->eventHandler_->preRead(ctx, \"" <<
6614 service_func_name <<
"\");" << endl <<
6615 indent() <<
"}" << endl << endl <<
6616 indent() << argsname <<
" args;" << endl <<
6617 indent() <<
"args.read(iprot);" << endl <<
6618 indent() <<
"iprot->readMessageEnd();" << endl <<
6619 indent() <<
"uint32_t bytes = iprot->getTransport()->readEnd();" <<
6621 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6622 indent() <<
" this->eventHandler_->postRead(ctx, \"" <<
6623 service_func_name <<
"\", bytes);" << endl <<
6624 indent() <<
"}" << endl <<
6630 indent() << resultname <<
" result;" << endl;
6635 indent() <<
"try {" << endl;
6644 out <<
"iface_->" << tfunction->
get_name() <<
"(result.success";
6646 out <<
"result.success = iface_->" << tfunction->
get_name() <<
"(";
6650 "iface_->" << tfunction->
get_name() <<
"(";
6652 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
6658 out <<
"args." << (*f_iter)->get_name();
6660 out <<
");" << endl;
6665 indent() <<
"result.__isset.success = true;" << endl;
6672 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
6673 out <<
" catch (" <<
type_name((*x_iter)->get_type()) <<
" &" <<
6674 (*x_iter)->get_name() <<
") {" << endl;
6678 indent() <<
"result." << (*x_iter)->get_name() <<
" = " <<
6679 (*x_iter)->get_name() <<
";" << endl <<
6680 indent() <<
"result.__isset." << (*x_iter)->get_name() <<
6690 out <<
" catch (const std::exception& e) {" << endl;
6694 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6695 indent() <<
" this->eventHandler_->handlerError(ctx, \"" <<
6696 service_func_name <<
"\");" << endl <<
6702 indent() <<
"contrail::sandesh::TApplicationException x(e.what());" <<
6704 indent() <<
"oprot->writeMessageBegin(\"" << tfunction->
get_name() <<
6705 "\", contrail::sandesh::protocol::T_EXCEPTION, seqid);" << endl <<
6706 indent() <<
"x.write(oprot);" << endl <<
6707 indent() <<
"oprot->writeMessageEnd();" << endl <<
6708 indent() <<
"oprot->getTransport()->writeEnd();" << endl <<
6709 indent() <<
"oprot->getTransport()->flush();" << endl;
6711 out <<
indent() <<
"return;" << endl;
6713 out <<
indent() <<
"}" << endl << endl;
6718 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6719 indent() <<
" this->eventHandler_->asyncComplete(ctx, \"" <<
6720 service_func_name <<
"\");" << endl <<
6721 indent() <<
"}" << endl << endl <<
6722 indent() <<
"return;" << endl;
6724 out <<
"}" << endl <<
6731 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6732 indent() <<
" this->eventHandler_->preWrite(ctx, \"" <<
6733 service_func_name <<
"\");" << endl <<
6734 indent() <<
"}" << endl << endl <<
6735 indent() <<
"oprot->writeMessageBegin(\"" << tfunction->
get_name() <<
6736 "\", contrail::sandesh::protocol::T_REPLY, seqid);" << endl <<
6737 indent() <<
"result.write(oprot);" << endl <<
6738 indent() <<
"oprot->writeMessageEnd();" << endl <<
6739 indent() <<
"bytes = oprot->getTransport()->writeEnd();" << endl <<
6740 indent() <<
"oprot->getTransport()->flush();" << endl << endl <<
6741 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6742 indent() <<
" this->eventHandler_->postWrite(ctx, \"" <<
6743 service_func_name <<
"\", bytes);" << endl <<
6757 indent() <<
"template <class Protocol_>" << endl;
6760 "void " << tservice->
get_name() <<
"AsyncProcessor" << class_suffix <<
6761 "::process_" << tfunction->
get_name() <<
6762 "(std::tr1::function<void(bool ok)> cob, int32_t seqid, " <<
6763 prot_type <<
"* iprot, " << prot_type <<
"* oprot)" << endl;
6772 indent() <<
"Protocol_* _iprot = dynamic_cast<Protocol_*>(iprot);" <<
6774 indent() <<
"Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" <<
6776 indent() <<
"if (_iprot && _oprot) {" << endl <<
6778 "(cob, seqid, _iprot, _oprot);" << endl <<
6779 indent() <<
"}" << endl <<
6780 indent() <<
"T_GENERIC_PROTOCOL(this, iprot, _iprot);" << endl <<
6781 indent() <<
"T_GENERIC_PROTOCOL(this, oprot, _oprot);" << endl << endl;
6786 indent() <<
"(void) seqid;" << endl <<
6787 indent() <<
"(void) oprot;" << endl;
6792 "_args args;" << endl <<
6793 indent() <<
"void* ctx = NULL;" << endl <<
6794 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6795 indent() <<
" ctx = this->eventHandler_->getContext(\"" <<
6796 service_func_name <<
"\", NULL);" << endl <<
6797 indent() <<
"}" << endl <<
6798 indent() <<
"contrail::sandesh::TProcessorContextFreer freer(" <<
6799 "this->eventHandler_.get(), ctx, \"" << service_func_name <<
"\");" <<
6801 indent() <<
"try {" << endl;
6804 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6805 indent() <<
" this->eventHandler_->preRead(ctx, \"" <<
6806 service_func_name <<
"\");" << endl <<
6807 indent() <<
"}" << endl <<
6808 indent() <<
"args.read(iprot);" << endl <<
6809 indent() <<
"iprot->readMessageEnd();" << endl <<
6810 indent() <<
"uint32_t bytes = iprot->getTransport()->readEnd();" <<
6812 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6813 indent() <<
" this->eventHandler_->postRead(ctx, \"" <<
6814 service_func_name <<
"\", bytes);" << endl <<
6820 indent() <<
"catch (const std::exception& exn) {" << endl <<
6821 indent() <<
" if (this->eventHandler_.get() != NULL) {" << endl <<
6822 indent() <<
" this->eventHandler_->handlerError(ctx, \"" <<
6823 service_func_name <<
"\");" << endl <<
6824 indent() <<
" }" << endl <<
6825 indent() <<
" return cob(false);" << endl <<
6830 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6831 indent() <<
" this->eventHandler_->asyncComplete(ctx, \"" <<
6832 service_func_name <<
"\");" << endl <<
6837 indent() <<
"freer.unregister();" << endl;
6843 "std::tr1::bind(cob, true)" << endl;
6846 string ret_arg, ret_placeholder;
6850 ret_placeholder =
", std::tr1::placeholders::_1";
6857 indent() <<
"void (" << tservice->
get_name() <<
"AsyncProcessor" <<
6858 class_suffix <<
"::*return_fn)(std::tr1::function<void(bool ok)> " <<
6859 "cob, int32_t seqid, " << prot_type <<
"* oprot, void* ctx" <<
6860 ret_arg <<
") =" << endl;
6863 class_suffix <<
"::return_" << tfunction->
get_name() <<
";" << endl;
6864 if (!xceptions.empty()) {
6866 indent() <<
"void (" << tservice->
get_name() <<
"AsyncProcessor" <<
6867 class_suffix <<
"::*throw_fn)(std::tr1::function<void(bool ok)> " <<
6868 "cob, int32_t seqid, " << prot_type <<
"* oprot, void* ctx, " <<
6869 "::contrail::sandesh::TDelayedException* _throw) =" << endl;
6872 class_suffix <<
"::throw_" << tfunction->
get_name() <<
";" << endl;
6879 indent() <<
"std::tr1::bind(return_fn, this, cob, seqid, oprot, ctx" <<
6880 ret_placeholder <<
")";
6881 if (!xceptions.empty()) {
6884 indent() <<
"std::tr1::bind(throw_fn, this, cob, seqid, oprot, " <<
6885 "ctx, std::tr1::placeholders::_1)";
6890 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
6893 indent() <<
"args." << (*f_iter)->get_name();
6895 out <<
");" << endl;
6902 string ret_arg_decl, ret_arg_name;
6906 ret_arg_name =
", _return";
6910 indent() <<
"template <class Protocol_>" << endl;
6913 "void " << tservice->
get_name() <<
"AsyncProcessor" << class_suffix <<
6914 "::return_" << tfunction->
get_name() <<
6915 "(std::tr1::function<void(bool ok)> cob, int32_t seqid, " <<
6916 prot_type <<
"* oprot, void* ctx" << ret_arg_decl <<
')' << endl;
6923 indent() <<
"Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" <<
6925 indent() <<
"if (_oprot) {" << endl <<
6927 "(cob, seqid, _oprot, ctx" << ret_arg_name <<
");" << endl <<
6928 indent() <<
"}" << endl <<
6929 indent() <<
"T_GENERIC_PROTOCOL(this, oprot, _oprot);" <<
6935 "_presult result;" << endl;
6940 indent() <<
"result.success = const_cast<" <<
6943 indent() <<
"result.__isset.success = true;" << endl;
6948 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6949 indent() <<
" ctx = this->eventHandler_->getContext(\"" <<
6950 service_func_name <<
"\", NULL);" << endl <<
6951 indent() <<
"}" << endl <<
6952 indent() <<
"contrail::sandesh::TProcessorContextFreer freer(" <<
6953 "this->eventHandler_.get(), ctx, \"" << service_func_name <<
6954 "\");" << endl << endl <<
6955 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6956 indent() <<
" this->eventHandler_->preWrite(ctx, \"" <<
6957 service_func_name <<
"\");" << endl <<
6958 indent() <<
"}" << endl << endl <<
6959 indent() <<
"oprot->writeMessageBegin(\"" << tfunction->
get_name() <<
6960 "\", contrail::sandesh::protocol::T_REPLY, seqid);" << endl <<
6961 indent() <<
"result.write(oprot);" << endl <<
6962 indent() <<
"oprot->writeMessageEnd();" << endl <<
6963 indent() <<
"uint32_t bytes = oprot->getTransport()->writeEnd();" <<
6965 indent() <<
"oprot->getTransport()->flush();" << endl <<
6966 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
6967 indent() <<
" this->eventHandler_->postWrite(ctx, \"" <<
6968 service_func_name <<
"\", bytes);" << endl <<
6969 indent() <<
"}" << endl <<
6970 indent() <<
"return cob(true);" << endl;
6976 if (!tfunction->
is_oneway() && !xceptions.empty()) {
6979 indent() <<
"template <class Protocol_>" << endl;
6982 "void " << tservice->
get_name() <<
"AsyncProcessor" << class_suffix <<
6983 "::throw_" << tfunction->
get_name() <<
6984 "(std::tr1::function<void(bool ok)> cob, int32_t seqid, " <<
6985 prot_type <<
"* oprot, void* ctx, " <<
6986 "::contrail::sandesh::TDelayedException* _throw)" << endl;
6993 indent() <<
"Protocol_* _oprot = dynamic_cast<Protocol_*>(oprot);" <<
6995 indent() <<
"if (_oprot) {" << endl <<
6997 "(cob, seqid, _oprot, ctx, _throw);" << endl <<
6998 indent() <<
"}" << endl <<
6999 indent() <<
"T_GENERIC_PROTOCOL(this, oprot, _oprot);" <<
7006 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
7007 indent() <<
" ctx = this->eventHandler_->getContext(\"" <<
7008 service_func_name <<
"\", NULL);" << endl <<
7009 indent() <<
"}" << endl <<
7010 indent() <<
"contrail::sandesh::TProcessorContextFreer freer(" <<
7011 "this->eventHandler_.get(), ctx, \"" << service_func_name <<
"\");" <<
7017 "_result result;" << endl << endl <<
7018 indent() <<
"try {" << endl;
7021 indent() <<
"_throw->throw_it();" << endl <<
7022 indent() <<
"return cob(false);" << endl;
7026 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
7027 out <<
" catch (" <<
type_name((*x_iter)->get_type()) <<
" &" <<
7028 (*x_iter)->get_name() <<
") {" << endl;
7031 indent() <<
"result." << (*x_iter)->get_name() <<
" = " <<
7032 (*x_iter)->get_name() <<
";" << endl <<
7033 indent() <<
"result.__isset." << (*x_iter)->get_name() <<
7039 out <<
" catch (std::exception& e) {" << endl;
7042 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
7043 indent() <<
" this->eventHandler_->handlerError(ctx, \"" <<
7044 service_func_name <<
"\");" << endl <<
7045 indent() <<
"}" << endl <<
7047 indent() <<
"contrail::sandesh::TApplicationException x(e.what());" <<
7049 indent() <<
"oprot->writeMessageBegin(\"" << tfunction->
get_name() <<
7050 "\", contrail::sandesh::protocol::T_EXCEPTION, seqid);" << endl <<
7051 indent() <<
"x.write(oprot);" << endl <<
7052 indent() <<
"oprot->writeMessageEnd();" << endl <<
7053 indent() <<
"oprot->getTransport()->writeEnd();" << endl <<
7054 indent() <<
"oprot->getTransport()->flush();" << endl <<
7058 indent() <<
"return cob(true);" << endl;
7063 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
7064 indent() <<
" this->eventHandler_->preWrite(ctx, \"" <<
7065 service_func_name <<
"\");" << endl <<
7066 indent() <<
"}" << endl << endl <<
7067 indent() <<
"oprot->writeMessageBegin(\"" << tfunction->
get_name() <<
7068 "\", contrail::sandesh::protocol::T_REPLY, seqid);" << endl <<
7069 indent() <<
"result.write(oprot);" << endl <<
7070 indent() <<
"oprot->writeMessageEnd();" << endl <<
7071 indent() <<
"uint32_t bytes = oprot->getTransport()->writeEnd();" <<
7073 indent() <<
"oprot->getTransport()->flush();" << endl <<
7074 indent() <<
"if (this->eventHandler_.get() != NULL) {" << endl <<
7075 indent() <<
" this->eventHandler_->postWrite(ctx, \"" <<
7076 service_func_name <<
"\", bytes);" << endl <<
7077 indent() <<
"}" << endl <<
7078 indent() <<
"return cob(true);" << endl;
7091 string svcname = tservice->
get_name();
7094 string f_skeleton_name =
get_out_dir()+svcname+
"_server.skeleton.cpp";
7098 ofstream f_skeleton;
7099 f_skeleton.open(f_skeleton_name.c_str());
7101 "// This autogenerated skeleton file illustrates how to build a server." << endl <<
7102 "// You should copy it to another filename to avoid overwriting it." << endl <<
7105 "#include <protocol/TBinaryProtocol.h>" << endl <<
7106 "#include <server/TSimpleServer.h>" << endl <<
7107 "#include <transport/TServerSocket.h>" << endl <<
7108 "#include <transport/TBufferTransports.h>" << endl <<
7110 "using namespace ::contrail::sandesh;" << endl <<
7111 "using namespace ::contrail::sandesh::protocol;" << endl <<
7112 "using namespace ::contrail::sandesh::transport;" << endl <<
7113 "using namespace ::contrail::sandesh::server;" << endl <<
7115 "using boost::shared_ptr;" << endl <<
7120 "using namespace " << string(ns, 0, ns.size()-2) <<
";" << endl <<
7125 "class " << svcname <<
"Handler : virtual public " << svcname <<
"If {" << endl <<
7129 indent() << svcname <<
"Handler() {" << endl <<
7130 indent() <<
" // Your initialization goes here" << endl <<
7131 indent() <<
"}" << endl <<
7135 vector<t_function*>::iterator f_iter;
7136 for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
7139 indent() <<
" // Your implementation goes here" << endl <<
7140 indent() <<
" printf(\"" << (*f_iter)->get_name() <<
"\\n\");" << endl <<
7141 indent() <<
"}" << endl <<
7151 indent() <<
"int main(int argc, char **argv) {" << endl;
7154 indent() <<
"int port = 9090;" << endl <<
7155 indent() <<
"shared_ptr<" << svcname <<
"Handler> handler(new " << svcname <<
"Handler());" << endl <<
7156 indent() <<
"shared_ptr<TProcessor> processor(new " << svcname <<
"Processor(handler));" << endl <<
7157 indent() <<
"shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));" << endl <<
7158 indent() <<
"shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());" << endl <<
7159 indent() <<
"shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());" << endl <<
7161 indent() <<
"TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);" << endl <<
7162 indent() <<
"server.serve();" << endl <<
7163 indent() <<
"return 0;" << endl;
7182 if (
type->is_void()) {
7183 throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
7187 string name = prefix + tfield->
get_name() + suffix;
7189 if (
type->is_struct() ||
type->is_xception()) {
7191 }
else if (
type->is_container()) {
7193 }
else if (
type->is_base_type()) {
7195 "if ((ret = iprot->";
7199 throw "compiler error: cannot serialize void field in a struct: " + name;
7203 case t_base_type::TYPE_STATIC_CONST_STRING:
7206 out <<
"readBinary(" << name <<
")) < 0) {" << endl;
7209 out <<
"readString(" << name <<
")) < 0) {" << endl;
7213 case t_base_type::TYPE_XML:
7214 out <<
"readXML(" << name <<
")) < 0) {" << endl;
7216 case t_base_type::TYPE_UUID:
7217 out <<
"readUUID(" << name <<
")) < 0) {" << endl;
7221 out <<
"readBool(" << name <<
")) < 0) {" << endl;
7224 out <<
"readByte(" << name <<
")) < 0) {" << endl;
7227 out <<
"readI16(" << name <<
")) < 0) {" << endl;
7230 out <<
"readI32(" << name <<
")) < 0) {" << endl;
7233 out <<
"readI64(" << name <<
")) < 0) {" << endl;
7236 case t_base_type::TYPE_U16:
7237 out <<
"readU16(" << name <<
")) < 0) {" << endl;
7239 case t_base_type::TYPE_U32:
7240 out <<
"readU32(" << name <<
")) < 0) {" << endl;
7242 case t_base_type::TYPE_U64:
7243 out <<
"readU64(" << name <<
")) < 0) {" << endl;
7245 case t_base_type::TYPE_IPV4:
7246 out <<
"readIPV4(" << name <<
")) < 0) {" << endl;
7248 case t_base_type::TYPE_IPADDR:
7249 out <<
"readIPADDR(" << name <<
")) < 0) {" << endl;
7253 out <<
"readDouble(" << name <<
")) < 0) {" << endl;
7259 out <<
indent() <<
"return ret;" << endl;
7261 out <<
indent() <<
"xfer += ret;" << endl;
7264 }
else if (
type->is_enum()) {
7265 string t =
tmp(
"ecast");
7267 indent() <<
"int32_t " << t <<
";" << endl <<
7268 indent() <<
"if ((ret = iprot->readI32(" << t <<
")) < 0) {" << endl <<
7269 indent() <<
" return ret;" << endl <<
7270 indent() <<
"}" << endl <<
7271 indent() <<
"xfer += ret;" << endl <<
7274 printf(
"DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
7290 "if ((ret = " << prefix <<
".read(iprot)) < 0) {" << endl;
7292 indent(out) <<
"return ret;" << endl;
7294 indent(out) <<
"xfer += ret;" << endl;
7302 string size =
tmp(
"_size");
7303 string ktype =
tmp(
"_ktype");
7304 string vtype =
tmp(
"_vtype");
7305 string etype =
tmp(
"_etype");
7311 prefix <<
".clear();" << endl <<
7312 indent() <<
"uint32_t " << size <<
";" << endl;
7317 indent() <<
"::contrail::sandesh::protocol::TType " << ktype <<
";" << endl <<
7318 indent() <<
"::contrail::sandesh::protocol::TType " << vtype <<
";" << endl <<
7319 indent() <<
"if ((ret = iprot->readMapBegin(" <<
7320 ktype <<
", " << vtype <<
", " << size <<
")) < 0) {" << endl <<
7321 indent() <<
" return ret;" << endl <<
7322 indent() <<
"}" << endl <<
7323 indent() <<
"xfer += ret;" << endl;
7324 }
else if (ttype->
is_set()) {
7326 indent() <<
"::contrail::sandesh::protocol::TType " << etype <<
";" << endl <<
7327 indent() <<
"if ((ret = iprot->readSetBegin(" <<
7328 etype <<
", " << size <<
")) < 0) {" << endl <<
7329 indent() <<
" return ret;" << endl <<
7330 indent() <<
"}" << endl <<
7331 indent() <<
"xfer += ret;" << endl;
7332 }
else if (ttype->
is_list()) {
7334 indent() <<
"::contrail::sandesh::protocol::TType " << etype <<
";" << endl <<
7335 indent() <<
"if ((ret = iprot->readListBegin(" <<
7336 etype <<
", " << size <<
")) < 0) {" << endl <<
7337 indent() <<
" return ret;" << endl <<
7338 indent() <<
"}" << endl <<
7339 indent() <<
"xfer += ret;" << endl;
7341 indent(out) << prefix <<
".resize(" << size <<
");" << endl;
7347 string i =
tmp(
"_i");
7349 indent() <<
"uint32_t " << i <<
";" << endl <<
7350 indent() <<
"for (" << i <<
" = 0; " << i <<
" < " << size <<
"; ++" << i <<
")" << endl;
7356 }
else if (ttype->
is_set()) {
7360 indent(out) <<
"if ((ret = iprot->readContainerElementBegin()) < 0) {" << endl;
7362 indent(out) <<
"return ret;" << endl;
7364 indent(out) <<
"xfer += ret;" << endl;
7370 indent(out) <<
"if ((ret = iprot->readContainerElementEnd()) < 0) {" << endl;
7372 indent(out) <<
"return ret;" << endl;
7374 indent(out) <<
"xfer += ret;" << endl;
7377 }
else if (ttype->
is_list()) {
7381 indent(out) <<
"if ((ret = iprot->readContainerElementBegin()) < 0) {" << endl;
7383 indent(out) <<
"return ret;" << endl;
7385 indent(out) <<
"xfer += ret;" << endl;
7391 indent(out) <<
"if ((ret = iprot->readContainerElementEnd()) < 0) {" << endl;
7393 indent(out) <<
"return ret;" << endl;
7395 indent(out) <<
"xfer += ret;" << endl;
7405 indent(out) <<
"if ((ret = iprot->readMapEnd()) < 0) {" << endl;
7407 indent(out) <<
"return ret;" << endl;
7409 indent(out) <<
"xfer += ret;" << endl;
7411 indent(out) <<
"iprot->readMapEnd();" << endl;
7413 }
else if (ttype->
is_set()) {
7415 indent(out) <<
"if ((ret = iprot->readSetEnd()) < 0) {" << endl;
7417 indent(out) <<
"return ret;" << endl;
7419 indent(out) <<
"xfer += ret;" << endl;
7421 indent(out) <<
"iprot->readSetEnd();" << endl;
7423 }
else if (ttype->
is_list()) {
7425 indent(out) <<
"if ((ret = iprot->readListEnd()) < 0) {" << endl;
7427 indent(out) <<
"return ret;" << endl;
7429 indent(out) <<
"xfer += ret;" << endl;
7431 indent(out) <<
"iprot->readListEnd();" << endl;
7445 string key =
tmp(
"_key");
7446 string val =
tmp(
"_val");
7456 indent(out) <<
"if ((ret = iprot->readContainerElementBegin()) < 0) {" << endl;
7458 indent(out) <<
"return ret;" << endl;
7460 indent(out) <<
"xfer += ret;" << endl;
7466 indent(out) <<
"if ((ret = iprot->readContainerElementEnd()) < 0) {" << endl;
7468 indent(out) <<
"return ret;" << endl;
7470 indent(out) <<
"xfer += ret;" << endl;
7475 prefix <<
"[" << key <<
"];" << endl;
7480 indent(out) <<
"if ((ret = iprot->readContainerElementBegin()) < 0) {" << endl;
7482 indent(out) <<
"return ret;" << endl;
7484 indent(out) <<
"xfer += ret;" << endl;
7490 indent(out) <<
"if ((ret = iprot->readContainerElementEnd()) < 0) {" << endl;
7492 indent(out) <<
"return ret;" << endl;
7494 indent(out) <<
"xfer += ret;" << endl;
7502 string elem =
tmp(
"_elem");
7511 prefix <<
".insert(" << elem <<
");" << endl;
7520 string elem =
tmp(
"_elem");
7524 indent(out) << prefix <<
".push_back(" << elem <<
");" << endl;
7544 string name = prefix + tfield->
get_name() + suffix;
7547 if (
type->is_void()) {
7548 throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + name;
7553 if (
type->is_struct() ||
type->is_xception()) {
7557 }
else if (
type->is_container()) {
7559 }
else if (
type->is_base_type() ||
type->is_enum()) {
7562 "if ((ret = oprot->";
7564 if (
type->is_base_type()) {
7569 "compiler error: cannot serialize void field in a struct: " + name;
7573 case t_base_type::TYPE_STATIC_CONST_STRING:
7576 out <<
"writeBinary(" << name <<
")) < 0) {" << endl;
7579 out <<
"writeString(" << name <<
")) < 0) {" << endl;
7583 case t_base_type::TYPE_XML:
7584 out <<
"writeXML(" << name <<
")) < 0) {" << endl;
7586 case t_base_type::TYPE_UUID:
7587 out <<
"writeUUID(" << name <<
")) < 0) {" << endl;
7591 out <<
"writeBool(" << name <<
")) < 0) {" << endl;
7594 out <<
"writeByte(" << name <<
")) < 0) {" << endl;
7597 out <<
"writeI16(" << name <<
")) < 0) {" << endl;
7600 out <<
"writeI32(" << name <<
")) < 0) {" << endl;
7603 out <<
"writeI64(" << name <<
")) < 0) {" << endl;
7606 case t_base_type::TYPE_U16:
7607 out <<
"writeU16(" << name <<
")) < 0) {" << endl;
7609 case t_base_type::TYPE_U32:
7610 out <<
"writeU32(" << name <<
")) < 0) {" << endl;
7612 case t_base_type::TYPE_U64:
7613 out <<
"writeU64(" << name <<
")) < 0) {" << endl;
7615 case t_base_type::TYPE_IPV4:
7616 out <<
"writeIPV4(" << name <<
")) < 0) {" << endl;
7618 case t_base_type::TYPE_IPADDR:
7619 out <<
"writeIPADDR(" << name <<
")) < 0) {" << endl;
7623 out <<
"writeDouble(" << name <<
")) < 0) {" << endl;
7628 }
else if (
type->is_enum()) {
7629 out <<
"writeI32((int32_t)" << name <<
")) < 0) {" << endl;
7632 out <<
indent() <<
"return ret;" << endl;
7634 out <<
indent() <<
"xfer += ret;" << endl;
7637 }
else if (
type->is_sandesh()) {
7641 printf(
"DO NOT KNOW HOW TO SERIALIZE FIELD '%s' TYPE '%s'\n",
7654 void t_cpp_generator::generate_serialize_sandesh(ofstream& out,
7659 "if ((ret = " << prefix <<
".write(oprot)) < 0) {" << endl;
7661 indent(out) <<
"return ret;" << endl;
7663 indent(out) <<
"xfer += ret;" << endl;
7678 "if ((ret = " << prefix <<
".write(oprot)) < 0) {" << endl;
7680 indent(out) <<
"return ret;" << endl;
7682 indent(out) <<
"xfer += ret;" << endl;
7692 "if ((ret = oprot->writeMapBegin(" <<
7695 "static_cast<uint32_t>(" << prefix <<
".size()))) < 0) {" << endl;
7697 indent(out) <<
"return ret;" << endl;
7699 indent(out) <<
"xfer += ret;" << endl;
7700 }
else if (ttype->
is_set()) {
7702 "if ((ret = oprot->writeSetBegin(" <<
7704 "static_cast<uint32_t>(" << prefix <<
".size()))) < 0) {" << endl;
7706 indent(out) <<
"return ret;" << endl;
7708 indent(out) <<
"xfer += ret;" << endl;
7709 }
else if (ttype->
is_list()) {
7711 "if ((ret = oprot->writeListBegin(" <<
7713 "static_cast<uint32_t>(" << prefix <<
".size()))) < 0) {" << endl;
7715 indent(out) <<
"return ret;" << endl;
7717 indent(out) <<
"xfer += ret;" << endl;
7720 string iter =
tmp(
"_iter");
7722 indent() <<
type_name(ttype) <<
"::const_iterator " << iter <<
";" << endl <<
7723 indent() <<
"for (" << iter <<
" = " << prefix <<
".begin(); " << iter <<
" != " << prefix <<
".end(); ++" << iter <<
")" << endl;
7727 }
else if (ttype->
is_set()) {
7731 indent(out) <<
"if ((ret = oprot->writeContainerElementBegin()) < 0) {" << endl;
7733 indent(out) <<
"return ret;" << endl;
7735 indent(out) <<
"xfer += ret;" << endl;
7741 indent(out) <<
"if ((ret = oprot->writeContainerElementEnd()) < 0) {" << endl;
7743 indent(out) <<
"return ret;" << endl;
7745 indent(out) <<
"xfer += ret;" << endl;
7748 }
else if (ttype->
is_list()) {
7752 indent(out) <<
"if ((ret = oprot->writeContainerElementBegin()) < 0) {" << endl;
7754 indent(out) <<
"return ret;" << endl;
7756 indent(out) <<
"xfer += ret;" << endl;
7762 indent(out) <<
"if ((ret = oprot->writeContainerElementEnd()) < 0) {" << endl;
7764 indent(out) <<
"return ret;" << endl;
7766 indent(out) <<
"xfer += ret;" << endl;
7774 "if ((ret = oprot->writeMapEnd()) < 0) {" << endl;
7776 indent(out) <<
"return ret;" << endl;
7778 indent(out) <<
"xfer += ret;" << endl;
7779 }
else if (ttype->
is_set()) {
7781 "if ((ret = oprot->writeSetEnd()) < 0) {" << endl;
7783 indent(out) <<
"return ret;" << endl;
7785 indent(out) <<
"xfer += ret;" << endl;
7786 }
else if (ttype->
is_list()) {
7788 "if ((ret = oprot->writeListEnd()) < 0) {" << endl;
7790 indent(out) <<
"return ret;" << endl;
7792 indent(out) <<
"xfer += ret;" << endl;
7808 indent(out) <<
"if ((ret = oprot->writeContainerElementBegin()) < 0) {" << endl;
7810 indent(out) <<
"return ret;" << endl;
7812 indent(out) <<
"xfer += ret;" << endl;
7819 indent(out) <<
"if ((ret = oprot->writeContainerElementEnd()) < 0) {" << endl;
7821 indent(out) <<
"return ret;" << endl;
7823 indent(out) <<
"xfer += ret;" << endl;
7829 indent(out) <<
"if ((ret = oprot->writeContainerElementBegin()) < 0) {" << endl;
7831 indent(out) <<
"return ret;" << endl;
7833 indent(out) <<
"xfer += ret;" << endl;
7840 indent(out) <<
"if ((ret = oprot->writeContainerElementEnd()) < 0) {" << endl;
7842 indent(out) <<
"return ret;" << endl;
7844 indent(out) <<
"xfer += ret;" << endl;
7882 string result =
" ::";
7884 if (ns.size() == 0) {
7887 string::size_type loc;
7888 while ((loc = ns.find(
".")) != string::npos) {
7889 result += ns.substr(0, loc);
7891 ns = ns.substr(loc+1);
7893 if (ns.size() > 0) {
7894 result += ns +
"::";
7906 if (ns.size() == 0) {
7910 string separator =
"";
7911 string::size_type loc;
7912 while ((loc = ns.find(
".")) != string::npos) {
7913 result += separator;
7914 result +=
"namespace ";
7915 result += ns.substr(0, loc);
7918 ns = ns.substr(loc+1);
7920 if (ns.size() > 0) {
7921 result += separator +
"namespace " + ns +
" {";
7933 if (ns.size() == 0) {
7936 string result =
"}";
7937 string::size_type loc;
7938 while ((loc = ns.find(
".")) != string::npos) {
7940 ns = ns.substr(loc+1);
7942 result +=
" // namespace";
7961 || ((
t_base_type*)ttype)->get_base() == t_base_type::TYPE_XML
7962 || ((
t_base_type*)ttype)->get_base() == t_base_type::TYPE_UUID
7963 || ((
t_base_type*)ttype)->get_base() == t_base_type::TYPE_IPADDR
7966 return "const " + bname +
"&";
7968 return "const " + bname;
7979 }
else if (ttype->
is_map()) {
7981 cname =
"std::map<" +
7984 }
else if (ttype->
is_set()) {
7987 }
else if (ttype->
is_list()) {
7993 return "const " + cname +
"&";
7999 string class_prefix;
8001 class_prefix =
"class ";
8007 if (program != NULL && program !=
program_) {
8013 pname = class_prefix + ttype->
get_name();
8022 return "const " + pname +
"&";
8024 return "const " + pname;
8042 case t_base_type::TYPE_STATIC_CONST_STRING:
8043 return "static std::string";
8047 case t_base_type::TYPE_XML:
8049 return "std::string";
8061 case t_base_type::TYPE_U16:
8063 case t_base_type::TYPE_U32:
8065 case t_base_type::TYPE_U64:
8067 case t_base_type::TYPE_IPV4:
8069 case t_base_type::TYPE_IPADDR:
8070 return "boost::asio::ip::address";
8071 case t_base_type::TYPE_UUID:
8072 return "boost::uuids::uuid";
8104 result +=
" " + tfield->
get_name();
8108 if (
type->is_base_type()) {
8114 case t_base_type::TYPE_UUID:
8115 result +=
" = boost::uuids::nil_uuid()";
8117 case t_base_type::TYPE_STATIC_CONST_STRING:
8118 case t_base_type::TYPE_XML:
8121 result +=
" = \"\"";
8124 result +=
" = false";
8131 case t_base_type::TYPE_U16:
8132 case t_base_type::TYPE_U32:
8133 case t_base_type::TYPE_U64:
8134 case t_base_type::TYPE_IPV4:
8139 result +=
" = (double)0";
8142 case t_base_type::TYPE_IPADDR:
8143 result +=
" = boost::asio::ip::address()";
8149 }
else if (
type->is_enum()) {
8177 bool is_static = tfunction->is_static();
8178 bool is_virtual = tfunction->is_virtual();
8179 string func_begin_modifier =
"", func_end_modifier =
"";
8184 func_begin_modifier =
"static ";
8185 }
else if (is_virtual) {
8186 func_begin_modifier =
"virtual ";
8187 func_end_modifier =
" = 0";
8195 func_begin_modifier +
8197 "void " + prefix + tfunction->
get_name() +
8198 "(" +
type_name(ttype) + (name_params ?
"& _return" :
"& /* _return */") +
8207 func_begin_modifier +
8216 }
else if (style.substr(0,3) ==
"Cob") {
8219 if (style ==
"CobCl") {
8222 cob_type +=
"T<Protocol_>";
8224 cob_type +=
"* client)";
8225 }
else if (style ==
"CobSv") {
8228 : (
"(" +
type_name(ttype) +
" const& _return)"));
8229 if (has_xceptions) {
8230 exn_cob =
", std::tr1::function<void(::contrail::sandesh::TDelayedException* _throw)> /* exn_cob */";
8233 throw "UNKNOWN STYLE";
8237 "void " + prefix + tfunction->
get_name() +
8238 "(std::tr1::function<void" + cob_type +
"> cob" + exn_cob +
8241 throw "UNKNOWN STYLE";
8254 const vector<t_field*>& fields = tstruct->
get_members();
8255 vector<t_field*>::const_iterator f_iter;
8256 bool first = !start_comma;
8257 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
8263 result +=
type_name((*f_iter)->get_type(),
false,
true) +
" " +
8264 (name_params ? (*f_iter)->get_name() :
"/* " + (*f_iter)->get_name() +
" */");
8278 if (
type->is_base_type()) {
8282 throw "NO T_VOID CONSTRUCT";
8284 return "::contrail::sandesh::protocol::T_STRING";
8286 return "::contrail::sandesh::protocol::T_BOOL";
8288 return "::contrail::sandesh::protocol::T_BYTE";
8290 return "::contrail::sandesh::protocol::T_I16";
8292 return "::contrail::sandesh::protocol::T_I32";
8294 return "::contrail::sandesh::protocol::T_I64";
8296 return "::contrail::sandesh::protocol::T_DOUBLE";
8298 case t_base_type::TYPE_XML:
8299 return "::contrail::sandesh::protocol::T_XML";
8300 case t_base_type::TYPE_UUID:
8301 return "::contrail::sandesh::protocol::T_UUID";
8302 case t_base_type::TYPE_U16:
8303 return "::contrail::sandesh::protocol::T_U16";
8304 case t_base_type::TYPE_U32:
8305 return "::contrail::sandesh::protocol::T_U32";
8306 case t_base_type::TYPE_U64:
8307 return "::contrail::sandesh::protocol::T_U64";
8308 case t_base_type::TYPE_IPV4:
8309 return "::contrail::sandesh::protocol::T_IPV4";
8310 case t_base_type::TYPE_IPADDR:
8311 return "::contrail::sandesh::protocol::T_IPADDR";
8312 case t_base_type::TYPE_STATIC_CONST_STRING:
8313 case t_base_type::TYPE_SANDESH_SYSTEM:
8314 case t_base_type::TYPE_SANDESH_REQUEST:
8315 case t_base_type::TYPE_SANDESH_RESPONSE:
8316 case t_base_type::TYPE_SANDESH_TRACE:
8317 case t_base_type::TYPE_SANDESH_TRACE_OBJECT:
8318 case t_base_type::TYPE_SANDESH_BUFFER:
8319 case t_base_type::TYPE_SANDESH_UVE:
8320 case t_base_type::TYPE_SANDESH_ALARM:
8321 case t_base_type::TYPE_SANDESH_OBJECT:
8322 case t_base_type::TYPE_SANDESH_FLOW:
8323 case t_base_type::TYPE_SANDESH_SESSION:
8324 return "::contrail::sandesh::protocol::T_STRING";
8327 }
else if (
type->is_enum()) {
8328 return "::contrail::sandesh::protocol::T_I32";
8329 }
else if (
type->is_struct()) {
8330 return "::contrail::sandesh::protocol::T_STRUCT";
8331 }
else if (
type->is_xception()) {
8332 return "::contrail::sandesh::protocol::T_STRUCT";
8334 }
else if (
type->is_sandesh()) {
8335 return "::contrail::sandesh::protocol::T_SANDESH";
8337 }
else if (
type->is_map()) {
8338 return "::contrail::sandesh::protocol::T_MAP";
8339 }
else if (
type->is_set()) {
8340 return "::contrail::sandesh::protocol::T_SET";
8341 }
else if (
type->is_list()) {
8342 return "::contrail::sandesh::protocol::T_LIST";
8345 throw "INVALID TYPE IN type_to_enum: " +
type->get_name();
8368 }
else if (ttype->
is_enum()) {
8388 return nspace +
"trlo_" + prefix +
"_" + prog +
"_" + name;
8394 (include_prefix.size() > 0 && include_prefix[0] ==
'/')) {
8399 string::size_type last_slash = string::npos;
8400 if ((last_slash = include_prefix.rfind(
"/")) != string::npos) {
8401 return include_prefix.substr(0, last_slash) +
"/" +
out_dir_base_ +
"/";
8415 const vector<t_field*>& fields = tstruct->
get_members();
8416 vector<t_field*>::const_iterator f_iter;
8421 indent() <<
"int32_t xfer = 0, ret;" << endl <<
8422 indent() <<
"std::string fname;" << endl <<
8423 indent() <<
"::contrail::sandesh::protocol::TType ftype;" << endl <<
8424 indent() <<
"int16_t fid;" << endl <<
8427 "if ((ret = iprot->read" << struct_type_name <<
"Begin(fname)) < 0) {" <<
8429 indent() <<
" return ret;" << endl <<
8430 indent() <<
"}" << endl <<
8431 indent() <<
"xfer += ret;" << endl <<
8435 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
8437 indent(out) <<
"bool isset_" << (*f_iter)->get_name() <<
" = false;" << endl;
8443 "while (true)" << endl;
8448 "if ((ret = iprot->readFieldBegin(fname, ftype, fid)) < 0) {" << endl;
8450 indent(out) <<
"return ret;" << endl;
8452 indent(out) <<
"xfer += ret;" << endl;
8456 indent() <<
"if (ftype == ::contrail::sandesh::protocol::T_STOP) {" << endl <<
8457 indent() <<
" break;" << endl <<
8463 if (fields.empty()) {
8465 indent() <<
"if ((ret = iprot->skip(ftype)) < 0) {" << endl <<
8466 indent() <<
" return ret;" << endl <<
8467 indent() <<
"}" << endl <<
8468 indent() <<
"xfer += ret;" << endl;
8472 "switch (fid)" << endl;
8477 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
8479 "case " << (*f_iter)->get_key() <<
":" << endl;
8482 "if (ftype == " <<
type_to_enum((*f_iter)->get_type()) <<
") {" << endl;
8485 const char *isset_prefix =
8494 indent() <<
"if (" << isset_prefix << (*f_iter)->get_name() <<
")" << endl <<
8495 indent() <<
" throw TProtocolException(TProtocolException::INVALID_DATA);" << endl;
8498 if (pointers && !(*f_iter)->get_type()->is_xception()) {
8504 indent() << isset_prefix << (*f_iter)->get_name() <<
" = true;" << endl;
8507 indent() <<
"} else {" << endl <<
8508 indent() <<
" if ((ret = iprot->skip(ftype)) < 0) {" << endl <<
8509 indent() <<
" return ret;" << endl <<
8510 indent() <<
" }" << endl <<
8511 indent() <<
" xfer += ret;" << endl <<
8515 indent() <<
"}" << endl <<
8516 indent() <<
"break;" << endl;
8522 indent() <<
"default:" << endl <<
8523 indent() <<
" if ((ret = iprot->skip(ftype)) < 0) {" << endl <<
8524 indent() <<
" return ret;" << endl <<
8525 indent() <<
" }" << endl <<
8526 indent() <<
" xfer += ret;" << endl <<
8527 indent() <<
" break;" << endl;
8534 "if ((ret = iprot->readFieldEnd()) < 0) {" << endl;
8536 indent(out) <<
"return ret;" << endl;
8538 indent(out) <<
"xfer += ret;" << endl;
8545 "if ((ret = iprot->read" << struct_type_name <<
"End()) < 0) {" << endl;
8548 indent(out) <<
"return ret;" << endl;
8550 indent(out) <<
"xfer += ret;" << endl;
8556 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
8559 indent() <<
"if (!isset_" << (*f_iter)->get_name() <<
") {" << endl <<
8560 indent() <<
" LOG(ERROR, __func__ << \": Required field " << (*f_iter)->get_name()
8561 <<
" not set\");" << endl <<
8562 indent() <<
" return -1;" << endl <<
8566 indent(out) <<
"return xfer;" << endl;
8580 const vector<t_field*>& fields = tstruct->
get_members();
8581 vector<t_field*>::const_iterator f_iter;
8584 indent() <<
"int32_t xfer = 0, ret;" << endl;
8587 "if ((ret = oprot->write" << struct_type_name <<
"Begin(\"" << name <<
"\")) < 0) {" <<
8591 indent(out) <<
"return ret;" << endl;
8593 indent(out) <<
"xfer += ret;" << endl;
8594 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
8596 indent(out) <<
"if (this->__isset." << (*f_iter)->get_name() <<
") {" << endl;
8600 if (!((*f_iter)->annotations_.empty())) {
8601 out <<
indent() <<
"{" << endl;
8603 out <<
indent() <<
"std::map<std::string, std::string> annotations_;" << endl;
8604 std::map<std::string, std::string>::iterator it;
8605 for (it = (*f_iter)->annotations_.begin(); it != (*f_iter)->annotations_.end(); it++) {
8606 if (!tstruct->is_sandesh() &&
8607 ((*f_iter)->get_name() ==
"name") && (it->first ==
"key")) {
8608 out <<
indent() <<
"assert(!table_.empty());" << endl;
8609 out <<
indent() <<
"annotations_.insert("
8610 "std::make_pair(\"key\", table_));" << endl;
8612 out <<
indent() <<
"annotations_.insert(std::make_pair(\"" << (*it).first
8613 <<
"\"" <<
", \"" << (*it).second <<
"\"));" << endl;
8617 indent() <<
"if ((ret = oprot->writeFieldBegin(" <<
8618 "\"" << (*f_iter)->get_name() <<
"\", " <<
8620 (*f_iter)->get_key() <<
", &annotations_)) < 0) {" << endl;
8622 indent(out) <<
"return ret;" << endl;
8624 indent(out) <<
"xfer += ret;" << endl;
8626 out <<
indent() <<
"}" << endl;
8629 indent() <<
"if ((ret = oprot->writeFieldBegin(" <<
8630 "\"" << (*f_iter)->get_name() <<
"\", " <<
8632 (*f_iter)->get_key() <<
")) < 0) {" << endl;
8634 indent(out) <<
"return ret;" << endl;
8636 indent(out) <<
"xfer += ret;" << endl;
8640 if (pointers && !(*f_iter)->get_type()->is_xception()) {
8647 "if ((ret = oprot->writeFieldEnd()) < 0) {" << endl;
8649 indent(out) <<
"return ret;" << endl;
8651 indent(out) <<
"xfer += ret;" << endl;
8654 indent(out) <<
'}' << endl;
8660 indent() <<
"if ((ret = oprot->writeFieldStop()) < 0) {" << endl <<
8661 indent() <<
" return ret;" << endl <<
8662 indent() <<
"}" << endl <<
8663 indent() <<
"xfer += ret;" << endl <<
8665 "if ((ret = oprot->write" << struct_type_name <<
"End()) < 0) {" <<
8667 indent() <<
" return ret;" << endl <<
8668 indent() <<
"}" << endl <<
8669 indent() <<
"xfer += ret;" << endl <<
8670 indent() <<
"return xfer;" << endl;
8677 " cob_style: Generate \"Continuation OBject\"-style classes.\n"
8678 " no_client_completion:\n"
8679 " Omit calls to completion__() in CobClient class.\n"
8680 " templates: Generate templatized reader/writer methods.\n"
8681 " pure_enums: Generate pure enums instead of wrapper classes.\n"
8682 " dense: Generate type specifications for the dense protocol.\n"
8683 " include_prefix: Use full include paths in generated files.\n"
std::ostream & indent(std::ostream &os)
void generate_class_definition()
void generate_process_functions()
string factory_class_name_
string call_context_decl_
std::string type_name(t_type *ttype, bool in_typedef=false, bool arg=false)
ProcessorGenerator(t_cpp_generator *generator, t_service *service, const string &style)
string extends_processor_
std::ofstream & f_header_
t_cpp_generator * generator_
void generate_process_fn()
static std::string t_base_name(t_base tbase)
std::string get_uuid() const
const std::vector< t_const_value * > & get_list() const
double get_double() const
std::string get_string() const
t_const_value_type get_type() const
const std::map< t_const_value *, t_const_value * > & get_map() const
int64_t get_integer() const
std::string get_cpp_name()
void generate_xception(t_struct *txception)
void generate_serialize_list_element(std::ofstream &out, t_list *tlist, std::string iter)
void generate_service_async_skeleton(t_service *tservice)
std::string function_signature(t_function *tfunction, std::string style, std::string prefix="", bool name_params=true)
void generate_common_struct_reader_body(std::ofstream &out, t_struct_common *tstruct, bool pointers=false)
void generate_serialize_set_element(std::ofstream &out, t_set *tmap, std::string iter)
void generate_deserialize_field(std::ofstream &out, t_field *tfield, std::string prefix="", std::string suffix="")
void generate_deserialize_struct(std::ofstream &out, t_struct *tstruct, std::string prefix="")
std::ofstream f_service_tcc_
t_cpp_generator(t_program *program, const std::map< std::string, std::string > &parsed_options, const std::string &option_string)
void generate_typedef(t_typedef *ttypedef)
void generate_serialize_struct(std::ofstream &out, t_struct *tstruct, std::string prefix="")
void generate_struct_result_writer(std::ofstream &out, t_struct *tstruct, bool pointers=false)
void generate_deserialize_list_element(std::ofstream &out, t_list *tlist, std::string prefix, bool push_back, std::string index)
void print_const_value(std::ofstream &out, std::string name, t_type *type, t_const_value *value)
std::ofstream f_types_tcc_
void generate_service_skeleton(t_service *tservice)
void generate_serialize_container(std::ofstream &out, t_type *ttype, std::string prefix="")
void generate_service_client(t_service *tservice, string style)
bool gen_no_client_completion_
void generate_function_call(ostream &out, t_function *tfunction, string target, string iface, string arg_prefix)
void generate_function_helpers(t_service *tservice, t_function *tfunction)
std::string base_type_name(t_base_type::t_base tbase)
std::string type_name(t_type *ttype, bool in_typedef=false, bool arg=false)
void generate_service_helpers(t_service *tservice)
void generate_deserialize_container(std::ofstream &out, t_type *ttype, std::string prefix="")
void generate_service_interface_factory(t_service *tservice, string style)
void generate_local_reflection(std::ofstream &out, t_type *ttype, bool is_definition)
std::string render_const_value(std::ofstream &out, std::string name, t_type *type, t_const_value *value)
std::string type_to_enum(t_type *ttype)
void generate_struct_reader(std::ofstream &out, t_struct *tstruct, bool pointers=false)
void generate_struct_fingerprint(std::ofstream &out, t_struct *tstruct, bool is_definition)
void generate_local_reflection_pointer(std::ofstream &out, t_type *ttype)
std::string namespace_open(std::string ns)
void generate_cpp_struct(t_struct *tstruct, bool is_exception)
void set_use_include_prefix(bool use_include_prefix)
void generate_process_function(t_service *tservice, t_function *tfunction, string style, bool specialized=false)
void generate_service_multiface(t_service *tservice)
std::string declare_field(t_field *tfield, bool init=false, bool pointer=false, bool constant=false, bool reference=false)
std::string namespace_prefix(std::string ns)
void generate_struct(t_struct *tstruct)
void generate_enum_constant_list(std::ofstream &f, const vector< t_enum_value * > &constants, const char *prefix, const char *suffix, bool include_values)
void generate_service_null(t_service *tservice, string style)
void generate_deserialize_map_element(std::ofstream &out, t_map *tmap, std::string prefix="")
std::string local_reflection_name(const char *, t_type *ttype, bool external=false)
std::string get_include_prefix(const t_program &program) const
void generate_consts(std::vector< t_const * > consts)
std::string cob_function_signature(t_function *tfunction, std::string prefix="", bool name_params=true)
void generate_struct_definition(std::ofstream &out, t_struct *tstruct, bool is_exception=false, bool pointers=false, bool read=true, bool write=true)
std::set< std::string > reflected_fingerprints_
void generate_enum(t_enum *tenum)
std::string argument_list(t_struct *tstruct, bool name_params=true, bool start_comma=false)
void generate_serialize_field(std::ofstream &out, t_field *tfield, std::string prefix="", std::string suffix="")
std::string namespace_close(std::string ns)
void generate_serialize_map_element(std::ofstream &out, t_map *tmap, std::string iter)
void generate_common_struct_writer_body(std::ofstream &out, t_struct_common *tstruct, bool pointers=false)
void generate_struct_writer(std::ofstream &out, t_struct *tstruct, bool pointers=false)
void generate_service(t_service *tservice)
bool is_complex_type(t_type *ttype)
void generate_service_interface(t_service *tservice, string style)
std::ofstream f_types_impl_
void generate_service_processor(t_service *tservice, string style)
void generate_deserialize_set_element(std::ofstream &out, t_set *tset, std::string prefix="")
const std::vector< t_enum_value * > & get_constants()
const std::string & get_name() const
t_type * get_type() const
t_type * get_returntype() const
t_struct * get_arglist() const
t_struct * get_xceptions() const
const std::string & get_name() const
virtual std::string get_out_dir() const
std::string tmp(std::string name)
std::string service_name_
static t_type * get_true_type(t_type *type)
const t_program * get_program() const
std::string out_dir_base_
t_type * get_elem_type() const
t_type * get_key_type() const
t_type * get_val_type() const
void scope_down(std::ostream &out)
void scope_up(std::ostream &out)
const std::string & get_include_prefix() const
const std::string & get_namespace() const
const std::string & get_name() const
virtual bool has_key_annotation() const
bool is_level_category_supported() const
const t_type * get_type()
t_service * get_extends()
const std::vector< t_function * > & get_functions() const
t_type * get_elem_type() const
bool append(t_field *elem)
const members_type & get_sorted_members()
virtual std::string get_struct_type_name()=0
const members_type & get_members()
const uint8_t * get_binary_fingerprint() const
virtual void set_name(const std::string &name)
const uint32_t get_4byte_fingerprint() const
bool has_fingerprint() const
virtual bool is_string() const
virtual bool is_set() const
virtual bool is_xception() const
static const int fingerprint_len
virtual bool is_struct() const
virtual bool is_enum() const
virtual const std::string & get_name() const
t_program * get_program()
static std::string byte_to_hex(uint8_t byte)
virtual bool is_base_type() const
virtual std::string get_fingerprint_material() const =0
std::string get_ascii_fingerprint() const
virtual bool is_list() const
std::map< std::string, std::string > annotations_
virtual bool is_container() const
virtual bool is_map() const
virtual bool is_void() const
t_type * get_type() const
const std::string & get_symbolic() const
static const char namespace_prefix[]
#define LOG(_Level, _Msg)
int32_t skip(Protocol_ &prot, TType type)
#define THRIFT_REGISTER_GENERATOR(language, long_name, doc)