00001
00002
00003
00004
00005
00006
00007
00008 #ifndef Y2AgentComponent_h
00009 #define Y2AgentComponent_h
00010
00011
00012 #include <ycp/y2log.h>
00013 #include <y2/Y2Component.h>
00014
00015 #include <ycp/YCPVoid.h>
00016
00017 class SCRAgent;
00018
00019
00023 template <class Agent> class Y2AgentComp : public Y2Component
00024 {
00025
00026 public:
00027
00031 Y2AgentComp (const char*);
00032
00036 ~Y2AgentComp ();
00037
00041 string name () const { return my_name; }
00042
00046 YCPValue evaluate (const YCPValue &command);
00047
00051 SCRAgent* getSCRAgent ();
00052
00053 YCPValue Y2AgentComp<Agent>::Read (const YCPPath &path);
00054
00055 private:
00056
00060 const char* my_name;
00061
00065 Agent* agent;
00066
00067 };
00068
00069
00070 template <class Agent>
00071 Y2AgentComp<Agent>::Y2AgentComp (const char* my_name)
00072 : my_name (my_name),
00073 agent (0)
00074 {
00075 }
00076
00077
00078 template <class Agent>
00079 Y2AgentComp<Agent>::~Y2AgentComp ()
00080 {
00081 if (agent)
00082 {
00083 delete agent;
00084 }
00085 }
00086
00087
00088 template <class Agent> YCPValue
00089 Y2AgentComp<Agent>::evaluate (const YCPValue& v)
00090 {
00091 y2debug ("evaluate (%s)", v->toString ().c_str ());
00092
00093 if (!agent)
00094 getSCRAgent ();
00095
00096 y2debug ("Going to evaluate %s", v->toString ().c_str ());
00097
00098 YCPValue value = v;
00099 if (value->isCode ())
00100 value = value->asCode ()->evaluate ();
00101
00102 if (value.isNull () || value->isVoid ())
00103 return value;
00104
00105 y2debug ("After code evaluation: %s", value->toString ().c_str ());
00106
00107 if( value->isTerm () ) {
00108 YCPTerm term = value ->asTerm ();
00109 string command = term->name ();
00110 YCPList args = term->args ();
00111
00112
00113 if( command == "Read" ) {
00114 return getSCRAgent ()-> Read (args->value (0)->asPath (), args->size() > 1 ? args->value (1) : YCPNull ()) ;
00115 }
00116 else if( command == "Write" ) {
00117 return getSCRAgent ()-> Write (args->value (0)->asPath (), args->value (1), args->size () > 2 ? args->value (2) : YCPNull ()) ;
00118 }
00119 else if( command == "Dir" ) {
00120 return getSCRAgent ()-> Dir (args->value (0)->asPath ()) ;
00121 }
00122 else if( command == "Error" ) {
00123 return getSCRAgent ()-> Error (args->value (0)->asPath ()) ;
00124 }
00125 else if( command == "Execute" ) {
00126 y2debug( "Execute, arg size is %d", args->size() );
00127 switch( args->size() ) {
00128 case 1:
00129 return getSCRAgent ()-> Execute (args->value (0)->asPath ()) ;
00130 case 2:
00131 return getSCRAgent ()-> Execute (args->value (0)->asPath (), args->value (1)) ;
00132 default:
00133 return getSCRAgent ()-> Execute (args->value (0)->asPath (), args->value (1), args->value (2)) ;
00134 }
00135 }
00136 else {
00137 y2debug( "Passing term to otherCommand" );
00138 return getSCRAgent ()-> otherCommand (term);
00139 }
00140 }
00141 #if 0
00142 if( value->isCode () ) {
00143 y2debug( "Passing (evaluated) code to otherCommand" );
00144 return getSCRAgent ()-> otherCommand (value->asCode ()->evaluate ()->asTerm ());
00145 }
00146 #endif
00147
00148 y2error( "Unhandled value (%d): %s", value->valuetype (), value->toString ().c_str () );
00149
00150 return YCPVoid();
00151 }
00152
00153
00154 template <class Agent> SCRAgent*
00155 Y2AgentComp<Agent>::getSCRAgent ()
00156 {
00157 if (!agent)
00158 {
00159 agent = new Agent ();
00160 }
00161
00162 return agent;
00163 }
00164
00165 template <class Agent> YCPValue Y2AgentComp<Agent>::Read (const YCPPath &path)
00166 {
00167 y2error( "Y2AgentComp::Read" );
00168 return getSCRAgent()->Read (path);
00169 }
00170
00171 #endif // Y2AgentComponent_h