NCBI C Toolkit Cross Reference

C/access/vecscnapi.c


  1 /*   vecscnapi.c
  2 * ===========================================================================
  3 *
  4 *                            PUBLIC DOMAIN NOTICE
  5 *            National Center for Biotechnology Information (NCBI)
  6 *
  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 do not place any restriction on its use or reproduction.
 13 *  We would, however, appreciate having the NCBI and the author cited in
 14 *  any work or product based on this material
 15 *
 16 *  Although all reasonable efforts have been taken to ensure the accuracy
 17 *  and reliability of the software and data, the NLM and the U.S.
 18 *  Government do not and cannot warrant the performance or results that
 19 *  may be obtained by using this software or data. The NLM and the U.S.
 20 *  Government disclaim all warranties, express or implied, including
 21 *  warranties of performance, merchantability or fitness for any particular
 22 *  purpose.
 23 *
 24 * ===========================================================================
 25 *
 26 * File Name:  vecscnapi.c
 27 *
 28 * Author:  Jonathan Kans
 29 *
 30 * Version Creation Date:   6/13/00
 31 *
 32 * $Revision: 1.16 $
 33 *
 34 * File Description: 
 35 *
 36 * Modifications:  
 37 * --------------------------------------------------------------------------
 38 *
 39 * ==========================================================================
 40 */
 41 
 42 #include <ncbi.h>
 43 #include <urlquery.h>
 44 #include <vecscnapi.h>
 45 #include <tofasta.h>
 46 #include <sqnutils.h>
 47 
 48 #ifdef OS_MAC
 49 #include <Events.h>
 50 #endif
 51 
 52 static CharPtr ReadALine (CharPtr str, size_t size, FILE *fp)
 53 
 54 {
 55   Char     ch;
 56   CharPtr  ptr;
 57   CharPtr  rsult;
 58 
 59   if (str == NULL || size < 1 || fp == NULL) return NULL;
 60   *str = '\0';
 61   rsult = fgets (str, size, fp);
 62   if (rsult != NULL) {
 63     ptr = str;
 64     ch = *ptr;
 65     while (ch != '\0' && ch != '\n' && ch != '\r') {
 66       ptr++;
 67       ch = *ptr;
 68     }
 69     *ptr = '\0';
 70   }
 71   return rsult;
 72 }
 73 
 74 NLM_EXTERN CONN VecScreenOpenConnection (
 75   CharPtr query
 76 )
 77 
 78 {
 79   /*
 80   return QUERY_OpenUrlQuery ("yar.ncbi.nlm.nih.gov", 6224,
 81                              "/VecScreen/vecscreenQB.cgi",
 82                              query, "vecscreenapp",
 83                              30, eMIME_T_NcbiData, eMIME_Fasta, eENCOD_Url,
 84                              fHCC_UrlDecodeInput | fHCC_UrlEncodeOutput);
 85   */
 86 
 87   StringCat (query, "\n");
 88   return QUERY_OpenServiceQuery ("VecScreen", query, 30);
 89 }
 90 
 91 NLM_EXTERN EIO_Status VecScreenWaitForReply (
 92   CONN conn
 93 )
 94 
 95 {
 96   time_t           currtime, starttime;
 97   time_t           max = 0;
 98   EIO_Status       status;
 99   STimeout         timeout;
100 #ifdef OS_MAC
101   EventRecord      currEvent;
102 #endif
103 
104   if (conn == NULL) return eIO_Unknown;
105 
106 #ifdef OS_MAC
107   timeout.sec = 0;
108   timeout.usec = 0;
109 #else
110   timeout.sec = 100;
111   timeout.usec = 0;
112 #endif
113 
114   starttime = GetSecs ();
115   while ((status = CONN_Wait (conn, eIO_Read, &timeout)) == eIO_Timeout && max < 300) {
116     currtime = GetSecs ();
117     max = currtime - starttime;
118 #ifdef OS_MAC
119     WaitNextEvent (0, &currEvent, 0, NULL);
120 #endif
121   }
122 
123   return status;
124 }
125 
126 typedef struct VQueueTag {
127   Char                   rid [64];
128   Char                   seqid [41];
129   time_t                 estTime;
130   time_t                 initialTime;
131   time_t                 postedTime;
132   Int2                   secondsToWait;
133   VecScreenResultProc    resultproc;
134   VecScreenAnnounceProc  announceproc;
135   Nlm_VoidPtr            userdata;
136   QUEUE                  connqueue;
137   Boolean                done;
138   struct VQueueTag*      next;
139 } VecScreenQueue, PNTR VQueuePtr;
140 
141 static Boolean LIBCALLBACK SecondVecScreenCallback (
142   CONN conn,
143   Nlm_VoidPtr userdata,
144   EIO_Status  status
145 )
146 
147 {
148   VQueuePtr  cqp;
149   FILE       *fp;
150   Char       line [256];
151   Char       path [PATH_MAX];
152   CharPtr    rid;
153   CharPtr    sttus;
154   CharPtr    str;
155   Boolean    success = FALSE;
156   Boolean    waiting = FALSE;
157 
158   /* look for waiting, failure, or success */
159 
160   cqp = (VQueuePtr) userdata;
161 
162   TmpNam (path);
163   fp = FileOpen (path, "w");
164   QUERY_CopyResultsToFile (conn, fp);
165   FileClose (fp);
166 
167   fp = FileOpen (path, "r");
168   str = ReadALine (line, sizeof (line), fp);
169   while (str != NULL) {
170     if (! StringHasNoText (line)) {
171       if (line [0] == '>') {
172         if (StringNICmp (line, ">Vector", 7) == 0) {
173           rid = StringStr (line, "RID: ");
174           if (rid != NULL) {
175             rid += 5;
176             sttus = StringStr (rid, " Status: ");
177             if (sttus != NULL) {
178               *sttus = '\0';
179               sttus += 9;
180               if (StringCmp (cqp->rid, rid) != 0) {
181                 ErrPostEx (SEV_ERROR, 0, 0, "RID mismatch '%s' vs '%s'", cqp->rid, rid);
182                 cqp->done = TRUE;
183               } else if (StringStr (sttus, "FAILED") != NULL) {
184                 cqp->done = TRUE;
185               } else if (StringStr (sttus, "unknown") != NULL) {
186                 ErrPostEx (SEV_ERROR, 0, 0, "RID unknown '%s'", rid);
187                 cqp->done = TRUE;
188               } else if (StringStr (sttus, "SUCCESS") != NULL) {
189                 success = TRUE;
190               } else if (StringStr (sttus, "WAITING") != NULL) {
191                 waiting = TRUE;
192                 /*
193                 Message (MSG_POST, "WAITING");
194                 */
195               }
196             }
197           }
198         } else if (StringNICmp (line, ">Message", 8) == 0) {
199           str = ReadALine (line, sizeof (line), fp);
200           while (str != NULL && StringNCmp (line, "//", 2) != 0) {
201             Message (MSG_POST, "%s\n", str);
202             if (StringStr (line, "FAILURE") != NULL) {
203               if (! waiting) {
204                 cqp->done = TRUE;
205               }
206             }
207             str = ReadALine (line, sizeof (line), fp);
208           }
209         }
210       }
211     }
212     str = ReadALine (line, sizeof (line), fp);
213   }
214   FileClose (fp);
215 
216   if (success) {
217     cqp->resultproc (path, cqp->userdata, cqp->rid, cqp->seqid, success);
218     cqp->done = TRUE;
219   } else if (cqp->done) {
220     cqp->resultproc (NULL, cqp->userdata, cqp->rid, cqp->seqid, success);
221   }
222 
223   FileRemove (path);
224 
225   return TRUE;
226 }
227 
228 static Boolean LIBCALLBACK FirstVecScreenCallback (
229   CONN conn,
230   Nlm_VoidPtr userdata,
231   EIO_Status  status
232 )
233 
234 {
235   VQueuePtr  cqp;
236   FILE       *fp;
237   Char       line [256];
238   Char       path [PATH_MAX];
239   CharPtr    rid;
240   CharPtr    rtoe;
241   CharPtr    str;
242   long int   val;
243 
244   /* read rID or failure message */
245 
246   cqp = (VQueuePtr) userdata;
247 
248   TmpNam (path);
249   fp = FileOpen (path, "w");
250   QUERY_CopyResultsToFile (conn, fp);
251   FileClose (fp);
252 
253   fp = FileOpen (path, "r");
254   str = ReadALine (line, sizeof (line), fp);
255   while (str != NULL) {
256     if (! StringHasNoText (line)) {
257       if (line [0] == '>') {
258         if (StringNICmp (line, ">Vector", 7) == 0) {
259           rid = StringStr (line, "RID: ");
260           if (rid != NULL) {
261             rid += 5;
262             rtoe = StringStr (rid, " RTOE: ");
263             if (rtoe != NULL) {
264               *rtoe = '\0';
265               rtoe += 7;
266               StringNCpy_0 (cqp->rid, rid, sizeof (cqp->rid));
267               if (sscanf (rtoe, "%ld", &val) == 1) {
268                 cqp->estTime = (time_t) val;
269                 cqp->secondsToWait = (Int2) val + 2;
270               } else {
271                 cqp->secondsToWait = 15;
272               }
273               if (cqp->secondsToWait > 15) {
274                 cqp->secondsToWait = 15;
275               }
276               if (cqp->announceproc != NULL) {
277                 cqp->announceproc (rid, cqp->seqid, (Int2) val);
278               }
279             }
280           } else if (StringStr (line, "FAILED") != NULL) {
281             cqp->done = TRUE;
282             if (cqp->resultproc != NULL) {
283               cqp->resultproc (NULL, cqp->userdata, cqp->rid, cqp->seqid, FALSE);
284             }
285           }
286         } else if (StringNICmp (line, ">Message", 8) == 0) {
287           str = ReadALine (line, sizeof (line), fp);
288           while (str != NULL && StringNCmp (line, "//", 2) != 0) {
289             Message (MSG_POST, "%s\n", str);
290             if (StringStr (line, "FAILURE") != NULL) {
291               cqp->done = TRUE;
292             }
293             str = ReadALine (line, sizeof (line), fp);
294           }
295         }
296       }
297     }
298     str = ReadALine (line, sizeof (line), fp);
299   }
300   FileClose (fp);
301 
302   FileRemove (path);
303 
304   return TRUE;
305 }
306 
307 static void VecScreen_AddToQueue (
308   VQUEUE * queue,
309   VecScreenResultProc resultproc,
310   VecScreenAnnounceProc announceproc,
311   Nlm_VoidPtr userdata,
312   CONN conn,
313   BioseqPtr bsp
314 )
315 
316 {
317   VQueuePtr       cqp;
318   VQueuePtr PNTR  qptr;
319   VQueuePtr       tmp;
320 
321   if (queue == NULL || resultproc == NULL || conn == NULL || bsp == NULL) return;
322 
323   /* allocate queue element */
324 
325   cqp = (VQueuePtr) MemNew (sizeof (VecScreenQueue));
326   if (cqp == NULL) return;
327 
328   cqp->rid [0] = '\0';
329   SeqIdWrite (bsp->id, cqp->seqid, PRINTID_FASTA_LONG, 40);
330   cqp->estTime = 0;
331   cqp->initialTime = GetSecs ();
332   cqp->postedTime = cqp->initialTime;
333   cqp->secondsToWait = 15;
334   cqp->resultproc = resultproc;
335   cqp->announceproc = announceproc;
336   cqp->userdata = userdata;
337   cqp->connqueue = NULL;
338   cqp->done = FALSE;
339 
340   /* add to polling queue */
341 
342   qptr = (VQueuePtr PNTR) queue;
343   if (qptr != NULL) {
344     if (*qptr != NULL) {
345       tmp = *qptr;
346       if (tmp != NULL) {
347         while (tmp->next != NULL) {
348           tmp = tmp->next;
349         }
350         tmp->next = cqp;
351       }
352     } else {
353       *qptr = cqp;
354     }
355   }
356 
357   /* queue the request for a rID */
358 
359   QUERY_AddToQueue (&(cqp->connqueue), conn, FirstVecScreenCallback, (Pointer) cqp, TRUE);
360 }
361 
362 NLM_EXTERN Boolean VecScreenAsynchronousRequest (
363   CharPtr database,
364   BioseqPtr bsp,
365   VQUEUE* queue,
366   VecScreenResultProc resultproc,
367   VecScreenAnnounceProc announceproc,
368   VoidPtr userdata
369 )
370 
371 {
372   CONN  conn;
373   FILE  *fp;
374   Char  path [PATH_MAX];
375   Char  str [128];
376 
377   if (bsp == NULL || queue == NULL || resultproc == NULL) return FALSE;
378 
379   if (StringHasNoText (database)) {
380     database = "UniVec";
381   }
382   sprintf (str, "db=%s", database);
383   conn = VecScreenOpenConnection (str);
384   if (conn == NULL) return FALSE;
385 
386   TmpNam (path);
387 
388   fp = FileOpen (path, "w");
389   BioseqToFasta (bsp, fp, ISA_na (bsp->mol));
390   FileClose (fp);
391 
392   fp = FileOpen (path, "r");
393   QUERY_CopyFileToQuery (conn, fp);
394   FileClose (fp);
395 
396   QUERY_SendQuery (conn);
397 
398   VecScreen_AddToQueue (queue, resultproc, announceproc, userdata, conn, bsp);
399 
400   FileRemove (path);
401 
402   return TRUE;
403 }
404 
405 static void VecScreen_RemoveFromQueue (
406   VQUEUE* queue, VQueuePtr freeme
407 )
408 
409 {
410   VQueuePtr       curr;
411   VQueuePtr       next;
412   VQueuePtr PNTR  prev;
413   VQueuePtr PNTR  qptr;
414 
415   qptr = (VQueuePtr PNTR) queue;
416   if (qptr == NULL || *qptr == NULL || freeme == NULL) return;
417 
418   prev = qptr;
419   curr = *qptr;
420 
421   while (curr != NULL) {
422     next = curr->next;
423     if (curr == freeme) {
424       *(prev) = next;
425       curr->next = NULL;
426       MemFree (curr);
427     } else {
428       prev = &(curr->next);
429     }
430     curr = next;
431   }
432 }
433 
434 NLM_EXTERN Int4 VecScreenCheckQueue (
435   VQUEUE* queue
436 )
437 
438 {
439   CONN            conn;
440   Nlm_Int4        count = 0;
441   VQueuePtr       curr;
442   time_t          currtime;
443   VQueuePtr       next;
444   VQueuePtr PNTR  qptr;
445   Char            str [128];
446 
447   qptr = (VQueuePtr PNTR) queue;
448   if (qptr == NULL || *qptr == NULL) return 0;
449 
450   curr = *qptr;
451 
452   while (curr != NULL) {
453     next = curr->next;
454 
455     /* check for return of rID, WAITING, of SUCCESS message */
456 
457     QUERY_CheckQueue (&(curr->connqueue));
458 
459     if (curr->done) {
460       VecScreen_RemoveFromQueue (queue, curr);
461 
462     } else {
463       if (curr->connqueue == NULL) {
464         currtime = GetSecs ();
465         if (currtime - curr->postedTime < curr->secondsToWait) {
466         } else if (! StringHasNoText (curr->rid)) {
467 
468           /* estimated wait time has expired, so queue another check */
469 
470           if (curr->secondsToWait < 300) {
471             curr->secondsToWait *= 2;
472             if (curr->secondsToWait > 300) {
473               curr->secondsToWait = 300;
474             }
475           }
476           curr->postedTime = GetSecs ();
477 
478           sprintf (str, "req=%s", curr->rid);
479           conn = VecScreenOpenConnection (str);
480           QUERY_SendQuery (conn);
481           QUERY_AddToQueue (&(curr->connqueue), conn, SecondVecScreenCallback, (Pointer) curr, TRUE);
482         }
483       }
484 
485       count++;
486     }
487 
488     curr = next;
489   }
490 
491   return count;
492 }
493 
494 NLM_EXTERN Int4 PrintVecScreenQueue (
495   VQUEUE* queue,
496   FILE *fp
497 )
498 
499 {
500   Nlm_Int4        count = 0;
501   VQueuePtr       curr;
502   time_t          currtime;
503   VQueuePtr       next;
504   VQueuePtr PNTR  qptr;
505 
506   qptr = (VQueuePtr PNTR) queue;
507   if (qptr == NULL || *qptr == NULL || fp == NULL) return 0;
508 
509   currtime = GetSecs ();
510   curr = *qptr;
511 
512   while (curr != NULL) {
513     next = curr->next;
514 
515     if (! curr->done) {
516 
517       fprintf (fp, "%s\t%s\t%d\t%d\n", curr->rid, curr->seqid,
518                (int) (currtime - curr->initialTime), (int) curr->estTime);
519       count++;
520     }
521 
522     curr = next;
523   }
524 
525   return count;
526 }
527 
528 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.