(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 kumpf    1.30 		 MessageQueueService::get_thread_pool()->cleanupIdleThreads();
221 a.arora  1.21       }
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               
316               /////////////////////////////////////////////////////////////////////////////
317               // CIMListenerRep
318               /////////////////////////////////////////////////////////////////////////////
319               class CIMListenerRep
320               {
321               public:
322               	CIMListenerRep(Uint32 portNumber, SSLContext* sslContext=NULL);
323                 ~CIMListenerRep();
324               
325               	Uint32 getPortNumber() const;
326               
327               	SSLContext* getSSLContext() const;
328               	void setSSLContext(SSLContext* sslContext);
329               	
330               	void start();
331               	void stop();
332               
333               	Boolean isAlive();
334               
335 tony     1.8  	Boolean addConsumer(CIMIndicationConsumer* consumer);
336               	Boolean removeConsumer(CIMIndicationConsumer* consumer);
337               
338               private:
339 chuck    1.23   Boolean waitForPendingRequests(Uint32 shutdownTimeout);
340               
341                 Uint32 _portNumber;
342                 SSLContext* _sslContext;
343 tony     1.8  
344                 CIMListenerIndicationDispatcher* _dispatcher;
345 chuck    1.23   ThreadPool* _thread_pool;
346 chuck    1.20   CIMListenerService* _svc;  
347                 Semaphore *_listener_sem;
348 tony     1.8  };
349               
350               CIMListenerRep::CIMListenerRep(Uint32 portNumber, SSLContext* sslContext)
351               :_portNumber(portNumber)
352               ,_sslContext(sslContext)
353               ,_dispatcher(new CIMListenerIndicationDispatcher())
354               ,_thread_pool(NULL)
355 chuck    1.20 ,_svc(NULL)
356               ,_listener_sem(NULL)
357 tony     1.8  {
358               }
359               CIMListenerRep::~CIMListenerRep()
360               {
361 chuck    1.20   // if port is alive, clean up the port
362                 if (_thread_pool != NULL)
363                 {
364                   // Block incoming export requests and unbind the port
365                   _svc->stopClientConnection();
366 chuck    1.23 
367                   // Wait until pending export requests in the server are done.
368                   waitForPendingRequests(10);
369 chuck    1.20     
370                   // Shutdown the CIMListenerService
371                   _svc->shutdown();   
372                 }
373               
374                 if(_sslContext!=NULL)
375                   delete _sslContext;
376               
377                 if(_dispatcher!=NULL)
378                   delete _dispatcher;
379               
380                 if(_thread_pool!=NULL)
381                   delete _thread_pool;
382 tony     1.8  
383 chuck    1.20   if(_listener_sem!=NULL)
384                   delete _listener_sem;
385 tony     1.8  
386 chuck    1.20   // don't delete _svc, this is deleted by _listener_routine
387 tony     1.8  }
388               
389               Uint32 CIMListenerRep::getPortNumber() const
390               {
391               	return _portNumber;
392               }
393 kumpf    1.1  
394 tony     1.8  SSLContext* CIMListenerRep::getSSLContext() const
395               {
396               	return _sslContext;
397 kumpf    1.1  }
398 tony     1.8  void CIMListenerRep::setSSLContext(SSLContext* sslContext)
399               {
400               	if(_sslContext!=NULL)
401               		delete _sslContext;
402 kumpf    1.1  
403 tony     1.8  	_sslContext = sslContext;
404               }
405               void CIMListenerRep::start()
406 kumpf    1.1  {
407 chuck    1.20   // spawn a thread to do this
408                 if(_thread_pool==NULL)
409                 {
410                   CIMListenerService* svc = new CIMListenerService(_portNumber,_sslContext);
411                   try
412                   {
413                     // Try to initialize the service (bug 1394)
414                     svc->setIndicationDispatcher(_dispatcher);
415                     svc->init(); 
416                   }
417                   catch(...)
418                   {
419                     // Error. Exit without creating the ThreadPool, so that this listener
420                     // is not 'alive'
421                     delete svc;
422                     throw;
423                   }
424               
425 kumpf    1.30     struct timeval deallocateWait = {15, 0};
426                   _thread_pool = new ThreadPool(0, "Listener", 0, 1, deallocateWait);
427 chuck    1.20 
428                   _listener_sem = new Semaphore(0);
429                   _thread_pool->allocate_and_awaken(svc,
430               				      CIMListenerService::_listener_routine,
431               				      _listener_sem);
432               
433                   _svc = svc;
434               
435                   Logger::put(Logger::STANDARD_LOG,System::CIMLISTENER,
436               		Logger::INFORMATION,
437               		"CIMListener started");
438 chuck    1.17 
439 chuck    1.20     PEGASUS_STD(cerr) << "CIMlistener started" << PEGASUS_STD(endl);
440                 }
441 tony     1.8  }
442 kumpf    1.1  
443 tony     1.8  void CIMListenerRep::stop()
444               {
445 chuck    1.20   if(_thread_pool!=NULL)
446                 { 
447                   //
448                   // Graceful shutdown of the listener service
449                   //
450               
451                   // Block incoming export requests and unbind the port
452                   _svc->stopClientConnection();
453 chuck    1.23 
454                   // Wait until pending export requests in the server are done.
455 chuck    1.24     waitForPendingRequests(10);
456 chuck    1.23    
457 chuck    1.20     // Shutdown the CIMListenerService
458                   _svc->shutdown();
459               
460                   // Wait for the _listener_routine thread to exit.
461                   // The thread could be delivering an export, so give it 3sec.
462                   // Note that _listener_routine deletes the CIMListenerService,
463                   // so no need to delete _svc.
464                   try
465                   {
466                     _listener_sem->time_wait(3000); 
467                   }
468                   catch (TimeOut &)
469                   {
470                     // No need to do anything, the thread pool will be deleted below
471                     // to cancel the _listener_routine thread if it is still running.
472                   }
473               
474                   delete _listener_sem;
475                   _listener_sem = NULL;
476                   
477                   // Delete the thread pool.  This cancels the listener thread if it is still
478 chuck    1.20     // running.
479                   delete _thread_pool;
480                   _thread_pool = NULL;
481               
482                   Logger::put(Logger::STANDARD_LOG,System::CIMLISTENER,
483               		Logger::INFORMATION,
484               		"CIMListener stopped");
485                 }
486 kumpf    1.1  }
487               
488 tony     1.8  Boolean CIMListenerRep::isAlive()
489 kumpf    1.1  {
490 tony     1.8  	return (_thread_pool!=NULL)?true:false;
491               }
492 kumpf    1.1  
493 tony     1.8  Boolean CIMListenerRep::addConsumer(CIMIndicationConsumer* consumer)
494               {
495               	return _dispatcher->addConsumer(consumer);
496               }
497               Boolean CIMListenerRep::removeConsumer(CIMIndicationConsumer* consumer)
498               {
499               	return _dispatcher->removeConsumer(consumer);
500               }
501 kumpf    1.1  
502 chuck    1.23 Boolean CIMListenerRep::waitForPendingRequests(Uint32 shutdownTimeout)
503               {
504                 // Wait for 10 sec max
505                 Uint32 reqCount;
506                 Uint32 countDown = shutdownTimeout * 10;
507                 for (; countDown > 0; countDown--)
508                 {
509                   reqCount = _svc->getOutstandingRequestCount();
510                   if (reqCount > 0)
511                     pegasus_sleep(100);
512                   else
513                     return true;
514                 }
515                 
516                 return false;
517               } 
518               
519 tony     1.8  /////////////////////////////////////////////////////////////////////////////
520               // CIMListener
521               /////////////////////////////////////////////////////////////////////////////
522               CIMListener::CIMListener(Uint32 portNumber, SSLContext* sslContext)
523               :_rep(new CIMListenerRep(portNumber,sslContext))
524               {
525               }
526               CIMListener::~CIMListener()
527               {
528               	if(_rep!=NULL)
529 tony     1.9  		delete static_cast<CIMListenerRep*>(_rep);
530 tony     1.8  	_rep=NULL;
531               }
532               	
533               Uint32 CIMListener::getPortNumber() const
534               {
535               	return static_cast<CIMListenerRep*>(_rep)->getPortNumber();
536 kumpf    1.1  }
537               
538 tony     1.8  SSLContext* CIMListener::getSSLContext() const
539               {
540               	return static_cast<CIMListenerRep*>(_rep)->getSSLContext();
541               }
542               void CIMListener::setSSLContext(SSLContext* sslContext)
543               {
544               	static_cast<CIMListenerRep*>(_rep)->setSSLContext(sslContext);
545               }
546               void CIMListener::start()
547 kumpf    1.1  {
548 tony     1.8  	static_cast<CIMListenerRep*>(_rep)->start();
549               }
550               void CIMListener::stop()
551               {
552               	static_cast<CIMListenerRep*>(_rep)->stop();
553               }
554 kumpf    1.1  
555 tony     1.8  Boolean CIMListener::isAlive()
556               {
557               	return static_cast<CIMListenerRep*>(_rep)->isAlive();
558               }
559 kumpf    1.1  
560 tony     1.8  Boolean CIMListener::addConsumer(CIMIndicationConsumer* consumer)
561               {
562               	return static_cast<CIMListenerRep*>(_rep)->addConsumer(consumer);
563               }
564               Boolean CIMListener::removeConsumer(CIMIndicationConsumer* consumer)
565               {
566               	return static_cast<CIMListenerRep*>(_rep)->removeConsumer(consumer);
567 kumpf    1.1  }
568               
569               PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2