~ubuntu-branches/ubuntu/maverick/ncbi-tools6/maverick

« back to all changes in this revision

Viewing changes to connect/test/test_ncbi_service_connector.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2005-03-27 12:00:15 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050327120015-embhesp32nj73p9r
Tags: 6.1.20041020-3
* Fix FTBFS under GCC 4.0 caused by inconsistent use of "static" on
  functions.  (Closes: #295110.)
* Add a watch file, now that we can.  (Upstream's layout needs version=3.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Id: test_ncbi_service_connector.c,v 6.15 2001/09/24 20:36:22 lavr Exp $
 
1
/*  $Id: test_ncbi_service_connector.c,v 6.29 2004/02/23 15:23:43 lavr Exp $
2
2
 * ===========================================================================
3
3
 *
4
4
 *                            PUBLIC DOMAIN NOTICE
28
28
 * File Description:
29
29
 *   Standard test for the service connector
30
30
 *
 
31
 */
 
32
 
 
33
#include "../ncbi_ansi_ext.h"
 
34
#include "../ncbi_priv.h"
 
35
#include <connect/ncbi_service_connector.h>
 
36
#include <stdlib.h>
 
37
/* This header must go last */
 
38
#include "test_assert.h"
 
39
 
 
40
 
 
41
int main(int argc, const char* argv[])
 
42
{
 
43
    static char obuf[8192 + 2] = "UUUUUZZZZZZUUUUUUZUZUZZUZUZUZUZUZ\n";
 
44
    const char* service = argc > 1 && *argv[1] ? argv[1] : "bounce";
 
45
    const char* host = argc > 2 && *argv[2] ? argv[2] : "www.ncbi.nlm.nih.gov";
 
46
    SConnNetInfo *net_info;
 
47
    CONNECTOR connector;
 
48
    EIO_Status status;
 
49
    char ibuf[1024];
 
50
    CONN conn;
 
51
    size_t n;
 
52
 
 
53
    CORE_SetLOGFormatFlags(fLOG_Full | fLOG_DateTime);
 
54
    CORE_SetLOGFILE(stderr, 0/*false*/);
 
55
 
 
56
    net_info = ConnNetInfo_Create(service);
 
57
    strcpy(net_info->host, host);
 
58
    if (argc > 3) {
 
59
        strncpy0(obuf, argv[3], sizeof(obuf) - 2);
 
60
        obuf[n = strlen(obuf)] = '\n';
 
61
        obuf[++n]              = 0;
 
62
    }
 
63
    strcpy(net_info->args, "testarg=testval&service=none");
 
64
 
 
65
    connector = SERVICE_CreateConnectorEx(service, fSERV_Any, net_info, 0);
 
66
    ConnNetInfo_Destroy(net_info);
 
67
 
 
68
    if (!connector)
 
69
        CORE_LOG(eLOG_Fatal, "Failed to create service connector");
 
70
 
 
71
    if (CONN_Create(connector, &conn) != eIO_Success)
 
72
        CORE_LOG(eLOG_Fatal, "Failed to create connection");
 
73
 
 
74
#if 0
 
75
    for (n = 0; n < 10; n++) {
 
76
        int m;
 
77
        for (m = 0; m < sizeof(obuf) - 2; m++)
 
78
            obuf[m] = "01234567890\n"[rand() % 12];
 
79
        obuf[m++] = '\n';
 
80
        obuf[m]   = '\0';
 
81
 
 
82
        if (CONN_Write(conn, obuf, strlen(obuf), &m, eIO_WritePersist)
 
83
            != eIO_Success) {
 
84
            if (!n) {
 
85
                CONN_Close(conn);
 
86
                CORE_LOG(eLOG_Fatal, "Error writing to connection");
 
87
            } else
 
88
                break;
 
89
        }
 
90
        assert(m == strlen(obuf));
 
91
    }
 
92
#else
 
93
    if (CONN_Write(conn, obuf, strlen(obuf), &n, eIO_WritePersist)
 
94
        != eIO_Success) {
 
95
        CONN_Close(conn);
 
96
        CORE_LOG(eLOG_Fatal, "Error writing to connection");
 
97
    }
 
98
    assert(n == strlen(obuf));
 
99
#endif
 
100
 
 
101
    for (;;) {
 
102
        STimeout timeout;
 
103
        timeout.sec  = 5;
 
104
        timeout.usec = 12345;
 
105
        if (CONN_Wait(conn, eIO_Read, &timeout) != eIO_Success) {
 
106
            CONN_Close(conn);
 
107
            CORE_LOG(eLOG_Fatal, "Error waiting for reading");
 
108
        }
 
109
 
 
110
        status = CONN_Read(conn, ibuf, sizeof(ibuf), &n, eIO_ReadPersist);
 
111
        if (n) {
 
112
            char* descr = CONN_Description(conn);
 
113
            CORE_DATAF(ibuf, n, ("%lu bytes read from service (%s%s%s):",
 
114
                                 (unsigned long) n, CONN_GetType(conn),
 
115
                                 descr ? ", " : "", descr ? descr : ""));
 
116
            if (descr)
 
117
                free(descr);
 
118
        }
 
119
        if (status != eIO_Success) {
 
120
            if (status != eIO_Closed)
 
121
                CORE_LOGF(n ? eLOG_Error : eLOG_Fatal,
 
122
                          ("Read error: %s", IO_StatusStr(status)));
 
123
            break;
 
124
        }
 
125
    }
 
126
    CONN_Close(conn);
 
127
 
 
128
#if 0
 
129
    CORE_LOG(eLOG_Note, "Trying ID1 service");
 
130
 
 
131
    net_info = ConnNetInfo_Create(service);
 
132
    connector = SERVICE_CreateConnectorEx("ID1", fSERV_Any, net_info);
 
133
    ConnNetInfo_Destroy(net_info);
 
134
 
 
135
    if (!connector)
 
136
        CORE_LOG(eLOG_Fatal, "Service ID1 not available");
 
137
 
 
138
    if (CONN_Create(connector, &conn) != eIO_Success)
 
139
        CORE_LOG(eLOG_Fatal, "Failed to create connection");
 
140
 
 
141
    if (CONN_Write(conn, "\xA4\x80\x02\x01\x02\x00", 7, &n, eIO_WritePersist)
 
142
        != eIO_Success) {
 
143
        CONN_Close(conn);
 
144
        CORE_LOG(eLOG_Fatal, "Error writing to service ID1");
 
145
    }
 
146
    assert(n == 7);
 
147
 
 
148
    if (CONN_Read(conn, ibuf, sizeof(ibuf), &n, eIO_ReadPlain) != eIO_Success){
 
149
        CONN_Close(conn);
 
150
        CORE_LOG(eLOG_Fatal, "Error reading from service ID1");
 
151
    }
 
152
 
 
153
    CORE_LOGF(eLOG_Note, ("%d bytes read from service ID1", n));
 
154
    CONN_Close(conn);
 
155
#endif
 
156
 
 
157
    CORE_SetLOG(0);
 
158
    return 0/*okay*/;
 
159
}
 
160
 
 
161
 
 
162
/*
31
163
 * --------------------------------------------------------------------------
32
164
 * $Log: test_ncbi_service_connector.c,v $
 
165
 * Revision 6.29  2004/02/23 15:23:43  lavr
 
166
 * New (last) parameter "how" added in CONN_Write() API call
 
167
 *
 
168
 * Revision 6.28  2003/10/21 11:37:47  lavr
 
169
 * Set write timeout from the environment/registry instead of explicitly
 
170
 *
 
171
 * Revision 6.27  2003/07/24 16:38:59  lavr
 
172
 * Add conditional check for early connection drops (#if 0'd)
 
173
 *
 
174
 * Revision 6.26  2003/05/29 18:03:49  lavr
 
175
 * Extend read error message with a reason (if any)
 
176
 *
 
177
 * Revision 6.25  2003/05/14 03:58:43  lavr
 
178
 * Match changes in respective APIs of the tests
 
179
 *
 
180
 * Revision 6.24  2003/05/05 20:31:23  lavr
 
181
 * Add date/time stamp to each log message printed
 
182
 *
 
183
 * Revision 6.23  2003/04/04 21:01:06  lavr
 
184
 * Modify readout procedure
 
185
 *
 
186
 * Revision 6.22  2002/10/28 15:47:12  lavr
 
187
 * Use "ncbi_ansi_ext.h" privately and use strncpy0()
 
188
 *
 
189
 * Revision 6.21  2002/09/24 15:10:09  lavr
 
190
 * Fix test not to dereference NULL pointer resulting from failed connection
 
191
 *
 
192
 * Revision 6.20  2002/08/07 16:38:08  lavr
 
193
 * EIO_ReadMethod enums changed accordingly; log moved to end
 
194
 *
 
195
 * Revision 6.19  2002/03/22 19:47:41  lavr
 
196
 * Test_assert.h made last among the include files
 
197
 *
 
198
 * Revision 6.18  2002/03/21 22:02:16  lavr
 
199
 * Change default server from "ray" into "www.ncbi.nlm.nih.gov"
 
200
 *
 
201
 * Revision 6.17  2002/02/05 21:45:55  lavr
 
202
 * Included header files rearranged
 
203
 *
 
204
 * Revision 6.16  2002/01/16 21:23:15  vakatov
 
205
 * Utilize header "test_assert.h" to switch on ASSERTs in the Release mode too
 
206
 *
33
207
 * Revision 6.15  2001/09/24 20:36:22  lavr
34
208
 * Adjusted parameters in SERVICE_CreateConnectorEx()
35
209
 *
77
251
 *
78
252
 * ==========================================================================
79
253
 */
80
 
 
81
 
#if defined(NDEBUG)
82
 
#  undef NDEBUG
83
 
#endif 
84
 
 
85
 
#include "../ncbi_priv.h"
86
 
#include <connect/ncbi_util.h>
87
 
#include <connect/ncbi_service_connector.h>
88
 
#include <assert.h>
89
 
#include <stdio.h>
90
 
#include <stdlib.h>
91
 
#include <string.h>
92
 
 
93
 
int main(int argc, const char* argv[])
94
 
{
95
 
    static char obuf[128] = "UUUUUZZZZZZUUUUUUZUZUZZUZUZUZUZUZ\n";
96
 
    const char* service = argc > 1 ? argv[1] : "bounce";
97
 
    const char* host = argc > 2 ? argv[2] : "ray";
98
 
    CONNECTOR connector;
99
 
    SConnNetInfo *info;
100
 
    STimeout  timeout;
101
 
    char ibuf[1024];
102
 
    CONN conn;
103
 
    size_t n;
104
 
 
105
 
    CORE_SetLOGFILE(stderr, 0/*false*/);
106
 
 
107
 
    info = ConnNetInfo_Create(service);
108
 
    strcpy(info->host, host);
109
 
    if (argc > 3) {
110
 
        strncpy(obuf, argv[3], sizeof(obuf) - 2);
111
 
        obuf[sizeof(obuf) - 2] = 0;
112
 
        obuf[n = strlen(obuf)] = '\n';
113
 
        obuf[++n]              = 0;
114
 
    }
115
 
 
116
 
    connector = SERVICE_CreateConnectorEx(service, fSERV_Any, info, 0);
117
 
    ConnNetInfo_Destroy(info);
118
 
 
119
 
    if (!connector)
120
 
        CORE_LOG(eLOG_Fatal, "Failed to create service connector");
121
 
 
122
 
    if (CONN_Create(connector, &conn) != eIO_Success)
123
 
        CORE_LOG(eLOG_Fatal, "Failed to create connection");
124
 
 
125
 
    timeout.sec  = 5;
126
 
    timeout.usec = 123456;
127
 
 
128
 
    CONN_SetTimeout(conn, eIO_ReadWrite, &timeout);
129
 
 
130
 
    if (CONN_Write(conn, obuf, strlen(obuf), &n) != eIO_Success ||
131
 
        n != strlen(obuf)) {
132
 
        CONN_Close(conn);
133
 
        CORE_LOG(eLOG_Fatal, "Error writing to connection");
134
 
    }
135
 
 
136
 
    if (CONN_Wait(conn, eIO_Read, &timeout) != eIO_Success) {
137
 
        CONN_Close(conn);
138
 
        CORE_LOG(eLOG_Fatal, "Error waiting for reading");
139
 
    }
140
 
 
141
 
    if (CONN_Read(conn, ibuf, n, &n, eIO_Persist) != eIO_Success) {
142
 
        CONN_Close(conn);
143
 
        CORE_LOG(n ? eLOG_Error : eLOG_Fatal, "Error reading from connection");
144
 
    }
145
 
 
146
 
    CORE_LOGF(eLOG_Note,
147
 
              ("%d bytes read from service (%s):\n%.*s",
148
 
               (int)n, CONN_GetType(conn), (int)n, ibuf));
149
 
    CONN_Close(conn);
150
 
 
151
 
#if 0
152
 
    CORE_LOG(eLOG_Note, "Trying ID1 service");
153
 
 
154
 
    info = ConnNetInfo_Create(service);
155
 
    connector = SERVICE_CreateConnectorEx("ID1", fSERV_Any, info);
156
 
    ConnNetInfo_Destroy(info);
157
 
 
158
 
    if (!connector)
159
 
        CORE_LOG(eLOG_Fatal, "Service ID1 not available");
160
 
 
161
 
    if (CONN_Create(connector, &conn) != eIO_Success)
162
 
        CORE_LOG(eLOG_Fatal, "Failed to create connection");
163
 
 
164
 
    if (CONN_Write(conn, "\xA4\x80\x02\x01\x02\x00\x00", 7, &n) !=
165
 
        eIO_Success || n != 7) {
166
 
        CONN_Close(conn);
167
 
        CORE_LOG(eLOG_Fatal, "Error writing to service ID1");
168
 
    }
169
 
 
170
 
    if (CONN_Read(conn, ibuf, sizeof(ibuf), &n, eIO_Plain) != eIO_Success) {
171
 
        CONN_Close(conn);
172
 
        CORE_LOG(eLOG_Fatal, "Error reading from service ID1");
173
 
    }
174
 
 
175
 
    CORE_LOGF(eLOG_Note, ("%d bytes read from service ID1", n));
176
 
    CONN_Close(conn);
177
 
#endif
178
 
 
179
 
    return 0/*okay*/;
180
 
}