gsoap使用总结

来源:互联网 发布:java事务怎么实现 编辑:程序博客网 时间:2024/05/22 16:42

项目中新加网管平台,所有应用都需要向网管平台(web应用)定时主动上报监控的数据统计。各相关应用与网管平台部署在不同服务器,网络互通。
各应用包含java,c++的实现。其中c++访问web service可以通过gsoap来实现。

gsoap的官网地址:https://www.genivia.com,包含商业版,以及GPLv2的开源版本。
1、下载地址:https://www.genivia.com/download/gsoap-2.8.54.zip,发布版本中包含平台无关的源代码,预构建的windows、MAC OS的可执行二进制。

2、需要用到gsoap的soapcpp2 与wsdl2h 来生成自己的webservice调用的数据结构与应用。
解压,在gsoap/bin 目录可以看到自带的MAC OS与windows 编译库。
soapcpp2:gsoap代码生成器。
wsdl2h:自身是gsoap的应用,解析WSDL,XML模式,SOAP/XML。通过wsdl2h 解析 webservice的wsdl文件,可以生成对应的调用代码。
将wsdl文件转换为 头文件。

3、 Unix/Linux 上安装gsoap
上传并解压gsoap-2.8.54.zip,在解压目录下

./configure
make
sudo make install
在gsoap目录下生成soapcpp2,wsdl2h,make install将其复制到/usr/local/bin/目录下。

4、生成自己应用对应的代码文件。
网管应用的wsdl文件已提供URL,如:http://10.118.203.187:58080/monitor/rpc/kpi?wsdl

使用 wsdl2h 将wsdl对应的xml 转为 头文件。,实现WSDL文件到.h文件的数据映射
wsdl2h -s -o kpi.h http://10.118.203.187:58080/monitor/rpc/kpi?wsdl
kpi.h中包含了web rpc 的数据结构
ns1__reportKpiData
soap_call_ns1__reportKpiData

使用soapcpp2 将kpi.h 生成对应的代码。

soapcpp2 -i -x -C -L kpi.h

Saving soapStub.h annotated copy of the source interface file
Saving soapH.h serialization functions to #include in projects
Using ns1 service name: KpiDataServicePortBinding
Using ns1 service style: document
Using ns1 service encoding: literal
Using ns1 service location: http://10.118.203.187:58080/monitor/rpc/kpi
Using ns1 schema namespace: http://ws.monitor.zte.com/
Saving soapKpiDataServicePortBindingProxy.h client proxy class
Saving soapKpiDataServicePortBindingProxy.cpp client proxy class
Saving KpiDataServicePortBinding.nsmap namespace mapping table
Saving soapC.cpp serialization functions

Compilation successful

至此,我们已经得到与webservice rpc调用的相关代码与头文件。写个测试代码试试。
编译需要的文件
KpiDataServicePortBinding.nsmap
soapC.cpp
soapH.h
soapStub.h
soapKpiDataServicePortBindingProxy.cpp
soapKpiDataServicePortBindingProxy.h
stdsoap2.cpp –gsoap目录下
stdsoap2.h –gsoap目录下
gsoaptest.cpp

cat gsoaptest.cpp

include “KpiDataServicePortBinding.nsmap”

include “soapKpiDataServicePortBindingProxy.h”

int main()
{
KpiDataServicePortBindingProxy client(“http://10.118.203.187:58080/monitor/rpc/kpi“);
struct ns1__reportKpiDataResponse response;
char * msg = “R_receive_mr,2017-10-23 10:19:00,0,1,,,,10.118.203.188,9753”;
if (client.reportKpiData(msg,response) != SOAP_OK)

    std::cout << "reportKpiData is not SOAP_OK "  << std::endl;    return 0;

}

对应的编译过程

cat make.sh

!/bin/bash

g++ -g -I ./ -c stdsoap2.cpp -o stdsoap.o
g++ -g -I ./ -c soapC.cpp -o soapC.o
g++ -g -I ./gsoap -I ./ -c soapKpiDataServicePortBindingProxy.cpp -o soapKpiDataServicePortBindingProxy.o
g++ -g -I ./ -c gsoaptest.cpp -o gsoaptest.o
g++ -g stdsoap.o soapC.o soapKpiDataServicePortBindingProxy.o gsoaptest.o -o gsoapclient

rm -rf *.o
测试OK,web service 可以接受到发送的消息。

集成到原来应用中遇到的问题:
1、gsoap/stdsoap2_cpp.cpp:6798:undefined reference to `namespaces’
将代码加入原来应用时,未将KpiDataServicePortBinding.nsmap 未include包含代码中。

2、multiple definition of `namespaces’
排除soapKpiDataServicePortBindingProxy.h 中包含判断后,KpiDataServicePortBinding.nsmap的数据结构定义。include的位置从头文件.h移到.cpp文件。

完结。