summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/PSS/PSDL_Code_Gen.cpp
blob: 5148e9085d52e03683b97c4d9f1bd1125e742a4f (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
149
150
// -*- C++ -*-
// $Id$

#include "PSDL_Code_Gen.h"
#include "PSDL_Datastore.h"
#include "tao/OctetSeqC.h"
#include "tao/AnyTypeCode/Any.h"
#include "tao/CodecFactory/CodecFactory.h"

ACE_RCSID (PSS, PSDL_Code_Gen, "$Id$")

TAO_PSDL_Code_Gen::TAO_PSDL_Code_Gen (CORBA::ORB_ptr orb)
  : file_name_ ("Data_Store"),
    psdl_datastore_ (),
    orb_ (orb),
    codec_ (0)
{
  this->set_codec ();
  ACE_NEW (this->psdl_datastore_,
           TAO_PSDL_Datastore);

}

TAO_PSDL_Code_Gen::~TAO_PSDL_Code_Gen (void)
{
  delete this->psdl_datastore_;
}

int
TAO_PSDL_Code_Gen::set_codec (void)
{

  // Obtain a reference to the CodecFactory.
  CORBA::Object_var obj =
    this->orb_->resolve_initial_references ("CodecFactory");

  IOP::CodecFactory_var codec_factory =
    IOP::CodecFactory::_narrow (obj.in ());

  // Set up a structure that contains information necessary to
  // create a GIOP 1.1 CDR encapsulation Codec.
  IOP::Encoding encoding;
  encoding.format = IOP::ENCODING_CDR_ENCAPS;
  encoding.major_version = 1;
  encoding.minor_version = 1;

  // Obtain the CDR encapsulation Codec.
  this->codec_ =
    codec_factory->create_codec (encoding);

  if (this->codec_.in () == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "codec pointer not set correctly\n"));
      return -1;
    }
  return 0;
}

int
TAO_PSDL_Code_Gen::set_name_obj_ref (const char *name,
                                     const char *string_obj_ref)
{
  // Invoke the helper encode method which will
  // convert the stringified object reference to a CORBA::OctetSeq.
  // Insert the name-CORBA::OCtetSeq pair to a hash_map and save the
  // hash_map to the database.

  // Encode the stringified object reference to a CORBA::OctetSeq *
  CORBA::OctetSeq_var octet_seq = this->encode (string_obj_ref);

  // Insert the new entry to the hash map which contains all the
  // name-octet_seq entries. And, write the hash_map to a file.
  int result = this->psdl_datastore_->bind (name,
                                            octet_seq.in ());

  if (result == -1)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Bind not done successfully\n"));
    }
  else if (result == 1)
    {
      /*ACE_DEBUG ((LM_DEBUG,
                    "Bind already done.\n"));
      */
      return 0;
    }

  return result;
}

const char *
TAO_PSDL_Code_Gen::get_obj_ref (const char *name)
{
  // Get from the hash_map saved in the database, the corresponding entry
  // (CORBA::OctetSeq *) for the name. Then, decode the octetseq to
  // get the stringified object reference and return it.

  CORBA::OctetSeq octet_seq;

  // Find the octet_seq for the name.
  int result = this->psdl_datastore_->find (name,
                                            octet_seq);

  if (result == 0)
    {
      // Decode the octet_seq.
      const char *obj_ref = this->decode (octet_seq);

      return CORBA::string_dup (obj_ref);
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "An entry for name %s is not found\n",
                  name));
      return 0;
    }
}


CORBA::OctetSeq *
TAO_PSDL_Code_Gen::encode (const char *string_obj_ref)
{
  CORBA::Any data;
  data <<= string_obj_ref;

  CORBA::OctetSeq *encoded_data = 0;

  encoded_data = this->codec_->encode (data);

  CORBA::OctetSeq_var safe_encoded_data = encoded_data;

  return safe_encoded_data._retn ();
}

const char *
TAO_PSDL_Code_Gen::decode (const CORBA::OctetSeq &data)
{
  const char *extracted_value;

  // Extract the data from the octet sequence.
  CORBA::Any_var decoded_data =
    this->codec_->decode (data);

  decoded_data.in() >>= extracted_value;

  return CORBA::string_dup (extracted_value);
}