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