summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/NodeManager/MonitorController.cpp
blob: fc009f1b3fe4fb586d33efb463cc9e0417bda522 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// $Id$
//----------------------------------------------------------------------------------
/**
 * @file MonitorController.cpp
 *
 * @brief The Monitor Controller implementation.
 *
 * This is the facade class for Monitor
 *
 * @author Nilabja Roy <nilabjar@dre.vanderbilt.edu>
 */
//----------------------------------------------------------------------------------

#include "MonitorController.h"
#include "BaseMonitor.h"
#include "MonitorCB.h"
#include "CIAO_common.h"

#include "ace/Log_Msg.h"
#include "ace/DLL.h"
#include "ace/SString.h"

namespace CIAO
{
  typedef MonitorBase* (*MonitorFactory) (void);

  /// for the CIAO monitor
  const char* monitor_lib_name = "ciaomonlib";

  // The interval after which update will be sent.
  // This value will sent by the EM in the later implementation
  const int interval = 10;

  static const char* factory_func = "createMonitor";
}

CIAO::MonitorController::MonitorController (CORBA::ORB_ptr orb,
                                            ::Deployment::Domain& domain,
                                            ::Deployment::TargetManager_ptr target
                                            )
  : target_facet_i_ (::Deployment::TargetManager::_duplicate (target)),
    terminate_flag_ (0),
    orb_ (orb),
    initial_domain_ (domain)
{
}

int CIAO::MonitorController::svc (void)
{
    ACE_DLL dll;

  // forming the library name
  ACE_CString lib_name = ACE_DLL_PREFIX;
  lib_name  += monitor_lib_name;

  int retval
    = dll.open (lib_name.c_str ());

  if (retval != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p",
                       "dll.open"),
                      -1);


  MonitorFactory factory =
    (MonitorFactory) dll.symbol (factory_func);

  if (factory == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p",
                       "dll.symbol"),
                      -1);
  {
    ACE_TRACE ((LM_DEBUG "Inside the init call\n"));

    monitor_.reset ((MonitorBase*) factory ());
    monitor_->initialize_params (initial_domain_,
                                 target_facet_i_.in (),
                                 interval);


    // Start the Monitor
    monitor_->start (orb_);
    auto_ptr <CIAO::MonitorCB> monitor_callback (new CIAO::MonitorCB (orb_,
                                                                      target_facet_i_.in (),
                                                                      interval));

    // The loop in which UpdateData is called
    while (!terminating ())
      {
        // data will be updated in intervals of 10 secs.
        // in the latest version of spec , this value will
        // come from Execution Manager
        ACE_OS::sleep (interval);
        //      ACE_DEBUG ((LM_DEBUG , "=The Terminate is %d\n", terminate_flag_));
        ::Deployment::Domain* domain =
          monitor_->get_current_data ();

        monitor_callback->update_data (*domain);
      }
    monitor_->stop ();
  }

  dll.close ();

  if (CIAO::debug_level () > 9)
    {
      ACE_DEBUG ((LM_DEBUG , "Terminating Monitor\n"));
    }
  return 0;
}

CIAO::MonitorController::~MonitorController ()
{
  terminate ();
  wait ();
}

void CIAO::MonitorController::terminate ()
{
  // make the terminate flag false
  ACE_GUARD (ACE_SYNCH_MUTEX,
             guard,
             lock_
             );
  ACE_DEBUG ((LM_DEBUG , "WITHIN TERMINATE CALL  ......"));
  terminate_flag_=1;
}

bool CIAO::MonitorController::terminating ()
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX,
                    guard,
                    lock_,
                    0
                    );
  return terminate_flag_;
}