2009-03-21 04:23:35 +03:00
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2009, Distributed Systems Architecture Group, Universidad */
/* Complutense de Madrid (dsa-research.org) */
/* */
/* 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 HOOK_H_
# define HOOK_H_
# include <vector>
# include <string>
using namespace std ;
//Forward definition of Hookable
class Hookable ;
/**
* This class is an abstract representation of a hook , provides a method to
* check if the hook has to be executed , and a method to invoke the hook . The
* hook is a program that can be executed locally or in a remote host when a
* condition is satisfied
*/
class Hook
{
public :
2009-03-30 01:34:37 +04:00
/**
* Defines the hook type , so a whole hook class can be masked
*/
enum HookType
{
ALLOCATE = 0x1 ,
UPDATE = 0x2 ,
REMOVE = 0x4
} ;
2009-03-21 04:23:35 +03:00
//--------------------------------------------------------------------------
// Constructor and Destructor
//--------------------------------------------------------------------------
2009-04-04 03:34:33 +04:00
Hook ( const string & _name ,
const string & _cmd ,
2009-03-30 01:34:37 +04:00
const string & _args ,
HookType _ht ,
bool _remote ) :
2009-04-04 03:34:33 +04:00
name ( _name ) , cmd ( _cmd ) , args ( _args ) , hook_type ( _ht ) , remote ( _remote ) { } ;
2009-03-21 04:23:35 +03:00
virtual ~ Hook ( ) { } ;
2009-03-30 01:34:37 +04:00
2009-03-21 04:23:35 +03:00
//--------------------------------------------------------------------------
// Hook methods
//--------------------------------------------------------------------------
2009-03-30 01:34:37 +04:00
/**
* Returns the hook_type
*/
HookType type ( )
{
return hook_type ;
}
2009-03-21 04:23:35 +03:00
/**
* Check if the object where we are attached should execute de hook or not
2009-03-23 18:03:09 +03:00
* @ param arg arguments for the hook
2009-03-21 04:23:35 +03:00
* @ return true if the hook has to be executed
*/
2009-03-23 18:03:09 +03:00
virtual bool check_hook ( void * arg ) = 0 ;
2009-03-21 04:23:35 +03:00
/**
* Executes the hook it self ( usually with the aid of the ExecutionManager )
2009-03-23 18:03:09 +03:00
* @ param arg additional arguments for the hook
2009-03-21 04:23:35 +03:00
*/
2009-03-23 18:03:09 +03:00
virtual void do_hook ( void * arg ) = 0 ;
2009-03-21 04:23:35 +03:00
protected :
2009-04-04 03:34:33 +04:00
/**
* Name of the Hook
*/
string name ;
2009-03-21 04:23:35 +03:00
/**
* The command to be executed
*/
2009-03-30 01:34:37 +04:00
string cmd ;
2009-03-21 04:23:35 +03:00
/**
* The arguments for the command
*/
2009-03-30 01:34:37 +04:00
string args ;
2009-03-21 04:23:35 +03:00
2009-03-30 01:34:37 +04:00
/**
* The Hook Type
*/
HookType hook_type ;
2009-03-21 04:23:35 +03:00
/**
* True if the command is to be executed remotely
*/
2009-03-30 01:34:37 +04:00
bool remote ;
2009-03-21 04:23:35 +03:00
} ;
/**
* Objects that inherits from hookable will allow others to hook into it . The
* Hookable interface handles the list of hooks and provide a method to invoke
* the hooks .
*/
class Hookable
{
public :
//--------------------------------------------------------------------------
// Constructor and Destructor Methods
//--------------------------------------------------------------------------
Hookable ( ) { } ;
virtual ~ Hookable ( )
{
2009-03-27 03:32:02 +03:00
int sz = static_cast < int > ( hooks . size ( ) ) ;
2009-03-21 04:23:35 +03:00
for ( int i = 0 ; i < sz ; i + + )
{
delete hooks [ i ] ;
}
} ;
//--------------------------------------------------------------------------
// Hook Operations
//--------------------------------------------------------------------------
/**
* Hook in to the object
* @ param hk pointer to the hook , MUST be allocated in the HEAP
*/
void add_hook ( Hook * hk )
{
hooks . push_back ( hk ) ;
} ;
/**
* Removes all hooks from the object
*/
void clear_hooks ( )
{
2009-03-27 03:32:02 +03:00
int sz = static_cast < int > ( hooks . size ( ) ) ;
2009-03-21 04:23:35 +03:00
for ( int i = 0 ; i < sz ; i + + )
{
delete hooks [ i ] ;
}
hooks . clear ( ) ;
} ;
/**
* Iterates through the hooks , checking if they have to be executed and
* invokes them .
2009-03-23 18:03:09 +03:00
* @ param arg additional arguments for the hook
2009-03-21 04:23:35 +03:00
*/
2009-03-30 01:34:37 +04:00
void do_hooks ( void * arg = 0 , int hook_mask = 0xFF )
2009-03-21 04:23:35 +03:00
{
2009-03-27 03:32:02 +03:00
int sz = static_cast < int > ( hooks . size ( ) ) ;
2009-03-21 04:23:35 +03:00
for ( int i = 0 ; i < sz ; i + + )
{
2009-03-30 01:34:37 +04:00
if ( ( hooks [ i ] - > type ( ) & hook_mask ) & &
( hooks [ i ] - > check_hook ( arg ) = = true ) )
2009-03-21 04:23:35 +03:00
{
2009-03-23 18:03:09 +03:00
hooks [ i ] - > do_hook ( arg ) ;
2009-03-21 04:23:35 +03:00
}
}
} ;
private :
/**
* Those that hooked in the object
*/
vector < Hook * > hooks ;
} ;
2009-03-27 03:32:02 +03:00
# endif