zoukankan      html  css  js  c++  java
  • Axis2/c 笔记

    主要参考:
    http://axis.apache.org/axis2/c/core/docs/axis2c_manual.html

    摘要:

    Axis2是实现Web Service的一种技术框架(架构)。对于用户而言,只需要按照axis2的框架(skeleton)实现server端的逻辑(Server端的逻辑以dll的形式呈现),再实现client端的main方法进行访问即可。

     

    当然在main函数中需要指定访问server端的逻辑,例如:

    axis2_options_set_actionoptions, env, "http://ws.apache.org/axis2/c/samples/hello"

    其中c/samples/hello就是具体访问的逻辑了。

     

    在server端,则是根据client端用户的请求,返回相应的结果,所以在server端需要配置文件来指定特定的服务,这通过service.xml来配置:

    <service name="hello"> <parameter name="ServiceClass" locked="xsd:false">hello</parameter> <description> Quick start guide hello service sample. </description> <operation name="greet"/></service>

    "ServiceClasshello将作为一个映射map到发布引擎的服务,就是上面提到的dll。所以最终的结构如下:

    实例:

    http://axis.apache.org/axis2/c/core/docs/axis2c_manual.html 中有一个简单的实例,按照步骤实现如下:

    1. 下载axis2软件包,并解压,在环境变量中添加变量AXIS2_HOME,其值为解压后的路径,例如c:\axis2c;

    2. 在系统环境变量中找到Path项,编辑该项,在它的最开始加入C:\VS8\Common7\IDE,添加%AXIS2_HOME%\lib,从而保证代码编译时可以找到相关的依赖项的路径;

    3. 创建server端的逻辑,hello_svc.c,代码如下:

     
    View Code
      1 #include <axis2_svc_skeleton.h>
      2 
      3 #include <axutil_log_default.h>
      4 
      5 #include <axutil_error_default.h>
      6 
      7 #include <axutil_array_list.h>
      8 
      9 #include <axiom_text.h>
     10 
     11 #include <axiom_node.h>
     12 
     13 #include <axiom_element.h>
     14 
     15 #include <stdio.h>
     16 
     17 
     18 
     19 axiom_node_t *axis2_hello_greet(const axutil_env_t *env,
     20 
     21         axiom_node_t *node);
     22 
     23 
     24 
     25 int AXIS2_CALL
     26 
     27 hello_free(axis2_svc_skeleton_t *svc_skeleton,
     28 
     29         const axutil_env_t *env);
     30 
     31 
     32 
     33 axiom_node_t* AXIS2_CALL
     34 
     35 hello_invoke(axis2_svc_skeleton_t *svc_skeleton,
     36 
     37         const axutil_env_t *env,
     38 
     39         axiom_node_t *node,
     40 
     41         axis2_msg_ctx_t *msg_ctx);
     42 
     43 
     44 
     45 
     46 
     47 int AXIS2_CALL
     48 
     49 hello_init(axis2_svc_skeleton_t *svc_skeleton,
     50 
     51         const axutil_env_t *env);
     52 
     53 
     54 
     55 axiom_node_t* AXIS2_CALL
     56 
     57 hello_on_fault(axis2_svc_skeleton_t *svc_skeli,
     58 
     59         const axutil_env_t *env, axiom_node_t *node);
     60 
     61 
     62 
     63 
     64 
     65 axiom_node_t *
     66 
     67 build_greeting_response(const axutil_env_t *env, 
     68 
     69         axis2_char_t *greeting);
     70 
     71 
     72 
     73 axiom_node_t *
     74 
     75 axis2_hello_greet(const axutil_env_t *env, axiom_node_t *node)
     76 
     77 {
     78     
     79     
     80     axiom_node_t *client_greeting_node = NULL;
     81 
     82     axiom_node_t *return_node = NULL;
     83 
     84 
     85 
     86     AXIS2_ENV_CHECK(env, NULL);
     87 
     88 
     89     printf("axis2_hello_greet \n");
     90 
     91     if (node)
     92 
     93     {
     94 
     95         client_greeting_node = axiom_node_get_first_child(node, env);
     96 
     97         if (client_greeting_node &&
     98 
     99                 axiom_node_get_node_type(client_greeting_node, env) == AXIOM_TEXT)
    100 
    101         {
    102 
    103             axiom_text_t *greeting = (axiom_text_t *)axiom_node_get_data_element(client_greeting_node, env);
    104 
    105             if (greeting && axiom_text_get_value(greeting , env))
    106 
    107             {
    108 
    109                 const axis2_char_t *greeting_str = axiom_text_get_value(greeting, env);
    110 
    111                 printf("Client greeted saying \"%s\" \n", greeting_str);
    112 
    113                 return_node = build_greeting_response(env, "Hello Client!");
    114 
    115             }
    116 
    117         }
    118 
    119     }
    120 
    121     else
    122 
    123     {
    124 
    125         AXIS2_ERROR_SET(env->error, AXIS2_ERROR_SVC_SKEL_INVALID_XML_FORMAT_IN_REQUEST, AXIS2_FAILURE);
    126 
    127         printf("ERROR: invalid XML in request\n");
    128 
    129         return_node = build_greeting_response(env, "Client! Who are you?");
    130 
    131     }
    132 
    133 
    134 
    135     return return_node;
    136 
    137 }
    138 
    139 
    140 
    141 axiom_node_t *
    142 
    143 build_greeting_response(const axutil_env_t *env, axis2_char_t *greeting)
    144 
    145 {
    146         
    147     axiom_node_t* greeting_om_node = NULL;
    148 
    149     axiom_element_t * greeting_om_ele = NULL;
    150 
    151 
    152 
    153     greeting_om_ele = axiom_element_create(env, NULL, "greetResponse", NULL, &greeting_om_node);
    154 
    155 
    156 
    157     axiom_element_set_text(greeting_om_ele, env, greeting, greeting_om_node);
    158 
    159 
    160     printf("build_greeting_response \n");
    161 
    162     return greeting_om_node;
    163 
    164 }
    165 
    166 
    167 
    168 static const axis2_svc_skeleton_ops_t hello_svc_skeleton_ops_var = {
    169 
    170 
    171     hello_init,
    172 
    173     hello_invoke,
    174 
    175     hello_on_fault,
    176 
    177     hello_free
    178 
    179 };
    180 
    181 
    182 
    183 axis2_svc_skeleton_t *
    184 
    185 axis2_hello_create(const axutil_env_t *env)
    186 
    187 {
    188 
    189     axis2_svc_skeleton_t *svc_skeleton = NULL;
    190 
    191     svc_skeleton = AXIS2_MALLOC((env->allocator), sizeof(axis2_svc_skeleton_t));
    192 
    193 
    194 
    195     svc_skeleton->ops = &hello_svc_skeleton_ops_var;
    196 
    197 
    198 
    199     svc_skeleton->func_array = NULL;
    200 
    201     printf("axis2_hello_create \n");
    202  
    203     return svc_skeleton;
    204 
    205 }
    206 
    207 
    208 
    209 int AXIS2_CALL
    210 
    211 hello_init(axis2_svc_skeleton_t *svc_skeleton,
    212 
    213         const axutil_env_t *env)
    214 
    215 {
    216 
    217 printf("hello_init \n");
    218     svc_skeleton->func_array = axutil_array_list_create(env, 0);
    219 
    220     axutil_array_list_add(svc_skeleton->func_array, env, "helloString");
    221 
    222     return AXIS2_SUCCESS;
    223 
    224 }
    225 
    226 
    227 
    228 axiom_node_t* AXIS2_CALL
    229 
    230 hello_invoke(axis2_svc_skeleton_t *svc_skeleton,
    231 
    232         const axutil_env_t *env,
    233 
    234         axiom_node_t *node,
    235 
    236         axis2_msg_ctx_t *msg_ctx)
    237 
    238 {
    239 
    240 printf("hello_invoke \n");
    241 
    242     return axis2_hello_greet(env, node);
    243 
    244 }
    245 
    246 
    247 
    248 axiom_node_t* AXIS2_CALL
    249 
    250 hello_on_fault(axis2_svc_skeleton_t *svc_skeli,
    251 
    252         const axutil_env_t *env, axiom_node_t *node)
    253 
    254 {
    255 
    256     axiom_node_t *error_node = NULL;
    257 
    258     axiom_node_t* text_node = NULL;
    259 
    260     axiom_element_t *error_ele = NULL;
    261 
    262     error_ele = axiom_element_create(env, node, "EchoServiceError", NULL,
    263 
    264             &error_node);
    265 
    266     axiom_element_set_text(error_ele, env, "Echo service failed ",
    267 
    268             text_node);
    269             
    270     printf("hello_on_fault \n");
    271 
    272     return error_node;
    273 
    274 }
    275 
    276 
    277 
    278 int AXIS2_CALL
    279 
    280 hello_free(axis2_svc_skeleton_t *svc_skeleton,
    281 
    282         const axutil_env_t *env)
    283 
    284 {
    285 
    286     if (svc_skeleton->func_array)
    287 
    288     {
    289 
    290         axutil_array_list_free(svc_skeleton->func_array, env);
    291 
    292         svc_skeleton->func_array = NULL;
    293 
    294     }
    295 
    296 
    297 
    298     if (svc_skeleton)
    299 
    300     {
    301 
    302         AXIS2_FREE(env->allocator, svc_skeleton);
    303 
    304         svc_skeleton = NULL;
    305 
    306     }
    307 
    308 
    309     printf("hello_free \n");
    310 
    311     return AXIS2_SUCCESS;
    312 
    313 }
    314 
    315 
    316 
    317 
    318 
    319 AXIS2_EXPORT int
    320 
    321 axis2_get_instance(axis2_svc_skeleton_t **inst,
    322 
    323         const axutil_env_t *env)
    324 
    325 {
    326 
    327     *inst = axis2_hello_create(env);
    328 
    329     if (!(*inst))
    330 
    331     {
    332 
    333         return AXIS2_FAILURE;
    334 
    335     }
    336 
    337 
    338 
    339     printf("axis2_get_instance \n");
    340     return AXIS2_SUCCESS;
    341 
    342 }
    343 
    344 
    345 
    346 AXIS2_EXPORT int
    347 
    348 axis2_remove_instance(axis2_svc_skeleton_t *inst,
    349 
    350         const axutil_env_t *env)
    351 
    352 {
    353 
    354     axis2_status_t status = AXIS2_FAILURE;
    355 
    356     if (inst)
    357 
    358     {
    359 
    360         status = AXIS2_SVC_SKELETON_FREE(inst, env);
    361 
    362     }
    363     
    364     printf("axis2_remove_instance \n");
    365 
    366     return status;
    367 
    368 }

      4. 编译连接如下:

    C:\VS8\VC\bin\cl.exe /"WIN32" /"_WINDOWS" /"_MBCS" /"AXIS2_DECLARE_EXPORT" /"AXIS2_SVR_MULTI_THREADED" //nologo /%AXIS2C_HOME%\\include /I C:\\VS8\\VC\\include /c hello_svc.c

    C:\VS8\VC\bin\link.exe 
    /nologo /LIBPATH:%AXIS2C_HOME%\\lib /LIBPATH:C:\\VS8\\VC\\lib axutil.lib axiom.lib axis2_parser.lib axis2_engine.lib /DLL /OUT:hello.dll *.obj

     从而生成hello.dll,将这个dll复制到C:\axis2c\services\hello文件夹下,作为逻辑。同时创建上面提到的services.xml,也位于这个文件夹下。

    5。创建client端的main方法,文件hello.c,代码如下:

    View Code
      1 #include <stdio.h>
      2 
      3 #include <axiom.h>
      4 
      5 #include <axis2_util.h>
      6 
      7 #include <axiom_soap.h>
      8 
      9 #include <axis2_client.h>
     10 
     11 
     12 
     13 axiom_node_t *
     14 
     15 build_om_request(const axutil_env_t *env);
     16 
     17 
     18 
     19 const axis2_char_t *
     20 
     21 process_om_response(const axutil_env_t *env,
     22 
     23         axiom_node_t *node);
     24 
     25 
     26 
     27 int main(int argc, char** argv)
     28 
     29 {
     30 
     31     const axutil_env_t *env = NULL;
     32 
     33     const axis2_char_t *address = NULL;
     34 
     35     axis2_endpoint_ref_t* endpoint_ref = NULL;
     36 
     37     axis2_options_t *options = NULL;
     38 
     39     const axis2_char_t *client_home = NULL;
     40 
     41     axis2_svc_client_t* svc_client = NULL;
     42 
     43     axiom_node_t *payload = NULL;
     44 
     45     axiom_node_t *ret_node = NULL;
     46 
     47 
     48 
     49     env = axutil_env_create_all("hello_client.log", AXIS2_LOG_LEVEL_TRACE);
     50 
     51 
     52 
     53     options = axis2_options_create(env);
     54 
     55 
     56 
     57     address = "http://localhost:9090/axis2/services/hello";
     58 
     59     if (argc > 1)
     60 
     61         address = argv[1];
     62 
     63     if (axutil_strcmp(address, "-h"== 0)
     64 
     65     {
     66 
     67         printf("Usage : %s [endpoint_url]\n", argv[0]);
     68 
     69         printf("use -h for help\n");
     70 
     71         return 0;
     72 
     73     }
     74 
     75     printf("Using endpoint : %s\n", address);
     76 
     77     endpoint_ref = axis2_endpoint_ref_create(env, address);
     78 
     79     axis2_options_set_to(options, env, endpoint_ref);
     80 
     81 
     82 
     83     client_home = AXIS2_GETENV("AXIS2C_HOME");
     84 
     85     if (!client_home && !strcmp(client_home, ""))
     86 
     87         client_home = "../..";
     88 
     89 
     90 
     91     svc_client = axis2_svc_client_create(env, client_home);
     92 
     93     if (!svc_client)
     94 
     95     {
     96 
     97         printf("Error creating service client\n");
     98 
     99         AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Stub invoke FAILED: Error code:"
    100 
    101                 " %d :: %s", env->error->error_number,
    102 
    103                 AXIS2_ERROR_GET_MESSAGE(env->error));
    104 
    105         return -1;
    106 
    107     }
    108 
    109 
    110 
    111     axis2_svc_client_set_options(svc_client, env, options);
    112 
    113 
    114 
    115     payload = build_om_request(env);
    116 
    117 
    118 
    119     ret_node = axis2_svc_client_send_receive(svc_client, env, payload);
    120 
    121 
    122 
    123     if (ret_node)
    124 
    125     {
    126 
    127         const axis2_char_t *greeting = process_om_response(env, ret_node);
    128 
    129         if (greeting)
    130 
    131             printf("\nReceived greeting: \"%s\" from service\n", greeting);
    132 
    133 
    134 
    135         axiom_node_free_tree(ret_node, env);
    136 
    137         ret_node = NULL;
    138 
    139     }
    140 
    141     else
    142 
    143     {
    144 
    145         AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Stub invoke FAILED: Error code:"
    146 
    147                 " %d :: %s", env->error->error_number,
    148 
    149                 AXIS2_ERROR_GET_MESSAGE(env->error));
    150 
    151         printf("hello client invoke FAILED!\n");
    152 
    153     }
    154 
    155 
    156 
    157     if (svc_client)
    158 
    159     {
    160 
    161         axis2_svc_client_free(svc_client, env);
    162 
    163         svc_client = NULL;
    164 
    165     }
    166 
    167 
    168 
    169     if (env)
    170 
    171     {
    172 
    173         axutil_env_free((axutil_env_t *) env);
    174 
    175         env = NULL;
    176 
    177     }
    178 
    179 
    180 
    181     return 0;
    182 
    183 }
    184 
    185 
    186 
    187 axiom_node_t *
    188 
    189 build_om_request(const axutil_env_t *env)
    190 
    191 {
    192 
    193     axiom_node_t* greet_om_node = NULL;
    194 
    195     axiom_element_t * greet_om_ele = NULL;
    196 
    197 
    198 
    199     greet_om_ele = axiom_element_create(env, NULL, "greet", NULL, &greet_om_node);
    200 
    201     axiom_element_set_text(greet_om_ele, env, "Hello Server!", greet_om_node);
    202 
    203 
    204 
    205     return greet_om_node;
    206 
    207 }
    208 
    209 
    210 
    211 const axis2_char_t *
    212 
    213 process_om_response(const axutil_env_t *env,
    214 
    215         axiom_node_t *node)
    216 
    217 {
    218 
    219     axiom_node_t *service_greeting_node = NULL;
    220 
    221     axiom_node_t *return_node = NULL;
    222 
    223 
    224 
    225     if (node)
    226 
    227     {
    228 
    229         service_greeting_node = axiom_node_get_first_child(node, env);
    230 
    231         if (service_greeting_node &&
    232 
    233                 axiom_node_get_node_type(service_greeting_node, env) == AXIOM_TEXT)
    234 
    235         {
    236 
    237             axiom_text_t *greeting = (axiom_text_t *)axiom_node_get_data_element(service_greeting_node, env);
    238 
    239             if (greeting && axiom_text_get_value(greeting , env))
    240 
    241             {
    242 
    243                 return axiom_text_get_value(greeting, env);
    244 
    245             }
    246 
    247         }
    248 
    249     }
    250 
    251     return NULL;
    252 
    253 }

     6. 编译连接如下:

    C:\VS8\VC\bin\cl.exe /nologo /"WIN32" /"_WINDOWS" /"_MBCS" /%AXIS2C_HOME%\\include /I C:\\VS8\\VC\\include /I C:\\VS8\\VC\\PlatformSDK\\Include /c hello.c

    C:\VS8\VC\bin\link.exe 
    /LIBPATH:%AXIS2C_HOME%\lib axutil.lib axiom.lib axis2_parser.lib axis2_engine.lib /LIBPATH:C:\VS8\VC\lib uuid.lib  /OUT:hello.exe *.obj

     生成可执行程序hello.exe

     7. 启动Axis2服务器,执行如下命令:

    C:\axis2c\bin\axis2_http_server.exe

     8. 运行hello.exe

     此时server端和client端即实现通信。

  • 相关阅读:
    C#后台去除字符串最后一个字符
    C#后台验证含0的正整数
    jQuery提交表单的几种方式
    C#后台获取当前时间并格式化
    获取EasyUI日期输入框的值
    jQuery将字符串转换为数字
    JQ和JS获取span标签的内容
    正则
    json 拖拽
    event事件对象
  • 原文地址:https://www.cnblogs.com/luweiseu/p/2098737.html
Copyright © 2011-2022 走看看