summaryrefslogtreecommitdiff
path: root/CIAO/ciao/FTComponentServer/AppMonitor/AppSideReg.cpp
blob: 62afb94113b8f70b144c9d1b81b171e19ba1acb5 (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
/**
 *  @file  C++ Implementation: AppSideReg
 *
 *  @brief Defines implementation of AppSideReg.
 *
 */

#include "AppSideReg.h"
#include "monitorC.h"
#include "ace/Barrier.h"
#include <sstream>
#include <stdexcept>
#include <algorithm>
#include <iostream>
#include "Name_Helper_T.h"

AppSideReg::AppSideReg(ACE_Barrier *ext_barrier, CORBA::ORB_ptr orb)
  : orb_ (CORBA::ORB::_duplicate (orb)),
    external_barrier_ (ext_barrier)
{
}


AppSideReg::~AppSideReg()
{
  monitor_->stop ();
  orb_->destroy ();
}

void AppSideReg::unregister_process (void)
{
  hmvar_->unregister_process (this->get_process_id ().c_str ());
}

int AppSideReg::svc(void)
{
  try
  {
    Name_Helper_T <HostMonitor> hmh (orb_.in (), true);

    hmvar_ = hmh.resolve ("FLARe/" + 
			  hmh.escape_dots (this->get_hostname ()) + 
			  "/HostMonitor");

    u_short port = hmvar_->heartbeat_port (this->get_process_id ().c_str ());
    
    ACE_Barrier internal_thread_barrier (2);
    monitor_ = std::auto_ptr <AppSideMonitor_Thread> 
      (new AppSideMonitor_Thread (&internal_thread_barrier,
				  port));
    monitor_->activate ();
    
    //ACE_DEBUG ((LM_DEBUG, "Monitor activated\n"));
    
    internal_thread_barrier.wait ();
      
    /// Waiting for the AppSideMonitor_Thread to finish its socket stuff.
    try
      {
        hmvar_->dump ();
      }
    catch (CORBA::Exception &)
      {
        ACE_DEBUG ((LM_DEBUG,"exception from dump.\n"));
        throw;
      }

    try
      {
        if (hmvar_->register_process (
              this->get_process_id ().c_str (),
              this->get_hostname ().c_str (),
              port))
          {
          }
        else
          {
            ACE_DEBUG ((LM_ERROR, "Registeration with the monitor failed.\n"));
          }
      }
    catch (CORBA::Exception &)
      {
	ACE_DEBUG ((LM_DEBUG,"exception from register_process.\n"));
	throw;
      }


    //ACE_DEBUG ((LM_DEBUG, "Registering process\n"));
  }
  catch (CORBA::Exception &ex)
  {
    ACE_PRINT_EXCEPTION (ex, "AppSideReg::svc -  CORBA exception was raised:");
    return -1;
  }
  catch (Name_Helper_Exception & ex)
    {
      ACE_ERROR ((LM_ERROR, "AppSideReg::svc - "
		   "Name helper exception: %s", ex.what ()));
    }
  catch (...)
  {
    ACE_DEBUG((LM_ERROR, "AppSideReg::svc - Unknown exception raised!"));
    return -1;
  }

  //ACE_DEBUG ((LM_DEBUG, "AppSideReg::svc waiting on barrier.\n"));

  external_barrier_->wait ();
  return 0;
}

std::string
AppSideReg::get_hostname ()
{
  char hn_str [100];
  gethostname (hn_str, sizeof (hn_str));
  
  return std::string (hn_str);
}

std::string
AppSideReg::get_process_id ()
{
  pid_t pid = ACE_OS::getpid ();
  std::stringstream ss;
  ss << pid;
  
  return ss.str ();
}