gSoap + RabbitMQ, Hello, world example C++

edited January 16 in English

First of all you need to install this packages:
bison, byacc, flex

Ubuntu/Debian

$ sudo apt-get install bison byacc flex

Linux installation

This installation guide is about Linux, if you using another OS, please watch INSTALL.txt file in gSoap root folder.

Download ZIP file of gSoap from this url https://sourceforge.net/projects/gsoap2/.
After that unzip gSoap to any folder.
Then install gSoap on your computer:

$ cd <gsoap_folder>
$ ./configure
$ make && sudo make install

I work on both Linux and OSx, but i forgot how to insall gSoap on OSx, it should be similar to Linux. Later i will tell you if you will ask me in this topic. Anyway read INSTALL.txt from gSoap folder.

Make XML parser

Create several XSD files, something similar to bottom screenshot

Several xsd files

Generate code from several XSD files

$ wsdl2h -i xsd/v.1.0/* -o wsdl2h/xsd.h
$ soapcpp2 wsdl2h/xsd.h -d wsdl2h/

And i've got something like this

Generated headers and cpp files

Reading from RabbitMQ

I will use SimpleAmqpClient

cpp code:

#include <iostream>
#include <sstream>
#include <amqpcpp.h>
#include <SimpleAmqpClient/SimpleAmqpClient.h>
#include "wsdl2h/ns2.nsmap"
#include "wsdl2h/soapH.h"

int main() {
    AmqpClient::Channel::ptr_t connection = AmqpClient::Channel::Create(
            "localhost", 5672, "guest", "admin000"
    );
    std::string consume_tag = connection->BasicConsume("KEYWORDS_IN", "");
    struct soap *soap = soap_new1(SOAP_C_UTFSTRING | SOAP_XML_IGNORENS);
    std::stringstream ss;

    while (true) {
        try {
            AmqpClient::Envelope::ptr_t envelope = connection->BasicConsumeMessage(consume_tag);

            std::cout << "Read message" << std::endl;

            _ns2__Envelope envelopeObj;
            ss.str(envelope->Message()->Body());
            soap->is = &ss;
            soap_read__ns2__Envelope(soap, &envelopeObj);
            ss.clear();
            soap_cleanup(soap);

            _ns4__ParseKeywords keywordsObj;
            ss.str(envelopeObj.Body);
            soap->is = &ss;
            soap_read__ns4__ParseKeywords(soap, &keywordsObj);
            std::cout << keywordsObj.Keyword.size() << " Keywords deteced." << std::endl;
            for (ns4__KeywordType *k : keywordsObj.Keyword) {
                std::cout << "Keyword: " << (std::string) k->__item << std::endl;
            }
            soap_cleanup(soap);
            ss.clear();
        } catch (std::exception e) {
            std::cout << e.what() << std::endl;
        }
    }
    return 0;
}

The output:

Read message
2 Keywords deteced.
Keyword: Keyword0
Keyword: Keyword1

XML Sample file

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:ns1="com.idurdyev.idcommerce:CommonTypes:1.0"
 xmlns:ns2="com.idurdyev.idcommerce:MessageInfo:1.0"
 xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Header>
        <ns2:MessageInfo>
            <ns2:EnvelopeID>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</ns2:EnvelopeID>
            <ns2:RefEnvelopeID>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</ns2:RefEnvelopeID>
            <ns2:MessageCode>0000000000</ns2:MessageCode>
        </ns2:MessageInfo>
    </Header>
    <Body>
        <ns1:ParseKeywords xmlns:ns1="com.idurdyev.idcommerce:ParseKeywords:1.0"
            xmlns:ns0="com.idurdyev.idcommerce:CommonTypes:1.0"
            xmlns:ns2="com.idurdyev.idcommerce:MessageInfo:1.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ns1:Keyword>Keyword0</ns1:Keyword>
            <ns1:Keyword>Keyword1</ns1:Keyword>
        </ns1:ParseKeywords>        
    </Body>
</env:Envelope>

Project source code available at GitHub https://github.com/id-tutorials/gSoapRabbitMQ

Tagged:

Comments

Sign In or Register to comment.