浅谈Java中通信机制及与C/C API的集成

来源:IT168 作者:佚名 2008-03-21 出处:pcdog.com

.net  eclipse  google  java  java虚拟机  
上一页 1 2 3 下一页 

客户根据 WSDL 描述文档,会生成一个 SOAP 请求消息。Web Services 都是放在Web服务器后面,客户生成的SOAP请求会被嵌入在一个HTTP POST请求中,发送到 Web 服务器来。Web 服务器再把这些请求转发给 Web Services 请求处理器。

请求处理器的作用在于,解析收到的 SOAP 请求,调用 Web Services,然后再生成相应的 SOAP 应答。Web 服务器得到 SOAP 应答后,会再通过 HTTP应答的方式把信息送回到客户端。

WSDL是Web服务中客户端和服务端沟通的桥梁,描述了对象提供的方法。SOAP帮我们制定了一份被官方认可的对象的封装方法。有了WSDL,客户端只关心如何把参数用Soap封装起来发出去,并获取结果。服务端只关心如何对Soap进行拆包->服务->封包。gSOAP可以帮我们实现上述过程中的拆包和封包,而我们可以只关心服务的实现。

言归正传,在这里我们以一个简单的实现加、减、开放的Web Services的服务为例子,介绍gSOAP的使用:

为了发布这个Web服务,首先我们需要把服务的接口定义好,这个服务可能是一个现有服务的Adapter,为此我们定义头文件。

calc.h:

typedef double xsd__double;

int ns__add(xsd__double a, xsd__double b, xsd__double &result);

int ns__sub(xsd__double a, xsd__double b, xsd__double &result);

int ns__sqrt(xsd__double a, xsd__double &result);

注意到这里面我们把double定义成了xsd__double(两个下划线),这是为了告诉gSOAP,我们需要的soap格式和WSDL格式是基于Document/literal的而非rpc/encoded.为了不把事情搞复杂,在这里我只能说,Java1.6自带的Web Services工具只支持Document/literal格式的WSDL,所以我们生成这种格式的WSDL。至于这两种格式之间选择和他们的long story,大家可以参考下面的文章:http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/

编写好头文件后,我们就可以利用gSOAP提供的工具进行生成了:

/usr/lib/gsoap-2.7/bin/soapcpp2 -S -2 calc.h

生成的主要文件详见附件。

下面我们实现calc.h中定义的函数:

// Contents of file "calc.cpp":

#include "soapH.h"
#include "ns.nsmap"
#include
int main()
{
struct soap soap;
int m, s; // master and slave sockets

soap_init(&soap);
m = soap_bind(&soap, "localhost", 9999, 100);
if (m < 0)
soap_print_fault(&soap, stderr);
else
{
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for (int i = 1; ; i++)
{
s = soap_accept(&soap);
if (s < 0)
{
soap_print_fault(&soap, stderr);
break;
}
fprintf(stderr, "%d: accepted connection from IP=%d.%d.%d.%d socket=%d", i,
(soap.ip >> 24)&0xFF, (soap.ip >> 16)&0xFF, (soap.ip >> 8)&0xFF, soap.ip&0xFF, s);
if (soap_serve(&soap) != SOAP_OK) // process RPC request

soap_print_fault(&soap, stderr); // print error

fprintf(stderr, "request served\n");
soap_destroy(&soap); // clean up class instances

soap_end(&soap); // clean up everything and close socket

}
}
soap_done(&soap); // close master socket and detach environment

}
// Implementation of the "add" remote method:

int ns__add(struct soap *soap, double a, double b, double &result)
{
result = a + b;
return SOAP_OK;
}
// Implementation of the "sub" remote method:

int ns__sub(struct soap *soap, double a, double b, double &result)
{
result = a - b;
return SOAP_OK;
}
// Implementation of the "sqrt" remote method:

int ns__sqrt(struct soap *soap, double a, double &result)
{
if (a >= 0)
{
result = sqrt(a);
return SOAP_OK;
}
else
{
return soap_sender_fault(soap, "Square root of negative value",
"I can only compute the square root of a non-negative value");
}
}

前文提到过,我们不希望为了发布基于Web Services的C语言的API而开发或应用一个大的Web服务器。我们代码中的main函数实现了一个最简单的Web Server(基于Socket)。这个Server利用gSOAP生成的API来提供针对SOAP的处理。



上一页 1 2 3 下一页 
上一篇:在Java编程中的“模式思想”与框架关系
下一篇:Java程序员必须了解的开源协议