summaryrefslogtreecommitdiff
path: root/TAO/tests/Bug_3499_Regression/DLL_Service_Host.cpp
blob: 3bbdb0f0d4a61dc2bdb30e853a932d9057c4a9b5 (plain)
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
// $Id$

#include "DLL_Service.h"
#include "ace/DLL.h"
#include "ace/Service_Gestalt.h"
#include "ace/Service_Config.h"
#include "ace/CORBA_macros.h"
#include "ace/OS_NS_unistd.h"
//
// main
//
int ACE_TMAIN (int argc, ACE_TCHAR * argv [])
{
  try
  {
    ACE_DLL module;

    ACE_Service_Gestalt * gestalt = 0;
    ACE_NEW_NORETURN (gestalt, ACE_Service_Gestalt ());
    if (gestalt == 0)
      {
        ACE_throw_bad_alloc;
      }

    ACE_Intrusive_Auto_Ptr <ACE_Service_Gestalt> auto_clean (gestalt);

    // Without the following line, the application will crash while ACE
    // is *cleaning* the ACE_Service_Config::global () object. We want
    // to force all services to be loaded under this configuration.
    ACE_Service_Config_Guard guard (gestalt);

    if (module.open (ACE_TEXT ("Bug_3499_Regression_ACE_DLL_TAO_Service")) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("failed to load ACE_DLL_TAO_Service\n")),
                         -1);
    }

    void * symbol = module.symbol (ACE_TEXT ("_make_ACE_DLL_TAO_Service"));

    if (symbol == 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("failed to load symbol _make_ACE_DLL_TAO_Service\n")),
                         -1);
    }

    typedef ACE_DLL_Service * (*factory_type) (void);
    ptrdiff_t tmp_ptr = reinterpret_cast <ptrdiff_t> (symbol);
    factory_type f = reinterpret_cast <factory_type> (tmp_ptr);

    ACE_DLL_Service * svc = (*f) ();

    if (svc != 0)
    {
      // Initialize the service.
      //
      // If '-ORBGestalt CURRENT' does not appear in the command-line options,
      // which is a fix for the bug in question, then the service will fail
      // when trying to resolve to RootPOA.
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) %T - %M - initializing the loaded service\n")));
      svc->init (argc, argv);

      // Sleep for a few seconds
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) %T - %M - sleeping for 5 seconds\n")));
      ACE_OS::sleep (5);

      // Finalize the service.
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) %T - %M - finalizing the service\n")));
      svc->fini ();

      // Destroy the service.
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) %T - %M - destroying the service\n")));
      svc->destroy ();
    }
  }
  catch (...)
  {
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%t) %T - %M - caught unknown exception\n")));
  }

  return 0;
}