Simplifying network programming with juggle

Source: Internet
Author: User

General network programming, in the message processing will probably use the following way

struct msg{    int  msg_id;     int Msg_len;
... msg_info};

Define the message structure as above

After the receiver receives, the data is parsed by the message structure as above

struct msg * _msg = (struct msg*) data;

Then the message is processed, and the message is distributed to different message handlers by pressing MSG_ID

Switch (_msg->msg_id) {     case1:        //doanything         break;     default :          Break ;  }

RPC is a cumbersome message processing process is encapsulated, automatically generate one by one corresponding message response code, so that developers can focus on the processing of logic.

The typical RPC framework has PROTOBUF, thrift, which provide the DSL language defined as a network message, the user uses the DSL language to define the required network events, and the RPC framework automatically generates the client's send and service-side response codes. Instead of having to deal with tedious and repetitive message response codes, users can focus on the programming of the logical code by simply implementing the part of the network communication as constrained by the framework.

Juggle is the RPC framework in the Abelkhan server framework.

The framework uses a highly decoupled design that separates the packaging parts of the data and focuses on the generation of the message response code. Currently supports both C + + and C # 2 languages.

Here's how to use juggle:

Defining a DSL Script

module test{    void test_func (stringint  argv2);

Generate code using CodeGen:

Juggle is written in python3.5, need to install python3.5 before use, and then enter the directory where juggle is located

To generate C + + code Test Execution gencpp.py script, to generate C # code test Execution gencsharp.py script

The generated code is as follows:

C + + code:

Client:

/*This caller file was codegen by juggle for C + +*/#include<sstream>#include<tuple>#include<string>#include<Icaller.h>#include<Ichannel.h>#include<boost/any.hpp>#include<boost/make_shared.hpp>namespacecaller{classTest: PublicJuggle::icaller { Public: Test (boost::shared_ptr<juggle::Ichannel>_ch): Icaller (_ch) {module_name="Test"; }    ~Test () {}voidTest_func (std::stringargv0,int64_t argv1) {Auto V= boost::make_shared<std::vector<boost::any> >(); V->push_back ("Test"); V->push_back ("Test_func"); V->push_back (Boost::make_shared<std::vector<boost::any> >()); (Boost::any_cast<boost::shared_ptr<std::vector<boost::any> > > ((*V) [2]),push_back (argv0); (Boost::any_cast<boost::shared_ptr<std::vector<boost::any> > > ((*V) [2]),push_back (ARGV1); CH-push (v); }};}

Server-side

/*this module file is codegen by juggle for c++*/#include <Imodule.h> #include <boost/shared_ptr.hpp> #include <boost/signals2.hpp> #include <string>namespace module{class test:public juggle::imodule {public:    Test () {        module_name = "Test";        Protcolcall_set.insert (Std::make_pair ("Test_func", Boost::bind (&test::test_func, this, _1)));    ~test () {    }    boost::signals2::signal<void (std::string, int64_t) > sigtest_funchandle;    void Test_func (Boost::shared_ptr<std::vector<boost::any> > _event) {        Sigtest_funchandle (            Boost::any_cast<std::string> ((*_event) [0]),             boost::any_cast<int64_t> ((*_event) [1]);}}    ;}

When using the client-side definition Caller::test object called Test_func, the server-end definition Module::test Connect the response function to the sigtest_funchandle.

Juggle Address: Https://github.com/qianqians/abelkhan/tree/master/common/juggle

Welcome to Abelkhan's relevant forum to make bugs and comments: http://abelkhan.com/forum.php?mod=forumdisplay&fid=2

Simplifying network programming with juggle

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.