summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/LWFT/AppSideReg.cpp
blob: 1fd659402e7c1f310b8b467bda4461f192877ecf (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
140
141
142
143
144
145
146
147
148
// -*- C++ -*-
// $Id$

/**
 *  @file  C++ Implementation: AppSideReg
 *
 *  @brief Defines implementation of AppSideReg.
 *
 */
#include "ace/Barrier.h"

#include "AppSideReg.h"
#include "AppOptions.h"

AppSideReg::AppSideReg (CORBA::ORB_ptr orb)
  : HM_ior_ (AppOptions::instance ()->host_monitor_ior ()),
    orb_ (CORBA::ORB::_duplicate (orb))
{
}

AppSideReg::~AppSideReg (void)
{
  // Now that this class is no longer a thread, the line below
  // will have to appear somewhere else.
//  monitor_->stop ();
}

int
AppSideReg::register_process (void)
{
  try
    {
      CORBA::Object_var obj = orb_->string_to_object (HM_ior_.c_str());

      if (CORBA::is_nil (obj))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "AppSideReg::svc: nil HM reference\n"),
                            1);
        }

      /// Downcast the object reference to a reference of type HostMonitor.
      this->hmvar_ = HostMonitor::_narrow (obj);
      
      if (CORBA::is_nil (hmvar_))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "AppSideReg::svc: HostMonitor "
                             "narrow failed.\n"),
                            1);
        }

      // retrieve the heartbeat port number from the HostMonitor if no
      // port parameter has been given.
      if (AppOptions::instance ()->port () == 0)
	      {
	        try
	          {
	            port_ = hmvar_->heartbeat_port ();
	          }
	        catch (CORBA::SystemException &)
	          {
	            ACE_ERROR_RETURN ((LM_ERROR,
			                           "AppSideReg::svc: HostMonitor "
			                           "heartbeat_port () call failed.\n"),
			                          1);
	          }
	      }
      else
	      {
	        port_ = AppOptions::instance ()->port ();
	      }

      ACE_DEBUG ((LM_TRACE,
                  "AppSideReg: creating the host monitor\n"));

      ACE_Barrier internal_thread_barrier (2);
      AppSideMonitor_Thread *mon =
          new AppSideMonitor_Thread (internal_thread_barrier,
                                     port_);

      mon->activate ();

      //ACE_DEBUG ((LM_TRACE, "Host monitor activated\n"));

      internal_thread_barrier.wait ();
      
      // Store monitor in singleton so it will stay around.
      AppOptions::instance ()->monitor (mon);

      //ACE_DEBUG ((LM_TRACE, "AppSideReg::svc before registering process.\n"));

      try
        {
          //ACE_DEBUG ((LM_TRACE, "AppSideReg::svc - got heartbeat port %d from hm.\n", port_));
          CORBA::Boolean good_register =
            hmvar_->register_process (
              AppOptions::instance ()->process_id ().c_str (),
              AppOptions::instance ()->host_id ().c_str (),
              port_);
              
          if (good_register)
            {
	      /*
              ACE_DEBUG ((LM_TRACE,
                          "Registered successfully %s with host monitor.\n",
                          AppOptions::instance()->process_id().c_str())); 
	      */
            }
          else
            {
              ACE_DEBUG ((LM_ERROR,
                          "Registeration with the monitor failed. Maybe the "
                          "hostmonitor needs to be set to another "
                          "port range.\n"));
            }
        }
      catch (CORBA::Exception &)
        {
          ACE_DEBUG ((LM_ERROR,
                      "AppSideReg: exception from register_process.\n"));
          throw;
        }

      //ACE_DEBUG ((LM_DEBUG, "Registering process\n"));
    }
  catch (CORBA::Exception &ex)
    {
      ACE_PRINT_EXCEPTION (ex,
                           "AppSideReg: A CORBA exception was raised:");
      return -1;
    }
  catch (...)
    {
      ACE_DEBUG ((LM_ERROR, "AppSideReg: Unknown exception raised!"));
      return -1;
    }

  return 0;
}

void
AppSideReg::unregister_process (void)
{
  hmvar_->unregister_process (
    AppOptions::instance ()->process_id ().c_str ());
}