summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/ImplRepo_Service/Repository.cpp
blob: 37b9d4f73fe994b42dbf0fc6d99acbe102cc3697 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* -*- C++ -*- */
// $Id$
#include "Repository.h"

ACE_RCSID(ImplRepo_Service, Repository, "$Id$")

// Default Constructor
Repository::Repository ()
{
  this->repository_.open (ACE_Naming_Context::PROC_LOCAL);
}

// Add a new server to the Repository
int
Repository::add (const char *key, const Repository::Record &rec)
{
  char *temp; // Temporary string to hold all the variables in.

  // Needs to be as long as all the lengths plus the separators and the
  // null character (6) and then also the size that the host/port could
  // take up.
  ACE_NEW_RETURN (temp,
                  char [ACE_OS::strlen (rec.comm_line)
                        + ACE_OS::strlen (rec.env)
                        + ACE_OS::strlen (rec.wdir)
                        + ACE_OS::strlen (rec.ping_ior)
                        + ACE_OS::strlen (rec.host)
                        + 40],
                  -1);

  // Put them all in a string
  // Why use the extra space?  Well, strtok doesn't like null strings
  // because they show up as \n\n, which it skips past.
  ACE_OS::sprintf (temp,
                   " %s\n %s\n %s\n %s\n %hu\n %s\n",
                   rec.comm_line,
                   rec.env,
                   rec.wdir,
                   rec.host,
                   rec.port,
                   rec.ping_ior);

  // Store the record in the repository.
  int retval = this->repository_.bind (key, temp);

  // Clean up and exit.
  delete [] temp;

  return retval;
}

int 
Repository::update (const char *key, const Repository::Record &rec)
{
  this->remove (key);
  return this->add (key, rec);
}

// Removes the server from the Repository
int
Repository::remove (const char *key)
{
  return this->repository_.unbind (key);
}


// Find the key record in the Repository
int
Repository::resolve (const char *key, Repository::Record &rec)
{
  char *value = 0, *type = 0; // Temp variables needed for resolve
  int retval = this->repository_.resolve (key, value, type);

  if (retval == 0)
    {
      // +1 to get rid of the space
      rec.comm_line = ACE::strnew (ACE_OS::strtok (value, "\n") + 1);
      rec.env = ACE::strnew (ACE_OS::strtok (NULL, "\n") + 1);
      rec.wdir = ACE::strnew (ACE_OS::strtok (NULL, "\n") + 1);
      rec.host = ACE::strnew (ACE_OS::strtok (NULL, "\n") + 1);
      ::sscanf (ACE_OS::strtok (NULL, "\n"), "%hu", &rec.port);
      rec.ping_ior = ACE::strnew (ACE_OS::strtok (NULL, "\n") + 1);
    }
  else
    {
      retval = -1;
    }

  delete [] value;
  delete [] type;

  return retval;
}
// = Accessor methods

int
Repository::get_comm_line (const char *key, char *&comm_line)
{
  Repository::Record rec;
  int retval = this->resolve (key, rec);

  if (retval == 0)
    {
      comm_line = rec.comm_line;
      delete [] rec.env;
      delete [] rec.wdir;
      delete [] rec.ping_ior;
    }

  return retval;
}


int
Repository::get_env (const char *key, char *&env)
{
  Repository::Record rec;
  int retval = this->resolve (key, rec);

  if (retval == 0)
    {
      delete [] rec.comm_line;
      env = rec.env;
      delete [] rec.wdir;
      delete [] rec.host;
      delete [] rec.ping_ior;
    }

  return retval;
}

int
Repository::get_wdir (const char *key, char *&wdir)
{
  Repository::Record rec;
  int retval = this->resolve (key, rec);

  if (retval == 0)
    {
      delete [] rec.comm_line;
      delete [] rec.env;
      wdir = rec.wdir;
      delete [] rec.host;
      delete [] rec.ping_ior;
    }

  return retval;
}

int
Repository::get_hostport (const char *key, char *&host, unsigned short &port)
{
  Repository::Record rec;
  int retval = this->resolve (key, rec);

  if (retval == 0)
    {
      delete [] rec.comm_line;
      delete [] rec.env;
      delete [] rec.wdir;
      host = rec.host;
      port = rec.port;
      delete [] rec.ping_ior;
    }

  return retval;
}

int
Repository::get_ping_ior (const char *key, char *&ping_ior)
{
  Repository::Record rec;
  int retval = this->resolve (key, rec);

  if (retval == 0)
    {
      delete [] rec.comm_line;
      delete [] rec.env;
      delete [] rec.wdir;
      delete [] rec.host;
      ping_ior = rec.ping_ior;
    }

  return retval;
}

void
Repository::dump (void)
{
  this->repository_.dump ();
}