#1 2012-05-07 19:42:33

simon
Member
Registered: 2012-05-07
Posts: 11

Loading multiple modules

//! [Activator]
#include "DictionaryService.h"

#include <usModuleActivator.h>
#include <usModuleContext.h>
#include <usServiceProperties.h>

// Replace that include with your own base class declaration
#include <usBase.h>

#include <algorithm>
#include <memory>

US_USE_NAMESPACE

/**
 * This class implements a module activator that uses the module
 * context to register an English language dictionary service
 * with the C++ Micro Services registry during static initialization
 * of the module. The dictionary service interface is
 * defined in a separate file and is implemented by a nested class.
 */
class US_ABI_LOCAL MyActivator : public ModuleActivator
{

private:

  /**
   * A private inner class that implements a dictionary service;
   * see DictionaryService for details of the service.
   */
  class DictionaryImpl : public Base, public DictionaryService
  {
    // The set of words contained in the dictionary.
    US_UNORDERED_SET_TYPE<std::string> m_dictionary;

  public:

    DictionaryImpl()
    {
      m_dictionary.insert("welcome");
      m_dictionary.insert("to");
      m_dictionary.insert("the");
      m_dictionary.insert("micro");
      m_dictionary.insert("services");
      m_dictionary.insert("tutorial");
    }

    /**
     * Implements DictionaryService.checkWord(). Determines
     * if the passed in word is contained in the dictionary.
     * @param word the word to be checked.
     * @return true if the word is in the dictionary,
     *         false otherwise.
     **/
    bool checkWord(const std::string& word)
    {
      std::string lword(word);
      std::transform(lword.begin(), lword.end(), lword.begin(), ::tolower);

      return m_dictionary.find(lword) != m_dictionary.end();
    }
  };

  std::auto_ptr<DictionaryImpl> m_dictionaryService;


public:

  /**
   * Implements ModuleActivator::Load(). Registers an
   * instance of a dictionary service using the module context;
   * attaches properties to the service that can be queried
   * when performing a service look-up.
   * @param context the context for the module.
   */
  void Load(ModuleContext* context)
  {
	std::cout << "I'm here!!!" << std::endl;
    m_dictionaryService.reset(new DictionaryImpl);
    ServiceProperties props;
    props["Language"] = std::string("English");
    context->RegisterService("foo", m_dictionaryService.get(), props);
  }

  /**
   * Implements ModuleActivator::Unload(). Does nothing since
   * the C++ Micro Services library will automatically unregister any registered services.
   * @param context the context for the module.
   */
  void Unload(ModuleContext* /*context*/)
  {
    // NOTE: The service is automatically unregistered
  }

};

US_EXPORT_MODULE_ACTIVATOR(DictionaryServiceModule, MyActivator)
//![Activator]

#include <usModuleInitialization.h>

US_INITIALIZE_MODULE("DictionaryServiceModule", "", "", "1.0.0")

Last edited by simon (2012-05-07 20:26:43)

Offline

#2 2012-05-08 01:45:13

sascha
Administrator
Registered: 2012-05-04
Posts: 45

Re: Loading multiple modules

US_INITIALIZE_MODULE("DictionaryServiceModule", "", "", "1.0.0")

Offline

#3 2012-05-08 14:09:17

simon
Member
Registered: 2012-05-07
Posts: 11

Re: Loading multiple modules

Hi and thanks! I got it working following your instructions smile.

I'll post a bug report now, but this is a holiday here so I'll be back for more questions probably tomorrow wink.

Last edited by simon (2012-05-08 14:10:16)

Offline

#4 2012-05-08 17:57:10

simon
Member
Registered: 2012-05-07
Posts: 11

Re: Loading multiple modules

Ok, I posted a bit fast sad.

The service is found, but calling dictionaryService->checkWord("Tutorial") doesn't seem to work. Actually I can call any method I define on the service, but if I add a debug trace in the method definition (service- side) it is not displayed, and the result displayed is always some random number.

$ ./build.sh && LD_LIBRARY_PATH="/usr/local/lib" ./testApp 
Success
I'm here!!!
CppMicroServices
MainModule
DictionaryServiceModule
1
(foo)
get service ref foo for module MainModule = 1 refs
Dictionary contains 'Tutorial': 99

(I also tried with a method "toUppercase" in DictionaryService and it doesn't work either).

Last edited by simon (2012-05-08 17:57:48)

Offline

#5 2012-05-08 21:45:30

sascha
Administrator
Registered: 2012-05-04
Posts: 45

Re: Loading multiple modules

DictionaryService* dictionaryService = (DictionaryService*) GetModuleContext()->GetService(dictionaryServiceRef);

Offline

#6 2012-05-08 23:00:01

simon
Member
Registered: 2012-05-07
Posts: 11

Re: Loading multiple modules

Thanks! I had moved to a C-cast because the compiler complained without usBase.h, and I did not think it made a difference besides the fact that it might be more prone to crashes.

I have everything working now.

Offline

Board footer

Powered by FluxBB