Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

netnode.h

Go to the documentation of this file.
00001 #ifndef NETNODE_H
00002 #define NETNODE_H
00003 
00004 #include <qtextstream.h>
00005 #include <qlist.h>
00006 #include <qdict.h>
00007 #include <qpixmap.h>
00008 #include <qstringlist.h>
00009 #include <qobject.h>
00010 #include <time.h>
00011 
00012 #include <Utils.h>
00013 #include <system.h>
00014 
00015 // difference feature interfaces
00016 class AsDevice;
00017 class AsLine;
00018 class AsNetworkSetup;
00019 class AsFullSetup;
00020 
00021 // needed for plugin creation function
00022 #include <qlist.h>
00023 
00024 class ANetNode;
00025 class ANetNodeInstance;
00026 class NetworkSetup;
00027 class QTextStream;
00028 class RuntimeInfo;
00029 class InterfaceInfo;
00030 class NSResources;
00031 
00032 extern QString & deQuote( QString & X );
00033 extern QString quote( QString X );
00034 
00035 #include "systemfile.h"
00036 
00037 typedef enum State {
00038     // if we have not yet detected the state of the device
00039     Unchecked = 0,
00040     // if we cannot determine the state
00041     Unknown = 1,
00042     // if networkSetup cannot be established e.g. because
00043     // the hardware is not available
00044     Unavailable = 2,
00045     // if the networkSetup cannot be establishec but NOT
00046     // because it is physically impossible but because
00047     // it has been disabled for FUNCTIONAL reasons
00048     Disabled = 3,
00049     // if networkSetup is available to is currently down
00050     // i.e. the corresponding hardware is not activated
00051     Off = 4,
00052     // if networkSetup is available to be used (i.e. the
00053     // devices if fully ready to be used
00054     Available = 5,
00055     // if networkSetup is being used
00056     IsUp = 6
00057 } State_t;
00058 
00059 typedef enum Action {
00060     // to make the device unavailable functionally -> to disabled
00061     Disable = 0,
00062     // to make the device available functionally -> to off
00063     Enable = 1,
00064     // bring the hardware up -> to Available
00065     Activate = 2,
00066     // bring the hardware down -> to off
00067     Deactivate = 3,
00068     // bring the networkSetup up -> to IsUp
00069     Up = 4,
00070     // bring the networkSetup down -> to Available
00071     Down = 5
00072 } Action_t;
00073 
00074 class ANetNode : public QObject {
00075 
00076 public:
00077 
00078     typedef QArray<ANetNode *> NetNodeList;
00079 
00080     ANetNode( const char * Name ) : QObject( 0, Name ) {}
00081     virtual ~ANetNode(){};
00082 
00083     //
00084     //
00085     // standard methods with sensible default
00086     //
00087     //
00088 
00089     inline int done( void ) 
00090       { return Done; }
00091     inline void setDone( int D ) 
00092       { Done = D; }
00093 
00094     // does this Node provide a NetworkSetup
00095     bool isToplevel( void );
00096 
00097     // set the value of an attribute
00098     void setAttribute( QString & Attr, QString & Value ) ;
00099     void saveAttributes( QTextStream & TS ) ;
00100 
00101     // compiled references to 'needed' NetNodes -> needs list
00102     inline void setAlternatives( NetNodeList * Alt )
00103       { Alternatives = Alt; }
00104     inline NetNodeList & alternatives( void ) 
00105       { return *Alternatives; }
00106 
00107     //
00108     //
00109     // Virtual methods with sensible default
00110     //
00111     //
00112 
00113     // do instances of this noce class have data for this file
00114     virtual bool hasDataForFile( SystemFile & ) 
00115       { return 0; }
00116 
00117     // open proper file SF identified by S
00118     // this method is called by NS2.  
00119     //
00120     // overrule this ONLY if this proper file is a common file
00121     // for all NNI of this node class and the data generated
00122     // by each of the NNI needs to be put in one file
00123     //
00124     // the function can return a set of folders that should
00125     // be created (or perhaps already exist) prior to opening 
00126     // the file
00127     //
00128     // if this is the case the file should be (re)opened in append
00129     // return 0 if file cannot be opened
00130     virtual bool openFile( SystemFile &SF,
00131                            ANetNodeInstance * NNI,
00132                            QStringList & PathToCreate );
00133 
00134     // generate instance independent stuff
00135     // 0 : data output, 1 no data, 2 error
00136     virtual short generatePreamble( SystemFile & )
00137       { return 1; }
00138 
00139     // generate instance independent stuff
00140     // 0 : data output, 1 no data, 2 error
00141     virtual short generatePostamble( SystemFile & )
00142       { return 1; }
00143 
00144     // generate instance dependent but instance common stuff
00145     // 0 : data output, 1 no data, 2 error
00146     virtual short generateFile( SystemFile &,
00147                                ANetNodeInstance * ,
00148                                long ) 
00149       { return 1; }
00150 
00151     // generate NIC name based on instance nr
00152     // only relevant if node instances are devices
00153     virtual QString genNic( long ) 
00154       { return QString(""); }
00155 
00156     // max number of instances for this node type
00157     // only relevant if node instances are devices
00158     virtual long instanceCount( void ) 
00159       { return 1; }
00160 
00161     // return ID list for each file generated specially for
00162     // this node type
00163     virtual QStringList properFiles( void )
00164       { return QStringList(); }
00165 
00166     //
00167     //
00168     // pure virtual methods with sensible default
00169     //
00170     //
00171 
00172     // pixmap needed for this NetNode
00173     virtual const QString pixmapName() = 0;
00174 
00175     // description for  this NetNode
00176     virtual const QString nodeDescription() = 0;
00177 
00178     // create a blank instance of a net node
00179     virtual ANetNodeInstance * createInstance( void ) = 0;
00180 
00181     // return features this NetNode provides
00182     virtual const char ** provides( void ) = 0;
00183 
00184     // return features this NetNode needs
00185     virtual const char ** needs( void ) = 0;
00186 
00187 protected :
00188 
00189     NetNodeList * Alternatives;
00190 
00191 private :
00192 
00193     virtual void setSpecificAttribute( QString & , QString & ) = 0;
00194     virtual void saveSpecificAttribute( QTextStream & ) = 0;
00195     int Done;
00196 
00197 };
00198 
00199 class ANetNodeInstance : public QObject {
00200 
00201 public:
00202 
00203     ANetNodeInstance( ANetNode * NN ) : QObject()
00204         { IsModified=0; NodeType = NN; IsNew = TRUE; }
00205     virtual ~ANetNodeInstance( void ) { }
00206 
00207     inline int done( void ) 
00208       { return Done; }
00209     inline void setDone( int D ) 
00210       { Done = D; }
00211 
00212     // return data was modified
00213     inline void setModified( bool M ) 
00214       {  IsModified = M; }
00215     inline bool isModified( void ) 
00216       {  return IsModified; }
00217 
00218     // get next node
00219     ANetNodeInstance * nextNode();
00220     // return NetNode this is an instance of
00221     ANetNode * nodeClass( void ) 
00222         { return NodeType; }
00223 
00224     // intialize am instance of a net node
00225     void initialize( void );
00226 
00227     // set the value of an attribute
00228     void setAttribute( QString & Attr, QString & Value ) ;
00229     void saveAttributes( QTextStream & TS ) ;
00230 
00231     // return true if node isntance is NEW and not loaded
00232     inline void setNew( bool IsN ) 
00233       { IsNew = IsN; }
00234     inline bool isNew( void ) 
00235       { return IsNew; }
00236 
00237     // return description for this instance
00238     inline QString & description( void ) 
00239       { return Description; }
00240     inline void setDescription( const QString & S ) 
00241       { Description = S; }
00242 
00243     // pixmap for this instance -> from NetNode
00244     inline const QString pixmapName( void )
00245       { return NodeType->pixmapName(); }
00246 
00247     inline const char ** provides( void )
00248       { return NodeType->provides(); }
00249 
00250     inline const char ** needs( void )
00251       { return NodeType->needs(); }
00252 
00253     inline void setNetworkSetup( NetworkSetup * NC ) 
00254       { TheNetworkSetup = NC; }
00255     inline NetworkSetup * networkSetup( void ) 
00256       { return TheNetworkSetup; }
00257 
00258     //
00259     //
00260     // Virtual methods with sensible defaults
00261     //
00262     //
00263 
00264 
00265 
00266     // open proper file identified by S
00267     virtual bool openFile( SystemFile &,
00268                            QStringList & )
00269       { return 0; }
00270 
00271     // check if this node (or sub nodes) have data for this file
00272     virtual bool hasDataForFile( SystemFile & S ) 
00273       { return nodeClass()->hasDataForFile( S ); }
00274 
00275     // generate code specific for this node but embedded
00276     // in the section of the parent
00277     // this is called within the code of the parent
00278     virtual short generateFileEmbedded( SystemFile & SF,
00279                                        long DevNr ) 
00280       { ANetNodeInstance * NNI = nextNode();
00281         return (NNI) ? NNI->generateFileEmbedded( SF, DevNr ) : 1;
00282       }
00283 
00284     // generate code specific for this node 
00285     // (or find the first node that does)
00286     virtual short generateFile( SystemFile & SF,
00287                                long  DevNr ) 
00288       { ANetNodeInstance * NNI = nextNode();
00289         return (NNI) ? NNI->generateFile( SF, DevNr ) : 1;
00290       }
00291 
00292     // return true if this node instance is triggered by this trigger
00293     // could be delegated to deeper instances
00294     virtual bool triggeredBy( const QString & )
00295       { return 0; }
00296 
00297     //
00298     //
00299     // Pure virtual functions
00300     //
00301     //
00302 
00303     // return runtime information for this node
00304     virtual RuntimeInfo * runtime( void ) = 0;
00305 
00306     // create edit widget under parent
00307     virtual QWidget * edit( QWidget * parent ) = 0;
00308 
00309     // is given data acceptable
00310     virtual QString acceptable( void ) = 0;
00311 
00312     // get data from GUI and store in node
00313     virtual void commit( void ) = 0;
00314 
00315     // returns node specific data -> only useful for 'buddy'
00316     virtual void * data( void ) = 0;
00317 
00318 protected :
00319 
00320     virtual void setSpecificAttribute( QString & , QString & ) = 0;
00321     virtual void saveSpecificAttribute( QTextStream & ) = 0;
00322 
00323     ANetNode * NodeType;
00324     // networkSetup to which this node belongs to
00325     NetworkSetup * TheNetworkSetup;
00326     QString   Description;
00327     bool      IsModified;
00328     // true if this nodeinstance was just created (and not
00329     // loaded from file
00330     bool      IsNew;
00331     int       Done;
00332 
00333     static long InstanceCounter;
00334 };
00335 
00336 class ErrorNNI: public ANetNodeInstance {
00337 
00338 public:
00339 
00340     ErrorNNI( const QString & _Name ) : ANetNodeInstance( 0 ) {
00341       setName( _Name.latin1() );
00342     }
00343 
00344     RuntimeInfo * runtime( void ) {
00345       return 0;
00346     }
00347 
00348     // create edit widget under parent
00349     QWidget * edit( QWidget * parent ) {
00350       return 0;
00351     }
00352 
00353     // is given data acceptable
00354     QString acceptable( void ) {
00355       return QString();
00356     }
00357 
00358     // get data from GUI and store in node
00359     void commit( void ) {
00360     }
00361 
00362     // returns node specific data -> only useful for 'buddy'
00363     void * data( void ) {
00364       return 0;
00365     }
00366 
00367 protected :
00368 
00369     void setSpecificAttribute( QString & , QString & ) {
00370     }
00371 
00372     void saveSpecificAttribute( QTextStream & ) {
00373     }
00374 };
00375 
00376 class RuntimeInfo : public QObject {
00377 
00378       Q_OBJECT
00379 
00380 public :
00381 
00382       RuntimeInfo( ANetNodeInstance * TheNNI )
00383         { NNI = TheNNI; }
00384 
00385       //
00386       //
00387       // methods to be overloaded by networkSetup capable
00388       // runtimes
00389       //
00390       //
00391 
00392 
00393       //
00394       //
00395       // methods to be overloaded by device capable
00396       // runtimes
00397       //
00398       //
00399 
00400       // does this node handles this interface e.g.eth0
00401       // recurse deeper if this node cannot answer that question
00402       virtual bool handlesInterface( const QString & S ) {
00403         RuntimeInfo * RI = device();
00404         if( RI ) {
00405           return RI->handlesInterface( S );
00406         }
00407         return 0;
00408       }
00409       bool handlesInterface( const InterfaceInfo & I ) {
00410         RuntimeInfo * RI = device();
00411         if( RI ) {
00412           return RI->handlesInterface( I );
00413         }
00414         return 0;
00415       }
00416 
00417       //
00418       //
00419       // methods to be overloaded by full setup capable
00420       // runtimes
00421       //
00422       //
00423 
00424       // return description for this full setup
00425       virtual const QString & description( void ) {
00426         return fullSetup()->description( );
00427       }
00428       // return triggers that should fire when this 
00429       // setup is brought up
00430       virtual const QStringList & triggers( void ) {
00431         return fullSetup()->triggers( );
00432       }
00433 
00434       //
00435       //
00436       // methods to be overloaded by line capable
00437       // runtimes
00438       //
00439       //
00440 
00441       // return the device file ('/dev/xxx') created
00442       // by this line capable runtime
00443       virtual QString deviceFile( void ) {
00444         RuntimeInfo * RI = line();
00445         if( RI ) {
00446           return RI->deviceFile();
00447         }
00448         return QString();
00449       }
00450 
00451       //
00452       //
00453       // runtime interface
00454       //
00455       //
00456 
00457       // return the node that offers device capability
00458       virtual RuntimeInfo * device( void )
00459         { RuntimeInfo * RI = nextNode();
00460           return (RI) ? RI->device() : 0;
00461         }
00462 
00463       // return the node that offers connection capability
00464       virtual RuntimeInfo * connection( void )
00465         { RuntimeInfo * RI = nextNode();
00466           return (RI) ? RI->connection() : 0;
00467         }
00468 
00469       // return the node that offers line capability
00470       virtual RuntimeInfo * line( void )
00471         { RuntimeInfo * RI = nextNode();
00472           return (RI) ? RI->line() : 0;
00473         }
00474 
00475       // return the node that offers full setup capability
00476       virtual RuntimeInfo * fullSetup( void )
00477         { RuntimeInfo * RI = nextNode();
00478           return (RI) ? RI->fullSetup() : 0;
00479         }
00480 
00481       inline ANetNodeInstance * netNode() 
00482         { return NNI; }
00483 
00484       inline NetworkSetup * networkSetup() 
00485         { return NNI->networkSetup(); }
00486 
00487       virtual State_t detectState( void ) = 0;
00488       // public API to set the state
00489       virtual QString setState( NetworkSetup * NC, 
00490                                 Action_t A, 
00491                                 bool Force = 0 );
00492 
00493       inline RuntimeInfo * nextNode( void ) {
00494          ANetNodeInstance * NNI = netNode()->nextNode();
00495          return (NNI) ? NNI->runtime() : 0;
00496       }
00497 
00498 signals :
00499 
00500       // sent by device if state changes
00501       void stateChanged( State_t S, ANetNodeInstance * NNI );
00502 
00503 protected :
00504 
00505       // set state of this node (private API)
00506       virtual QString setMyState( NetworkSetup * NC, 
00507                                 Action_t A, 
00508                                 bool Force = 0 ) = 0;
00509 
00510       // networkSetup this runtime info belongs to
00511       ANetNodeInstance * NNI;
00512 };
00513 
00514 class NetworkSetup : public QList<ANetNodeInstance> {
00515 
00516 public :
00517 
00518       NetworkSetup( void );
00519       NetworkSetup( QTextStream & TS, bool & Dangling );
00520       ~NetworkSetup( void );
00521 
00522       // copy settings from NC to this 
00523       void copyFrom( const NetworkSetup & NC );
00524 
00525       inline int done( void ) 
00526         { return Done; }
00527       inline void setDone( int D ) 
00528         { Done = D; }
00529 
00530       inline int number( void ) 
00531         { return Number; }
00532       inline void setNumber( int i ) 
00533         { Number = i; }
00534       inline bool isNew( void ) 
00535         { return IsNew; }
00536       inline void setNew( bool N ) 
00537         { IsNew = N ; }
00538       inline bool isModified( void ) 
00539         { return IsModified; }
00540       inline void setModified( bool N ) 
00541         { IsModified = N ; }
00542 
00543       inline bool handlesInterface( const QString & S ) {
00544         return getToplevel()->runtime()->handlesInterface( S );
00545       }
00546 
00547       // return the interface in the OS that is assigned to
00548       // this device
00549       inline InterfaceInfo * assignedInterface( void ) {
00550         return AssignedInterface;
00551       }
00552 
00553       // assign the interface to this device
00554       inline void assignInterface( InterfaceInfo * NI ) {
00555         // cleanup previous
00556         if( AssignedInterface ) {
00557           AssignedInterface->assignToNetworkSetup( 0 );
00558         }
00559         if( NI ) {
00560           // assign new
00561           NI->assignToNetworkSetup( this );
00562         }
00563         AssignedInterface = NI;
00564       }
00565 
00566       inline RuntimeInfo * device() {
00567         return getToplevel()->runtime()->device();
00568       }
00569 
00570       const QStringList & triggers();
00571 
00572       State_t state( bool Update = 0 );
00573 
00574       // get the ixmap for this device
00575       QPixmap devicePixmap( void );
00576       QPixmap statePixmap( State_t S );
00577       inline QPixmap statePixmap( bool Update = 0 ) 
00578         { return statePixmap( state(Update) ); }
00579       QString stateName( State_t );
00580       inline QString stateName( bool Update = 0 ) 
00581         { return stateName( state(Update) ); }
00582 
00583       QString setState( Action_t A, bool Force = 0 );
00584 
00585       void save( QTextStream & TS );
00586 
00587       void append( ANetNodeInstance * NNI );
00588 
00589       // makes sure that all items in the networkSetup point to 
00590       // that connectoin
00591       void reassign( void );
00592 
00593       ANetNodeInstance * getToplevel( void );
00594       ANetNodeInstance * findNext( ANetNodeInstance * NNI );
00595       ANetNodeInstance * findByName( const QString & S );
00596 
00597       inline const QString & name()
00598         { return Name; }
00599 
00600       const QString & description( void );
00601 
00602       inline void setName( const QString & N)
00603         { Name = N; }
00604 
00605       inline State_t currentState( void )
00606         { return CurrentState; }
00607       inline void setCurrentState( State_t S )
00608         { CurrentState = S; }
00609 
00610       // return TRUE if this node can have data to be inserted in 
00611       // file identified by S
00612       bool hasDataForFile( SystemFile & S );
00613       ANetNodeInstance * firstWithDataForFile( SystemFile & );
00614 
00615       // generate items for this file -> toplevel call
00616       short generateFile( SystemFile & SF,
00617                          long DN // device number
00618                        )
00619         { return getToplevel()->generateFile( SF, DN ); }
00620 
00621       bool triggeredBy( const QString & Trigger )
00622         { return getToplevel()->triggeredBy( Trigger ); }
00623 
00624 private :
00625 
00626       int compareItems ( QCollection::Item item1, 
00627                          QCollection::Item item2 );
00628 
00629       long Number;
00630 
00631       // state of this networkSetup
00632       State_t CurrentState;
00633 
00634       QString Name;
00635       // true if this collection was just created (and not
00636       // loaded from file
00637       bool    IsNew;
00638       // index in listbox
00639       int     Index;
00640       bool    IsModified;
00641       int     Done;
00642 
00643       InterfaceInfo * AssignedInterface;
00644 
00645 };
00646 
00647 #endif

Generated on Sat Nov 5 16:17:54 2005 for OPIE by  doxygen 1.4.2