(file) Return to peg_slp_agent.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / slp

  1 mday  1.1 //%///-*-c++-*-/////////////////////////////////////////////////////////////////
  2           //
  3           // Copyright (c) 2003 BMC Software, Hewlett-Packard Company, IBM,
  4           // The Open Group, Tivoli Systems
  5           //
  6           // Permission is hereby granted, free of charge, to any person obtaining a copy
  7           // of this software and associated documentation files (the "Software"), to
  8           // deal in the Software without restriction, including without limitation the
  9           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 10           // sell copies of the Software, and to permit persons to whom the Software is
 11           // furnished to do so, subject to the following conditions:
 12           // 
 13           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 14           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 15           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 16           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 17           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 18           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 19           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 20           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21           //
 22 mday  1.1 //==============================================================================
 23           //
 24           // Author: Mike Day (mdday@us.ibm.com)
 25           //
 26           // Modified By:
 27           //
 28           //%/////////////////////////////////////////////////////////////////////////////
 29           
 30           #include "peg_slp_agent.h"
 31           
 32           PEGASUS_NAMESPACE_BEGIN
 33           
 34 mday  1.2 class sa_reg_params
 35           {
 36              public:
 37           
 38                 sa_reg_params(const char*, const char*, const char*, const char*, unsigned short);
 39                 ~sa_reg_params(void);
 40                 
 41                 int8* url;
 42                 int8* attrs;
 43                 int8* type;
 44                 int8* scopes;
 45 mday  1.4       Uint32 lifetime;
 46                 Uint32 expire;
 47 mday  1.2       
 48           
 49              private:
 50                 sa_reg_params(void);
 51                 sa_reg_params(const sa_reg_params&);
 52                 sa_reg_params& operator=(const sa_reg_params&);
 53                 
 54           };
 55           
 56           
 57           
 58            sa_reg_params::sa_reg_params(const char*  _url, 
 59           			     const char*  _attrs, 
 60           			     const char*  _type,
 61           			     const char*  _scopes,
 62           			     unsigned short _lifetime)
 63           {
 64              if(_url)
 65                 url = strdup(_url);
 66              
 67              if(_attrs)
 68 mday  1.2       attrs = strdup(_attrs);
 69              
 70              if(_type)
 71                 type = strdup(_type);
 72              
 73              if(_scopes)
 74                 scopes = strdup(_scopes);
 75              
 76           
 77              lifetime = _lifetime;
 78 mday  1.4    Uint32 msec, now;
 79              System::getCurrentTime(now, msec);
 80              expire = 0;
 81 mday  1.2 }
 82           
 83           sa_reg_params::~sa_reg_params(void)
 84           {
 85              if(url)
 86                 free(url);
 87              if(attrs)
 88                 free(attrs);
 89              if(type)
 90                 free(type);
 91              if(scopes)
 92                 free(scopes);
 93           }
 94           
 95 mday  1.1 
 96           slp_service_agent::slp_service_agent(void)
 97 mday  1.2    : _listen_thread(service_listener, this, false),
 98              _initialized(0)
 99           
100 mday  1.1 {
101              try 
102              {
103                 _init();
104              }
105              catch(...)
106              {
107                 
108              }
109 mday  1.2    if(_initialized.value() && _lib_handle && _create_client)
110 mday  1.1    {
111                 _rep = _create_client("239.255.255.253",
112           			    0,
113           			    427,
114           			    "DSA",
115           			    "DEFAULT",
116           			    TRUE,
117           			    FALSE);
118              }
119           }
120           
121           slp_service_agent::slp_service_agent(const char *local_interface, 
122           				     unsigned short target_port,
123           				     const char *scopes,
124           				     Boolean listen,
125           				     Boolean use_da)
126 mday  1.2    : _listen_thread(service_listener, this, false),
127              _initialized(0)
128 mday  1.1 {
129              
130              try 
131              {
132                 _init();
133              }
134              catch(...)
135              {
136                 
137              }
138 mday  1.2    if(_initialized.value() && _lib_handle && _create_client)
139 mday  1.1    {
140                 _rep = _create_client("239.255.255.253",
141           			    local_interface,
142           			    target_port,
143           			    "DSA",
144           			    "scpoes",
145           			    listen,
146           			    use_da);
147              }
148           }
149           
150 mday  1.3 slp_service_agent::~slp_service_agent(void)
151           {
152              try
153              {
154                 _de_init();
155              }
156              catch(...)
157              {
158              }
159              
160           }
161           
162           
163 mday  1.1 void slp_service_agent::_init(void)
164           {
165 mday  1.4 
166              _initialized = 0;
167 mday  1.1 #ifdef PEGASUS_OS_TYPE_WINDOWS
168              _lib_fileName.append("slp_client.dll");
169           #elif defined(PEGASUS_OS_HPUX)
170              _lib_fileName.append("slp_client.sl");
171           #elif defined(PEGASUS_OS_OS400)
172              _lib_fileName.append("slp_client");
173           #else
174 mday  1.4    _lib_fileName.append("libslp_client.so");
175 mday  1.1 #endif
176 mday  1.2    _lib_handle = System::loadDynamicLibrary((const char *)_lib_fileName.getCString());
177 mday  1.1 
178              if(_lib_handle)
179              {
180                 _create_client = 
181           	 (slp_client * (*)(const int8*, const int8*,  uint16, const int8*, const int8*, BOOL, BOOL))
182           	 System::loadDynamicSymbol(_lib_handle, "create_slp_client");
183           
184 mday  1.2       _destroy_client = 
185           	 (void (*)(struct slp_client *)) 
186           	 System::loadDynamicSymbol(_lib_handle, "destroy_slp_client");
187                 
188                 _find_das = 
189           	 (int (*)(struct slp_client *, const int8 *,  const int8 *))
190           	 System::loadDynamicSymbol(_lib_handle, "find_das");
191           
192 mday  1.1       _test_reg = 
193           	 (uint32 (*)(int8*, int8*, int8*, int8*))
194 mday  1.2 	 System::loadDynamicSymbol(_lib_handle, "test_srv_reg");
195 mday  1.4 
196 mday  1.2       _initialized = 1;
197 mday  1.4      
198                 if(_create_client == 0 || _destroy_client == 0 || _find_das == 0 || _test_reg == 0)
199                 {
200           	 _initialized = 0;
201           	 System::unloadDynamicLibrary(_lib_handle);
202                 }
203 mday  1.1    }
204           }
205           
206           void slp_service_agent::_de_init(void)
207           {
208 mday  1.2    if(_initialized.value() && _lib_handle)
209 mday  1.1    {
210 mday  1.2       if(_rep)
211                 {
212           	 _destroy_client(_rep);
213           	 _rep = 0;
214                 }
215           
216 mday  1.1       try 
217                 {
218           	 System::unloadDynamicLibrary(_lib_handle);
219           	 _lib_handle = 0;
220                 }
221                 catch(...)
222                 {
223           	 _lib_handle = 0;
224                 }
225              }
226           }
227 mday  1.2 
228           
229           Boolean slp_service_agent::srv_register(const char* url, 
230           					const char* attributes, 
231           					const char* type,
232           					const char* scopes, 
233           					unsigned short lifetime)
234           {
235 mday  1.4    if(_initialized.value() == 0 )
236                 throw UninitializedObjectException();
237 mday  1.2 
238              if(url == 0 || attributes == 0 || type == 0)
239                 return false;
240           
241 mday  1.3    sa_reg_params* rp = 0;
242 mday  1.2    _internal_regs.lookup(url, rp);
243              if(rp )
244              {
245                 _internal_regs.remove(url);
246                 delete rp;
247              }
248              
249              rp = new sa_reg_params(url, attributes, type, scopes, lifetime);
250              
251              _internal_regs.insert(url, rp);
252              
253              
254              return true;
255               
256           }
257           
258           
259           void slp_service_agent::unregister(void)
260           {
261 mday  1.4    if(_initialized.value() == 0 )
262                 throw UninitializedObjectException();
263              _should_listen = 0;
264              _listen_thread.join();
265              
266 mday  1.2    while(slp_reg_table::Iterator i = _internal_regs.start())
267              {
268                 sa_reg_params *rp = i.value();
269                 _internal_regs.remove(rp->url);
270                 delete rp;
271              }
272           }
273           
274           
275           
276 mday  1.4 Uint32 slp_service_agent::test_registration(const char *url, 
277 mday  1.2 					    const char *attrs, 
278 mday  1.4 					    const char *type, 
279            					    const char *scopes)
280 mday  1.2 {
281           
282 mday  1.4    if(_initialized.value() == 0 )
283                 throw UninitializedObjectException();
284 mday  1.2    
285              if(type ==  0)
286                 return 1;
287              
288              if(url == 0)
289                 return 2;
290              
291              if(attrs == 0)
292                 return 3;
293              
294              if(scopes == 0)
295                 return 4;
296              
297              int8* _type = strdup(type);
298              int8* _url = strdup(url);
299              int8* _attrs = strdup(attrs);
300              int8* _scopes = strdup(scopes);
301           
302              Uint32 ccode = _test_reg(_type, _url, _attrs, _scopes);
303              
304              free(_type);
305 mday  1.2    free(_url);
306              free(_attrs);
307              free(_scopes);
308              return ccode;
309           }
310           
311           
312           void slp_service_agent::start_listener(void)
313           {
314              // see if we need to use an slp directory agent 
315 mday  1.4    if(_initialized.value() == 0 )
316                 throw UninitializedObjectException();
317              
318 mday  1.2    _using_das = _find_das(_rep, NULL, "DEFAULT");
319 mday  1.4    
320 mday  1.2    _should_listen = 1;
321              _listen_thread.run();
322              
323           }
324           
325           PEGASUS_THREAD_RETURN 
326           PEGASUS_THREAD_CDECL slp_service_agent::service_listener(void *parm)
327           {
328              Thread *myself = (Thread *)parm;
329              if(myself == 0)
330                 throw NullPointer();
331              
332              slp_service_agent *agent = 
333                 (slp_service_agent *)myself->get_parm();
334 mday  1.4 
335           
336 mday  1.2    
337              lslpMsg msg_list;
338              
339              while(agent->_should_listen.value())
340              {
341 mday  1.4       Uint32 now, msec;
342                 System::getCurrentTime(now, msec);
343 mday  1.2 	 // now register everything
344                 
345                 for(slp_reg_table::Iterator i = agent->_internal_regs.start(); i ; i++)
346                 {
347           	 sa_reg_params *rp = i.value();
348 mday  1.4 
349           	 if(rp->expire == 0 || rp->expire < now - 1)
350 mday  1.2 	 {
351           	    rp->expire = now + rp->lifetime;
352           	    
353           	    if(agent->_using_das.value())
354           	    { 
355           	       agent->_rep->srv_reg_all(
356           		  agent->_rep, 
357           		  rp->url, 
358           		  rp->attrs, 
359           		  rp->type, 
360           		  rp->scopes, 
361           		  rp->lifetime);
362           	    }
363           	    else
364           	    {
365 mday  1.4 	       agent->_rep->srv_reg_local(
366 mday  1.2 		  agent->_rep, 
367           		  rp->url, 
368           		  rp->attrs, 
369           		  rp->type, 
370           		  rp->scopes, 
371           		  rp->lifetime);
372           	    }
373           	 }
374                 }
375                 agent->_rep->service_listener(agent->_rep, 0, &msg_list);
376                 _LSLP_SLEEP(1);
377              }
378              myself->exit_self((PEGASUS_THREAD_RETURN) 0) ;
379              return(0);
380           }
381 mday  1.1 
382           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2