summaryrefslogtreecommitdiffstats
path: root/src/core/clientapi/callback_recv.c
blob: f45ed87a5d7fed4f69221418f0ff80caac0a5b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

#include "axis2_callback_recv.h"
#include <axis2_const.h>
#include <axutil_hash.h>

struct axis2_callback_recv
{
    /** base context struct */
    axis2_msg_recv_t *base;
    axis2_bool_t base_deep_copy;

    /** callback map */
    axutil_hash_t *callback_map;
    axutil_thread_mutex_t *mutex;
};

static axis2_status_t AXIS2_CALL axis2_callback_recv_receive(
    axis2_msg_recv_t * msg_recv,
    const axutil_env_t * env,
    axis2_msg_ctx_t * msg_ctx,
    void *callback_recv_param);

AXIS2_EXTERN axis2_callback_recv_t *AXIS2_CALL
axis2_callback_recv_create(
    const axutil_env_t * env)
{
    axis2_callback_recv_t *callback_recv = NULL;

    AXIS2_ENV_CHECK(env, NULL);

    callback_recv = AXIS2_MALLOC(env->allocator, sizeof(axis2_callback_recv_t));

    if(!callback_recv)
    {
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "No memory. Cannot create callback receive.");
        return NULL;
    }

    callback_recv->base = NULL;
    callback_recv->base_deep_copy = AXIS2_TRUE;
    callback_recv->callback_map = NULL;
    callback_recv->mutex = NULL;

    callback_recv->base = axis2_msg_recv_create(env);
    if(!callback_recv->base)
    {
        axis2_callback_recv_free(callback_recv, env);
        return NULL;
    }
    axis2_msg_recv_set_derived(callback_recv->base, env, callback_recv);
    axis2_msg_recv_set_receive(callback_recv->base, env, axis2_callback_recv_receive);

    callback_recv->callback_map = axutil_hash_make(env);
    if(!callback_recv->callback_map)
    {
        axis2_callback_recv_free(callback_recv, env);
        return NULL;
    }

    callback_recv->mutex = axutil_thread_mutex_create(env->allocator, AXIS2_THREAD_MUTEX_DEFAULT);
    return callback_recv;
}

AXIS2_EXTERN axis2_msg_recv_t *AXIS2_CALL
axis2_callback_recv_get_base(
    axis2_callback_recv_t * callback_recv,
    const axutil_env_t * env)
{
    callback_recv->base_deep_copy = AXIS2_FALSE;
    return callback_recv->base;
}

AXIS2_EXTERN void AXIS2_CALL
axis2_callback_recv_free(
    axis2_callback_recv_t * callback_recv,
    const axutil_env_t * env)
{
    if(callback_recv->mutex)
    {
        axutil_thread_mutex_destroy(callback_recv->mutex);
    }

    if(callback_recv->callback_map)
    {
        axutil_hash_index_t *hi = NULL;
        const void *key = NULL;
        void *val = NULL;
        for(hi = axutil_hash_first(callback_recv->callback_map, env); hi; hi = axutil_hash_next(
            env, hi))
        {
            axutil_hash_this(hi, &key, NULL, &val);
            if(key)
            {
                AXIS2_FREE(env->allocator, (char *)key);
            }
            if(val)
            {
                axis2_callback_t *callback = (axis2_callback_t *) val;
                axis2_callback_free(callback, env);
            }

        }

        axutil_hash_free(callback_recv->callback_map, env);
    }

    if(callback_recv->base && callback_recv->base_deep_copy)
    {
        axis2_msg_recv_free(callback_recv->base, env);
    }

    if(callback_recv)
    {
        AXIS2_FREE(env->allocator, callback_recv);
    }
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_callback_recv_add_callback(
    axis2_callback_recv_t * callback_recv,
    const axutil_env_t * env,
    const axis2_char_t * msg_id,
    axis2_callback_t * callback)
{
    if(msg_id)
    {
        axis2_char_t *mid = axutil_strdup(env, msg_id);
        axutil_hash_set(callback_recv->callback_map, mid, AXIS2_HASH_KEY_STRING, callback);
    }
    return AXIS2_SUCCESS;
}

/* In the dual channel invocations client set a callback function to be invoked when a resonse
 * is received from the server. When the response is received by the listening port of the
 * listener manager, in the engine receive call the message is finally hit by callback receiver
 * which is an implementation of the axis2 message receiver. This is the function that is called
 * at that stage. The client set callback function is called from within here.
 */
static axis2_status_t AXIS2_CALL
axis2_callback_recv_receive(
    axis2_msg_recv_t * msg_recv,
    const axutil_env_t * env,
    axis2_msg_ctx_t * msg_ctx,
    void *callback_recv_param)
{
    axis2_callback_recv_t *callback_recv = NULL;
    axis2_relates_to_t *relates_to = NULL;
    axis2_msg_info_headers_t *msg_info_headers = NULL;

    callback_recv = axis2_msg_recv_get_derived(msg_recv, env);

    msg_info_headers = axis2_msg_ctx_get_msg_info_headers(msg_ctx, env);
    if(msg_info_headers)
    {
        relates_to = axis2_msg_info_headers_get_relates_to(msg_info_headers, env);
        if(relates_to)
        {
            const axis2_char_t *msg_id = axis2_relates_to_get_value(relates_to, env);
            if(msg_id)
            {
                axis2_async_result_t *result = NULL;
                axis2_callback_t *callback = (axis2_callback_t *)axutil_hash_get(
                    callback_recv->callback_map, msg_id, AXIS2_HASH_KEY_STRING);

                result = axis2_async_result_create(env, msg_ctx);
                if(callback && result)
                {
                    axis2_callback_invoke_on_complete(callback, env, result);
                    axis2_callback_set_complete(callback, env, AXIS2_TRUE);
                    axis2_msg_ctx_set_soap_envelope(msg_ctx, env, NULL);
                }

                axis2_async_result_free(result, env);
                if(callback && result)
                    return AXIS2_SUCCESS;
            }
        }
    }

    return AXIS2_FAILURE;
}