00001 using System; 00002 using System.Collections.Generic; 00003 using System.Text; 00004 using System.Runtime.InteropServices; 00005 00330 namespace LibMC 00331 { 00341 public partial class MCAgency 00342 { 00349 public enum MCAgencyState 00350 { 00351 NoState = -1, 00352 Initialized = 0, 00353 Running, 00354 Halted, 00355 Ended, 00356 } 00357 00363 public enum ChShellType 00364 { 00365 CH_REGULARCH = 0, 00366 CH_SAFECH = 1, 00367 } 00368 00369 private IntPtr agency_p = IntPtr.Zero; 00370 private MCAgencyOptions_t options; 00371 private int port = -1; 00372 private MCAgencyState state = MCAgencyState.NoState; 00373 00381 public MCAgency() 00382 { 00383 options = new MCAgencyOptions_t(); 00384 _MC_InitializeAgencyOptions(ref options); 00385 state = MCAgencyState.Initialized; 00386 } 00387 00388 private IntPtr Agency 00389 { 00390 get 00391 { 00392 if (agency_p == IntPtr.Zero) 00393 throw new InvalidAgentException(); 00394 else 00395 return agency_p; 00396 } 00397 set 00398 { 00399 if (value == IntPtr.Zero) 00400 throw new InvalidAgentException("Attempting to assign null pointer to agency internal pointer!"); 00401 else if (agency_p == IntPtr.Zero) 00402 agency_p = value; 00403 else 00404 throw new InvalidAgentException("Attempting to assign new agency to non-zero agency pointer!"); 00405 } 00406 } 00407 00417 public int Port 00418 { 00419 get 00420 { 00421 return port; 00422 } 00423 set 00424 { 00425 port = value; 00426 } 00427 } 00428 00437 public MCAgencyState State 00438 { 00439 get 00440 { 00441 return state; 00442 } 00443 } 00444 00445 /* 00446 * Agency functions 00447 */ 00448 00459 public int Initialize() 00460 { 00461 if (port == -1) 00462 return -1; 00463 else 00464 { 00465 Agency = MCAgency._MC_Initialize(port, ref options); 00466 state = MCAgencyState.Running; 00467 return 0; 00468 } 00469 } 00470 00481 public int End() 00482 { 00483 int temp = MCAgency._MC_End(Agency); 00484 state = MCAgencyState.Ended; 00485 return temp; 00486 } 00487 00499 public int ChInitializeOptions(ChShellType shellType, String home) 00500 { 00501 ChOptions_t options = new ChOptions_t(); 00502 options.chhome = home; 00503 options.shelltype = (int)shellType; 00504 return _MC_ChInitializeOptions(Agency, options); 00505 } 00506 00507 /* \brief Sets all threads for the agency to "on." 00508 * 00509 * Sets all threads for the agency to "on." This is also the default state 00510 * for the agency. 00511 * 00512 * \returns The return value of the underlying MC_SetThreadsAllOn function. 00513 * 00514 * \note This function must be called before the agency is started. 00515 */ 00516 public int SetThreadsAllOn() 00517 { 00518 return _MC_SetThreadsAllOn(ref options); 00519 } 00520 00530 public int SetThreadsAllOff() 00531 { 00532 return _MC_SetThreadsAllOff(ref options); 00533 } 00534 00546 public int SetThreadOn(MC_ThreadIndex_e index) 00547 { 00548 return _MC_SetThreadOn(ref options, index); 00549 } 00550 00561 public int SetThreadOff(MC_ThreadIndex_e index) 00562 { 00563 return _MC_SetThreadOff(ref options, index); 00564 } 00565 00576 public int HaltAgency() 00577 { 00578 int temp = _MC_HaltAgency(Agency); 00579 state = MCAgencyState.Halted; 00580 return temp; 00581 } 00582 00593 public int ResumeAgency() 00594 { 00595 int temp = _MC_ResumeAgency(Agency); 00596 state = MCAgencyState.Running; 00597 return temp; 00598 } 00599 00609 public int SetDefaultAgentStatus(MCAgent.MC_AgentStatus_e status) 00610 { 00611 return _MC_SetDefaultAgentStatus(Agency, status); 00612 } 00613 00622 public MCAgent WaitRetrieveAgent() 00623 { 00624 IntPtr temp = _MC_WaitRetrieveAgent(Agency); 00625 if (temp != IntPtr.Zero) 00626 return new MCAgent(temp); 00627 else 00628 return new MCAgent(); 00629 } 00630 00639 public int WaitAgent() 00640 { 00641 return _MC_WaitAgent(Agency); 00642 } 00643 00644 /* 00645 * Migration functions 00646 */ 00647 00658 public int SendAgentMigrationMessageFile(String filename, String hostname, int port) 00659 { 00660 return MCAgency._MC_SendAgentMigrationMessageFile(Agency, filename, hostname, port); 00661 } 00662 00672 public int LoadAgentMigrationMessageFile(String filename) 00673 { 00674 return MCAgency._MC_SendAgentMigrationMessageFile(Agency, filename, "localhost", this.Port); 00675 } 00676 00687 public int SendAgentMigrationMessage(String message, String hostname, int port) 00688 { 00689 return _MC_SendAgentMigrationMessage(Agency, message, hostname, port); 00690 } 00691 00692 /* 00693 * Signals and threading functions 00694 */ 00695 00705 public int CondBroadcast(int id) 00706 { 00707 return _MC_CondBroadcast(Agency, id); 00708 } 00709 00719 public int CondSignal(int id) 00720 { 00721 return _MC_CondSignal(Agency, id); 00722 } 00723 00734 public int CondReset(int id) 00735 { 00736 return _MC_CondReset(Agency, id); 00737 } 00738 00749 public int CondWait(int id) 00750 { 00751 return _MC_CondWait(Agency, id); 00752 } 00753 00764 public int MutexLock(int id) 00765 { 00766 return _MC_MutexLock(Agency, id); 00767 } 00768 00778 public int MutexUnlock(int id) 00779 { 00780 return _MC_MutexUnlock(Agency, id); 00781 } 00782 00792 public int SemaphorePost(int id) 00793 { 00794 return _MC_SemaphorePost(Agency, id); 00795 } 00796 00807 public int SemaphoreWait(int id) 00808 { 00809 return _MC_SemaphoreWait(Agency, id); 00810 } 00811 00820 public int ResetSignal() 00821 { 00822 return _MC_ResetSignal(Agency); 00823 } 00824 00834 public int SyncDelete(int id) 00835 { 00836 return _MC_SyncDelete(Agency, id); 00837 } 00838 00850 public int SyncInit(int id) 00851 { 00852 return _MC_SyncInit(Agency, id); 00853 } 00854 00863 public int WaitSignal(MC_Signal_e /*int*/ signals) 00864 { 00865 return _MC_WaitSignal(Agency, (int)signals); 00866 } 00867 00877 public int BarrierDelete(int id) 00878 { 00879 return _MC_BarrierDelete(Agency, id); 00880 } 00881 00891 public int BarrierInit(int id, int num_procs) 00892 { 00893 return _MC_BarrierInit(Agency, id, num_procs); 00894 } 00895 00896 /* 00897 * Steering functions 00898 */ 00899 00909 public MC_SteerCommand_e SteerControl() 00910 { 00911 return _MC_SteerControl(); 00912 } 00913 00926 /*public int _MC_Steer(IntPtr agency, int (*funcptr)(void* data), void *arg);*/ 00927 public int Steer(IntPtr funcptr, IntPtr arg) 00928 { 00929 //return _MC_Steer(Agency, IntPtr funcptr, IntPtr arg); 00930 throw new Exception("Steer() is not yet implemented!"); 00931 } 00932 00933 /* 00934 * Services 00935 */ 00936 00952 public int RegisterService(MCAgent agent, int agentID, String agentName, String[] serviceNames, int numServices) 00953 { 00954 return _MC_RegisterService(Agency, agent.Agent, agentID, agentName, serviceNames, numServices); 00955 } 00956 00974 /*private static extern int _MC_SearchForService(IntPtr agency, String searchString, char*** agentNames, char*** serviceNames, int** agentIDs, int* numResults);*/ 00975 public int SearchForService(String searchString, IntPtr agentNames, IntPtr serviceNames, IntPtr agentIDs, IntPtr numResults) 00976 { 00977 //return _MC_SearchForService(IntPtr agency, String searchString, IntPtr agentNames, IntPtr serviceNames, IntPtr agentIDs, IntPtr numResults); 00978 throw new Exception("SearchForService() is not yet implemented!"); 00979 } 00980 00981 /* 00982 * Agent functions 00983 */ 00984 00993 public int AddAgent(MCAgent agent) 00994 { 00995 return _MC_AddAgent(Agency, agent.Agent); 00996 } 00997 01006 public MCAgent FindAgentByName(String name) 01007 { 01008 return new MCAgent(_MC_FindAgentByName(Agency, name)); 01009 } 01010 01019 public MCAgent FindAgentByID(int id) 01020 { 01021 return new MCAgent(_MC_FindAgentByID(Agency, id)); 01022 } 01023 01031 public MCAgent RetrieveAgent() 01032 { 01033 return new MCAgent(_MC_RetrieveAgent(Agency)); 01034 } 01035 01036 /* 01037 * ACL Functions 01038 */ 01039 01048 public int AclSend(MCAclMessage acl_message) 01049 { 01050 return MCAgency._MC_AclSend(Agency, acl_message.AclMsg); 01051 } 01052 01061 public int MainLoop() 01062 { 01063 return _MC_MainLoop(Agency); 01064 } 01065 } 01066 01074 public class InvalidAgencyException : SystemException 01075 { 01076 private const String msg = "Private agency pointer not valid."; 01077 01085 public InvalidAgencyException() 01086 : base(msg) 01087 { 01088 } 01089 01096 public InvalidAgencyException(String exc) 01097 : base(exc) 01098 { 01099 } 01100 } 01101 }