2008-06-17 20:27:32 +04:00
/* -------------------------------------------------------------------------- */
2014-01-09 14:51:20 +04:00
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
2008-06-17 20:27:32 +04:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef MAD_MANAGER_H_
# define MAD_MANAGER_H_
# include <pthread.h>
# include <sys/types.h>
# include <string>
# include <vector>
# include <sstream>
# include <vector>
# include "Mad.h"
# include "Attribute.h"
using namespace std ;
2012-05-31 20:38:14 +04:00
class SyncRequest ;
2008-06-17 20:27:32 +04:00
extern " C " void * mad_manager_listener ( void * _mm ) ;
/**
* Provides general functionality for driver management . The MadManager serves
* Nebula managers as base clase .
*/
class MadManager
{
public :
/**
* Function to initialize the MAD management system . This function
* MUST be called once before using the MadManager class . This function
* blocks the SIG_PIPE ( broken pipe ) signal that may occur when a driver
* crashes
*/
static void mad_manager_system_init ( ) ;
2013-10-25 17:16:44 +04:00
2008-06-17 20:27:32 +04:00
/**
* Loads Virtual Machine Manager Mads defined in configuration file
2013-10-25 17:16:44 +04:00
* @ param uid of the user executing the driver . When uid is 0 the nebula
2008-06-17 20:27:32 +04:00
* identity will be used . Otherwise the Mad will be loaded through the
2013-10-25 17:16:44 +04:00
* sudo application .
2008-06-17 20:27:32 +04:00
*/
2013-10-25 17:16:44 +04:00
virtual int load_mads ( int uid ) = 0 ;
2012-05-31 20:38:14 +04:00
/**
* Notify the result of an auth request
*/
void notify_request ( int id , bool result , const string & message ) ;
2008-06-17 20:27:32 +04:00
protected :
MadManager ( vector < const Attribute * > & _mads ) ;
virtual ~ MadManager ( ) ;
/**
2013-10-25 17:16:44 +04:00
* Vector containing Mad configuration for this Manager , as described in
2008-06-17 20:27:32 +04:00
* the nebula location
*/
vector < const Attribute * > mad_conf ;
2013-10-25 17:16:44 +04:00
2008-06-17 20:27:32 +04:00
/**
* This function initialize the communication pipes to register new MADs
* in the manager , and starts a listener to wait for driver messages .
*/
virtual int start ( ) ;
2013-10-25 17:16:44 +04:00
2008-06-17 20:27:32 +04:00
/**
2013-10-25 17:16:44 +04:00
* This function closes the communication pipes , stops the listener thread ,
2008-06-17 20:27:32 +04:00
* and finalize the associated drivers .
*/
2013-10-25 17:16:44 +04:00
virtual void stop ( ) ;
2008-06-17 20:27:32 +04:00
/**
* Get a mad
*/
virtual const Mad * get ( int uid , const string & name , const string & value ) ;
/**
* Register a new mad in the manager . The Mad is previously started , and
* then the listener thread is notified through the pipe_w stream . In case
* of failure the calling function MUST free the Mad .
* @ param mad pointer to the mad to be added to the manager .
* @ return 0 on success .
*/
int add ( Mad * mad ) ;
2012-05-31 20:38:14 +04:00
/**
2013-10-25 17:16:44 +04:00
* This function can be periodically executed to check time_outs on
* request . It will fail requests with an expired timeout and will notify
2012-05-31 20:38:14 +04:00
* the clients .
*/
void check_time_outs_action ( ) ;
/**
* Add a new request to the Request map
* @ param ar pointer to the request
* @ return the id for the request
*/
2012-06-01 14:01:23 +04:00
void add_request ( SyncRequest * ar ) ;
2012-05-31 20:38:14 +04:00
/**
* Gets request from the Request map
* @ param id for the request
* @ return pointer to the Request
*/
SyncRequest * get_request ( int id ) ;
2008-06-17 20:27:32 +04:00
private :
/**
* Function to lock the Manager
*/
void lock ( )
{
pthread_mutex_lock ( & mutex ) ;
} ;
/**
* Function to unlock the Manager
*/
void unlock ( )
{
pthread_mutex_unlock ( & mutex ) ;
} ;
2013-10-25 17:16:44 +04:00
2008-06-17 20:27:32 +04:00
/**
* Function to execute the listener method within a new pthread ( requires
* C linkage )
*/
friend void * mad_manager_listener ( void * _mm ) ;
/**
* Synchronization mutex ( listener & manager threads )
*/
pthread_mutex_t mutex ;
/**
* Thread id for the listener process
*/
pthread_t listener_thread ;
/**
2013-10-25 17:16:44 +04:00
* Communication pipe ( read end ) to link the Manager and the listener
2008-06-17 20:27:32 +04:00
* thread
*/
int pipe_r ;
2013-10-25 17:16:44 +04:00
2008-06-17 20:27:32 +04:00
/**
2013-10-25 17:16:44 +04:00
* Communication pipe ( write end ) to link the Manager and the listener
2008-06-17 20:27:32 +04:00
* thread
2013-10-25 17:16:44 +04:00
*/
2008-06-17 20:27:32 +04:00
int pipe_w ;
/**
* This vector contains the file descriptors of the driver pipes ( to read
* Mads responses )
*/
vector < int > fds ;
2013-10-25 17:16:44 +04:00
2008-06-17 20:27:32 +04:00
/**
* The sets of Mads managed by the MadManager
*/
vector < Mad * > mads ;
2013-10-25 17:16:44 +04:00
2008-06-17 20:27:32 +04:00
/**
* Read buffer for the listener . This variable is in the class so it
* can be free upon listener thread cancellation .
*/
ostringstream buffer ;
2012-05-31 20:38:14 +04:00
/**
* List of pending requests
*/
map < int , SyncRequest * > sync_requests ;
2013-10-25 17:16:44 +04:00
2008-06-17 20:27:32 +04:00
/**
* Listener thread implementation .
*/
2012-05-31 20:38:14 +04:00
void listener ( ) ;
2008-06-17 20:27:32 +04:00
} ;
# endif /*MAD_MANAGER_H_*/