gsoap入門
1 使用gsoap搭建web service
參考了參考文檔[2]中的介紹幼衰,有一點點修改秒咨。
1.1 編譯calc客戶端
- 根據(jù)
wsdl
文件生成gsoap用的頭文件
wsdl2h -o calc.h http://www.genivia.com/calc.wsdl
- 如果用我們自己搭建的webserver仓手,需要修改頭文件中的如下代碼两嘴,
把port字段修改為自己的webservice的url歹嘹。
//gsoap ns2 service port: http://websrv.cs.fsu.edu/~engelen/calcserver.cgi
改為
//gsoap ns2 service port: http://localhost:9999/cgi-bin/calcserver.cgi
- 生成client proxy類的源代碼
soapcpp2 -j -CL -I/usr/share/gsoap/import calc.h
-j
生成C++ proxy類享甸,供client調(diào)用截碴,-CL
生成客戶端程序,-I
指明gsoap相關(guān)頭文件路徑蛉威,這個根據(jù)gsoap安裝位置而定日丹,例子中的路徑是ubuntu通過apt-get
安裝的gsoap的路徑。
- 編寫client程序
calcclient.cpp
#include "calc.nsmap" // XML namespace mapping table (only needed once at the global level)
#include "soapcalcProxy.h" // the proxy class, also #includes "soapH.h" and "soapStub.h"
int main()
{
calcProxy calc;
double sum;
if (calc.add(1.23, 4.56, sum) == SOAP_OK)
std::cout << "Sum = " << sum << std::endl;
else
calc.soap_stream_fault(std::cerr);
calc.destroy(); // same as: soap_destroy(calc.soap);
soap_end(calc.soap);
}
調(diào)用proxy類calcProxy
的 add
方法蚯嫌,傳遞了2個參數(shù)1.23
4.56
哲虾,打印結(jié)果。
- 編譯
c++ -o calcclient calcclient.cpp soapC.cpp soapcalcProxy.cpp -lgsoap++
如果第二步?jīng)]修改port的話择示,這時候執(zhí)行calcclient
就能得到結(jié)果了束凑,如果修改了port,我們需要自己搭建一個webservice來提供calc服務(wù)栅盲。
1.2 搭建服務(wù)器
1.2.1 編譯服務(wù)器端程序
- 與client第一步一樣汪诉,生成
calc.h
wsdl2h -o calc.h http://www.genivia.com/calc.wsdl
- 生成server端的接口類
soapcpp2 -j -SL -I/usr/share/gsoap/import calc.h
- 編寫cgi程序
calcserver.cpp
,實現(xiàn)calc服務(wù)器端的接口
#include "calc.nsmap" // XML namespace mapping table (only needed once at the global level)
#include "soapcalcService.h" // the service class, also #includes "soapH.h" and "soapStub.h"
int main()
{
calcService calc(SOAP_XML_INDENT);
if (calc.serve() != SOAP_OK)
calc.soap_stream_fault(std::cerr);
calc.destroy(); // same as: soap_destroy(calc.soap);
soap_end(calc.soap);
}
int calcService::add(double a, double b, double &result)
{
result = a + b;
return SOAP_OK;
}
int calcService::sub(double a, double b, double &result)
{
result = a - b;
return SOAP_OK;
}
int calcService::mul(double a, double b, double &result)
{
result = a * b;
return SOAP_OK;
}
int calcService::div(double a, double b, double &result)
{
if (b == 0.0)
{
char *msg = (char*)soap_malloc(this->soap, 1024);
snprintf(msg, 1024, "Trying to divide %f by zero", a);
return this->soap_senderfault(msg, NULL);
}
result = a / b;
return SOAP_OK;
}
int calcService::pow(double a, double b, double &result)
{
result = ::pow(a, b);
// soap_errno is like errno, but compatible with Win32
if (soap_errno == EDOM)
{
char *msg = (char*)soap_malloc(this->soap, 1024);
snprintf(msg, 1024, "<error xmlns=\"http://tempuri.org/\">Can't take power of %f to %f</error>", a, b);
return this->soap_senderfault("Power function domain error", msg);
}
return SOAP_OK;
}
- 編譯谈秫,測試
c++ -o calcserver calcserver.cpp soapC.cpp soapcalcService.cpp -lgsoap++
./calcserver < calc.add.req.xml
這時候calcserver
是一個cgi程序扒寄,從標(biāo)準(zhǔn)輸入讀取soap命令,輸出到標(biāo)準(zhǔn)輸出上拟烫。接下來搭建一個webserver來提供cgi服務(wù)该编。
1.2.2 搭建cgi服務(wù)器
簡單起見,使用docker啟動一個apache來作為web server硕淑。
- 下載apache鏡像
docker pull httpd:2.4
- 修改
http.conf
文件课竣,啟動apache對cgi的支持嘉赎。
取得http.conf文件,修改其中的cgi支持稠氮,打開下面內(nèi)容的注釋即可
LoadModule cgi_module modules/mod_cgi.so
- http.conf在docker container的路徑為:
/usr/local/apache2/conf/httpd.conf
- 這步不做的話曹阔,運行client的時候半开,會提示如下錯誤隔披,即apache把cgi請求當(dāng)成了下載文件的請求。
Error 200 fault: SOAP-ENV:Client[no subcode]
"Error 200"
Detail: [no detail]
- 啟動服務(wù),在
calcserver
和http.conf
所在目錄輸入如下命令:
docker run -it --rm --name my-apache-app -p 9999:80 -v "$PWD":/usr/local/apache2/cgi-bin -v "$PWD"/http.conf:/usr/local/apache2/conf/httpd.conf -v /usr/lib/x86_64-linux-gnu/libgsoap++.so.4:/usr/lib/x86_64-linux-gnu/libgsoap++.so.4 httpd:2.4
1.3 其他
1.3.1 問題
gsoap也支持自己作為web服務(wù)器提供服務(wù)寂拆,只需要修改server代碼奢米,即可,但我沒有試驗成功纠永。
int main()
{
calcService calc(SOAP_XML_INDENT);
//if (calc.serve() != SOAP_OK) <--這里改為調(diào)用run
if (calc.run(9999) != SOAP_OK)
calc.soap_stream_fault(std::cerr);
calc.destroy(); // same as: soap_destroy(calc.soap);
soap_end(calc.soap);
}
這時運行服務(wù)器程序時始終提示:
setsockopt unset IPV6_V6ONLY failed in soap_bind()
1.3.2 curl調(diào)試webservice服務(wù)器
可以使用curl
模擬客戶端請求一個soap服務(wù)[4]鬓长,命令如下:
curl -v --header "content-type: application/soap+xml" --data @calc.add.req.xml localhost:9999/cgi-bin/calcserver.cgi
2 編譯arm-linux版本的gsoap
2.1 下載gsoap源碼
http://liquidtelecom.dl.sourceforge.net/project/gsoap2/gSOAP/gsoap_2.8.33.zip
2.2 配置編譯環(huán)境
- 下載linaro toolchain,解壓到目錄
/usr/local/linaro-multilib-2014.06-gcc4.9
- 設(shè)置
PATH
環(huán)境變量尝江。export PATH=/usr/local/linaro-multilib-2014.06-gcc4.9:$PATH
2.3 config&make
2.3.1 生成makefile
gsoap-2.8$ ./configure --host=arm-linux-gnueabihf --prefix=/usr/local/arm-linux
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for arm-linux-gnueabihf-strip... arm-linux-gnueabihf-strip
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... arm-unknown-linux-gnueabihf
checking whether make sets $(MAKE)... (cached) yes
checking for arm-linux-gnueabihf-g++... arm-linux-gnueabihf-g++
checking whether the C++ compiler works... yes
checking for C++ compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... yes
...
-
--host=arm-linux-gnueabihf
- 表明編譯的庫是運行在arm linux
上涉波,這個也是gcc編譯器的前綴。 -
--prefix=/usr/local/arm-linux
- 表明變編譯好的庫的安裝路徑
2.3.2 手動生成wsdl.h文件
gsoap-2.8$ cd gsoap/wsdl
gsoap-2.8/gsoap/wsdl$ ../../target-amd64/bin/soapcpp2 -SC -pwsdl -I. -I../../gsoap/import ./wsdl.h
** The gSOAP code generator for C and C++, soapcpp2 release 2.8.33
** Copyright (C) 2000-2016, Robert van Engelen, Genivia Inc.
** All Rights Reserved. This product is provided "as is", without any warranty.
** The soapcpp2 tool and its generated software are released under the GPL.
** ----------------------------------------------------------------------------
** A commercial use license is available from Genivia Inc., contact@genivia.com
** ----------------------------------------------------------------------------
soapcpp2: using both options -C and -S omits client/server code
Saving wsdlStub.h annotated copy of the source interface file
Saving wsdlH.h serialization functions to #include in projects
Saving xmime.nsmap namespace mapping table
Saving wsdlC.cpp serialization functions
Compilation successful
../../target-amd64/bin/soapcpp2
這個是你的host機(jī)器可以運行的soapcpp2
如果不做這步炭序,在編譯過程中會提示如下錯誤啤覆,因為編譯時,生成的soapcpp2是只能在arm linux上運行的惭聂。
make[4]: Entering directory `/home/meng/git/gsoap-2.8/gsoap/wsdl'
../../gsoap/src/soapcpp2 -SC -pwsdl -I. -I../../gsoap/import ./wsdl.h
/bin/bash: ../../gsoap/src/soapcpp2: cannot execute binary file: Exec format error
make[4]: *** [wsdlC.cpp] Error 126
2.3.3 修改代碼
修改config.h
文件窗声,刪除最后一行的#define malloc rpl_malloc
否則編譯會出錯
`
2.3.4 修改makefile
修改gsoap/wsdl
目錄下的Makefile
,刪除下面2行(因為wsdl2h對其他庫有依賴辜纲,并且我們也不需要arm版本的wsdl2h程序):
wsdl2h$(EXEEXT): $(wsdl2h_OBJECTS) $(wsdl2h_DEPENDENCIES) $(EXTRA_wsdl2h_DEPENDENCIES)
#@rm -f wsdl2h$(EXEEXT)
#$(AM_V_CXXLD)$(wsdl2h_LINK) $(wsdl2h_OBJECTS) $(wsdl2h_LDADD) $(LIBS)
2.3.5 編譯笨觅,安裝
gsoap-2.8$ make
gsoap-2.8$ make install
在目錄/usr/local/arm-linux
下是編譯好的gsoap庫,注:我們沒有編譯出來arm linux版本的wadl2h
耕腾。
2.4 編譯arm-linux版calcserver.cgi
- 編譯cgi版本的calcserver.cgi
calcserver$ arm-linux-gnueabihf-g++ -o calcserver.cgi calcserver.cpp soapC.cpp soapcalcService.cpp -lgsoap++ -DWITH_FASTCGI -L/usr/local/arm-linux/lib -I/usr/local/arm-linux/include
- 編譯支持fastcgi版本的calcserver.cgi[5]
calcserver$ arm-linux-gnueabihf-g++ -o calcserver.cgi calcserver.cpp soapC.cpp soapcalcService.cpp /gsoap-source-path/gsoap/stdsoap2.cpp -DWITH_FASTCGI -I/usr/local/arm-linux/include -I/fcgi-lib-path/fcgi/include -L/fcgi-lib-path/fcgi/usr/lib -lfcgi
-
fcgi-lib-path
- 這個指向arm linux版的fcgi庫 -
gsoap-source-path
- 這個指向gsoap的源碼路徑` - 我們編譯
stdsoap2.cpp
這個文件见剩,而不去鏈接libsoap++.a
- 定義
WITH_FASTCGI
宏
2.5 配置lighttpd
如果編譯的是cgi版本,直接把編譯好的calcserver.cgi放到cig-bin
目錄下即可扫俺。
如果是fastcgi版本苍苞,需要修改lighttpd配置文件,增加如下內(nèi)容(前提lighttpd已經(jīng)配置好支持fastcgi了):
fastcgi.server = (
"calcserver.cgi" =>
(( "socket" => "/tmp/fcgi.socket",
"check-local" => "disable",
"bin-path" => "/webSvr/web/cgi-bin/calcserver.cgi",
"max-procs" => 1,
))
)