OmniEvents
naming.cc
Go to the documentation of this file.
1// -*- Mode: C++; -*-
2// Package : omniEvents
3// naming.cc Created : 1/10/99
4// Author : Paul Nader (pwn)
5//
6// Copyright (C) 1998 Paul Nader, 2003-2005 Alex Tingle.
7//
8// This file is part of the omniEvents application.
9//
10// omniEvents is free software; you can redistribute it and/or
11// modify it under the terms of the GNU Lesser General Public
12// License as published by the Free Software Foundation; either
13// version 2.1 of the License, or (at your option) any later version.
14//
15// omniEvents is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23//
24// Description:
25//
26// naming Service Utility functions.
27//
28
29/*
30 $Log: naming.cc,v $
31 Revision 1.8.2.2 2005/05/10 14:28:11 alextingle
32 Updated copyrights to 2005.
33
34 Revision 1.8.2.1 2005/04/27 20:49:31 alextingle
35 Merge across changes from HEAD branch (see CHANGES_262. Change version number ready for release 2.6.2.
36
37 Revision 1.9 2005/04/13 14:04:02 alextingle
38 Fixed bug in str2name() naming.cc, that causes a SEGV on HP-UX.
39
40 Revision 1.8 2004/10/08 14:27:59 alextingle
41 Changed local variable initialisation style back to using '=' in order to please MS VC++.
42
43 Revision 1.7 2004/09/25 23:12:28 alextingle
44 New method: Orb::reportObjectFailure() - flags unexpected failures at a higher
45 priority than normal non-fatal exceptions.
46
47 New macro: NP_MINORSTRING() - a safe interface to
48 CORBA::SystemException::NP_minorString() that returns "??" when there is no
49 mapping for the exception's minor code.
50
51 Revision 1.6 2004/08/04 08:13:44 alextingle
52 Unix daemon & Windows service now both working. Accessed through interface class Daemon (in daemon.h).
53
54 Revision 1.5 2004/07/26 21:17:49 alextingle
55 Added missing #include <string>
56
57 Revision 1.4 2004/07/26 16:22:25 alextingle
58 New method: str2name() parses a stringified naming service name info a CosNaming::Name.
59
60 Revision 1.3 2004/07/02 15:20:39 alextingle
61 Added daemonization, syslog & pidfile support on Unix.
62 Corrected trace levels for consistency with omniORB.
63
64 Revision 1.2 2004/04/21 10:01:42 alextingle
65 Removed unused code. Now silently fails if the Orb has no naming service ref.
66
67 Revision 1.1 2003/12/21 16:19:49 alextingle
68 Moved into 'src' directory as part of the change to POA implementation.
69
70 Revision 1.3 2003/12/01 09:03:13 alextingle
71 Now reports more specific exceptions (only with omniORB4).
72
73 Revision 1.2 2003/11/03 22:45:31 alextingle
74 Removed all platform specific switches. Now uses autoconf, config.h.
75
76 Revision 1.1.1.1 2002/09/25 19:00:35 shamus13
77 Import of OmniEvents source tree from release 2.1.1
78
79 Revision 1.3 2000/09/26 08:44:58 naderp
80 Added stdlib.h include for exit function.
81
82 Revision 1.2 2000/09/04 03:45:52 naderp
83 Changed headers.
84
85 Revision 1.1 1999/11/01 17:00:16 naderp
86 Initial revision
87
88*/
89
90#include "naming.h"
91
92#include <string>
93
94#ifdef HAVE_IOMANIP
95# include <iomanip>
96#else
97# include <iomanip.h>
98#endif
99
100#ifdef HAVE_STDLIB_H
101# include <stdlib.h> // for exit
102#endif
103
104ostream& operator<<(ostream& os, const CosNaming::Name &n)
105{
106 for(CORBA::ULong i=0; i<n.length(); i++)
107 {
108 os<<"/"<<n[i].id.in();
109 const char* kind =n[i].kind.in();
110 if(kind && kind[0])
111 os<<"."<<kind;
112 }
113 return os;
114}
115
116
117CosNaming::Name str2name(const char* namestr)
118{
119 CosNaming::Name name;
120 CORBA::ULong nameLen=0;
121 name.length(nameLen);
122
123 string n =namestr;
124 string::size_type pos=0;
125 char last='/';
126 while(true)
127 {
128 pos=n.find_first_not_of("/.",pos);
129 if(string::npos==pos) break;
130 string::size_type sep =n.find_first_of("/.",pos);
131 string piece =n.substr(pos, (string::npos==sep? sep: sep-pos) );
132 if(last=='/')
133 {
134 name.length(++nameLen);
135 name[nameLen-1].id=CORBA::string_dup(piece.c_str());
136 }
137 else
138 {
139 name[nameLen-1].kind=CORBA::string_dup(piece.c_str());
140 }
141 if(string::npos==sep) break;
142 pos=sep;
143 last=n[sep];
144 }
145 return name;
146}
147
148
150 CosNaming::NamingContext_ptr namingContext,
151 const CosNaming::Name& name,
152 CORBA::Object_ptr obj
153)
154{
155 // If there is no naming service, then ignore this call.
156 if(CORBA::is_nil(namingContext))
157 return 1;
158
159 try
160 {
161
162 CosNaming::Name n;
163 n.length(1);
164 // Drill down through contexts.
165 for(CORBA::ULong i=0; i<(name.length()-1); ++i)
166 {
167 n[0]=name[i];
168 try
169 {
170 namingContext=namingContext->bind_new_context(n);
171 }
172 catch(CosNaming::NamingContext::AlreadyBound&)
173 {
174 CORBA::Object_var obj2 =namingContext->resolve(n);
175 namingContext=CosNaming::NamingContext::_narrow(obj2);
176 }
177 // One of the context names is already bound to an object. Bail out!
178 if(CORBA::is_nil(namingContext))
179 return 2;
180 }
181 // Bind the object
182 n[0]=name[name.length()-1];
183 try
184 {
185 namingContext->bind(n,obj);
186 }
187 catch(CosNaming::NamingContext::AlreadyBound& ex)
188 {
189 // overwrite previously bound object
190 namingContext->rebind(n,obj);
191 }
192 return 0;
193
194 }
195 catch (CORBA::COMM_FAILURE& ex)
196 {
197 cerr << "Caught system exception COMM_FAILURE, unable to contact the "
198 << "naming service." << endl;
199 }
200 catch (omniORB::fatalException& ex)
201 {
202 cerr << "Caught omniORB fatal exception binding " << name << endl;
203 throw;
204 }
205 catch (CORBA::SystemException& ex)
206 {
207 const char* exName =NULL;
208 const char* exMinor =NULL;
209#ifdef HAVE_OMNIORB4
210 exName =ex.NP_minorString();
211 exMinor=ex.NP_minorString();
212#endif
213 cerr<<"System exception binding "<<name;
214 if(exName)
215 cerr<<": "<<exName;
216 if(exMinor)
217 cerr<<" ("<<exMinor<<")";
218 cerr<<endl;
219 }
220 catch (CORBA::Exception& ex)
221 {
222 cerr<<"CORBA exception binding "<<name
223#ifdef HAVE_OMNIORB4
224 <<": "<<ex._name()
225#endif
226 << endl;
227 }
228 ::exit(1);
229}
int bindName2Object(CosNaming::NamingContext_ptr namingContext, const CosNaming::Name &name, CORBA::Object_ptr obj)
Binds CosNaming::Name to object in the naming service.
Definition naming.cc:149
CosNaming::Name str2name(const char *namestr)
Converts stringified name to naming service name.
Definition naming.cc:117
ostream & operator<<(ostream &os, const CosNaming::Name &n)
Definition naming.cc:104