AXIS2C启动虚拟机示例

创建WSDL文件

创建eucalyptusNC.wsdl

 

<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright 2003-2004 The Apache Software Foundation.                      -->

<!-- (c) Copyright IBM Corp. 2004, 2005 All Rights Reserved                   -->

<!--                                                                          -->

<!-- Licensed under the Apache License, Version 2.0 (the "License");          -->

<!-- you may not use this file except in compliance with the License.         -->

<!-- You may obtain a copy of the License at                                  -->

<!--                                                                          -->

<!--        http://www.apache.org/licenses/LICENSE-2.0                        -->

<!--                                                                          -->

<!-- Unless required by applicable law or agreed to in writing, software      -->

<!-- distributed under the License is distributed on an "AS IS" BASIS,        -->

<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->

<!-- See the License for the specific language governing permissions and      -->

<!-- limitations under the License.                                           -->

 

<wsdl:definitions targetNamespace="http://localhost/axis/eucalyptusNC" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost/axis/eucalyptusNC" xmlns:intf="http://localhost/axis/eucalyptusNC" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

     <wsdl:types>

           <schema elementFormDefault="qualified" targetNamespace="http://localhost/axis/eucalyptusNC" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost/axis/eucalyptusNC" xmlns:intf="http://localhost/axis/eucalyptusNC" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

                <element name="ncRunInstance">

                     <complexType>

                           <sequence>

                                <element name="cmd" type="xsd:string"/>

                           </sequence>

                     </complexType>

                </element>

                <element name="ncRunInstanceResponse">

                     <complexType>

                           <sequence>

                                <element name="result" type="xsd:string" />

                           </sequence>

                     </complexType>

                </element>

           </schema>

     </wsdl:types>

 

     <wsdl:message name="ncRunInstance">

 

           <wsdl:part element="impl:ncRunInstance" name="parameters"/>

 

     </wsdl:message>

 

     <wsdl:message name="ncRunInstanceResponse">

 

           <wsdl:part element="impl:ncRunInstanceResponse" name="parameters"/>

 

     </wsdl:message>

 

     <wsdl:portType name="eucalyptusNC">

 

           <wsdl:operation name="ncRunInstance">

 

                <wsdl:input message="impl:ncRunInstance" name="ncRunInstance"/>

 

                <wsdl:output message="impl:ncRunInstanceResponse" name="ncRunInstanceResponse"/>

 

           </wsdl:operation>

 

     </wsdl:portType>

 

     <wsdl:binding name="eucalyptusNCSoapBinding" type="impl:eucalyptusNC">

 

           <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

 

           <wsdl:operation name="ncRunInstance">

 

                <wsdlsoap:operation soapAction="eucalyptusNC#ncRunInstance"/>

 

                <wsdl:input name="ncRunInstance">

 

                     <wsdlsoap:body use="literal"/>

 

                </wsdl:input>

 

                <wsdl:output name="ncRunInstanceResponse">

 

                     <wsdlsoap:body use="literal"/>

 

                </wsdl:output>

 

           </wsdl:operation>

 

     </wsdl:binding>

 

     <wsdl:service name="eucalyptusNC">

 

           <wsdl:port binding="impl:eucalyptusNCSoapBinding" name="eucalyptusNC">

 

                <wsdlsoap:address location="http://localhost/axis/eucalyptusNC"/>

 

           </wsdl:port>

 

     </wsdl:service>

 

</wsdl:definitions>

 

配置环境变量

AXIS2_HOMEaxi2的放置目录

AXIS2C_HOMEaxis2c的安装目录

export AXIS2_HOME=/home/eucalyptus-src-deps/axis2-1.4/

export AXIS2C_HOME=/usr/local/axis2c/

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/axis2c/lib/

export PATH=$PATH:/usr/local/axis2c/bin/tools/wsdl2c/

生成服务端代码

sh WSDL2C.sh -uri eucalyptusNC.wsdl -sd -ss -d adb -u -f -o server

axis2_skel_eucalyptusNC.c文件adb_ncRunInstanceResponse_t* axis2_skel_eucalyptusNC_ncRunInstance (const axutil_env_t *env  ,

                                              adb_ncRunInstance_t* ncRunInstance )

函数中添加如下代码

 

adb_ncRunInstanceResponse_t* ref=NULL;

         char *cmd;

         cmd=strdup(adb_ncRunInstance_get_cmd(ncRunInstance,env));

         printf("cmd :%s/n",cmd);

         ref=adb_ncRunInstanceResponse_create(env);

         RunInstances();

         adb_ncRunInstanceResponse_set_result(ref,env,"Pending...");

 

          return ref;

其中RunInstances();包含在RunInstances.h头文件中,这个文件是启动虚拟机的代码

 

 

RunInstances.h源码如下:

 

#include <stdio.h>

#include <stdlib.h>

#include <libvirt/libvirt.h>

 

void closeConn(virConnectPtr conn)

{

     if(conn!=NULL)

           virConnectClose(conn);

}

 

int RunInstances()

{

     virConnectPtr conn=NULL;

     virDomainPtr dom=NULL;

     conn=virConnectOpen("");

     if(conn==NULL)

     {

           fprintf(stderr,"Fail To Connect Hypervisor/n");

           return 1;

     }

     const char *xmlconfig="<domain type='xen' id='18'>/

    <name>i-52C5093E</name>/

    <os>/

    <type>linux</type>/

        <kernel>/var/lib/xen/images/kernel</kernel>/

        <initrd>/var/lib/xen/images/ramdisk</initrd>/

        <root>/dev/sda1</root>/

        <cmdline> ro</cmdline>/

    </os>/

    <memory>524288</memory>/

    <vcpu>1</vcpu>/

    <devices>/

        <disk type='file'>/

            <source file='/var/lib/xen/images/root'/>/

            <target dev='sda1'/>/

        </disk>/

        <interface type='bridge'>/

            <source bridge='xenbr0'/>/

            <mac address='d0:0d:52:C5:09:3E'/>/

            <script path='/etc/xen/scripts/vif-bridge'/>/

        </interface>/

    </devices>/

</domain>";

 

     dom=virDomainDefineXML(conn,xmlconfig);

     if (!dom)

     {

    fprintf(stderr, "Domain definition failed");

    return 1;

     }

     if (virDomainCreate(dom) < 0)

     {

    virDomainFree(dom);

    fprintf(stderr, "Cannot boot guest");

    return 1;

     }

     fprintf(stderr, "Guest %s has booted/n", virDomainGetName(dom));

     virDomainFree(dom);

     return 0;

}

这个文件启动的虚拟机的镜像文件来自eucalyptus推荐的IMG,放在/var/lib/xen/images/root

这里为了方便,省去了两个空白文件,太大了而且是空文件,所以没考过来

编译服务器代码生成.so文件

gcc -g -fPIC -shared -olibeucalyptusNC.so -I $AXIS2C_HOME/include/axis2-1.6.0/ -Isrc -L$AXIS2C_HOME/lib  -laxutil  -laxis2_axiom  -laxis2_engine  -laxis2_parser   -lpthread -laxis2_http_sender     -laxis2_http_receiver     -lguththila   -lvirt  server/*.c

    axis2c的服务器文件夹下新建一个eucalyptusNC文件夹,并将生成的libeucalyptusNC.soserver.xml文件拷到这个文件夹下,然后启动服务器

生成客户端代码

sh WSDL2C.sh -uri eucalyptusNC.wsdl -g -ss -d adb -u -f -o client

将文件eucalyptusNC.c拷到client文件夹中

eucalyptusNC.c中代码如下

 

#include "axis2_stub_eucalyptusNC.h"

 

int main(int argc,char *argv)

{

    axutil_env_t * env = NULL;

    axis2_char_t * operation = NULL;

    axis2_char_t * client_home = NULL;

    axis2_char_t * endpoint_uri = NULL;

    axis2_stub_t * stub = NULL;

     adb_ncRunInstanceResponse_t* ref=NULL;

     adb_ncRunInstance_t* sent=NULL;

     char val[]="ncRunInstance";

     char ref_val[20] ;

    

     endpoint_uri = "http://10.51.3.88:9090/axis2/services/eucalyptusNC";

    env = axutil_env_create_all("alltest.log", AXIS2_LOG_LEVEL_TRACE);

   

    /* Set up deploy folder. */

    client_home = AXIS2_GETENV("AXIS2C_HOME");

    if (!client_home)

        client_home = "../../../deploy";

    

     stub=axis2_stub_create_eucalyptusNC(env,client_home,endpoint_uri);

     sent=adb_ncRunInstance_create(env);

     adb_ncRunInstance_set_cmd(sent,env,val);

     int s=0;

    

     ref= axis2_stub_op_eucalyptusNC_ncRunInstance(stub,env,sent);

     if (!ref)

    {

        printf("Error: response NULL/n");

        return -1;

    }

    printf("Result:%s /n", adb_ncRunInstanceResponse_get_result(ref, env));

    return 0;

}

其中有一行endpoint_uri = "http://10.51.3.88:9090/axis2/services/eucalyptusNC";这行代表服务器的地址,设置这个可以设置服务器的地址

生成客户端可执行文件

 

gcc -o eucalyptusNC -I$AXIS2C_HOME/include/axis2-1.6.0/ -L$AXIS2C_HOME/lib -laxutil -laxis2_axiom -laxis2_parser -laxis2_engine -lpthread -laxis2_http_sender -laxis2_http_receiver client/*.c -ldl -Wl,--rpath -Wl,$AXIS2C_HOME/lib

配置好环境变量运行客户端

将上面列子嵌入到eclipse工程

将上面serverclient文件更名为eucalyptusNCeucalyptusNCClient拷贝到eclipse工程文件夹下

eclipse工程文件生成libeucalyptusNC.so

1.  新建一个C动态库项目eucalyptusNC,如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. 设置环境变量

 

将“axis2c的安装目录”/include/axis2-1.6.0

axis2c的安装目录”/include/axis2-1.6.0/platforms

axis2c的安装目录”/include/axis2-1.6.0/platforms/unix 包含进来

 

将“axis2c的安装目录”/lib包含到库中

 

 

3.        设置编译规则

GCC C Compiler 选择下的Optimization栏右边添加-fPIC命令

 

GCC C Linker command中添加gcc -laxutil  -laxis2_axiom  -laxis2_engine  -laxis2_parser   -lpthread -laxis2_http_sender     -laxis2_http_receiver     -lguththila   -lvirt

 

编译工程得到libeucalyptusNC.so

生成客户端可执行文件

生成客户端可执行文件和上面步骤相似,只是建立的是一个可执行工程,在编译选项中不需要加-fPIC选项即可

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐