1
/* $Id: ncbi_memory_connector.c,v 6.5 2003/05/31 05:15:39 lavr Exp $
2
* ===========================================================================
5
* National Center for Biotechnology Information
7
* This software/database is a "United States Government Work" under the
8
* terms of the United States Copyright Act. It was written as part of
9
* the author's official duties as a United States Government employee and
10
* thus cannot be copyrighted. This software/database is freely available
11
* to the public for use. The National Library of Medicine and the U.S.
12
* Government have not placed any restriction on its use or reproduction.
14
* Although all reasonable efforts have been taken to ensure the accuracy
15
* and reliability of the software and data, the NLM and the U.S.
16
* Government do not and cannot warrant the performance or results that
17
* may be obtained by using this software or data. The NLM and the U.S.
18
* Government disclaim all warranties, express or implied, including
19
* warranties of performance, merchantability or fitness for any particular
22
* Please cite the author in any work or product based on this material.
24
* ===========================================================================
26
* Author: Anton Lavrentiev
31
* See <connect/ncbi_connector.h> for the detailed specification of
32
* the connector's methods and structures.
36
#include <connect/ncbi_buffer.h>
37
#include <connect/ncbi_memory_connector.h>
41
/***********************************************************************
42
* INTERNAL -- Auxiliary types and static functions
43
***********************************************************************/
45
/* All internal data necessary to perform the (re)connect and i/o
55
/***********************************************************************
56
* INTERNAL -- "s_VT_*" functions for the "virt. table" of connector methods
57
***********************************************************************/
61
#endif /* __cplusplus */
62
static const char* s_VT_GetType (CONNECTOR connector);
63
static EIO_Status s_VT_Open (CONNECTOR connector,
64
const STimeout* timeout);
65
static EIO_Status s_VT_Wait (CONNECTOR connector,
67
const STimeout* timeout);
68
static EIO_Status s_VT_Write (CONNECTOR connector,
72
const STimeout* timeout);
73
static EIO_Status s_VT_Read (CONNECTOR connector,
77
const STimeout* timeout);
78
static EIO_Status s_VT_Status (CONNECTOR connector,
80
static EIO_Status s_VT_Close (CONNECTOR connector,
81
const STimeout* timeout);
82
static void s_Setup (SMetaConnector* meta,
84
static void s_Destroy (CONNECTOR connector);
85
# ifdef IMPLEMENTED__CONN_WaitAsync
86
static EIO_Status s_VT_WaitAsync(void* connector,
87
FConnectorAsyncHandler func,
88
SConnectorAsyncHandler* data);
92
#endif /* __cplusplus */
96
static const char* s_VT_GetType
104
static EIO_Status s_VT_Open
105
(CONNECTOR connector,
106
const STimeout* timeout)
108
SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
110
xxx->r_status = eIO_Success;
111
xxx->w_status = eIO_Success;
117
static EIO_Status s_VT_Write
118
(CONNECTOR connector,
122
const STimeout* timeout)
124
SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
130
MT_LOCK_Do(xxx->lock, eMT_Lock);
131
written = BUF_Write(&xxx->buf, buf, size);
132
MT_LOCK_Do(xxx->lock, eMT_Unlock);
134
xxx->w_status = eIO_Unknown;
139
xxx->w_status = eIO_Success;
145
static EIO_Status s_VT_Read
146
(CONNECTOR connector,
150
const STimeout* timeout)
152
SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
157
MT_LOCK_Do(xxx->lock, eMT_Lock);
158
*n_read = BUF_Read(xxx->buf, buf, size);
159
MT_LOCK_Do(xxx->lock, eMT_Unlock);
161
xxx->r_status = eIO_Closed;
164
xxx->r_status = eIO_Success;
171
static EIO_Status s_VT_Wait
172
(CONNECTOR connector,
174
const STimeout* timeout)
180
static EIO_Status s_VT_Status
181
(CONNECTOR connector,
184
SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
188
return xxx->r_status;
190
return xxx->w_status;
192
assert(0); /* should never happen as checked by connection */
193
return eIO_InvalidArg;
199
static EIO_Status s_VT_Close
200
(CONNECTOR connector,
201
const STimeout* timeout)
203
SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
204
BUF_Destroy(xxx->buf);
210
(SMetaConnector* meta,
213
/* initialize virtual table */
214
CONN_SET_METHOD(meta, get_type, s_VT_GetType, connector);
215
CONN_SET_METHOD(meta, open, s_VT_Open, connector);
216
CONN_SET_METHOD(meta, wait, s_VT_Wait, connector);
217
CONN_SET_METHOD(meta, write, s_VT_Write, connector);
218
CONN_SET_METHOD(meta, flush, 0, 0);
219
CONN_SET_METHOD(meta, read, s_VT_Read, connector);
220
CONN_SET_METHOD(meta, status, s_VT_Status, connector);
221
CONN_SET_METHOD(meta, close, s_VT_Close, connector);
222
#ifdef IMPLEMENTED__CONN_WaitAsync
223
CONN_SET_METHOD(meta, wait_async, s_VT_WaitAsync, connector);
225
meta->default_timeout = 0/*infinite*/;
229
static void s_Destroy
230
(CONNECTOR connector)
232
SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
235
connector->handle = 0;
240
/***********************************************************************
241
* EXTERNAL -- the connector's "constructors"
242
***********************************************************************/
244
extern CONNECTOR MEMORY_CreateConnector(MT_LOCK lock)
246
CONNECTOR ccc = (SConnector*) malloc(sizeof(SConnector));
247
SMemoryConnector* xxx = (SMemoryConnector*) malloc(sizeof(*xxx));
249
/* initialize internal data structures */
252
/* initialize connector data */
256
ccc->setup = s_Setup;
257
ccc->destroy = s_Destroy;
264
* --------------------------------------------------------------------------
265
* $Log: ncbi_memory_connector.c,v $
266
* Revision 6.5 2003/05/31 05:15:39 lavr
267
* Add ARGSUSED where args are meant to be unused, remove Flush
269
* Revision 6.4 2003/05/14 03:53:13 lavr
272
* Revision 6.3 2002/10/22 15:11:24 lavr
273
* Zero connector's handle to crash if revisited
275
* Revision 6.2 2002/04/26 16:32:49 lavr
276
* Added setting of default timeout in meta-connector's setup routine
278
* Revision 6.1 2002/02/20 19:14:08 lavr
281
* ==========================================================================