Back to index

nordugrid-arc-nox  1.1.0~rc6
RLS.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #ifdef WIN32 
00008 #include <arc/win32.h>
00009 #include <fcntl.h>
00010 #endif
00011 
00012 #include <algorithm>
00013 
00014 #include <arc/Logger.h>
00015 #include <arc/URL.h>
00016 
00017 #include "RLS.h"
00018 
00019 namespace Arc {
00020 
00021   static Logger logger(Logger::rootLogger, "RLS");
00022 
00023   bool rls_find_lrcs(const URL& url, rls_lrc_callback_t callback, void *arg) {
00024     std::list<URL> rlis;
00025     std::list<URL> lrcs;
00026     rlis.push_back(url);
00027     lrcs.push_back(url);
00028     return rls_find_lrcs(rlis, lrcs, true, true, callback, arg);
00029   }
00030 
00031   bool rls_find_lrcs(const URL& url, std::list<URL> lrcs) {
00032     std::list<URL> rlis;
00033     rlis.push_back(url);
00034     lrcs.clear();
00035     lrcs.push_back(url);
00036     return rls_find_lrcs(rlis, lrcs, true, true, NULL, NULL);
00037   }
00038 
00039   bool rls_find_lrcs(std::list<URL> rlis, std::list<URL> lrcs,
00040                      rls_lrc_callback_t callback, void *arg) {
00041     return rls_find_lrcs(rlis, lrcs, true, true, callback, arg);
00042   }
00043 
00044   bool rls_find_lrcs(std::list<URL> rlis, std::list<URL> lrcs, bool down,
00045                      bool up, rls_lrc_callback_t callback, void *arg) {
00046     globus_result_t err;
00047     int errcode;
00048     char errmsg[MAXERRMSG + 32];
00049 
00050     globus_rls_client_set_timeout(30);
00051 
00052     std::list<URL>::iterator lrc_p;
00053     std::list<URL>::iterator rli_p;
00054     globus_list_t *rliinfo_list;
00055 
00056     // Check predefined LRCs and call callback if LRC.
00057     int lrc_num = 0;
00058     for (lrc_p = lrcs.begin(); lrc_p != lrcs.end();) {
00059       const URL& url = *lrc_p;
00060       globus_rls_handle_t *h = NULL;
00061       logger.msg(INFO, "Contacting %s", url.str());
00062       err = globus_rls_client_connect
00063               (const_cast<char*>(url.ConnectionURL().c_str()), &h);
00064       if (err != GLOBUS_SUCCESS) {
00065         globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00066                                      GLOBUS_FALSE);
00067         logger.msg(INFO, "Warning: can't connect to RLS server %s: %s",
00068                    url.str(), errmsg);
00069         lrc_p = lrcs.erase(lrc_p);
00070         continue;
00071       }
00072       err = globus_rls_client_lrc_rli_list(h, &rliinfo_list);
00073       if (err != GLOBUS_SUCCESS) {
00074         globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00075                                      GLOBUS_FALSE);
00076         if (errcode == GLOBUS_RLS_INVSERVER) { // Not LRC
00077           globus_rls_client_close(h);
00078           lrc_p = lrcs.erase(lrc_p);
00079           continue;
00080         }
00081         else if (errcode == GLOBUS_RLS_RLI_NEXIST) { // Top level LRC server !?
00082           if (callback)
00083             if (!callback(h, url, arg)) {
00084               globus_rls_client_close(h);
00085               return false;
00086             }
00087           globus_rls_client_close(h);
00088           ++lrc_p;
00089           lrc_num++;
00090           continue;
00091         }
00092         else {
00093           logger.msg(INFO,
00094                      "Warning: can't get list of RLIs from server %s: %s",
00095                      url.str(), errmsg);
00096           globus_rls_client_close(h);
00097           lrc_p = lrcs.erase(lrc_p);
00098           continue;
00099         }
00100       }
00101       else { // Add obtained RLIs to list
00102         if (up)
00103           for (globus_list_t *p = rliinfo_list; p; p = globus_list_rest(p)) {
00104             URL url(((globus_rls_rli_info_t*)globus_list_first(p))->url);
00105             if (std::find(rlis.begin(), rlis.end(), url) == rlis.end())
00106               rlis.push_back(url);
00107           }
00108         globus_rls_client_free_list(rliinfo_list);
00109         if (callback)
00110           if (!callback(h, url, arg)) {
00111             globus_rls_client_close(h);
00112             return false;
00113           }
00114       }
00115       globus_rls_client_close(h);
00116       ++lrc_p;
00117       lrc_num++;
00118     }
00119 
00120     if (up)  // Go up through hierarchy looking for new RLIs and LRCs
00121       for (rli_p = rlis.begin(); rli_p != rlis.end();) {
00122         globus_rls_handle_t *h = NULL;
00123         const URL& url = *rli_p;
00124         logger.msg(INFO, "Contacting %s", url.str());
00125         err = globus_rls_client_connect
00126                 (const_cast<char*>(url.ConnectionURL().c_str()), &h);
00127         if (err != GLOBUS_SUCCESS) {
00128           globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00129                                        GLOBUS_FALSE);
00130           logger.msg(INFO, "Warning: can't connect to RLS server %s: %s",
00131                      url.str(), errmsg);
00132           rli_p = rlis.erase(rli_p);
00133           continue;
00134         }
00135         bool bad_rli = false;
00136         err = globus_rls_client_rli_rli_list(h, &rliinfo_list);
00137         if (err != GLOBUS_SUCCESS) {
00138           globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00139                                        GLOBUS_FALSE);
00140           if (errcode == GLOBUS_RLS_INVSERVER)  // Not an RLI server
00141             bad_rli = true;
00142           else if (errcode == GLOBUS_RLS_RLI_NEXIST) { // Top level server ?
00143 
00144           }
00145           else
00146             logger.msg(INFO,
00147                        "Warning: can't get list of RLIs from server %s: %s",
00148                        url.str(), errmsg);
00149         }
00150         else { // Add obtained RLIs to list
00151           for (globus_list_t *p = rliinfo_list; p; p = globus_list_rest(p)) {
00152             URL url(((globus_rls_rli_info_t*)globus_list_first(p))->url);
00153             if (std::find(rlis.begin(), rlis.end(), url) == rlis.end())
00154               rlis.push_back(url);
00155           }
00156           globus_rls_client_free_list(rliinfo_list);
00157         }
00158         // Also check if this RLI is not LRC
00159         bool good_lrc = true;
00160         err = globus_rls_client_lrc_rli_list(h, &rliinfo_list);
00161         if (err != GLOBUS_SUCCESS) {
00162           globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00163                                        GLOBUS_FALSE);
00164           if (errcode == GLOBUS_RLS_INVSERVER)
00165             good_lrc = false;
00166           else if (errcode == GLOBUS_RLS_RLI_NEXIST) {
00167             // Top level LRC server !?
00168           }
00169           else {
00170             good_lrc = false;
00171             logger.msg(INFO,
00172                        "Warning: can't get list of RLIs from server %s: %s",
00173                        url.str(), errmsg);
00174           }
00175         }
00176         else { // Add obtained RLIs to list
00177           for (globus_list_t *p = rliinfo_list; p; p = globus_list_rest(p)) {
00178             URL url(((globus_rls_rli_info_t*)globus_list_first(p))->url);
00179             if (std::find(rlis.begin(), rlis.end(), url) == rlis.end())
00180               rlis.push_back(url);
00181           }
00182           globus_rls_client_free_list(rliinfo_list);
00183         }
00184         if (good_lrc) { // Add LRC to list
00185           if (std::find(lrcs.begin(), lrcs.end(), url) == lrcs.end()) {
00186             lrcs.push_back(url);
00187             if (callback)
00188               if (!callback(h, url, arg)) {
00189                 globus_rls_client_close(h);
00190                 return false;
00191               }
00192           }
00193           lrc_num++;
00194         }
00195         globus_rls_client_close(h);
00196         if (bad_rli)
00197           rli_p = rlis.erase(rli_p);
00198         else
00199           ++rli_p;
00200       }
00201 
00202     if (down)  // Go down through hierarchy looking for new RLIs and LRCs
00203       for (rli_p = rlis.begin(); rli_p != rlis.end();) {
00204         globus_rls_handle_t *h = NULL;
00205         const URL& url = *rli_p;
00206         logger.msg(INFO, "Contacting %s", url.str());
00207         err = globus_rls_client_connect
00208                 (const_cast<char*>(url.ConnectionURL().c_str()), &h);
00209         if (err != GLOBUS_SUCCESS) {
00210           globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00211                                        GLOBUS_FALSE);
00212           logger.msg(INFO, "Warning: can't connect to RLS server %s: %s",
00213                      url.str(), errmsg);
00214           rli_p = rlis.erase(rli_p);
00215           continue;
00216         }
00217         globus_list_t *senderinfo_list;
00218         err = globus_rls_client_rli_sender_list(h, &senderinfo_list);
00219         if (err != GLOBUS_SUCCESS) {
00220           globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00221                                        GLOBUS_FALSE);
00222           if (errcode == GLOBUS_RLS_INVSERVER) { // Not an RLI server
00223             globus_rls_client_close(h);
00224             rli_p = rlis.erase(rli_p);
00225             continue;
00226           }
00227           else if (errcode == GLOBUS_RLS_LRC_NEXIST) { // Empty RLI
00228             globus_rls_client_close(h);
00229             ++rli_p;
00230             continue;
00231           }
00232           else {
00233             logger.msg(INFO,
00234                        "Warning: can't get list of senders from server %s: %s",
00235                        url.str(), errmsg);
00236             globus_rls_client_close(h);
00237             ++rli_p;
00238             continue;
00239           }
00240         }
00241         else  // Add obtained LRCs to list
00242           for (globus_list_t *p = senderinfo_list; p; p = globus_list_rest(p)) {
00243             URL url(((globus_rls_sender_info_t*)globus_list_first(p))->url);
00244             if (std::find(lrcs.begin(), lrcs.end(), url) == lrcs.end())
00245               lrcs.push_back(url);
00246           }
00247         globus_rls_client_close(h);
00248         ++rli_p;
00249       }
00250 
00251     // Go through new LRCs and check if those are LRCs
00252     for (lrc_p = lrcs.begin(); lrc_p != lrcs.end();) {
00253       if (lrc_num) {
00254         ++lrc_p;
00255         lrc_num--;
00256         continue;
00257       }
00258       const URL& url = *lrc_p;
00259       globus_rls_handle_t *h = NULL;
00260       logger.msg(INFO, "Contacting %s", url.str());
00261       err = globus_rls_client_connect
00262               (const_cast<char*>(url.ConnectionURL().c_str()), &h);
00263       if (err != GLOBUS_SUCCESS) {
00264         globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00265                                      GLOBUS_FALSE);
00266         logger.msg(INFO, "Warning: can't connect to RLS server %s: %s",
00267                    url.str(), errmsg);
00268         lrc_p = lrcs.erase(lrc_p);
00269         continue;
00270       }
00271       err = globus_rls_client_lrc_rli_list(h, &rliinfo_list);
00272       if (err != GLOBUS_SUCCESS) {
00273         globus_rls_client_error_info(err, &errcode, errmsg, MAXERRMSG + 32,
00274                                      GLOBUS_FALSE);
00275         if (errcode == GLOBUS_RLS_INVSERVER) { // Not LRC
00276           globus_rls_client_close(h);
00277           lrc_p = lrcs.erase(lrc_p);
00278           continue;
00279         }
00280         else if (errcode == GLOBUS_RLS_RLI_NEXIST) { // Top level LRC server !?
00281           if (callback)
00282             if (!callback(h, url, arg)) {
00283               globus_rls_client_close(h);
00284               return false;
00285             }
00286           globus_rls_client_close(h);
00287           ++lrc_p;
00288           continue;
00289         }
00290         else {
00291           logger.msg(INFO,
00292                      "Warning: can't get list of RLIs from server %s: %s",
00293                      url.str(), errmsg);
00294           globus_rls_client_close(h);
00295           lrc_p = lrcs.erase(lrc_p);
00296           continue;
00297         }
00298       }
00299       else { // Call callback
00300         globus_rls_client_free_list(rliinfo_list);
00301         if (callback)
00302           if (!callback(h, url, arg)) {
00303             globus_rls_client_close(h);
00304             return false;
00305           }
00306       }
00307       globus_rls_client_close(h);
00308       ++lrc_p;
00309       lrc_num++;
00310     }
00311     return true;
00312   }
00313 
00314 } // namespace Arc