RMTEmulator.h 7.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***************************************************************************
 *   Copyright (C) 2004-2006 by Olivier Stezowski & Christian Finck        *
 *   stezow(AT)ipnl.in2p3.fr, cfinck(AT)ires.in2p3.fr                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
20

21
/** \file RMTEmulator.h header file for RMTEmulator.cpp */
22

23 24
#ifndef Gw_RMTEmulator
#define Gw_RMTEmulator
25

26
#include "DEmulator.h"
27 28 29

#include "TMath.h"
#include "TThread.h"
30
#include "TMutex.h"
31

32 33 34 35
using namespace ADF;

namespace Gw {

36 37 38 39 40 41 42
class RMTActorConnection : public DActorConnection 
{
protected:
	//! 
	FrameBlock *fBlockLeft, *fBlockRight;
	
protected:
43 44
	//! means the contain of the first block is moved to the second one and then the first block is reset.
	Bool_t Switch(FrameBlock *, FrameBlock *);
45 46 47 48 49 50 51
	
public:
 	RMTActorConnection(ALoadedActor *producer, ALoadedActor *consumer, Int_t size) : 
		DActorConnection(producer,consumer,size), 
		fBlockLeft(0x0),
		fBlockRight(0x0)
		{ 
52 53
			fBlockLeft = new OneBlock(size); 
			fBlockRight = new OneBlock(size);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		}
 	virtual ~RMTActorConnection()
	{ 
		if ( fBlockLeft ) 
			delete fBlockLeft ; 
		if ( fBlockRight ) 
			delete fBlockRight ;
	}	
	
	//! test if some data are ready to be sent to the consumer (right part of the connection) 
	virtual Bool_t IsReadyforConsumer() ; 
	//! Prepare the connection so that data can be consumed by the right actor: return false if someting wrong
	virtual Bool_t SetReadyforConsumer(); 
	
	//! test if some data can be collected be the producer (left part of the connection) 
	virtual Bool_t IsReadyforProducer() ; 
	
	//! Prepare the connection so that data can be produced by the left actor	: return false if someting wrong
	virtual Bool_t SetReadyforProducer(); 
	
	virtual FrameBlock *GetBlock(const Char_t opt)
	{
		if ( opt == 'p' )
			return fBlockLeft;
		if ( opt == 'c' )
			return fBlockRight;	
		
		return 0x0;
	}
	
};

//! Root Multi-threaded Actor's Loader 
/*!
 */
class RMTActor : public ALoadedActor
{	
private:
	TThread *fThread;
private:
	TCondition *fCondition;
		
public:
97
 	RMTActor(const Char_t *name);
98 99 100 101 102 103 104 105 106 107 108 109 110
 	virtual ~RMTActor();
	
	void SetCondition(TCondition *cond)
		{ fCondition = cond; }
		
	TCondition *GetCondition() const
		{ return fCondition; }

	TThread *GetThread() const
		{ return fThread; }
	
	Short_t IsToBeRun();
	
111
	virtual void ProcessActor () ; 	
112 113 114 115 116 117 118 119 120 121 122 123 124

	//! 
	/*!
	 */
	static void ProcessAsThread (void *);
 	
	//!
 	//virtual void Process (); 	
};

//! An general emulator, could run or not in multi-threading environement 
/*!
 */
125
class RMTEmulator : public BaseEmulator
126 127 128 129 130 131 132
{
private:
	TMutex *fMutex;
	TCondition *fCondition;
	
protected:
	//! link left and right	... depends on the emulator's implementation
133
	virtual void LinkActors(ALoadedActor *left, ALoadedActor *middle, ALoadedActor *right);
134 135 136 137 138 139 140 141
	
public:
 	RMTEmulator(std::string name = "RMTEmulator");
 	virtual ~RMTEmulator();

	//! to wait for all the actors to finish their job.
	void Join();
	
142

143
	template <class T_actor> 
144
	RMTActor *Add(const Char_t *name_left, const Char_t *name_new_actor, const Char_t *name_right = "-", const Char_t *conf_dir = "./")
145
	{
146
		return BaseEmulator::AddLoader<RMTActor,T_actor>(name_left,name_new_actor,name_right,conf_dir,T_actor::process_config);
147
	}
148 149 150 151 152
	template <class T_actor, class T_Configurator> 
	RMTActor *Add(const Char_t *name_left, const Char_t *name_new_actor, const Char_t *name_right = "-", const Char_t *conf_dir = "./")
	{
		return BaseEmulator::AddLoader<RMTActor,T_actor>(name_left,name_new_actor,name_right,conf_dir,T_Configurator::process_config);
	}		
153
	//! Init the emulator
154
	virtual void Init( const NamedItem<std::string> &init_sequence = NamedItem<std::string>("InitSequence","config load init") );
155
	//! Run max loop
156
	virtual void Run(Int_t max_loop = -1); 
157 158 159 160
	
	virtual void Draw();

};
161 162 163 164
	
} // end namespace

#endif
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

/// boost 

/*
# include <boost/thread/thread.hpp>
# include <boost/thread/mutex.hpp>
# include <boost/thread/condition.hpp>


class BOOSTActorConnection : public DActorConnection 
{
protected:
	//! 
	FrameBlock *fBlockLeft, *fBlockRight;
	
protected:
	void Switch(FrameBlock *, FrameBlock *);
	
public:
 	BOOSTActorConnection(ALoadedActor *producer, ALoadedActor *consumer, Int_t size) : 
	DActorConnection(producer,consumer,size), 
	fBlockLeft(0x0),
	fBlockRight(0x0)
	{ fBlockLeft = new FrameBlock("",size); fBlockRight = new FrameBlock("",size); ; }
 	virtual ~BOOSTActorConnection()
	{ if ( fBlockLeft ) delete fBlockLeft ; if ( fBlockRight ) delete fBlockRight ;}	
	
	//! test if some data are ready to be sent to the consumer (right part of the connection) 
	virtual Bool_t IsReadyforConsumer() const ; 
	//! Prepare the connection so that data can be consumed by the right actor: return false if someting wrong
	virtual Bool_t SetReadyforConsumer(); 
	
	//! test if some data can be collected be the producer (left part of the connection) 
	virtual Bool_t IsReadyforProducer() const; 
	
	//! Prepare the connection so that data can be produced by the left actor	: return false if someting wrong
	virtual Bool_t SetReadyforProducer(); 
	
	virtual FrameBlock *GetBlock(const Char_t opt)
	{
		if ( opt == 'p' )
			return fBlockLeft;
		if ( opt == 'c' )
			return fBlockRight;	
		
		return 0x0;
	}
	
};

//! Root Multi-threaded Actor's Loader 

class BOOSTActor : public ALoadedActor
{	
private:
	TThread *fThread;
private:
	TCondition *fCondition;
	
public:
 	BOOSTActor(const Char_t *name, NarvalInterface *narval_actor);
 	virtual ~BOOSTActor();
	
	void SetCondition(TCondition *cond)
	{ fCondition = cond; }
	
	TCondition *GetCondition() const
	{ return fCondition; }
	
	TThread *GetThread() const
	{ return fThread; }
	
	virtual void ProcessBlock () ; 	
	
	//! 

	static void *ProcessAsThread (void *);
 	
	//!
 	//virtual void Process (); 	
};

//! An general emulator, could run or not in multi-threading environement 

class BOOSTEmulator : public Emulator
{
private:
	TMutex *fMutex;
	TCondition *fCondition;
	
protected:
	//! link left and right	... depends on the emulator's implementation
	void LinkActors(ALoadedActor *left, ALoadedActor *right, Int_t);
	
public:
 	BOOSTEmulator();
 	virtual ~BOOSTEmulator();
	
	//! to wait for all the actors to finish their job.
	void Join();
	
	
	template <class T_actor> 
	T_actor *Add(const Char_t *name_left, const Char_t *name_new_actor, const Char_t *name_right = "-", 
					 const Char_t *conf_path = "./", UInt_t buffer_size_left = 100u, UInt_t buffer_size_right = 100u)
	{
		return Emulator::Add<T_actor,RMTActor>(name_left,name_new_actor,name_right,conf_path,buffer_size_left,buffer_size_right);
	}
	
	//! Init the emulator
	virtual Bool_t Init(); 
	//! Run max loop
	virtual Bool_t Run(UInt_t max_loop); 
	
	virtual void Draw();
	
};


 */