aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/libvoclient/vocSesame.c
blob: fe182320f8fd1b61aba1b4bfb047def9e01dac89 (plain) (blame)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/**
 *  VOCSESAME.C  -- Interface to the Sesame name resolver service.
 *
 *  @section DESCRIPTION
 *
 *  Sesame Name Resolver Interface:
 *  -------------------------------
 *
 *          sr = voc_nameResolver  (target)
 *      pos_str = voc_resolverPos  (sr)
 *         radeg = voc_resolverRA  (sr)
 *       decdeg = voc_resolverDEC  (sr)
 *     ra_err = voc_resolverRAErr  (sr)
 *   dec_err = voc_resolverDECErr  (sr)
 *    typ_str = voc_resolverOtype  (sr)
 *
 *	Client programs may be written in any language that can interface to
 *  C code.  Sample programs using the interface are provided as is a SWIG
 *  interface definition file.  This inferface is based closely on the DAL
 *  client code produced for the 2005 NVOSS, as that interface evolves 
 * 
 * 
 *  @file  	vocSesame.c
 *  @author  	Michael Fitzpatrick
 *  @version	June 2006
 *
 *************************************************************************
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>


#define _VOCLIENT_LIB_
#include "VOClient.h"


#define MAX_OBJECTS		128
#define SZ_TARGET		128


/**
 *  @struct Object
 *
 *  Structure for the object being queried.
 */
typedef struct {
    char    target[SZ_TARGET];		/* target name			*/
    char    hms_pos[SZ_TARGET];		/* sexagesimal position		*/
    double  ra, dec;			/* decimal degrees position	*/
    double  era, edec;			/* decimal degrees error	*/
    char    type[SZ_TARGET];		/* object type			*/
} Object, *ObjectPtr;


/*  @internal
 *
 *  The runtime cache is implemented as a circular array of MAX_OBJECTS.
 *  We first check to see if the requested object is in the runtime cache,
 *  then look on the disk cache for the information.  If not found we
 *  query the server and store the result.  The Sesame handle returned will
 *  be the negative of the index in the runtime cache, i.e "-(1+sr)".
 */
Object clientCache[MAX_OBJECTS];	/* runtime client cache		*/
int    cacheTop = 0;

extern VOClient *vo; 			/* Interface runtime struct	*/


static Sesame   voc_isCachedObject (char *target);
static Sesame   voc_cacheObject (Sesame sr, char *target);

static char    *voc_resStrVal (Sesame sr, char *method, char *ifcall);
static double 	voc_resDblVal (Sesame sr, char *method, char *ifcall);



/*****************************************************************************/

/**
 *  NAMERESOLVER -- Query the CDS Sesame service to resolve the target name
 *  to coordinates.  The query is done when creating the Sesame object, 
 *  thereafter we simply query the object data.
 * 
 *  @brief	Query the CDS Sesame name resolver service.
 *  @fn		handle = voc_nameResolver (char *name)
 *
 *  @param  target  	name of target to be resolved
 *  @returns		Sesame  Sesame object handle
 */
Sesame
voc_nameResolver (char *target)
{
    Sesame   sr = (Sesame) VOC_NULL;


    /*  Make sure we've been initialized properly first.
     */
    if (vo == (VOClient *) NULL) {
        if (voc_initVOClient (NULL) == ERR) {
            fprintf (stderr, "ERROR: Can't initialize VO Client....\n");
            exit (1);
        } else if (VOC_DEBUG)
            printf ("Warning: Initializing VO Client....\n");
    }

    /* Before we query the server, see whether this is a familiar
    ** object and we're using the cache.  Otherwise, return the cached result.
    */
    if (vo->use_cache && (sr=voc_isCachedObject (target)) != (Sesame)VOC_NULL)
	return (sr);

    if (target) {
        vocRes_t *result = (vocRes_t *) NULL;
        vocMsg_t *msg = (vocMsg_t *) msg_newCallMsg (0, "nameResolver", 0);

        msg_addStringParam (msg, target);
    
        /* Read result and check for any faults.
         */
        if (msg_resultStatus ((result=msg_sendMsg(vo->io_chan,msg))) == ERR) {
            if (!vo->quiet)
		fprintf (stderr, "ERROR: cannot resolve target: %s\n", target);
        } else
            sr = msg_getIntResult (result, 0);

        if (msg) free ((void *)msg);         /* free the pointers 	*/
        if (result) free ((void *)result);

	if (voc_resolverRA(sr)     == 0.0 &&
	    voc_resolverRAErr(sr)  == 0.0 &&
	    voc_resolverDEC(sr)    == 0.0 &&
	    voc_resolverDECErr(sr) == 0.0) {
		return (0);		    /* no match found		*/
	}

    } else if (!vo->quiet)
        fprintf (stderr, "ERROR: no target specified\n");


    /* Store the result in the cache.  We can change the return handle if
    ** the object was successfully cached.
    */
    sr = voc_cacheObject (sr, target);

    return (sr);
}


/**
 *  RESOLVERPOS --  Return a string containing the (ra,dec) position as
 *  sexagesimal strings. 
 *
 *  @brief	Return the (ra,dec) position for the object
 *  @fn		str = voc_resolverPos (Sesame sr)
 *
 *  @param  sr  	handle to previus query return
 *  @returns		string containing (ra,dec) position
 */
char *
voc_resolverPos (Sesame sr)
{
    if (sr < 0)
        return ( strdup(clientCache[-(1+sr)].hms_pos) );
    else
        return ( voc_resStrVal (sr, "srGetPOS", "resolverPos") );
}


/**
 *  RESOLVEROTYPE --  Return a string containing the object type description
 *
 *  @brief	Return a string containing the object type description
 *  @fn		str = voc_resolverOtype (Sesame sr)
 *
 *  @param  sr  	handle to previus query return
 *  @returns		string to object type description
 */
char *
voc_resolverOtype (Sesame sr)
{
    if (sr < 0)
        return ( strdup (clientCache[-(1+sr)].type) );
    else
        return ( voc_resStrVal (sr, "srGetOtype", "resolverOtype") );
}


/**
 *  RESOLVERRA --  Return the RA as a double precision value.
 *
 *  @brief	Return the RA as a double precision value.
 *  @fn		str = voc_resolverRA (Sesame sr)
 *
 *  @param  sr  	handle to previus query return
 *  @returns		object RA (decimal degrees)
 */
double      
voc_resolverRA (Sesame sr)
{
    if (sr < 0)
        return ( clientCache[-(1+sr)].ra );
    else
        return ( voc_resDblVal (sr, "srGetRA", "resolverRA") );
}


/**
 *  RESOLVERRAERR --  Return the RA error as a double precision value.
 *
 *  @brief	Return the RA error as a double precision value.
 *  @fn		str = voc_resolverRAErr (Sesame sr)
 *
 *  @param  sr  	handle to previus query return
 *  @returns		object RA error (decimal degrees)
 */
double      
voc_resolverRAErr (Sesame sr)
{
    if (sr < 0)
        return ( clientCache[-(1+sr)].era );
    else
        return ( voc_resDblVal (sr, "srGetRAErr", "resolverRAErr") );
}


/**
 *  RESOLVERDEC --  Return the DEC as a double precision value.
 *
 *  @brief	Return the DEC as a double precision value.
 *  @fn		str = voc_resolverDEC (Sesame sr)
 *
 *  @param  sr  	handle to previus query return
 *  @returns		object Declination (decimal degrees)
 */
double      
voc_resolverDEC (Sesame sr)
{
    if (sr < 0)
        return ( clientCache[-(1+sr)].dec );
    else
        return ( voc_resDblVal (sr, "srGetDEC", "resolverDEC") );
}


/**
 *  RESOLVERDECERR --  Return the Dec error as a double precision value.
 *
 *  @brief	Return the Dec error as a double precision value.
 *  @fn		str = voc_resolverDECErr (Sesame sr)
 *
 *  @param  sr  	handle to previus query return
 *  @returns		object DEC error (decimal degrees)
 */
double      
voc_resolverDECErr (Sesame sr)
{
    if (sr < 0)
        return ( clientCache[-(1+sr)].edec );
    else
        return ( voc_resDblVal (sr, "srGetDECErr", "resolverDECErr") );
}



/************************************************************
 *	Private procedures
 ***********************************************************/

extern char *voc_getCacheDir (char *subdir);
    

/**
 *  VOC_ISCACHEDOBJECT -- See if the requested object is in the cache.
 *
 *  @brief	See if the requested object is in the cache.
 *  @fn		sr = voc_isCachedObject (char *target)
 *
 *  @param  target	target name
 *  @returns		handle to cached object
 */
static Sesame
voc_isCachedObject (char *target)
{
    FILE  *fd;
    register int index;
    char  *ip, *op, *dir, fname[SZ_FNAME], path[SZ_FNAME], buf[256];
    Object *obj = (Object *) NULL;
    struct stat info;


    /* Look first for the object in the runtime cache.
    */
    for (index=0; index < cacheTop; index++) {
	if (strcmp (target, clientCache[index].target) == 0) {
	    return (-(index+1));
	}
    }

    /* Not in the runtime cache, check to see if we have it on disk.
    */

    /* Turn the target name into a filename, replacing the white space
    ** with an underscore.
    */
    for (ip=target, op=fname; *ip; ip++) 
	*op++ = ((isspace(*ip) || *ip == '_') ? '+' : *ip);
    *op = '\0';

    sprintf (path, "%s/%s", (dir = voc_getCacheDir("sesame")), fname);
    if (! (fd = fopen (path, "r")) )
	return ((Sesame) VOC_NULL);		/* not in cache		*/
    if (fstat (fileno (fd), &info) < 0)
	return ((Sesame) VOC_NULL);		/* invalid cache	*/
    if (info.st_size == 0)
	return ((Sesame) VOC_NULL);		/* invalid cache	*/

    fgets (buf, 256, fd);

    /* Save it in the runtime cache.
    */
    index = ((cacheTop++) % MAX_OBJECTS);	/* runtime cache 	*/
    obj = &clientCache[index];

    memset (obj, 0, sizeof(Object));
    for (op=&obj->target[0], ip=buf; *ip && *ip != ':'; )
	*op++ = *ip++;
    ip++;					/* skip colon		*/
    ip++;					/* skip space		*/
    op = &obj->hms_pos[0];
    while (*ip && !isspace(*ip)) *op++ = *ip++; /* get ra		*/
    while (*ip && isspace(*ip))  *op++ = *ip++; /* skip spaces		*/
    while (*ip && !isspace(*ip)) *op++ = *ip++; /* get dec		*/

    sscanf (ip, " %lf %lf %lf %lf %s\n", 
	&obj->ra, &obj->dec, &obj->era, &obj->edec, obj->type);

    fclose (fd);

    return ((Sesame) -(index+1));
}


/**
 *  VOC_CACHEOBJECT -- Store the object in the cache.
 *
 *  @brief	Store the object in the cache.
 *  @fn		sr = voc_cacheObject (Sesame sr, char *target)
 *
 *  @param  sr		handle to sesame query
 *  @param  target	target name
 *  @returns		handle to cached object
 */
static Sesame
voc_cacheObject (Sesame sr, char *target)
{
    FILE  *fd;
    register int index;
    char  *ip, *op, *dir, *s, fname[SZ_FNAME], path[SZ_FNAME];
    Object *obj = (Object *) NULL;


    if ((s = getenv("VOC_NO_CACHE")))
	return (sr);

    /* Turn the target name into a filename, replacing the white space
    ** with an underscore.
    */
    for (ip=target, op=fname; *ip; ip++) 
	*op++ = (isspace(*ip) ? '_' : *ip);
    *op = '\0';

    /* Open the cache file and pre-fetch the results.
    */
    sprintf (path, "%s/%s", (dir = voc_getCacheDir("sesame")), fname);
    if (! (fd = fopen (path, "a+")) )
	return (sr);				/* error return	 	*/

    index = ((cacheTop++) % MAX_OBJECTS);	/* runtime cache 	*/
    obj = &clientCache[index];

    fprintf (fd, "%s: %s %f %f %.2f %.2f %s\n", 
	strcpy(obj->target, target),
	strcpy(obj->hms_pos, voc_resolverPos (sr)),
	(obj->ra   = voc_resolverRA (sr)),
	(obj->dec  = voc_resolverDEC (sr)),
	(obj->era  = voc_resolverRAErr (sr)),
	(obj->edec = voc_resolverRAErr (sr)),
	strcpy(obj->type, voc_resolverOtype (sr)) );

    fclose (fd);

    /* Don't cache a NULL return.
    */
    if (((int)obj->ra + (int)obj->dec + (int)obj->era + (int)obj->edec) == 0) {
	    unlink (path);
            return (sr);
    }

    if (dir)
	free ((char *)dir);

    return ((Sesame) -(index + 1));		/* return new sr	*/
}


/**
 *  VOC_RESSTRVAL -- Return a string value from a method with no arguments.
 *
 *  @brief	Return a string value from a method with no args.
 *  @fn		str = voc_resStrVal (Sesame sr, char *method, char *ifcall)
 *
 *  @param  sr		handle to sesame query
 *  @param  method	mehod to call
 *  @param  ifcall	interface method name
 *  @returns		the value
 */
static char *
voc_resStrVal (Sesame sr, char *method, char *ifcall)
{
    char     *val = NULL, *str = NULL;
    int       len = 0;

    if (sr > 0) {
        vocRes_t *result = (vocRes_t *) NULL;
        vocMsg_t *msg = (vocMsg_t *) msg_newCallMsg (sr, method, 0);

        /* Send message and read result.
         */
        if (msg_resultStatus ((result=msg_sendMsg(vo->io_chan,msg))) == ERR) {
            if (!vo->quiet)
		fprintf (stderr, "ERROR: %s() failed\n", ifcall);
        } else {
            val = msg_getStringResult (result, 0);
            str = calloc (1, (len = strlen(val)+1));
            strncpy (str, val, len);
        }

        if (msg) free ((void *)msg);     /* free the pointers            */
        if (result) free ((void *)result);

    } else if (!vo->quiet)
        fprintf (stderr, "ERROR: Null object to %s()\n", ifcall);

    return (str);
}


/**
 *  VOC_RESDBLVAL -- Return a double precision value from a method with 
 *  no arguments.
 *
 *  @brief	Return a double precision value from a method with no args.
 *  @fn		dval = voc_resDblVal (Sesame sr, char *method, char *ifcall)
 *
 *  @param  sr		handle to sesame query
 *  @param  method	mehod to call
 *  @param  ifcall	interface method name
 *  @returns		the value
 */
static double      
voc_resDblVal (Sesame sr, char *method, char *ifcall)
{
    double   dval = (double) 0.0;


    if (sr > 0) {
        vocRes_t *result = (vocRes_t *) NULL;
        vocMsg_t *msg = (vocMsg_t *) msg_newCallMsg (sr, method, 0);

        /* Send message and read result.
         */
        if (msg_resultStatus ((result=msg_sendMsg(vo->io_chan,msg))) == ERR) {
            if (!vo->quiet)
		fprintf (stderr, "ERROR: %s() failed\n", ifcall);
        } else
            dval = msg_getFloatResult (result, 0);

        if (msg) free ((void *)msg);     /* free the pointers            */
        if (result) free ((void *)result);

    } else if (!vo->quiet)
        fprintf (stderr, "ERROR: Null object to %s()\n", ifcall);

    return (dval);
}