File: globus_xio_wrapblock.c

package info (click to toggle)
globus-xio 2.8-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,320 kB
  • ctags: 2,912
  • sloc: ansic: 33,944; sh: 8,579; makefile: 448
file content (441 lines) | stat: -rw-r--r-- 13,457 bytes parent folder | download | duplicates (2)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * Portions of this file Copyright 1999-2005 University of Chicago
 * Portions of this file Copyright 1999-2005 The University of Southern California.
 *
 * This file or a portion of this file is licensed under the
 * terms of the Globus Toolkit Public License, found at
 * http://www.globus.org/toolkit/download/license.html.
 * If you redistribute this file, with or without
 * modifications, you must include this notice in the file.
 */

#include "globus_i_xio.h"
#include "globus_xio_driver.h"
#include "globus_xio.h"
#include "globus_xio_load.h"
#include "globus_common.h"
#include "globus_xio_wrapblock.h"
#include "version.h"

typedef struct xio_l_wrapblock_handle_s
{
    void *                              driver_handle;
    struct xio_l_wrapblock_driver_s *   wrapblock_driver;
} xio_l_wrapblock_handle_t;

typedef struct xio_l_wrapblock_wrapper_s
{
    globus_xio_contact_t                ci;
    struct xio_l_wrapblock_driver_s *   wrapblock_driver;
    xio_l_wrapblock_handle_t *          wrapblock_handle;
    void *                              attr;
    void *                              link;
    globus_xio_iovec_t *                iovec;
    int                                 iovec_count;
    globus_xio_operation_t              op;
} xio_l_wrapblock_wrapper_t;

typedef struct xio_l_wrapblock_driver_s
{
    globus_xio_wrapblock_open_func_t    open_func;
    globus_xio_wrapblock_close_func_t   close_func;
    globus_xio_wrapblock_read_func_t    read_func;
    globus_xio_wrapblock_write_func_t   write_func;
    globus_xio_wrapblock_accept_func_t  accept_func;
} xio_l_wrapblock_driver_t;

static
void
xio_l_wrapblock_wrapper_destroy(
    xio_l_wrapblock_wrapper_t *               wrapper)
{
    globus_xio_contact_destroy(&wrapper->ci);
    if(wrapper->iovec != NULL)
    {
        globus_free(wrapper->iovec);
    }
    globus_free(wrapper);
}
/************************************************************************
 *                  iface functions
 *                  ---------------
 *  
 ***********************************************************************/


static
void
globus_l_xio_wrapblock_accept_kickout(
    void *                              user_arg)
{
    void *                              link;
    globus_result_t                     res;
    xio_l_wrapblock_wrapper_t *         wrapper;

    globus_thread_blocking_will_block();

    wrapper = (xio_l_wrapblock_wrapper_t *) user_arg;

    res = wrapper->wrapblock_driver->accept_func(
        wrapper->attr,
        &link);

    globus_xio_driver_finished_accept(
        wrapper->op, link, res);

    xio_l_wrapblock_wrapper_destroy(wrapper);
}

static
globus_result_t
globus_l_xio_wrapblock_server_accept(
    void *                              driver_server,
    globus_xio_operation_t              op)
{
    xio_l_wrapblock_wrapper_t *         wrapper;
    globus_xio_driver_t                 driver;
    globus_i_xio_op_t *                 xio_op;

    xio_op = (globus_i_xio_op_t *) op;
    driver = xio_op->_op_server->entry[op->ndx - 1].driver;

    wrapper = (xio_l_wrapblock_wrapper_t *)
        globus_calloc(1, sizeof(xio_l_wrapblock_wrapper_t));
    wrapper->wrapblock_driver = driver->wrap_data;
    wrapper->attr = driver_server;
    wrapper->op = op;

    globus_callback_register_oneshot(
        NULL,
        NULL,
        globus_l_xio_wrapblock_accept_kickout,
        wrapper);

    return GLOBUS_SUCCESS;
}

static
void
globus_l_xio_wrapblock_open_kickout(
    void *                              user_arg)
{
    globus_result_t                     res;
    xio_l_wrapblock_wrapper_t *         wrapper;

    globus_thread_blocking_will_block();

    wrapper = (xio_l_wrapblock_wrapper_t *) user_arg;

    res = wrapper->wrapblock_handle->wrapblock_driver->open_func(
        &wrapper->ci,
        wrapper->link,
        wrapper->attr,
        &wrapper->wrapblock_handle->driver_handle);

    globus_xio_driver_finished_open(
        wrapper->wrapblock_handle, wrapper->op, res);

    xio_l_wrapblock_wrapper_destroy(wrapper);
}

static
globus_result_t
globus_l_xio_wrapblock_open(
    const globus_xio_contact_t *        contact_info,
    void *                              driver_link,
    void *                              driver_attr,
    globus_xio_operation_t              op)
{
    globus_result_t                     result;
    xio_l_wrapblock_wrapper_t *         wrapper;
    xio_l_wrapblock_handle_t *          wrapblock_handle;
    globus_xio_driver_t                 driver;
    globus_i_xio_op_t *                 xio_op;

    xio_op = (globus_i_xio_op_t *) op;
    driver = xio_op->_op_context->entry[op->ndx - 1].driver;

    wrapblock_handle = globus_calloc(1, sizeof(xio_l_wrapblock_handle_t));
    wrapblock_handle->wrapblock_driver = driver->wrap_data;

    if(globus_xio_driver_operation_is_blocking(op))
    {
        result = wrapblock_handle->wrapblock_driver->open_func(
            contact_info,
            driver_link,
            driver_attr,
            &wrapblock_handle->driver_handle);

        globus_xio_driver_finished_open(wrapblock_handle, op, result);
    }
    else
    {
        wrapper = (xio_l_wrapblock_wrapper_t *)
            globus_calloc(1, sizeof(xio_l_wrapblock_wrapper_t));
        wrapper->wrapblock_handle = wrapblock_handle;
        wrapper->link = driver_link;
        wrapper->attr = driver_attr;
        wrapper->op = op;
        /* gotta copy contact info the hard way */
        globus_xio_contact_copy(&wrapper->ci, contact_info);

        globus_callback_register_oneshot(
            NULL,
            NULL,
            globus_l_xio_wrapblock_open_kickout,
            wrapper);
    }

    return GLOBUS_SUCCESS;
}

static
void
globus_l_xio_wrapblock_write_kickout(
    void *                              user_arg)
{
    globus_size_t                       nbytes;
    globus_result_t                     result;
    xio_l_wrapblock_wrapper_t *         wrapper;

    globus_thread_blocking_will_block();

    wrapper = (xio_l_wrapblock_wrapper_t *) user_arg;

    result = wrapper->wrapblock_handle->wrapblock_driver->write_func(
        wrapper->wrapblock_handle->driver_handle,
        wrapper->iovec,
        wrapper->iovec_count,
        &nbytes);
    globus_xio_driver_finished_write(wrapper->op, result, nbytes);

    xio_l_wrapblock_wrapper_destroy(wrapper);
}

static
globus_result_t
globus_l_xio_wrapblock_write(
    void *                              driver_specific_handle,
    const globus_xio_iovec_t *          iovec,
    int                                 iovec_count,
    globus_xio_operation_t              op)
{
    globus_size_t                       nbytes;
    globus_result_t                     result;
    int                                 i;
    xio_l_wrapblock_wrapper_t *         wrapper;
    xio_l_wrapblock_handle_t *          wrapblock_handle;

    wrapblock_handle = (xio_l_wrapblock_handle_t *) driver_specific_handle;
    if(globus_xio_driver_operation_is_blocking(op))
    {
        result = wrapblock_handle->wrapblock_driver->write_func(
            wrapblock_handle->driver_handle,
            iovec,
            iovec_count,
            &nbytes);
        globus_xio_driver_finished_write(op, result, nbytes);
    }
    else
    {
        wrapper = (xio_l_wrapblock_wrapper_t *)
            globus_calloc(1, sizeof(xio_l_wrapblock_wrapper_t));
        wrapper->iovec = (globus_xio_iovec_t *)
            globus_calloc(iovec_count, sizeof(globus_xio_iovec_t));
        wrapper->iovec_count = iovec_count;
        wrapper->op = op;
        wrapper->wrapblock_handle = driver_specific_handle;

        for(i = 0; i < iovec_count; i++)
        {
            wrapper->iovec[i].iov_base = iovec[i].iov_base;
            wrapper->iovec[i].iov_len = iovec[i].iov_len;
        }

        globus_callback_register_oneshot(
            NULL,
            NULL,
            globus_l_xio_wrapblock_write_kickout,
            wrapper);
    }
    return GLOBUS_SUCCESS;
}

static
void
globus_l_xio_wrapblock_read_kickout(
    void *                              user_arg)
{
    globus_size_t                       nbytes;
    globus_result_t                     result;
    xio_l_wrapblock_wrapper_t *         wrapper;

    globus_thread_blocking_will_block();

    wrapper = (xio_l_wrapblock_wrapper_t *) user_arg;

    result = wrapper->wrapblock_handle->wrapblock_driver->read_func(
        wrapper->wrapblock_handle->driver_handle,
        wrapper->iovec,
        wrapper->iovec_count,
        &nbytes);
    globus_xio_driver_finished_read(wrapper->op, result, nbytes);

    xio_l_wrapblock_wrapper_destroy(wrapper);
}


static
globus_result_t
globus_l_xio_wrapblock_read(
    void *                              driver_specific_handle,
    const globus_xio_iovec_t *          iovec,
    int                                 iovec_count,
    globus_xio_operation_t              op)
{
    globus_size_t                       nbytes;
    globus_result_t                     result;
    int                                 i;
    xio_l_wrapblock_wrapper_t *         wrapper;
    xio_l_wrapblock_handle_t *          wrapblock_handle;

    wrapblock_handle = (xio_l_wrapblock_handle_t *) driver_specific_handle;
    if(globus_xio_driver_operation_is_blocking(op))
    {
        result = wrapblock_handle->wrapblock_driver->read_func(
            wrapblock_handle->driver_handle,
            iovec,
            iovec_count,
            &nbytes);
        globus_xio_driver_finished_read(op, result, nbytes);
    }
    else
    {
        wrapper = (xio_l_wrapblock_wrapper_t *)
            globus_calloc(1, sizeof(xio_l_wrapblock_wrapper_t));
        wrapper->iovec = (globus_xio_iovec_t *)
            globus_calloc(iovec_count, sizeof(globus_xio_iovec_t));
        wrapper->iovec_count = iovec_count;
        wrapper->op = op;
        wrapper->wrapblock_handle = driver_specific_handle;

        for(i = 0; i < iovec_count; i++)
        {
            wrapper->iovec[i].iov_base = iovec[i].iov_base;
            wrapper->iovec[i].iov_len = iovec[i].iov_len;
        }

        globus_callback_register_oneshot(
            NULL,
            NULL,
            globus_l_xio_wrapblock_read_kickout,
            wrapper);
    }
    return GLOBUS_SUCCESS;
}

static
void
globus_l_xio_wrapblock_close_kickout(
    void *                              user_arg)
{
    xio_l_wrapblock_handle_t *          wrapblock_handle;
    globus_result_t                     result;
    xio_l_wrapblock_wrapper_t *         wrapper;

    globus_thread_blocking_will_block();

    wrapper = (xio_l_wrapblock_wrapper_t *) user_arg;
    wrapblock_handle = wrapper->wrapblock_handle;

    result = wrapper->wrapblock_handle->wrapblock_driver->close_func(
        wrapblock_handle->driver_handle,
        wrapper->attr);
    globus_xio_driver_finished_close(wrapper->op, result);

    xio_l_wrapblock_wrapper_destroy(wrapper);
    globus_free(wrapblock_handle);
}

static
globus_result_t
globus_l_xio_wrapblock_close(
    void *                              driver_specific_handle,
    void *                              attr,
    globus_xio_operation_t              op)
{
    globus_result_t                     result;
    xio_l_wrapblock_wrapper_t *         wrapper;
    xio_l_wrapblock_handle_t *          wrapblock_handle;

    wrapblock_handle = (xio_l_wrapblock_handle_t *) driver_specific_handle;
    if(globus_xio_driver_operation_is_blocking(op))
    {
        result = wrapblock_handle->wrapblock_driver->close_func(
            wrapblock_handle->driver_handle,
            attr);
        globus_xio_driver_finished_close(op, result);
        globus_free(wrapblock_handle);
    }
    else
    {
        wrapper = (xio_l_wrapblock_wrapper_t *)
            globus_calloc(1, sizeof(xio_l_wrapblock_wrapper_t));
        wrapper->attr = attr;
        wrapper->op = op;
        wrapper->wrapblock_handle = driver_specific_handle;

        globus_callback_register_oneshot(
            NULL,
            NULL,
            globus_l_xio_wrapblock_close_kickout,
            wrapper);
    }
    return GLOBUS_SUCCESS;
}


globus_result_t
globus_xio_wrapblock_init(
    globus_xio_driver_t                 driver,
    globus_xio_wrapblock_open_func_t    open_func,
    globus_xio_wrapblock_close_func_t   close_func,
    globus_xio_wrapblock_read_func_t    read_func,
    globus_xio_wrapblock_write_func_t   write_func,
    globus_xio_wrapblock_accept_func_t  accept_func)
{
    xio_l_wrapblock_driver_t *          wrapblock_driver;

    if(open_func != NULL)
    {
        driver->transport_open_func = globus_l_xio_wrapblock_open;
    }
    if(close_func != NULL)
    {
        driver->close_func = globus_l_xio_wrapblock_close;
    }
    if(read_func != NULL)
    {
        driver->read_func = globus_l_xio_wrapblock_read;
    }
    if(write_func != NULL)
    {
        driver->write_func = globus_l_xio_wrapblock_write;
    }
    if(accept_func != NULL)
    {
        driver->server_accept_func = globus_l_xio_wrapblock_server_accept;
    }

    wrapblock_driver = (xio_l_wrapblock_driver_t *)
        globus_calloc(1, sizeof(xio_l_wrapblock_driver_t));
    wrapblock_driver->open_func = open_func;
    wrapblock_driver->close_func = close_func;
    wrapblock_driver->read_func = read_func;
    wrapblock_driver->write_func = write_func;
    wrapblock_driver->accept_func = accept_func;

    driver->wrap_data = wrapblock_driver;

    return GLOBUS_SUCCESS;
}