summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/ImplRepo_Service/Forwarder.cpp
blob: 58513682945a0ea334b5731591d5ba67042da55c (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
//=============================================================================
/**
 *  @file   Forwarder.cpp
 *
 *  $Id$
 *
 *  @brief  Definition of ImR_Forwarder
 *
 *  @author Darrell Brunsch <brunsch@cs.wustl.edu>
 */
//=============================================================================

#include "Forwarder.h"
#include "ImplRepo_i.h"
#include "Options.h"

#include "tao/PortableServer/Object_Adapter.h"

/**
 * This constructor takes in orb and ImplRepo_i pointers to store for later
 * use.  It also grabs a reference to the POACurrent object for use in
 * preinvoke.
 */
ImR_Forwarder::ImR_Forwarder (ImplRepo_i *imr_impl)
  : imr_impl_ (imr_impl)
{
  CORBA::ORB_var orb = OPTIONS::instance ()->orb ();

  ACE_ASSERT (imr_impl != 0);

  ACE_TRY_NEW_ENV
    {
      CORBA::Object_var tmp =
        orb->resolve_initial_references ("POACurrent" TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      this->poa_current_var_ =
        PortableServer::Current::_narrow (tmp.in () TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      // Ignore
    }
  ACE_ENDTRY;

  ACE_ASSERT (!CORBA::is_nil (this->poa_current_var_.in ()));
}



/**
 * We figure out the intended recipient from the POA name.  After activating
 * the server, we throw a forwarding exception to the correct server.
 *
 * The big complicated thing here is that we have to create the forwarding
 * ior based on what we already have.  So we combine the endpoint received
 * from activate_server_i and append the objectid from the request to it.
 *
 * @todo Should we base the name on the entire POA hierarchy?
 * @todo Use ACE_TString for the IOR?  Can it always be a char?
 */
PortableServer::Servant
ImR_Forwarder::preinvoke (const PortableServer::ObjectId &,
                          PortableServer::POA_ptr poa,
                          const char *,
                          PortableServer::ServantLocator::Cookie &
                          TAO_ENV_ARG_DECL)
    ACE_THROW_SPEC ((CORBA::SystemException, PortableServer::ForwardRequest))
{
  CORBA::ORB_var orb = OPTIONS::instance ()->orb ();

  ACE_TString ior;
  CORBA::Object_ptr forward_obj = CORBA::Object::_nil ();

  ACE_TRY
    {
      // Activate.

      ior = this->imr_impl_->activate_server_i (poa->the_name (),
                                                1
                                                TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Add the key

      char *key_str = 0;

      // Unlike POA Current, this implementation cannot be cached.
      TAO_POA_Current *tao_current =
        ACE_dynamic_cast(TAO_POA_Current*, this->poa_current_var_.in ());
      TAO_POA_Current_Impl *impl = tao_current->implementation ();
      TAO_ObjectKey::encode_sequence_to_string (key_str,
                                                impl->object_key ());

      ior += key_str;
      CORBA::string_free (key_str);

      if (OPTIONS::instance()->debug () >= 2)
        ACE_DEBUG ((LM_DEBUG, "Forwarding to %s\n", ior.c_str ()));

      forward_obj = orb->string_to_object (ior.c_str () TAO_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

    }
  ACE_CATCH (CORBA::SystemException, sysex)
    {
      ACE_PRINT_EXCEPTION (sysex, "Forwarder system exception");
      ACE_RE_THROW;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Forwarder exception");
      ACE_TRY_THROW (CORBA::TRANSIENT (
          CORBA_SystemException::_tao_minor_code (TAO_IMPLREPO_MINOR_CODE, 0),
          CORBA::COMPLETED_NO));
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (0);

  if (!CORBA::is_nil (forward_obj))
    ACE_THROW_RETURN (PortableServer::ForwardRequest (forward_obj), 0);
  else
    ACE_ERROR ((LM_ERROR,
                "Error: Forward_to reference is nil.\n"));

  ACE_THROW_RETURN (CORBA::OBJECT_NOT_EXIST (), 0);
}

/**
 * The postinvoke method just deletes the passed in servant.
 */
void
ImR_Forwarder::postinvoke (const PortableServer::ObjectId &,
                           PortableServer::POA_ptr ,
                           const char * ,
                           PortableServer::ServantLocator::Cookie ,
                           PortableServer::Servant servant
                           TAO_ENV_ARG_DECL_NOT_USED)
    ACE_THROW_SPEC ((CORBA::SystemException))
{
  delete servant;
}