SAGA C++ API 1.6
service_description.cpp
Go to the documentation of this file.
00001 //  Copyright (c) 2009 STFC
00002 // 
00003 //  Distributed under the Boost Software License, Version 1.0. (See accompanying 
00004 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00005 #define SAGA_NO_IMPORT_ATTRIBUTE
00006 #include <iostream>
00007 #include <string>
00008 #include <vector>
00009 
00010 #include <boost/shared_ptr.hpp>
00011 
00012 #include <boost/assign/list_inserter.hpp>
00013 #include <boost/assign/std/vector.hpp>
00014 
00015 #include <saga/saga/sd.hpp>
00016 #include <saga/impl/sd.hpp>
00017 #include <saga/saga/exception.hpp>
00018 
00019 #include <saga/saga/detail/attribute_impl.hpp>
00020 
00021 #define SD_DEBUG 0
00022 
00024 namespace saga
00025 {
00026   namespace sd {
00027 
00028   service_description::service_description (saga::url loc)
00029     : object (new saga::impl::service_description())
00030   {
00031       using namespace boost::assign;
00032 
00033       // initialize list of valid keys
00034       static char const* const valid_keys[] = 
00035       {
00036           attributes::service_description_url,
00037           attributes::service_description_type,
00038           attributes::service_description_uid,
00039           attributes::service_description_site,
00040           attributes::service_description_name,
00041           attributes::service_description_relatedservices,
00042           attributes::service_description_information_provider_url,
00043           attributes::service_description_vo,
00044           attributes::service_description_interface_version,
00045           attributes::service_description_implementation_version,
00046           attributes::service_description_implementor,
00047           attributes::service_description_capability,
00048           NULL
00049       };
00050       this->init_keynames(valid_keys);
00051 
00052       static char const* const attributes_scalar_rw[] = 
00053       {
00054           attributes::service_description_url,
00055           attributes::service_description_type,
00056           attributes::service_description_uid,
00057           attributes::service_description_site,
00058           attributes::service_description_name,
00059           attributes::service_description_information_provider_url,
00060           attributes::service_description_interface_version,
00061           attributes::service_description_implementation_version,
00062           attributes::service_description_implementor,
00063           NULL,
00064       };
00065 
00066       static char const* const attributes_vector_rw[] = 
00067       {
00068           attributes::service_description_relatedservices,
00069           attributes::service_description_vo,
00070           attributes::service_description_capability,
00071           NULL,
00072       };
00073 
00074       // initialize attribute implementation
00075       this->init(NULL, attributes_scalar_rw, NULL, attributes_vector_rw);
00076       this->init(false, true); // not extensible and cache only implementation
00077 
00078       // initialize the information_provider_url attribute
00079       set_attribute(attributes::service_description_information_provider_url,
00080           loc.get_url());
00081   }
00082 
00083   service_description::service_description(saga::impl::service_description *impl, saga::url loc)
00084     : object (impl)
00085   {
00086     //std::cout << "SC2: Uid = " << this->get_id().string() << " Type = " << this->get_type() << std::endl;
00087 
00088     // initialize the information_provider_url attribute
00089     set_attribute(attributes::service_description_information_provider_url,
00090         loc.get_url());
00091   }
00092 
00093   service_description::~service_description (void) SAGA_THROW_SPEC(throw())
00094   {
00095   }
00096 
00098   service_description::service_description (saga::object const& o)
00099     : saga::object (o)
00100   {
00101       if (this->get_type() != saga::object::ServiceDescription)
00102       {
00103           SAGA_THROW("Bad type conversion.", saga::BadParameter);
00104       }
00105   }
00106 
00107   service_description &service_description::operator= (saga::object const& o)
00108      SAGA_THROW_SPEC(throw())
00109   {
00110       return saga::object::operator=(o), *this;
00111   }
00112 
00113   saga::impl::service_description* service_description::get_impl() const
00114   { 
00115       typedef saga::object base_type;
00116       return static_cast<saga::impl::service_description*>(this->base_type::get_impl()); 
00117   }
00118 
00119   TR1::shared_ptr <saga::impl::service_description> service_description::get_impl_sp(void) const
00120   { 
00121     // FIXME: this needs documentation
00122     typedef saga::object base_type;
00123     return TR1::static_pointer_cast <saga::impl::service_description>(
00124         this->base_type::get_impl_sp());
00125   }
00126 
00128   std::string service_description::get_url() SAGA_THROW_SPEC(throw())
00129   {
00130      if (!this->attribute_exists(attributes::service_description_url)) 
00131      {
00132         SAGA_THROW("Attribute Url does not exist", saga::BadParameter);
00133      }
00134      return this->get_attribute(attributes::service_description_url);
00135   }
00136 
00137   saga::sd::service_data &service_description::get_data() SAGA_THROW_SPEC(throw())
00138   {
00139     return get_impl()->get_data();
00140   }
00141 
00142   std::vector<saga::sd::service_description>
00143      service_description::get_related_services()
00144         SAGA_THROW_SPEC(throw(saga::authorization_failed,
00145               saga::authentication_failed,
00146               saga::timeout,
00147               saga::no_success))
00148   {
00149      std::vector<saga::sd::service_description> rsvc_list;
00150      
00151      if (!this->attribute_exists(attributes::service_description_relatedservices)) 
00152      {
00153         return rsvc_list;
00154         //SAGA_THROW("Attribute Related_Services does not exist", saga::BadParameter);
00155      }
00156      else
00157      {
00158         std::vector<std::string> rsvc_uids = this->get_vector_attribute(
00159                                             attributes::service_description_relatedservices);
00160         if( rsvc_uids.size() > 0)
00161         {
00162            std::string vo_filter = "";
00163            std::string data_filter = "";
00164            std::string svc_filter = "";
00165            if (rsvc_uids.size() > 1) svc_filter = "(";
00166            for (unsigned int i = 0; i < rsvc_uids.size(); i++) 
00167            {
00168                svc_filter.append("(Uid = '");
00169                svc_filter.append(rsvc_uids[i]);
00170                svc_filter.append("')");
00171                if (i != (rsvc_uids.size()-1) && rsvc_uids.size() > 1)
00172                   svc_filter.append(" OR ");
00173            }
00174            if (rsvc_uids.size() > 1) svc_filter.append(")");
00175 #if SD_DEBUG
00176            std::cout << "Related service filter = " << svc_filter << std::endl;
00177 #endif
00178            //Get information provider URL from our attributes
00179            std::string loc;
00180            if ( attribute_exists(attributes::service_description_information_provider_url) )
00181            {
00182               loc = get_attribute(attributes::service_description_information_provider_url);
00183            }
00184 
00185            discoverer d (this->saga::object::get_impl()->get_session(), loc);
00186            rsvc_list = d.list_services(svc_filter, data_filter, vo_filter);
00187         }
00188      }
00189      return rsvc_list;
00190   }
00191 
00192   void service_description::set_data(std::string data_key, std::string data_value)
00193   {
00194       get_impl()->set_data(data_key, data_value);
00195   }
00196 
00197   void service_description::set_session(TR1::shared_ptr<saga::session> sess)
00198   {
00199      static_cast<impl::service_description*>(
00200         this->saga::object::get_impl())->set_session(sess);
00201   }
00202  } // namespace sd
00203 
00204   namespace detail
00205   {
00207     //  implement the attribute functions (we need to explicitly specialize
00208     //  the template because the functions are not implemented inline)
00209     template struct SAGA_SD_PACKAGE_EXPORT_REPEAT attribute<sd::service_description>;
00210 
00211     template struct SAGA_SD_PACKAGE_EXPORT attribute_priv<sd::service_description, task_base::Async>;
00212     template struct SAGA_SD_PACKAGE_EXPORT attribute_priv<sd::service_description, task_base::Task>;
00213     template struct SAGA_SD_PACKAGE_EXPORT attribute_priv<sd::service_description, task_base::Sync>;
00214 
00215     template struct SAGA_SD_PACKAGE_EXPORT attribute_sync<sd::service_description>;
00216   }
00217 
00219 } // namespace saga
00220 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines