Information delivery tool PROTOBUF through PB reflection

Source: Internet
Author: User
Tags assert reflection

Recently because the project logic is almost done, so hurriedly to do the test work, so that the project on the line after the problem, so I intend to use the form of configuration to do a similar and white box test tool out of things.

Because the project uses PB for protocol communication, so the configuration of XML is similar to PB, the PB fields and types in the configuration, and then add value, a protocol structure can be, now only by modifying the value to do the test, the following will change the more intelligent some, such as the number of a behavior, a more random behavior, and so on.

To read the Chen Yu on the PB Processing protocol reflection, learned a lot of things, at the same time, some of the PB understanding more profound, Google or Daniel a lot.

1. How to handle PB reflection, generate a protocol dynamically via protocol string

PB provides a powerful Descriptorpool::generated_pool ()

The code is as follows:

The code is as follows Copy Code
Inline Google::p rotobuf::message* createmessage (const std::string& msg)
{
Google::p rotobuf::message* message = NULL;
Const Google::p rotobuf::D escriptor* descriptor = google::p rotobuf::D escriptorpool::generated_pool ()-> Findmessagetypebyname ("protomsg." + msg);
if (descriptor)
{
Const Google::p rotobuf::message* prototype = google::p rotobuf::messagefactory::generated_factory ()-> Getprototype (descriptor);
if (prototype)
{
message = Prototype->new ();
}
}

return message;
}

inline void Releasemessage (google::p rotobuf::message* pMsg)
{
if (NULL!= pMsg)
{
Pmsg->clear ();
Delete pMsg;
}
}



These two functions can dynamically generate PB message, where Protomsg is the name of your PB package

2. Set the value in the configuration into the PB field by reflection

PB's message base class provides a reflection, this class is very powerful

The code is as follows

The code is as follows Copy Code
Mstrcurmsg the currently executing protocol
Google::p rotobuf::message* pMsg = Test::createmessage (mstrcurmsg);
Const Google::p rotobuf::D escriptor* pdescriptor = google::p rotobuf::D escriptorpool::generated_pool ()-> Findmessagetypebyname ("protomsg." + mstrcurmsg);
ASSERT (NULL!= pdescriptor);

This one can get to the reflection class, and then you can assign values in the configuration
Const Google::p rotobuf::reflection* preflection = Pmsg->getreflection ();
ASSERT (NULL!= preflection);

for (int i = 0; i < Pdescriptor->field_count (); ++i)
{
Const Google::p rotobuf::fielddescriptor* pfielddescriptor = Pdescriptor->field (i);
ASSERT (NULL!= pfielddescriptor);

Const std::string& strFieldName = Pfielddescriptor->name ();

Const testconfigmodule::msgentry* pmsgentry = Pmsgstruct->getmsgentry (strfieldname);
ASSERT (NULL!= pmsgentry);

Read field type, incidentally you can do type checking
ASSERT (Pmsgentry->mntype = = Pfielddescriptor->type ());

Setting values
Switch (pmsgentry->mntype)
{
Case test::type_string:
Preflection->setstring (PMSG, Pfielddescriptor, Pmsgentry->mstrvalue);
Break
// ...
Default
Break
}
}

Std::string strdata;
if (!pmsg->serializetostring (&strdata))
{
M_plogmodule->lognormal ("Test stop, Cannot serializetostring", Mstrcurmsg, __function__, __line__);
Return
}

Test::releasemessage (PMSG);



With such steps, you can automatically create a message and assign a value to the field

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.