summaryrefslogtreecommitdiff
path: root/TAO/examples/CSD_Strategy/ThreadPool2/FooServantList.cpp
blob: 1c1fc13eaf6a0eee209306b84c7a81dad812dc2b (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
// $Id$
#include "FooServantList.h"
#include "Foo_i.h"
#include "OrbShutdownTask.h"
#include "ace/OS.h"


FooServantList::FooServantList(const char* prefix,
                               unsigned    num_servants,
                               unsigned    num_clients,
                               CORBA::ORB_ptr orb)
  : prefix_(prefix),
    num_servants_(num_servants),
    num_clients_(num_clients),
    orb_ (CORBA::ORB::_duplicate(orb))
{
  this->servants_ = new PortableServer::ServantBase_var[num_servants];
}


FooServantList::~FooServantList()
{
  delete [] this->servants_;
}


void
FooServantList::create_and_activate(PortableServer::POA_ptr poa
                                    ACE_ENV_ARG_DECL)
{
  for (unsigned i = 0; i < this->num_servants_; i++)
    {
      char buf[32];
      ACE_OS::sprintf(buf, "%02d", i + 1);
      ACE_CString servant_name = this->prefix_ + "_" + buf;

      this->servants_[i] = new Foo_i(servant_name.c_str(),this);

      PortableServer::ObjectId_var id =
                    PortableServer::string_to_ObjectId(servant_name.c_str());

      poa->activate_object_with_id(id.in(), 
                                   this->servants_[i].in() 
                                   ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      CORBA::Object_var obj = poa->id_to_reference(id.in()
                                                   ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      if (CORBA::is_nil(obj.in()))
        {
          ACE_ERROR((LM_ERROR,
                     "(%P|%t) Failed to activate servant (%s).\n",
                     servant_name.c_str()));
          ACE_THROW (TestException());
        }

      CORBA::String_var ior 
        = this->orb_->object_to_string(obj.in() 
                                       ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      ACE_CString filename = servant_name + ".ior";
      FILE* ior_file = ACE_OS::fopen(filename.c_str(), "w");

      if (ior_file == 0)
        {
          ACE_ERROR((LM_ERROR,
                     "(%P|%t) Cannot open output file (%s) for writing IOR.",
                     filename.c_str()));
          ACE_THROW (TestException());
        }

      ACE_OS::fprintf(ior_file, "%s", ior.in());
      ACE_OS::fclose(ior_file);
    }
}


void
FooServantList::client_done(ACE_ENV_SINGLE_ARG_DECL)
{
  unsigned num_left;

  {
    GuardType guard(this->num_clients_lock_);
    num_left = --this->num_clients_;
  }

  if (num_left == 0)
    {
      if (TheOrbShutdownTask::instance()->open(0) != 0)
        {
          ACE_ERROR((LM_ERROR, "(%P|%t)FooServantList::client_done: "
            "failed to create orb shutdown thread.\n"));
        }        
    }
}