File: rbzoomconnection.c

package info (click to toggle)
ruby-zoom 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 264 kB
  • sloc: ansic: 680; xml: 262; ruby: 255; makefile: 4
file content (344 lines) | stat: -rw-r--r-- 10,572 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2005 Laurent Sansonetti <lrz@chopine.be>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rbzoom.h"

#ifdef MAKING_RDOC_HAPPY
mZoom = rb_define_module("ZOOM");
#endif


/* Document-class: ZOOM::Connection
 * The Connection object is a session with a target.
 */
static VALUE cZoomConnection;


static VALUE
rbz_connection_make (ZOOM_connection connection)
{
    return connection != NULL
        ? Data_Wrap_Struct (cZoomConnection,
                            NULL,
                            ZOOM_connection_destroy,
                            connection)
        : Qnil;
}

static ZOOM_connection
rbz_connection_get (VALUE obj)
{
    ZOOM_connection connection;
        
    Data_Get_Struct (obj, struct ZOOM_connection_p, connection);
    assert (connection != NULL);

    return connection;
}

#define RAISE_IF_FAILED(connection)                     \
    do {                                                \
        int error;                                      \
        const char *errmsg;                             \
        const char *addinfo;                            \
                                                        \
        error = ZOOM_connection_error (connection,      \
                                       &errmsg,         \
                                       &addinfo);       \
        if (error != 0)                                 \
            rb_raise (rb_eRuntimeError, "%s (%d) %s",   \
                      errmsg, error, addinfo);          \
    }                                                   \
    while (0)

void rbz_connection_check(VALUE obj)
{
	ZOOM_connection connection;
	
	connection = rbz_connection_get(obj);
  	RAISE_IF_FAILED (connection);
}


/*
 * call-seq: 
 * 	open(host, port=nil) { |conn| ... }
 *
 * host: hostname of the target to connect to.
 *
 * port: network port of the target to connect to.
 *
 * A convenience method that creates a new connection and attempts to 
 * establish a network connection to the given target, basically calling
 * ZOOM::Connection.new and ZOOM::Connection#connect.
 *  
 * If a block is given, then it will be called once the connection is 
 * established, passing a reference to the connection object as a parameter, 
 * and destroying the connection automatically at the end of the block.
 * With no block, this method just returns the connection object.
 *
 * Returns: a newly created ZOOM::Connection object.
 */
static VALUE
rbz_connection_open (int argc, VALUE *argv, VALUE self)
{
    VALUE host;
    VALUE port;
    ZOOM_connection connection;
    VALUE rb_connection;
    
    rb_scan_args (argc, argv, "11", &host, &port);

    connection = ZOOM_connection_new (RVAL2CSTR (host),
                                      NIL_P (port) ? 0 : FIX2INT (port));
    RAISE_IF_FAILED (connection);
    
    rb_connection = rbz_connection_make (connection);
    if (rb_block_given_p ()) {
        rb_yield(rb_connection);
        return Qnil;
    }
    return rb_connection;
}

/*
 * call-seq: new(options=nil)
 *
 * options: options for the connection, as a Hash object.
 *
 * Creates a new connection object, but does not establish a network connection
 * immediately, allowing you to specify options before (if given). You can 
 * thus establish the connection using ZOOM::Connection#connect.
 * 
 * Returns: a newly created ZOOM::Connection object.
 */
static VALUE
rbz_connection_new (int argc, VALUE *argv, VALUE self)
{
    ZOOM_options options;
    ZOOM_connection connection;
    VALUE rb_options;
    
    rb_scan_args (argc, argv, "01", &rb_options);

    if (NIL_P (rb_options))
        options = ZOOM_options_create ();
    else
        options = ruby_hash_to_zoom_options (rb_options);

    connection = ZOOM_connection_create (options);
    ZOOM_options_destroy (options);
    RAISE_IF_FAILED (connection);

    return rbz_connection_make (connection);
}

/*
 * call-seq:
 * 	connect(host, port=nil)
 *
 * host: hostname of the target to connect to.
 *
 * port: network port of the target to connect to.
 *
 * Establishes a network connection to the target specified by the given
 * arguments.  If no port is given, 210 will be used.  A colon in the host
 * string denotes the beginning of a port number.  If the host string includes
 * a slash, the following part specifies a database for the connection.
 *
 * You can also prefix the host string with a scheme followed by a colon.
 * The default scheme is tcp (Z39.50 protocol).  The scheme http selects SRW
 * over HTTP.
 *
 * This method raises an exception on error.
 *
 * Returns: self.
 */
static VALUE
rbz_connection_connect (int argc, VALUE *argv, VALUE self)
{
    ZOOM_connection connection;
    VALUE host;
    VALUE port;
    
    rb_scan_args (argc, argv, "11", &host, &port);
  
    connection = rbz_connection_get (self);
    ZOOM_connection_connect (connection, 
                             RVAL2CSTR (host), 
                             NIL_P (port) ? 0 : FIX2INT (port));
    RAISE_IF_FAILED (connection); 

    return self;
}

/*
 * call-seq:
 * 	set_option(key, value)
 *
 * key: the name of the option, as a string.
 *
 * value: the value of this option (as a string, integer or boolean).
 *
 * Sets an option on the connection.
 * 
 * Returns: self.
 */
static VALUE
rbz_connection_set_option (VALUE self, VALUE key, VALUE val)
{
    ZOOM_connection connection;
    
    connection = rbz_connection_get (self);
    ZOOM_connection_option_set (connection,
                                RVAL2CSTR (key),
                                RVAL2CSTR (rb_obj_as_string (val)));
    RAISE_IF_FAILED (connection); 
    
    return self;
}

/*
 * call-seq: 
 * 	get_option(key)
 *
 * key: the name of the option, as a string.
 *
 * Gets the value of a connection's option.
 * 
 * Returns: the value of the given option, as a string, integer or boolean.
 */
static VALUE
rbz_connection_get_option (VALUE self, VALUE key)
{
    ZOOM_connection connection;
    const char *value;
 
    connection = rbz_connection_get (self);
    value = ZOOM_connection_option_get (connection,
                                        RVAL2CSTR (key));

    return zoom_option_value_to_ruby_value (value);
}

/*
 * call-seq: 
 * 	search(criterion)
 *
 * criterion: the search criterion, either as a ZOOM::Query object or as a string,
 * representing a PQF query.
 *  
 * Searches the connection from the given criterion.  You can either create and
 * pass a reference to a ZOOM::Query object, or you can simply pass a string
 * that represents a PQF query.
 *
 * This method raises an exception on error.
 * 
 * Returns: a result set from the search, as a ZOOM::ResultSet object,
 * empty if no results were found.
 */
static VALUE
rbz_connection_search (VALUE self, VALUE criterion)
{
    ZOOM_connection connection;
    ZOOM_resultset resultset;

    connection = rbz_connection_get (self);
    if (TYPE (criterion) == T_STRING)
        resultset = ZOOM_connection_search_pqf (connection,
                                                RVAL2CSTR (criterion));
    else
        resultset = ZOOM_connection_search (connection,
                                            rbz_query_get (criterion));
    RAISE_IF_FAILED (connection); 
    assert (resultset != NULL);
  
    return rbz_resultset_make (resultset);
}


/*
 * Constructs a new extended services ZOOM::Package using this connections host information.
 *
 * Note: The Perl script passes this connections options if already set, otherwise constructs a new ZOOM::Option object. 
 * Currently this method always constructs a new ZOOM::Option object for each package.
 * 
 * Returns: a new ZOOM::Package object.
 */
static VALUE
rbz_connection_package(VALUE self)
{
  ZOOM_connection connection;
  ZOOM_options options;
  VALUE package;

  connection = rbz_connection_get (self);
  options = ZOOM_options_create ();
  package = rbz_package_make(connection, options);
  return package;
}


void
Init_zoom_connection (VALUE mZoom)
{
    VALUE c;

    c = rb_define_class_under (mZoom, "Connection", rb_cObject); 
    rb_define_singleton_method (c, "open", rbz_connection_open, -1);
    rb_define_singleton_method (c, "new", rbz_connection_new, -1);
    rb_define_method (c, "connect", rbz_connection_connect, -1);
    rb_define_method (c, "set_option", rbz_connection_set_option, 2);
    rb_define_method (c, "get_option", rbz_connection_get_option, 1);
    rb_define_method (c, "package", rbz_connection_package, 0);

    define_zoom_option (c, "implementationName");
    define_zoom_option (c, "user");
    define_zoom_option (c, "group");
    define_zoom_option (c, "password");
    define_zoom_option (c, "host");
    define_zoom_option (c, "proxy");
    define_zoom_option (c, "async");
    define_zoom_option (c, "maximumRecordSize");
    define_zoom_option (c, "preferredMessageSize");
    define_zoom_option (c, "lang");
    define_zoom_option (c, "charset");
    define_zoom_option (c, "serverImplementationId");
    define_zoom_option (c, "targetImplementationName");
    define_zoom_option (c, "serverImplementationVersion");
    define_zoom_option (c, "databaseName");
    define_zoom_option (c, "piggyback");
    define_zoom_option (c, "smallSetUpperBound");
    define_zoom_option (c, "largeSetLowerBound");
    define_zoom_option (c, "mediumSetPresentNumber");
    define_zoom_option (c, "smallSetElementSetName");
    define_zoom_option (c, "mediumSetElementSetName");

    /* herited from Zoom::ResultSet */
    define_zoom_option (c, "start");
    define_zoom_option (c, "count");
    define_zoom_option (c, "presentChunk");
    define_zoom_option (c, "elementSetName");
    define_zoom_option (c, "preferredRecordSyntax");
    define_zoom_option (c, "schema");
    define_zoom_option (c, "setname");
    
    rb_define_method (c, "search", rbz_connection_search, 1);
    
    cZoomConnection = c;
}