28
28
* File Description:
29
29
* Standard test for the service connector
33
#include "../ncbi_ansi_ext.h"
34
#include "../ncbi_priv.h"
35
#include <connect/ncbi_service_connector.h>
37
/* This header must go last */
38
#include "test_assert.h"
41
int main(int argc, const char* argv[])
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;
53
CORE_SetLOGFormatFlags(fLOG_Full | fLOG_DateTime);
54
CORE_SetLOGFILE(stderr, 0/*false*/);
56
net_info = ConnNetInfo_Create(service);
57
strcpy(net_info->host, host);
59
strncpy0(obuf, argv[3], sizeof(obuf) - 2);
60
obuf[n = strlen(obuf)] = '\n';
63
strcpy(net_info->args, "testarg=testval&service=none");
65
connector = SERVICE_CreateConnectorEx(service, fSERV_Any, net_info, 0);
66
ConnNetInfo_Destroy(net_info);
69
CORE_LOG(eLOG_Fatal, "Failed to create service connector");
71
if (CONN_Create(connector, &conn) != eIO_Success)
72
CORE_LOG(eLOG_Fatal, "Failed to create connection");
75
for (n = 0; n < 10; n++) {
77
for (m = 0; m < sizeof(obuf) - 2; m++)
78
obuf[m] = "01234567890\n"[rand() % 12];
82
if (CONN_Write(conn, obuf, strlen(obuf), &m, eIO_WritePersist)
86
CORE_LOG(eLOG_Fatal, "Error writing to connection");
90
assert(m == strlen(obuf));
93
if (CONN_Write(conn, obuf, strlen(obuf), &n, eIO_WritePersist)
96
CORE_LOG(eLOG_Fatal, "Error writing to connection");
98
assert(n == strlen(obuf));
104
timeout.usec = 12345;
105
if (CONN_Wait(conn, eIO_Read, &timeout) != eIO_Success) {
107
CORE_LOG(eLOG_Fatal, "Error waiting for reading");
110
status = CONN_Read(conn, ibuf, sizeof(ibuf), &n, eIO_ReadPersist);
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 : ""));
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)));
129
CORE_LOG(eLOG_Note, "Trying ID1 service");
131
net_info = ConnNetInfo_Create(service);
132
connector = SERVICE_CreateConnectorEx("ID1", fSERV_Any, net_info);
133
ConnNetInfo_Destroy(net_info);
136
CORE_LOG(eLOG_Fatal, "Service ID1 not available");
138
if (CONN_Create(connector, &conn) != eIO_Success)
139
CORE_LOG(eLOG_Fatal, "Failed to create connection");
141
if (CONN_Write(conn, "\xA4\x80\x02\x01\x02\x00", 7, &n, eIO_WritePersist)
144
CORE_LOG(eLOG_Fatal, "Error writing to service ID1");
148
if (CONN_Read(conn, ibuf, sizeof(ibuf), &n, eIO_ReadPlain) != eIO_Success){
150
CORE_LOG(eLOG_Fatal, "Error reading from service ID1");
153
CORE_LOGF(eLOG_Note, ("%d bytes read from service ID1", n));
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
168
* Revision 6.28 2003/10/21 11:37:47 lavr
169
* Set write timeout from the environment/registry instead of explicitly
171
* Revision 6.27 2003/07/24 16:38:59 lavr
172
* Add conditional check for early connection drops (#if 0'd)
174
* Revision 6.26 2003/05/29 18:03:49 lavr
175
* Extend read error message with a reason (if any)
177
* Revision 6.25 2003/05/14 03:58:43 lavr
178
* Match changes in respective APIs of the tests
180
* Revision 6.24 2003/05/05 20:31:23 lavr
181
* Add date/time stamp to each log message printed
183
* Revision 6.23 2003/04/04 21:01:06 lavr
184
* Modify readout procedure
186
* Revision 6.22 2002/10/28 15:47:12 lavr
187
* Use "ncbi_ansi_ext.h" privately and use strncpy0()
189
* Revision 6.21 2002/09/24 15:10:09 lavr
190
* Fix test not to dereference NULL pointer resulting from failed connection
192
* Revision 6.20 2002/08/07 16:38:08 lavr
193
* EIO_ReadMethod enums changed accordingly; log moved to end
195
* Revision 6.19 2002/03/22 19:47:41 lavr
196
* Test_assert.h made last among the include files
198
* Revision 6.18 2002/03/21 22:02:16 lavr
199
* Change default server from "ray" into "www.ncbi.nlm.nih.gov"
201
* Revision 6.17 2002/02/05 21:45:55 lavr
202
* Included header files rearranged
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
33
207
* Revision 6.15 2001/09/24 20:36:22 lavr
34
208
* Adjusted parameters in SERVICE_CreateConnectorEx()
78
252
* ==========================================================================
85
#include "../ncbi_priv.h"
86
#include <connect/ncbi_util.h>
87
#include <connect/ncbi_service_connector.h>
93
int main(int argc, const char* argv[])
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";
105
CORE_SetLOGFILE(stderr, 0/*false*/);
107
info = ConnNetInfo_Create(service);
108
strcpy(info->host, host);
110
strncpy(obuf, argv[3], sizeof(obuf) - 2);
111
obuf[sizeof(obuf) - 2] = 0;
112
obuf[n = strlen(obuf)] = '\n';
116
connector = SERVICE_CreateConnectorEx(service, fSERV_Any, info, 0);
117
ConnNetInfo_Destroy(info);
120
CORE_LOG(eLOG_Fatal, "Failed to create service connector");
122
if (CONN_Create(connector, &conn) != eIO_Success)
123
CORE_LOG(eLOG_Fatal, "Failed to create connection");
126
timeout.usec = 123456;
128
CONN_SetTimeout(conn, eIO_ReadWrite, &timeout);
130
if (CONN_Write(conn, obuf, strlen(obuf), &n) != eIO_Success ||
133
CORE_LOG(eLOG_Fatal, "Error writing to connection");
136
if (CONN_Wait(conn, eIO_Read, &timeout) != eIO_Success) {
138
CORE_LOG(eLOG_Fatal, "Error waiting for reading");
141
if (CONN_Read(conn, ibuf, n, &n, eIO_Persist) != eIO_Success) {
143
CORE_LOG(n ? eLOG_Error : eLOG_Fatal, "Error reading from connection");
147
("%d bytes read from service (%s):\n%.*s",
148
(int)n, CONN_GetType(conn), (int)n, ibuf));
152
CORE_LOG(eLOG_Note, "Trying ID1 service");
154
info = ConnNetInfo_Create(service);
155
connector = SERVICE_CreateConnectorEx("ID1", fSERV_Any, info);
156
ConnNetInfo_Destroy(info);
159
CORE_LOG(eLOG_Fatal, "Service ID1 not available");
161
if (CONN_Create(connector, &conn) != eIO_Success)
162
CORE_LOG(eLOG_Fatal, "Failed to create connection");
164
if (CONN_Write(conn, "\xA4\x80\x02\x01\x02\x00\x00", 7, &n) !=
165
eIO_Success || n != 7) {
167
CORE_LOG(eLOG_Fatal, "Error writing to service ID1");
170
if (CONN_Read(conn, ibuf, sizeof(ibuf), &n, eIO_Plain) != eIO_Success) {
172
CORE_LOG(eLOG_Fatal, "Error reading from service ID1");
175
CORE_LOGF(eLOG_Note, ("%d bytes read from service ID1", n));