/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/win32/LibMC.net/LibMC/MCAgent.cs

Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Text;
00004 using System.Runtime.InteropServices;
00005 
00050 namespace LibMC
00051 {
00061     public class MCAgent
00062     {
00063         private IntPtr agent_p;
00064         private String name = "";
00065         private int id = -1;
00066         private int numTasks = -1;
00067         private MC_AgentStatus_e status = MC_AgentStatus_e.MC_NO_STATUS;
00068         private MC_AgentType_e type = MC_AgentType_e.MC_NONE;
00069 
00075         public enum MC_AgentType_e
00076         {
00077             MC_NONE = -1,           
00078             MC_REMOTE_AGENT = 0,    
00079             MC_LOCAL_AGENT,         
00080             MC_RETURN_AGENT         
00081         };
00082 
00088         public enum MC_AgentStatus_e
00089         {
00090             MC_NO_STATUS = -1, 
00091             MC_WAIT_CH = 0,    
00092             MC_WAIT_MESSGSEND, 
00093             MC_AGENT_ACTIVE,   
00094             MC_AGENT_NEUTRAL,  
00095             MC_AGENT_SUSPENDED,
00096             MC_WAIT_FINISHED   
00097         };
00098 
00104         public MCAgent()
00105         {
00106             // Not using wrapper - default value
00107             agent_p = IntPtr.Zero;
00108         }
00109 
00110         internal MCAgent(IntPtr ip)
00111         {
00112             Agent = ip;
00113         }
00114 
00126         public override string ToString()
00127         {
00128             StringBuilder sb = new StringBuilder();
00129             sb.AppendFormat("MCAgent:\n\tName: {0}\n\tID: {1}\n\tNumTasks: {2}\n\tStatus: {3}\n\tType: {4}", 
00130                 AgentName.ToString(), 
00131                 AgentID.ToString(),
00132                 AgentNumTasks.ToString(),
00133                 Enum.GetName(typeof(MC_AgentStatus_e), AgentStatus),
00134                 Enum.GetName(typeof(MC_AgentType_e), AgentType));
00135 
00136             return sb.ToString();
00137         }
00138 
00139         /*
00140          * Accessors
00141          */
00142         internal IntPtr Agent
00143         {
00144             get
00145             {
00146                 if (agent_p == IntPtr.Zero)
00147                     throw new InvalidAgentException();
00148                 else
00149                     return agent_p;
00150             }
00151             set
00152             {
00153                 if (agent_p == IntPtr.Zero)
00154                 {
00155                     agent_p = value;
00156                     GetAgentFields();
00157                 }
00158                 else
00159                     throw new InvalidAgentException("Attempting to assign new agent to non-zero agent pointer!");
00160             }
00161         }
00162 
00163         private void GetAgentFields()
00164         {
00165             id = MCAgency._MC_GetAgentID(Agent);
00166             name = MCAgency._MC_GetAgentName(Agent);
00167             numTasks = MCAgency._MC_GetAgentNumTasks(Agent);
00168             status = MCAgency._MC_GetAgentStatus(Agent);
00169             type = MCAgency._MC_GetAgentType(Agent);
00170         }
00171 
00180         public int AgentID
00181         {
00182             get
00183             {
00184                 return id;
00185             }
00186             /*set
00187             {
00188                 id = value;
00189             }*/
00190         }
00191 
00200         public String AgentName
00201         {
00202             get
00203             {
00204                 return name;
00205             }
00206             /*set
00207             {
00208                 name = value;
00209             }*/
00210         }
00211 
00219         public int AgentNumTasks
00220         {
00221             get
00222             {
00223                 return numTasks;
00224             }
00225             /*set
00226             {
00227                 numTasks = value;
00228             }*/
00229         }
00230 
00241         public MC_AgentStatus_e AgentStatus
00242         {
00243             get
00244             {
00245                 status = MCAgency._MC_GetAgentStatus(Agent);
00246                 return status;
00247             }
00248             set
00249             {
00250                 MCAgency._MC_SetAgentStatus(Agent, value);
00251                 status = MCAgency._MC_GetAgentStatus(Agent);
00252             }
00253         }
00254 
00263         public MC_AgentType_e AgentType
00264         {
00265             get
00266             {
00267                 return type;
00268             }
00269             /*set
00270             {
00271                 type = value;
00272             }*/
00273         }
00274 
00284         public bool Valid
00285         {
00286             get
00287             {
00288                 return (agent_p != IntPtr.Zero);
00289             }
00290             /*set
00291             {
00292                 type = value;
00293             }*/
00294         }
00295 
00296         /*
00297          * Static casting operators
00298          */
00299         static public implicit operator IntPtr(MCAgent agent)
00300         {
00301             return agent.Agent;
00302         }
00303 
00304         static public implicit operator MCAgent(IntPtr ip)
00305         {
00306             return new MCAgent(ip);
00307         }
00308 
00309         /*
00310          * Agent functions
00311          */
00312 
00321         public int DeleteAgent()
00322         {
00323             return MCAgency._MC_DeleteAgent(Agent);
00324         }
00325 
00334         public String GetAgentXMLString()
00335         {
00336             return MCAgency._MC_GetAgentXMLString(Agent);
00337         }
00338 
00347         public int PrintAgentCode()
00348         {
00349             return MCAgency._MC_PrintAgentCode(Agent);
00350         }
00351 
00359         public String RetrieveAgentCode()
00360         {
00361             return MCAgency._MC_RetrieveAgentCode(Agent);
00362         }
00363 
00372         public int TerminateAgent()
00373         {
00374             return MCAgency._MC_TerminateAgent(Agent);
00375         }
00376 
00377         /*public int _MC_CallAgentFunc(IntPtr agent, String funcName, void* returnVal, void* varg);*/
00378         /*public void* _MC_GetAgentExecEngine(IntPtr agent);*/
00379         /*public int _MC_GetAgentReturnData(IntPtr agent, int task_num, void **data, int *dim, int **extent);*/
00380 
00381         /*
00382          * ACL Functions
00383          */
00384 
00397         public int AclPost(MCAclMessage message)
00398         {
00399             return MCAgency._MC_AclPost(Agent, message.AclMsg);
00400         }
00401 
00413         public MCAclMessage AclRetrieve()
00414         {
00415             IntPtr temp = MCAgency._MC_AclRetrieve(Agent);
00416             if (temp == IntPtr.Zero)
00417                 return new MCAclMessage();
00418             else
00419                 return new MCAclMessage(temp);
00420         }
00421 
00432         public MCAclMessage AclWaitRetrieve()
00433         {
00434             IntPtr temp = MCAgency._MC_AclWaitRetrieve(Agent);
00435             if (temp == IntPtr.Zero)
00436                 return new MCAclMessage();
00437             else
00438                 return new MCAclMessage(temp);
00439         }
00440 
00458         public int CallAgentFunc(String funcName, IntPtr retval, IntPtr varg)
00459         {
00460             return MCAgency._MC_CallAgentFunc(Agent, funcName, retval, varg);
00461         }
00462 
00488         public int CallAgentFunc(String funcName, ref object retval, ref object varg)
00489         {
00490             int temp = -1;
00491             IntPtr vargp = IntPtr.Zero, retvalp = IntPtr.Zero;
00492             try
00493             {
00494                 vargp = Marshal.AllocHGlobal(Marshal.SizeOf(varg));
00495                 Marshal.StructureToPtr(varg, vargp, false);
00496                 retvalp = Marshal.AllocHGlobal(Marshal.SizeOf(retval));
00497 
00498                 temp = CallAgentFunc(funcName, // The function to call
00499                     retvalp, // A place for the return value (void*)
00500                     vargp);  // The marshaled pointer to the argument (a structure)
00501                 retval = Marshal.PtrToStructure(retvalp, retval.GetType());
00502                 varg = Marshal.PtrToStructure(vargp, varg.GetType());
00503             }
00504             catch (OutOfMemoryException ex)
00505             {
00506                 Console.WriteLine("Error allocating memory for calling agent functions: " + ex.Message);
00507             }
00508             catch (ArgumentException ex)
00509             {
00510                 Console.WriteLine("Error marshaling arguments for calling agent functions: " + ex.Message);
00511             }
00512             finally
00513             {
00514                 Marshal.FreeHGlobal(vargp);
00515                 Marshal.FreeHGlobal(retvalp);
00516             }
00517 
00518             return temp;
00519         }
00520 
00533         public IntPtr GetAgentExecEngine()
00534         {
00535             return MCAgency._MC_GetAgentExecEngine(Agent);
00536         }
00537 
00554         public int GetAgentReturnData(int task_num, IntPtr data, IntPtr dim, IntPtr extent)
00555         {
00556             //return _MC_GetAgentReturnData(IntPtr agent, int task_num, IntPtr data, IntPtr dim, IntPtr extent);
00557             throw new Exception("GetAgentReturnData is not yet implemented!");
00558         }
00559     }
00560 
00568     public class InvalidAgentException : SystemException
00569     {
00570         private const String msg = "Private agent pointer not valid.";
00571 
00579         public InvalidAgentException()
00580             : base(msg)
00581         {
00582         }
00583 
00590         public InvalidAgentException(String exc)
00591             : base(exc)
00592         {
00593         }
00594     }
00595 }

Generated on Fri Jul 11 17:59:46 2008 for Mobile-C by  doxygen 1.5.4