summaryrefslogtreecommitdiffstats
path: root/src/core/clientapi/callback.c
blob: 6f875df1be31ccfbb720baebd594eec7788a5451 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * 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.h>
#include <axis2_const.h>
#include <axutil_hash.h>

struct axis2_callback
{

    /** callback complete? */
    axis2_bool_t complete;

    /** envelope corresponding to the result */
    axiom_soap_envelope_t *envelope;

    /** message context corresponding to the result */
    axis2_msg_ctx_t *msg_ctx;

    /** error code */
    int error;

    /** to store callback specific data */
    void *data;
    axutil_thread_mutex_t *mutex;

    /**
     * This function is called by invoke_on_complete.
     * Users could provide this method so that they can define what to be 
     * done when the callback returns on completion.
     * @param callback pointer to callback struct
     * @param env pointer to environment struct
     * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE
     */
    axis2_status_t
    (
        AXIS2_CALL * on_complete)
    (
        axis2_callback_t * callback,
        const axutil_env_t * env);

    /**
     * This function is called by report_error.
     * Users could provide this method so that they can define what to be done
     * when the callback returns an error.
     * @param callback pointer to callback struct
     * @param env pointer to environment struct
     * @param exception error code representing the error
     * @return AXIS2_SUCCESS on success, else AXIS2_FAILURE
     */
    axis2_status_t
    (
        AXIS2_CALL * on_error)
( axis2_callback_t * callback,
const axutil_env_t * env,
const int exception);
};

static axis2_status_t AXIS2_CALL axis2_callback_on_complete(
    axis2_callback_t * callback,
    const axutil_env_t * env);

static axis2_status_t AXIS2_CALL axis2_callback_on_error(
    axis2_callback_t * callback,
    const axutil_env_t * env,
    int exception);

AXIS2_EXTERN axis2_callback_t *AXIS2_CALL
axis2_callback_create(
    const axutil_env_t * env)
{
    axis2_callback_t *callback = NULL;

    AXIS2_ENV_CHECK(env, NULL);

    callback = AXIS2_MALLOC(env->allocator, sizeof(axis2_callback_t));
    if(!callback)
    {
        AXIS2_ERROR_SET(env->error, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "No memory. Cannot create callback.");
        return NULL;
    }

    callback->complete = AXIS2_FALSE;
    callback->envelope = NULL;
    callback->msg_ctx = NULL;
    callback->error = AXIS2_ERROR_NONE;
    callback->data = NULL;
    callback->mutex = NULL;
    callback->on_complete = axis2_callback_on_complete;
    callback->on_error = axis2_callback_on_error;

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

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_callback_invoke_on_complete(
    axis2_callback_t * callback,
    const axutil_env_t * env,
    axis2_async_result_t * result)
{
    axis2_status_t status = AXIS2_FAILURE;

    callback->envelope = axis2_async_result_get_envelope(result, env);
    callback->msg_ctx = axis2_async_result_get_result(result, env);
    axis2_msg_ctx_increment_ref(callback->msg_ctx, env); /* this will be set in opclient's msgctx
                                                            map and will be deleted from there */

    status = callback->on_complete(callback, env);

    return status;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_callback_report_error(
    axis2_callback_t * callback,
    const axutil_env_t * env,
    const int exception)
{
    axis2_callback_set_error(callback, env, exception);
    return callback->on_error(callback, env, exception);
}

AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axis2_callback_get_complete(
    const axis2_callback_t * callback,
    const axutil_env_t * env)
{
    return callback->complete;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_callback_set_complete(
    axis2_callback_t * callback,
    const axutil_env_t * env,
    const axis2_bool_t complete)
{
    callback->complete = complete;
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axiom_soap_envelope_t *AXIS2_CALL
axis2_callback_get_envelope(
    const axis2_callback_t * callback,
    const axutil_env_t * env)
{
    return callback->envelope;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_callback_set_envelope(
    axis2_callback_t * callback,
    const axutil_env_t * env,
    axiom_soap_envelope_t * envelope)
{
    callback->envelope = envelope;
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_msg_ctx_t *AXIS2_CALL
axis2_callback_get_msg_ctx(
    const axis2_callback_t * callback,
    const axutil_env_t * env)
{
    return callback->msg_ctx;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_callback_set_msg_ctx(
    axis2_callback_t * callback,
    const axutil_env_t * env,
    axis2_msg_ctx_t * msg_ctx)
{
    callback->msg_ctx = msg_ctx;
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN int AXIS2_CALL
axis2_callback_get_error(
    const axis2_callback_t * callback,
    const axutil_env_t * env)
{
    return callback->error;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_callback_set_error(
    axis2_callback_t * callback,
    const axutil_env_t * env,
    const int error)
{
    callback->error = error;
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN void AXIS2_CALL
axis2_callback_free(
    axis2_callback_t * callback,
    const axutil_env_t * env)
{
    if(callback->mutex)
    {
        axutil_thread_mutex_destroy(callback->mutex);
    }

    AXIS2_FREE(env->allocator, callback);
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_callback_set_data(
    axis2_callback_t * callback,
    void *data)
{
    callback->data = (void *)data;

    return AXIS2_SUCCESS;
}

AXIS2_EXTERN void *AXIS2_CALL
axis2_callback_get_data(
    const axis2_callback_t * callback)
{
    return callback->data;
}

AXIS2_EXTERN void AXIS2_CALL
axis2_callback_set_on_complete(
    axis2_callback_t * callback,
    axis2_on_complete_func_ptr func)
{
    callback->on_complete = func;
}

AXIS2_EXTERN void AXIS2_CALL
axis2_callback_set_on_error(
    axis2_callback_t * callback,
    axis2_on_error_func_ptr func)
{
    callback->on_error = func;
}

static axis2_status_t AXIS2_CALL
axis2_callback_on_complete(
    axis2_callback_t * callback,
    const axutil_env_t * env)
{
    return AXIS2_SUCCESS;
}

static axis2_status_t AXIS2_CALL
axis2_callback_on_error(
    axis2_callback_t * callback,
    const axutil_env_t * env,
    int exception)
{
    return AXIS2_SUCCESS;
}