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

  1 karl  1.28 //%2005////////////////////////////////////////////////////////////////////////
  2 kumpf 1.1  //
  3 karl  1.25 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4            // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5            // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6 karl  1.12 // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.25 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.28 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 kumpf 1.1  //
 12            // Permission is hereby granted, free of charge, to any person obtaining a copy
 13            // of this software and associated documentation files (the "Software"), to
 14            // deal in the Software without restriction, including without limitation the
 15            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 16            // sell copies of the Software, and to permit persons to whom the Software is
 17            // furnished to do so, subject to the following conditions:
 18 kumpf 1.3  // 
 19 kumpf 1.1  // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 20            // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 21            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 22            // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 23            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 24            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 25            // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27            //
 28            //==============================================================================
 29            //
 30 tony  1.8  // Author: Dong Xiang, EMC Corporation (xiang_dong@emc.com)
 31 kumpf 1.1  //
 32 dj.gorey 1.14 // Modified By:   Dan Gorey (djgorey@us.ibm.com)
 33 a.arora  1.21 //                Amit K Arora, IBM (amita@in.ibm.com) for PEP#183
 34 kumpf    1.26 //                Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 35 kumpf    1.1  //
 36               //%/////////////////////////////////////////////////////////////////////////////
 37               
 38 tony     1.8  #include "CIMListener.h"
 39 kumpf    1.1  
 40 tony     1.8  #include <Pegasus/Common/Exception.h>
 41               #include <Pegasus/Common/SSLContext.h>
 42               #include <Pegasus/Common/Monitor.h>
 43 kumpf    1.1  #include <Pegasus/Common/HTTPAcceptor.h>
 44 kumpf    1.11 #include <Pegasus/Common/PegasusVersion.h>
 45 kumpf    1.2  
 46 kumpf    1.1  #include <Pegasus/ExportServer/CIMExportResponseEncoder.h>
 47               #include <Pegasus/ExportServer/CIMExportRequestDecoder.h>
 48               
 49 tony     1.8  #include <Pegasus/Consumer/CIMIndicationConsumer.h>
 50               #include <Pegasus/Listener/CIMListenerIndicationDispatcher.h>
 51               
 52               PEGASUS_NAMESPACE_BEGIN
 53               /////////////////////////////////////////////////////////////////////////////
 54               // CIMListenerService
 55               /////////////////////////////////////////////////////////////////////////////
 56               class CIMListenerService
 57               {
 58               public:
 59               	CIMListenerService(Uint32 portNumber, SSLContext* sslContext=NULL);
 60               	CIMListenerService(CIMListenerService& svc);
 61                 ~CIMListenerService();
 62               
 63               	void				init();
 64               	/** bind to the port
 65               	*/
 66               	void				bind();
 67               	/** runForever Main runloop for the server.
 68               	*/
 69               	void runForever();
 70 tony     1.8  	
 71               	/** Call to gracefully shutdown the server.  The server connection socket
 72               	will be closed to disable new connections from clients.
 73               	*/
 74               	void stopClientConnection();
 75               	
 76               	/** Call to gracefully shutdown the server.  It is called when the server
 77               	has been stopped and is ready to be shutdown.  Next time runForever()
 78               	is called, the server shuts down.
 79               	*/
 80               	void shutdown();
 81               	
 82               	/** Return true if the server has shutdown, false otherwise.
 83               	*/
 84               	Boolean terminated() { return _dieNow; };
 85               	
 86               	/** Call to resume the sever.
 87               	*/
 88               	void resume();
 89               	
 90               	/** Call to set the CIMServer state.  Also inform the appropriate
 91 tony     1.8  	message queues about the current state of the CIMServer.
 92               	*/
 93               	void setState(Uint32 state);
 94               	
 95               	Uint32 getOutstandingRequestCount();
 96               
 97               	/** Returns the indication listener dispatcher
 98               	 */
 99               	CIMListenerIndicationDispatcher* getIndicationDispatcher() const;
100               
101                 /** Returns the indication listener dispatcher
102               	 */
103               	void setIndicationDispatcher(CIMListenerIndicationDispatcher* dispatcher);
104               
105               	static PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL _listener_routine(void *param);
106               
107               private:
108               	Uint32			_portNumber;
109               	SSLContext* _sslContext;
110               	Monitor*				_monitor;
111                 HTTPAcceptor*   _acceptor;
112 dj.gorey 1.14 
113                 Boolean					_dieNow;
114 tony     1.8  
115                 CIMListenerIndicationDispatcher* _dispatcher;
116               
117                 CIMExportResponseEncoder* _responseEncoder;
118                 CIMExportRequestDecoder*  _requestDecoder;
119               
120               };
121               
122               CIMListenerService::CIMListenerService(Uint32 portNumber, SSLContext* sslContext)
123               :_portNumber(portNumber)
124               ,_sslContext(sslContext)
125               ,_monitor(NULL)
126               ,_acceptor(NULL)
127               ,_dieNow(false)
128               ,_dispatcher(NULL)
129               ,_responseEncoder(NULL)
130               ,_requestDecoder(NULL)
131               {
132               }
133               
134               CIMListenerService::CIMListenerService(CIMListenerService& svc)
135 tony     1.8  :_portNumber(svc._portNumber)
136               ,_sslContext(svc._sslContext)
137               ,_monitor(NULL)
138               ,_acceptor(NULL)
139               ,_dieNow(svc._dieNow)
140               ,_dispatcher(NULL)
141               ,_responseEncoder(NULL)
142               ,_requestDecoder(NULL)
143               {
144               }
145               CIMListenerService::~CIMListenerService()
146               {
147               	// if port is alive, clean up the port
148               	//if(_sslContext!=NULL)
149               	//	delete _sslContext;
150               
151               	if(_responseEncoder!=NULL)
152               		delete _responseEncoder;
153               
154               	if(_requestDecoder!=NULL)
155               		delete _requestDecoder;
156 kumpf    1.1  
157 tony     1.8  	//if(_dispatcher!=NULL)
158               	//	delete _dispatcher;
159 kumpf    1.1  
160 tony     1.8  	if(_monitor!=NULL)
161               		delete _monitor;
162 kumpf    1.1  
163 tony     1.8  	if(_acceptor!=NULL)
164               		delete _acceptor;
165               }
166 kumpf    1.1  
167 tony     1.8  void CIMListenerService::init()
168 kumpf    1.1  {
169 tony     1.8  	PEG_METHOD_ENTER(TRC_LISTENER, "CIMListenerService::init");
170 kumpf    1.1  
171 kumpf    1.27   _monitor = new Monitor();
172 dj.gorey 1.14   
173 tony     1.8  	//_dispatcher = new CIMListenerIndicationDispatcher();
174 kumpf    1.1  
175 chuck    1.20   _responseEncoder = new CIMExportResponseEncoder();
176 tony     1.8    _requestDecoder = new CIMExportRequestDecoder(
177               		_dispatcher,
178               		_responseEncoder->getQueueId());
179               
180 dj.gorey 1.14   _acceptor = new HTTPAcceptor(
181 tony     1.8  		 _monitor, 
182               		 _requestDecoder, 
183               		 false, 
184               		 _portNumber, 
185 kumpf    1.19 		 _sslContext,
186                                false);
187 kumpf    1.1  
188 chuck    1.20   bind();
189 kumpf    1.1  
190 chuck    1.20   PEG_METHOD_EXIT();
191 tony     1.8  }
192               void CIMListenerService::bind()
193               {
194 chuck    1.20   if(_acceptor!=NULL)
195                   { // Bind to the port
196                     _acceptor->bind();
197 tony     1.8  
198 chuck    1.20       PEGASUS_STD(cout) << "Listening on HTTP port " << _portNumber << PEGASUS_STD(endl);
199 tony     1.8  		
200 chuck    1.20       //listener.addAcceptor(false, portNumberHttp, false);
201                     Logger::put(Logger::STANDARD_LOG, System::CIMLISTENER, Logger::INFORMATION,
202 tony     1.8                          "Listening on HTTP port $0.", _portNumber);
203 kumpf    1.1  
204 chuck    1.20     }
205 tony     1.8  }
206 chuck    1.20 
207 tony     1.8  void CIMListenerService::runForever()
208               {
209 chuck    1.20   static int modulator = 0;
210 kumpf    1.1  
211 chuck    1.20   if(!_dieNow)
212 dj.gorey 1.14     {
213 a.arora  1.21       if(false == _monitor->run(500000)) 
214 chuck    1.20 	{	
215               	  modulator++;
216 a.arora  1.21       try 
217                     {
218               	     //MessageQueueService::_check_idle_flag = 1;
219               		 //MessageQueueService::_polling_sem.signal();
220               		 MessageQueueService::get_thread_pool()->kill_idle_threads();
221                     }
222               	  catch(...)
223                     {
224                     }
225 chuck    1.20 	}
226               /*
227                     if (handleShutdownSignal)
228                     {
229                       Tracer::trace(TRC_SERVER, Tracer::LEVEL3,
230               	"CIMServer::runForever - signal received.  Shutting down.");
231               
232               	ShutdownService::getInstance(this)->shutdown(true, 10, false);
233               	handleShutdownSignal = false;
234                     }
235 tony     1.8  */
236 chuck    1.20     }
237 tony     1.8  }
238 kumpf    1.1  
239 tony     1.8  void CIMListenerService::shutdown()
240               {
241                   PEG_METHOD_ENTER(TRC_LISTENER, "CIMListenerService::shutdown()");
242 kumpf    1.1  
243 tony     1.8      _dieNow = true;
244 a.arora  1.21     _monitor->tickle();
245 kumpf    1.1  
246 kumpf    1.4      PEG_METHOD_EXIT();
247 kumpf    1.1  }
248               
249 tony     1.8  void CIMListenerService::resume()
250 kumpf    1.1  {
251 tony     1.8      PEG_METHOD_ENTER(TRC_LISTENER, "CIMListenerService::resume()");
252 kumpf    1.1  
253 tony     1.8      if(_acceptor!=NULL)
254                       _acceptor->reopenConnectionSocket();
255 kumpf    1.1  
256 kumpf    1.4      PEG_METHOD_EXIT();
257 kumpf    1.1  }
258               
259 tony     1.8  void CIMListenerService::stopClientConnection()
260 kumpf    1.1  {
261 tony     1.8      PEG_METHOD_ENTER(TRC_LISTENER, "CIMListenerService::stopClientConnection()");
262 kumpf    1.1  
263 kumpf    1.10     // tell Monitor to stop listening for client connections
264 chuck    1.22     _monitor->stopListeningForConnections(true);
265 kumpf    1.10 
266                   //
267                   // Wait 150 milliseconds to allow time for the Monitor to stop
268                   // listening for client connections.
269                   //
270                   // This wait time is the timeout value for the select() call
271                   // in the Monitor's run() method (currently set to 100
272                   // milliseconds) plus a delta of 50 milliseconds.  The reason
273                   // for the wait here is to make sure that the Monitor entries
274                   // are updated before closing the connection sockets.
275                   //
276 a.arora  1.21     // pegasus_sleep(150); Not needed now due to the semaphore in the Monitor
277                   
278 tony     1.8      if(_acceptor!=NULL)
279                   _acceptor->closeConnectionSocket();
280 kumpf    1.1  
281 kumpf    1.4      PEG_METHOD_EXIT();
282 kumpf    1.1  }
283               
284 chuck    1.23 Uint32 CIMListenerService::getOutstandingRequestCount()
285               {
286                   return _acceptor->getOutstandingRequestCount();
287               }
288 tony     1.8  
289               CIMListenerIndicationDispatcher* CIMListenerService::getIndicationDispatcher() const
290 kumpf    1.1  {
291 tony     1.8  	return _dispatcher;
292               }
293               void CIMListenerService::setIndicationDispatcher(CIMListenerIndicationDispatcher* dispatcher)
294               {
295               	_dispatcher = dispatcher;
296 kumpf    1.1  }
297               
298 tony     1.8  PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL CIMListenerService::_listener_routine(void *param)
299 kumpf    1.1  {
300 tony     1.8    CIMListenerService *svc = reinterpret_cast<CIMListenerService *>(param);
301 kumpf    1.1  
302 chuck    1.20   //svc->init(); bug 1394 
303                 while(!svc->terminated())
304                 {
305               #if defined(PEGASUS_PLATFORM_DARWIN_PPC_GNU)
306                   pthread_testcancel();
307               #endif
308                   svc->runForever();
309                 }
310 chuck    1.23 
311 chuck    1.20   delete svc;
312 tony     1.8  
313 chuck    1.20   return 0;
314 tony     1.8  }
315               static struct timeval create_time = {0, 1};
316               static struct timeval destroy_time = {15, 0};
317               
318               /////////////////////////////////////////////////////////////////////////////
319               // CIMListenerRep
320               /////////////////////////////////////////////////////////////////////////////
321               class CIMListenerRep
322               {
323               public:
324               	CIMListenerRep(Uint32 portNumber, SSLContext* sslContext=NULL);
325                 ~CIMListenerRep();
326               
327               	Uint32 getPortNumber() const;
328               
329               	SSLContext* getSSLContext() const;
330               	void setSSLContext(SSLContext* sslContext);
331               	
332               	void start();
333               	void stop();
334               
335 tony     1.8  	Boolean isAlive();
336               
337               	Boolean addConsumer(CIMIndicationConsumer* consumer);
338               	Boolean removeConsumer(CIMIndicationConsumer* consumer);
339               
340               private:
341 chuck    1.23   Boolean waitForPendingRequests(Uint32 shutdownTimeout);
342               
343                 Uint32 _portNumber;
344                 SSLContext* _sslContext;
345 tony     1.8  
346                 CIMListenerIndicationDispatcher* _dispatcher;
347 chuck    1.23   ThreadPool* _thread_pool;
348 chuck    1.20   CIMListenerService* _svc;  
349                 Semaphore *_listener_sem;
350 tony     1.8  };
351               
352               CIMListenerRep::CIMListenerRep(Uint32 portNumber, SSLContext* sslContext)
353               :_portNumber(portNumber)
354               ,_sslContext(sslContext)
355               ,_dispatcher(new CIMListenerIndicationDispatcher())
356               ,_thread_pool(NULL)
357 chuck    1.20 ,_svc(NULL)
358               ,_listener_sem(NULL)
359 tony     1.8  {
360               }
361               CIMListenerRep::~CIMListenerRep()
362               {
363 chuck    1.20   // if port is alive, clean up the port
364                 if (_thread_pool != NULL)
365                 {
366                   // Block incoming export requests and unbind the port
367                   _svc->stopClientConnection();
368 chuck    1.23 
369                   // Wait until pending export requests in the server are done.
370                   waitForPendingRequests(10);
371 chuck    1.20     
372                   // Shutdown the CIMListenerService
373                   _svc->shutdown();   
374                 }
375               
376                 if(_sslContext!=NULL)
377                   delete _sslContext;
378               
379                 if(_dispatcher!=NULL)
380                   delete _dispatcher;
381               
382                 if(_thread_pool!=NULL)
383                   delete _thread_pool;
384 tony     1.8  
385 chuck    1.20   if(_listener_sem!=NULL)
386                   delete _listener_sem;
387 tony     1.8  
388 chuck    1.20   // don't delete _svc, this is deleted by _listener_routine
389 tony     1.8  }
390               
391               Uint32 CIMListenerRep::getPortNumber() const
392               {
393               	return _portNumber;
394               }
395 kumpf    1.1  
396 tony     1.8  SSLContext* CIMListenerRep::getSSLContext() const
397               {
398               	return _sslContext;
399 kumpf    1.1  }
400 tony     1.8  void CIMListenerRep::setSSLContext(SSLContext* sslContext)
401               {
402               	if(_sslContext!=NULL)
403               		delete _sslContext;
404 kumpf    1.1  
405 tony     1.8  	_sslContext = sslContext;
406               }
407               void CIMListenerRep::start()
408 kumpf    1.1  {
409 chuck    1.20   // spawn a thread to do this
410                 if(_thread_pool==NULL)
411                 {
412                   CIMListenerService* svc = new CIMListenerService(_portNumber,_sslContext);
413                   try
414                   {
415                     // Try to initialize the service (bug 1394)
416                     svc->setIndicationDispatcher(_dispatcher);
417                     svc->init(); 
418                   }
419                   catch(...)
420                   {
421                     // Error. Exit without creating the ThreadPool, so that this listener
422                     // is not 'alive'
423                     delete svc;
424                     throw;
425                   }
426               
427                   _thread_pool = new ThreadPool(0, "Listener", 0, 1, 
428 kumpf    1.29 				  create_time, destroy_time);
429 chuck    1.20 
430                   _listener_sem = new Semaphore(0);
431                   _thread_pool->allocate_and_awaken(svc,
432               				      CIMListenerService::_listener_routine,
433               				      _listener_sem);
434               
435                   _svc = svc;
436               
437                   Logger::put(Logger::STANDARD_LOG,System::CIMLISTENER,
438               		Logger::INFORMATION,
439               		"CIMListener started");
440 chuck    1.17 
441 chuck    1.20     PEGASUS_STD(cerr) << "CIMlistener started" << PEGASUS_STD(endl);
442                 }
443 tony     1.8  }
444 kumpf    1.1  
445 tony     1.8  void CIMListenerRep::stop()
446               {
447 chuck    1.20   if(_thread_pool!=NULL)
448                 { 
449                   //
450                   // Graceful shutdown of the listener service
451                   //
452               
453                   // Block incoming export requests and unbind the port
454                   _svc->stopClientConnection();
455 chuck    1.23 
456                   // Wait until pending export requests in the server are done.
457 chuck    1.24     waitForPendingRequests(10);
458 chuck    1.23    
459 chuck    1.20     // Shutdown the CIMListenerService
460                   _svc->shutdown();
461               
462                   // Wait for the _listener_routine thread to exit.
463                   // The thread could be delivering an export, so give it 3sec.
464                   // Note that _listener_routine deletes the CIMListenerService,
465                   // so no need to delete _svc.
466                   try
467                   {
468                     _listener_sem->time_wait(3000); 
469                   }
470                   catch (TimeOut &)
471                   {
472                     // No need to do anything, the thread pool will be deleted below
473                     // to cancel the _listener_routine thread if it is still running.
474                   }
475               
476                   delete _listener_sem;
477                   _listener_sem = NULL;
478                   
479                   // Delete the thread pool.  This cancels the listener thread if it is still
480 chuck    1.20     // running.
481                   delete _thread_pool;
482                   _thread_pool = NULL;
483               
484                   Logger::put(Logger::STANDARD_LOG,System::CIMLISTENER,
485               		Logger::INFORMATION,
486               		"CIMListener stopped");
487                 }
488 kumpf    1.1  }
489               
490 tony     1.8  Boolean CIMListenerRep::isAlive()
491 kumpf    1.1  {
492 tony     1.8  	return (_thread_pool!=NULL)?true:false;
493               }
494 kumpf    1.1  
495 tony     1.8  Boolean CIMListenerRep::addConsumer(CIMIndicationConsumer* consumer)
496               {
497               	return _dispatcher->addConsumer(consumer);
498               }
499               Boolean CIMListenerRep::removeConsumer(CIMIndicationConsumer* consumer)
500               {
501               	return _dispatcher->removeConsumer(consumer);
502               }
503 kumpf    1.1  
504 chuck    1.23 Boolean CIMListenerRep::waitForPendingRequests(Uint32 shutdownTimeout)
505               {
506                 // Wait for 10 sec max
507                 Uint32 reqCount;
508                 Uint32 countDown = shutdownTimeout * 10;
509                 for (; countDown > 0; countDown--)
510                 {
511                   reqCount = _svc->getOutstandingRequestCount();
512                   if (reqCount > 0)
513                     pegasus_sleep(100);
514                   else
515                     return true;
516                 }
517                 
518                 return false;
519               } 
520               
521 tony     1.8  /////////////////////////////////////////////////////////////////////////////
522               // CIMListener
523               /////////////////////////////////////////////////////////////////////////////
524               CIMListener::CIMListener(Uint32 portNumber, SSLContext* sslContext)
525               :_rep(new CIMListenerRep(portNumber,sslContext))
526               {
527               }
528               CIMListener::~CIMListener()
529               {
530               	if(_rep!=NULL)
531 tony     1.9  		delete static_cast<CIMListenerRep*>(_rep);
532 tony     1.8  	_rep=NULL;
533               }
534               	
535               Uint32 CIMListener::getPortNumber() const
536               {
537               	return static_cast<CIMListenerRep*>(_rep)->getPortNumber();
538 kumpf    1.1  }
539               
540 tony     1.8  SSLContext* CIMListener::getSSLContext() const
541               {
542               	return static_cast<CIMListenerRep*>(_rep)->getSSLContext();
543               }
544               void CIMListener::setSSLContext(SSLContext* sslContext)
545               {
546               	static_cast<CIMListenerRep*>(_rep)->setSSLContext(sslContext);
547               }
548               void CIMListener::start()
549 kumpf    1.1  {
550 tony     1.8  	static_cast<CIMListenerRep*>(_rep)->start();
551               }
552               void CIMListener::stop()
553               {
554               	static_cast<CIMListenerRep*>(_rep)->stop();
555               }
556 kumpf    1.1  
557 tony     1.8  Boolean CIMListener::isAlive()
558               {
559               	return static_cast<CIMListenerRep*>(_rep)->isAlive();
560               }
561 kumpf    1.1  
562 tony     1.8  Boolean CIMListener::addConsumer(CIMIndicationConsumer* consumer)
563               {
564               	return static_cast<CIMListenerRep*>(_rep)->addConsumer(consumer);
565               }
566               Boolean CIMListener::removeConsumer(CIMIndicationConsumer* consumer)
567               {
568               	return static_cast<CIMListenerRep*>(_rep)->removeConsumer(consumer);
569 kumpf    1.1  }
570               
571               PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2