/home/dko/projects/mobilec/trunk/src/include/libmc.h File Reference

MobileC api header file. More...

#include <embedch.h>

Go to the source code of this file.

Data Structures

struct  agency_s
 The agency handle. More...
struct  MCAgencyOptions_s
 User modifiable agency options. More...

Defines

#define EXPORTMC
#define MC_Wait(arg1)   MC_MainLoop(arg1)

Typedefs

typedef enum error_code_e error_code_t
typedef enum MC_SteerCommand_e MC_SteerCommand_t
typedef struct agency_s agency_t
typedef agency_tagency_p
typedef agency_p MCAgency_t
typedef struct MCAgencyOptions_s MCAgencyOptions_t
typedef struct agent_s agent_t
typedef agent_tMCAgent_t
typedef agent_tagent_p

Enumerations

enum  error_code_e {
  MC_SUCCESS = 0, MC_ERR, MC_ERR_CONNECT, MC_ERR_PARSE,
  MC_ERR_EMPTY, MC_ERR_INVALID, MC_ERR_INVALID_ARGS, MC_ERR_NOT_FOUND,
  MC_ERR_MEMORY, MC_ERR_SEND, MC_WARN_DUPLICATE, MC_SUCCESS = 0,
  ERR, MC_ERR_CONNECT, MC_ERR_PARSE, MC_ERR_EMPTY,
  MC_ERR_INVALID, MC_ERR_INVALID_ARGS, MC_ERR_NOT_FOUND, MC_ERR_MEMORY,
  MC_ERR_SEND, MC_WARN_DUPLICATE
}
enum  MC_ThreadIndex_e {
  MC_THREAD_DF = 0, MC_THREAD_AMS, MC_THREAD_ACC, MC_THREAD_CP,
  MC_THREAD_AGENT, MC_THREAD_ALL
}
 MobileC Module indices. More...
enum  MC_SteerCommand_e { MC_RUN = 0, MC_SUSPEND, MC_RESTART, MC_STOP }
 Available commands for MC_Steer. More...
enum  MC_Signal_e {
  MC_NO_SIGNAL = 0x00, MC_RECV_CONNECTION = 0x01, MC_RECV_MESSAGE = 0x02, MC_RECV_AGENT = 0x04,
  MC_RECV_RETURN = 0x08, MC_EXEC_AGENT = 0x0F, MC_ALL_SIGNALS = 0x10
}
 MobileC system signals. More...
enum  MC_AgentType_e { MC_NONE = -1, MC_REMOTE_AGENT = 0, MC_LOCAL_AGENT, MC_RETURN_AGENT }
enum  MC_AgentStatus_e {
  MC_WAIT_CH, MC_WAIT_MESSGSEND, MC_AGENT_ACTIVE, MC_AGENT_NEUTRAL,
  MC_AGENT_SUSPENDED, MC_WAIT_FINISHED
}
 An agent's current execution state. More...

Functions

int MC_AclDestroy (struct fipa_acl_message_s *message)
 Destroy a FIPA ACL message.
EXPORTMC struct
fipa_acl_message_s
MC_AclNew (void)
 Allocate a new ACL Message.
EXPORTMC int MC_AclPost (MCAgent_t agent, struct fipa_acl_message_s *message)
 Post ACL message to agent.
EXPORTMC struct
fipa_acl_message_s
MC_AclReply (struct fipa_acl_message_s *acl_message)
 Reply to an ACL message.
EXPORTMC struct
fipa_acl_message_s
MC_AclRetrieve (MCAgent_t agent)
 Retrieve an ACL message.
EXPORTMC int MC_AclSend (MCAgency_t attr, struct fipa_acl_message_s *acl)
 Send a composed ACL Message.
EXPORTMC struct
fipa_acl_message_s
MC_AclWaitRetrieve (MCAgent_t agent)
 Wait for and retrieve an ACL message.
EXPORTMC int MC_AddAgent (MCAgency_t attr, MCAgent_t agent)
 Add an agent to the agency 'attr'.
EXPORTMC int MC_BarrierDelete (MCAgency_t attr, int id)
 Find and delete an initialized MobileC Barrier.
EXPORTMC int MC_BarrierInit (MCAgency_t attr, int id, int num_procs)
 Initialize a MobileC Barrier.
EXPORTMC int MC_ChInitializeOptions (MCAgency_t attr, ChOptions_t *options)
 Use custom ChOptions_t type for internal Ch interpretor.
EXPORTMC int MC_CallAgentFunc (MCAgent_t agent, const char *funcName, void *returnVal, void *varg)
 Calls a function defined in an agent.
MCAgent_t MC_ComposeAgent (const char *name, const char *home, const char *owner, const char *code, const char *return_var_name, const char *server, int persistent)
EXPORTMC int MC_CondBroadcast (MCAgency_t attr, int id)
 Wakes up all agents/threads waiting on a condition variable.
EXPORTMC int MC_CondSignal (MCAgency_t attr, int id)
 Wakes up at least one thread waiting on a condition variable.
EXPORTMC int MC_CondReset (MCAgency_t attr, int id)
 Reset a previously signalled MobileC condition variable.
EXPORTMC int MC_CondWait (MCAgency_t attr, int id)
 Wait on a MobileC synchronization variable.
int MC_CopyAgent (MCAgent_t *agent_out, const MCAgent_t agent_in)
 Performs a deep-copy of an agent structure.
EXPORTMC int MC_DeleteAgent (MCAgent_t agent)
 Stop and remove an agent.
EXPORTMC int MC_End (MCAgency_t attr)
 End an agency.
int MC_DestroyServiceSearchResult (char **agentName, char **serviceName, int *agentID, int numResult)
 Free memory allocated by a Service Search operation.
EXPORTMC MCAgent_t MC_FindAgentByName (MCAgency_t attr, const char *name)
 Find an agent by its name.
EXPORTMC MCAgent_t MC_FindAgentByID (MCAgency_t attr, int ID)
 Find an agent by its id.
EXPORTMC void * MC_GetAgentExecEngine (MCAgent_t agent)
 Retrieve an agent's Ch interpreter.
EXPORTMC int MC_GetAgentID (MCAgent_t agent)
 Retrieve an agent's id.
EXPORTMC char * MC_GetAgentName (MCAgent_t agent)
EXPORTMC int MC_GetAgentNumTasks (MCAgent_t agent)
 Retrive the number of tasks an agent has.
EXPORTMC int MC_GetAgentReturnData (MCAgent_t agent, int task_num, void **data, int *dim, int **extent)
 Get an agent's return data.
EXPORTMC int MC_GetAgentStatus (MCAgent_t agent)
 Get an agent's current status.
EXPORTMC enum MC_AgentType_e MC_GetAgentType (MCAgent_t agent)
 Get an agent's type.
EXPORTMC char * MC_GetAgentXMLString (MCAgent_t agent)
 Get an agent's xml string.
EXPORTMC int MC_HaltAgency (MCAgency_t agency)
 Halt an agency: Do not process new entries in queues.
EXPORTMC MCAgency_t MC_Initialize (int port, MCAgencyOptions_t *options)
 Initialize and start a MobileC agency.
EXPORTMC int MC_InitializeAgencyOptions (struct MCAgencyOptions_s *options)
 Initialize MobileC options.
EXPORTMC int MC_MainLoop (MCAgency_t attr)
 Wait indefinitely.
EXPORTMC int MC_MutexLock (MCAgency_t attr, int id)
 Locks a MobileC synchronization variable as a mutex.
EXPORTMC int MC_MutexUnlock (MCAgency_t attr, int id)
EXPORTMC int MC_RegisterService (MCAgency_t agency, MCAgent_t agent, int agentID, const char *agentName, char **serviceNames, int numServices)
 Register a new service with the Directory Facilitator.
EXPORTMC int MC_ResumeAgency (MCAgency_t agency)
 Resumes a halted agency.
EXPORTMC MCAgent_t MC_RetrieveAgent (MCAgency_t attr)
 Retrieves the oldest agent from an agency.
EXPORTMC int MC_SemaphorePost (MCAgency_t attr, int id)
 Post to a MobileC synchronization variable semaphore.
EXPORTMC int MC_SemaphoreWait (MCAgency_t attr, int id)
 Decreases a MobileC synchronization variable semaphore count by one.
EXPORTMC int MC_SetDefaultAgentStatus (MCAgency_t agency, enum MC_AgentStatus_e status)
 Sets default incoming agent status.
EXPORTMC int MC_SetThreadOn (MCAgencyOptions_t *options, enum MC_ThreadIndex_e index)
 Sets a MobileC thread to "on" status.
EXPORTMC int MC_SetThreadsAllOn (MCAgencyOptions_t *options)
 Set all Mobile-C threads on.
EXPORTMC int MC_SetThreadOff (MCAgencyOptions_t *options, enum MC_ThreadIndex_e index)
 Sets a MobileC thread to "off" status.
EXPORTMC int MC_SetThreadsAllOff (MCAgencyOptions_t *options)
 Set all MobileC threads to 'off' status.
EXPORTMC int MC_PrintAgentCode (MCAgent_t agent)
 Prints an agents code to stdout.
EXPORTMC char * MC_RetrieveAgentCode (MCAgent_t agent)
 Retrieves an agent's Ch code.
EXPORTMC int MC_ResetSignal (MCAgency_t attr)
 Reset a MobileC signal.
EXPORTMC int MC_SearchForService (MCAgency_t attr, const char *searchString, char ***agentNames, char ***serviceNames, int **agentIDs, int *numResults)
 Search the directory facilitator for a service.
EXPORTMC int MC_SendAgentMigrationMessage (MCAgency_t attr, const char *message, const char *hostname, int port)
 Sends an agent migration message.
EXPORTMC int MC_SendAgentMigrationMessageFile (MCAgency_t attr, const char *filename, const char *hostname, int port)
 Sends an agent migration message.
EXPORTMC int MC_SetAgentStatus (MCAgent_t agent, int status)
 Set an agent's status.
EXPORTMC int MC_Steer (MCAgency_t attr, int(*funcptr)(void *data), void *arg)
 Set up a steerable algorithm.
EXPORTMC enum MC_SteerCommand_e MC_SteerControl (void)
 The MobileC user-algorithm steering function.
EXPORTMC int MC_SyncDelete (MCAgency_t attr, int id)
 Deletes a previously initialized synchronization variable.
EXPORTMC int MC_SyncInit (MCAgency_t attr, int id)
 Initializes a new MobileC synchronization variable.
EXPORTMC int MC_TerminateAgent (MCAgent_t agent)
 Halt a running agent.
EXPORTMC int MC_WaitAgent (MCAgency_t attr)
 Wait indefinitely.
EXPORTMC MCAgent_t MC_WaitRetrieveAgent (MCAgency_t attr)
 Wait and retrieve an agent.
EXPORTMC int MC_WaitSignal (MCAgency_t attr, int signals)
 Wait for a MobileC signal.


Detailed Description

MobileC api header file.

Definition in file libmc.h.


Define Documentation

#define EXPORTMC

Definition at line 130 of file libmc.h.

#define MC_Wait ( arg1   )     MC_MainLoop(arg1)

Definition at line 135 of file libmc.h.


Typedef Documentation

typedef agency_t* agency_p

Definition at line 221 of file libmc.h.

typedef struct agency_s agency_t

typedef agent_t* agent_p

Definition at line 241 of file libmc.h.

typedef struct agent_s agent_t

Definition at line 239 of file libmc.h.

typedef enum error_code_e error_code_t

typedef enum MC_SteerCommand_e MC_SteerCommand_t

typedef agency_p MCAgency_t

Definition at line 222 of file libmc.h.

typedef struct MCAgencyOptions_s MCAgencyOptions_t

typedef agent_t* MCAgent_t

Definition at line 240 of file libmc.h.


Enumeration Type Documentation

enum error_code_e

Enumerator:
MC_SUCCESS 
MC_ERR 
MC_ERR_CONNECT 
MC_ERR_PARSE 
MC_ERR_EMPTY 
MC_ERR_INVALID 
MC_ERR_INVALID_ARGS 
MC_ERR_NOT_FOUND 
MC_ERR_MEMORY 
MC_ERR_SEND 
MC_WARN_DUPLICATE 
MC_SUCCESS 
ERR 
MC_ERR_CONNECT 
MC_ERR_PARSE 
MC_ERR_EMPTY 
MC_ERR_INVALID 
MC_ERR_INVALID_ARGS 
MC_ERR_NOT_FOUND 
MC_ERR_MEMORY 
MC_ERR_SEND 
MC_WARN_DUPLICATE 

Definition at line 149 of file libmc.h.

enum MC_AgentStatus_e

An agent's current execution state.

Enumerator:
MC_WAIT_CH  Waiting to be started
MC_WAIT_MESSGSEND  Finished, waiting to migrate
MC_AGENT_ACTIVE  Running
MC_AGENT_NEUTRAL  Not running, but do not flush
MC_AGENT_SUSPENDED  Unused
MC_WAIT_FINISHED  Finished, waiting to be flushed

Definition at line 249 of file libmc.h.

enum MC_AgentType_e

Enumerator:
MC_NONE 
MC_REMOTE_AGENT 
MC_LOCAL_AGENT 
MC_RETURN_AGENT 

Definition at line 244 of file libmc.h.

enum MC_Signal_e

MobileC system signals.

Note:
Each signal is activated after the corresponding action. i.e. The 'MC_RECV_MESSAGE' signal is activated after a message is received.
See also:
MC_WaitSignal(), MC_ResetSignal
Enumerator:
MC_NO_SIGNAL 
MC_RECV_CONNECTION 
MC_RECV_MESSAGE 
MC_RECV_AGENT 
MC_RECV_RETURN 
MC_EXEC_AGENT 
MC_ALL_SIGNALS 

Definition at line 194 of file libmc.h.

enum MC_SteerCommand_e

Available commands for MC_Steer.

Enumerator:
MC_RUN  Continue the algorithm
MC_SUSPEND  Suspend/pause the algorithm
MC_RESTART  Restart the algorithm from the beginning
MC_STOP  Stop the algorithm

Definition at line 179 of file libmc.h.

enum MC_ThreadIndex_e

MobileC Module indices.

Enumerator:
MC_THREAD_DF  Directory Facilitator
MC_THREAD_AMS  Agent Managment system
MC_THREAD_ACC  Agency communications
MC_THREAD_CP  Command Prompt
MC_THREAD_AGENT  Agent threads
MC_THREAD_ALL 

Definition at line 167 of file libmc.h.


Function Documentation

int MC_AclDestroy ( struct fipa_acl_message_s message  ) 

Destroy a FIPA ACL message.

Parameters:
message The ACL message to destroy
Returns:
0 on success, error code on failure.

Definition at line 69 of file libmc.c.

References fipa_acl_message_Destroy().

Referenced by MC_AclDestroy_chdl().

EXPORTMC struct fipa_acl_message_s* MC_AclNew ( void   )  [read]

Allocate a new ACL Message.

Returns:
A newly allocated and empty ACL message.

Definition at line 75 of file libmc.c.

References fipa_acl_message_New().

Referenced by MC_AclNew_chdl().

EXPORTMC int MC_AclPost ( MCAgent_t  agent,
struct fipa_acl_message_s message 
)

Post ACL message to agent.

Parameters:
agent The agent to post the message to
message The message to post
Returns:
0 if successful, or error_code_t type.

Definition at line 80 of file libmc.c.

References agent_mailbox_Post(), and agent_s::mailbox.

Referenced by MC_AclPost_chdl(), and MC_AclSend().

EXPORTMC struct fipa_acl_message_s* MC_AclReply ( struct fipa_acl_message_s acl_message  )  [read]

Reply to an ACL message.

Parameters:
acl_message The incoming acl message to reply to
Returns:
A newly allocated ACL message
Note:
This function simply generates a new ACL message with the 'receiver' field automatically set to the 'sender' field of the incoming message.

Definition at line 86 of file libmc.c.

References fipa_Reply().

Referenced by MC_AclReply_chdl().

EXPORTMC struct fipa_acl_message_s* MC_AclRetrieve ( MCAgent_t  agent  )  [read]

Retrieve an ACL message.

Parameters:
agent Agent to retrieve message from.
Returns:
an ACL message struct on success or NULL on failure

Definition at line 92 of file libmc.c.

References agent_mailbox_Retrieve(), and agent_s::mailbox.

Referenced by MC_AclRetrieve_chdl().

EXPORTMC int MC_AclSend ( MCAgency_t  attr,
struct fipa_acl_message_s acl 
)

Send a composed ACL Message.

Parameters:
attr An initialized and running MobileC agency
acl An allocated and fully composed ACL message.
Returns:
0 if successful, error code on failure.

Definition at line 98 of file libmc.c.

References fipa_agent_identifier_s::addresses, mtp_http_s::content, mtp_http_content_s::content_type, mtp_http_content_s::data, dynstring_Destroy(), FIPA_ACL, fipa_acl_Compose(), fipa_agent_identifier_set_s::fipa_agent_identifiers, fipa_envelope_Compose(), mtp_http_s::host, http_to_hostport(), MC_AclPost(), MC_FindAgentByName(), dynstring_s::message, message_Destroy(), mtp_http_s::message_parts, message_Send(), message_s::message_type, mtp_http_CreateMessage(), mtp_http_Destroy(), mtp_http_New(), fipa_agent_identifier_s::name, fipa_url_sequence_s::num, fipa_agent_identifier_set_s::num, port, fipa_acl_message_s::receiver, fipa_url_s::str, mtp_http_s::target, message_s::target, and fipa_url_sequence_s::urls.

Referenced by MC_AclSend_chdl().

EXPORTMC struct fipa_acl_message_s* MC_AclWaitRetrieve ( MCAgent_t  agent  )  [read]

Wait for and retrieve an ACL message.

Parameters:
agent Agent to retrieve message from.
Returns:
an ACL message struct on success or NULL on failure

Definition at line 188 of file libmc.c.

References agent_mailbox_WaitRetrieve(), and agent_s::mailbox.

Referenced by MC_AclWaitRetrieve_chdl().

EXPORTMC int MC_AddAgent ( MCAgency_t  attr,
MCAgent_t  agent 
)

Add an agent to the agency 'attr'.

Parameters:
attr a MobileC agency
agent An initialized MobileC agent
Returns:
0 if successful, or error_code_t type

Definition at line 314 of file libmc.c.

References mc_platform_s::agent_queue, mc_platform_s::ams, COND_SIGNAL, agency_s::mc_platform, agent_s::mc_platform, MUTEX_LOCK, and MUTEX_UNLOCK.

Referenced by MC_AddAgent_chdl().

EXPORTMC int MC_BarrierDelete ( MCAgency_t  attr,
int  id 
)

Find and delete an initialized MobileC Barrier.

Parameters:
attr A running MobileC agency
id The id of the barrier node to delete
Returns:
returns 0 on success, error if the node is not found or other failure.

Definition at line 368 of file libmc.c.

References mc_platform_s::barrier_queue, barrier_queue_Delete(), and agency_s::mc_platform.

Referenced by MC_BarrierDelete_chdl().

EXPORTMC int MC_BarrierInit ( MCAgency_t  attr,
int  id,
int  num_procs 
)

Initialize a MobileC Barrier.

Parameters:
attr A running MobileC agency
id The requested barrier id
num_procs The number of agents/threads/processes that will wait on the barrier
Returns:
The allocated barrier id. May differ from the requested id if it is alread in use.

Examples

The following example demonstrates an agent which sets up an MC_Barrier.
<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
  <MESSAGE message="MOBILE_AGENT">
    <MOBILE_AGENT>
     <AGENT_DATA>
       <NAME>CommAgent1</NAME>
       <OWNER>IEL</OWNER>
       <HOME>localhost:5051</HOME>
       <TASK task="1" num="0">
          <DATA number_of_elements="0" name="no-return" complete="0" server="localhost:5050">
       </DATA>
          <AGENT_CODE>
          <![CDATA[
#include <stdio.h>
#define MC_BARRIER_ID 56
#define NUM_PROCS 2
int main()
{
    printf("Comm agent: Setting up the MC_Barriers now...\n");
    mc_BarrierInit(MC_BARRIER_ID, NUM_PROCS);
    mc_BarrierInit(MC_BARRIER_ID + 1, NUM_PROCS);
    mc_BarrierInit(MC_BARRIER_ID + 2, NUM_PROCS);
    printf("MC_Barrier Initialized.\n");
    return 0;
}
        ]]>
        </AGENT_CODE>
      </TASK>
      </AGENT_DATA>
    </MOBILE_AGENT>
  </MESSAGE>
</GAF_MESSAGE>

Definition at line 354 of file libmc.c.

References barrier_node_Initialize(), mc_platform_s::barrier_queue, barrier_queue_Add(), barrier_queue_Get(), MC_ERR, agency_s::mc_platform, MC_SUCCESS, and node.

Referenced by MC_BarrierInit_chdl().

EXPORTMC int MC_CallAgentFunc ( MCAgent_t  agent,
const char *  funcName,
void *  returnVal,
void *  varg 
)

Calls a function defined in an agent.

Parameters:
agent An initialized and executed MobileC agent
funcName The name of the function to call
returnVal The agent function's return value
varg The agent functions argument
Note:
The agent function must be of the form 'void* func(void* arg);'
Returns:
0 if successful, error_code_t type on failure

Example

#include <libmc.h>
#include <embedch.h>
#include <stdio.h>

struct arg_struct{
    int a;
    int b;
}arg;

int main(int argc, char *argv[])
{
    MCAgent_t agent;
    ChInterp_t interp;
    int retval;

    /* Init the agency */
    MCAgency_t agency;
    agency = MC_Initialize(
            5051,
            NULL);

    printf("Please press 'enter' once the sample agent has arrived.\n");
    getchar();
    agent = MC_FindAgentByName(agency, "mobagent1");
    if (agent == NULL) {
        printf("Could not find agent!\n");
        exit(0);
    }
    /* The following executution of code may be performed two different
       ways: The first way, which is commented out in this example, 
       involves retrieving the agent's interpreter with
       MC_GetAgentExecEngine() and using the Embedded Ch api to call
       the function. The second method involves using the Mobile-C
       api to call the function. Both of these methods used here produce
       identical results. */
    arg.a = 50;
    arg.b = 51;
    /*interp = MC_GetAgentExecEngine(agent);
    Ch_CallFuncByName(interp, "hello", &retval, arg); */
    MC_CallAgentFunc(
            agent,
            "hello",
            &retval,
            &arg);
    printf("Value of %d was returned.\n", retval);
    return 0;
}

Definition at line 374 of file libmc.c.

References agent_s::agent_interp, MUTEX_LOCK, MUTEX_UNLOCK, and agent_s::run_lock.

Referenced by MC_CallAgentFunc_chdl().

EXPORTMC int MC_ChInitializeOptions ( MCAgency_t  attr,
ChOptions_t *  options 
)

Use custom ChOptions_t type for internal Ch interpretor.

Parameters:
attr A running MobileC agency
options Initialized Ch options structure
Returns:
0 on success, error_code_t type on failure

Definition at line 392 of file libmc.c.

References mc_platform_s::interp_options, and agency_s::mc_platform.

MCAgent_t MC_ComposeAgent ( const char *  name,
const char *  home,
const char *  owner,
const char *  code,
const char *  return_var_name,
const char *  server,
int  persistent 
)

Definition at line 410 of file libmc.c.

References agent_datastate_s::agent_code, agent_datastate_s::agent_code_ids, agent_datastate_s::agent_codes, agent_datastate_New(), agent_New(), agent_s::agent_status, agent_task_New(), agent_s::agent_type, agent_s::datastate, agent_s::home, MC_LOCAL_AGENT, MC_WAIT_MESSGSEND, agent_s::name, agent_datastate_s::number_of_tasks, agent_s::orphan, agent_s::owner, agent_datastate_s::persistent, agent_task_s::server_name, agent_datastate_s::tasks, and agent_task_s::var_name.

Referenced by MC_ComposeAgent_chdl().

EXPORTMC int MC_CondBroadcast ( MCAgency_t  attr,
int  id 
)

Wakes up all agents/threads waiting on a condition variable.

Parameters:
attr A MobileC agency
id Synchronization variable id to broadcast to
See also:
MC_SyncInit(), MC_CondSignal()
Returns:
0 on success, error_code_t type on failure

Definition at line 480 of file libmc.c.

References syncListNode_s::cond, COND_BROADCAST, syncListNode_s::lock, MC_ERR_NOT_FOUND, agency_s::mc_platform, MUTEX_LOCK, MUTEX_UNLOCK, syncListNode_s::signalled, mc_platform_s::syncList, and syncListFind().

Referenced by MC_CondBroadcast_chdl().

EXPORTMC int MC_CondReset ( MCAgency_t  attr,
int  id 
)

Reset a previously signalled MobileC condition variable.

Parameters:
attr A MobileC Agency
id The synchronization variable id to reset
See also:
MC_SyncInit()
Returns:
0 on success, error_code_t type on failure

Definition at line 532 of file libmc.c.

References syncListNode_s::lock, MC_ERR_NOT_FOUND, agency_s::mc_platform, MUTEX_LOCK, MUTEX_UNLOCK, syncListNode_s::signalled, mc_platform_s::syncList, and syncListFind().

Referenced by MC_CondReset_chdl().

EXPORTMC int MC_CondSignal ( MCAgency_t  attr,
int  id 
)

Wakes up at least one thread waiting on a condition variable.

Parameters:
attr A MobileC agency
id synchronization variable id
See also:
MC_SyncInit()
Returns:
0 on success, error_code_t type on failure

Example

The following example demonstrates the agent-space version of the function, which is nearly identical to the binary space api function.
<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
 <MESSAGE message="MOBILE_AGENT">
  <MOBILE_AGENT>
   <AGENT_DATA>
    <NAME>wake_agent</NAME>
    <OWNER>IEL</OWNER>
    <HOME>localhost:5050</HOME>
    <TASK task="1" num="0">
     <DATA dim="0" name="no-return" complete="0" server="localhost:5051">
     </DATA>
     <AGENT_CODE>
      <![CDATA[
#include <stdio.h>

#define SYNC_ID 55
int main()
{
    int cond_id;
    cond_id = SYNC_ID;
    printf("This is the wake agent.\n");
    mc_CondSignal(cond_id);

    return 0;
}
      ]]>
     </AGENT_CODE>
    </TASK>
   </AGENT_DATA>
  </MOBILE_AGENT>
 </MESSAGE>
</GAF_MESSAGE>

Definition at line 495 of file libmc.c.

References syncListNode_s::cond, COND_SIGNAL, syncListNode_s::lock, MC_ERR_NOT_FOUND, agency_s::mc_platform, MUTEX_LOCK, MUTEX_UNLOCK, syncListNode_s::signalled, mc_platform_s::syncList, and syncListFind().

Referenced by MC_CondSignal_chdl().

EXPORTMC int MC_CondWait ( MCAgency_t  attr,
int  id 
)

Wait on a MobileC synchronization variable.

Parameters:
attr A MobileC agency
id a synchronization variable id
See also:
MC_SyncInit()
Returns:
0 on success, error_code_t type on failure

Example

The following example demonstrates the agent-space version of this function.
<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
 <MESSAGE message="MOBILE_AGENT">
  <MOBILE_AGENT>
   <AGENT_DATA>
    <NAME>sleep_agent</NAME>
    <OWNER>IEL</OWNER>
    <HOME>localhost:5050</HOME>
    <TASK task="1" num="0">
     <DATA dim="0" name="no-return" complete="0" server="localhost:5051">
     </DATA>
     <AGENT_CODE>
      <![CDATA[
#include <stdio.h>

#define SYNC_ID 55
int main()
{
    int cond_id;
    printf("Sleep agent has arrived.\n");
    cond_id = mc_SyncInit(SYNC_ID);
    if (cond_id != SYNC_ID) {
        printf("Possible error. Aborting...\n");
        exit(1);
    }
    printf("This is the sleep agent.\n");
    printf("I am going to sleep now...\n");
    mc_CondWait(cond_id);
    printf("This is the sleep agent: I am awake now. Continuing...\n");
    mc_SyncDelete(cond_id);

    return 0;
}
      ]]>
     </AGENT_CODE>
    </TASK>
   </AGENT_DATA>
  </MOBILE_AGENT>
 </MESSAGE>
</GAF_MESSAGE>

Definition at line 510 of file libmc.c.

References syncListNode_s::cond, COND_WAIT, syncListNode_s::lock, MC_ERR_NOT_FOUND, agency_s::mc_platform, MUTEX_LOCK, MUTEX_UNLOCK, syncListNode_s::signalled, mc_platform_s::syncList, and syncListFind().

Referenced by MC_CondWait_chdl().

int MC_CopyAgent ( MCAgent_t agent_out,
const MCAgent_t  agent_in 
)

Performs a deep-copy of an agent structure.

Parameters:
agent_out A pointer to the agent to copy to.
agent_in The agent to copy
Returns:
0 on success, error_code_t type on failure.

Definition at line 550 of file libmc.c.

References agent_Copy(), and MC_SUCCESS.

EXPORTMC int MC_DeleteAgent ( MCAgent_t  agent  ) 

Stop and remove an agent.

Parameters:
agent An agent in any state (running, waiting, etc)
Returns:
0 on success, error_code_t type on failure

Definition at line 557 of file libmc.c.

References CHECK_NULL, MC_ERR_INVALID, MC_SetAgentStatus(), MC_SUCCESS, MC_TerminateAgent(), and MC_WAIT_FINISHED.

Referenced by MC_DeleteAgent_chdl().

int MC_DestroyServiceSearchResult ( char **  agentName,
char **  serviceName,
int agentID,
int  numResult 
)

Free memory allocated by a Service Search operation.

Parameters:
agentName agent names returned by a search operation.
serviceName service names return by a search operation.
agentID list of agent id's returned by a search operation.
numResult The number of hits returned by a search operation.
Returns:
0 on success, error code on failure.

Definition at line 571 of file libmc.c.

Referenced by MC_DestroyServiceSearchResult_chdl().

EXPORTMC int MC_End ( MCAgency_t  attr  ) 

End an agency.

Parameters:
attr A running agency
Returns:
0 on success, error_code_t type on failure

Example

#include <stdio.h>
#include <stdlib.h>
#include <libmc.h>

int main(int argc, char *argv[]) 
{
  MCAgency_t agency;
  MCAgencyOptions_t options;

  MC_InitializeAgencyOptions(&options);
  MC_SetThreadOff(&options, MC_THREAD_CP);
  agency = MC_Initialize(5050, &options);

  printf("Mobile-C Started\n");
  /* Note: The third argument of the following function may also be a
     valid IP address in the form of a string. i.e. 192.168.0.1 */
  MC_SendAgentMigrationMessageFile(agency,
      "test1.xml",
      "localhost",
      5051);
  MC_End(agency);
  exit(0);
}

Definition at line 620 of file libmc.c.

References mc_platform_s::acc, mc_platform_s::ams, mc_platform_s::cmd_prompt, COND_SIGNAL, mc_platform_s::connection_queue, mc_platform_s::df, GET_THREAD_MODE, agency_s::hostName, agency_s::mc_platform, mc_platform_Destroy(), MC_THREAD_ACC, MC_THREAD_AMS, MC_THREAD_CP, MC_THREAD_DF, mc_platform_s::message_queue, MUTEX_LOCK, MUTEX_UNLOCK, mc_platform_s::quit, mc_platform_s::quit_lock, cmd_prompt_s::thread, THREAD_CANCEL, THREAD_JOIN, and agency_s::threads.

Referenced by MC_End_chdl().

EXPORTMC MCAgent_t MC_FindAgentByID ( MCAgency_t  attr,
int  ID 
)

Find an agent by its id.

Parameters:
attr the agency to search
ID the id to search for
Returns:
a valid agent on success, NULL on failure

Definition at line 674 of file libmc.c.

References mc_platform_s::agent_queue, and agency_s::mc_platform.

Referenced by MC_FindAgentByID_chdl().

EXPORTMC MCAgent_t MC_FindAgentByName ( MCAgency_t  attr,
const char *  name 
)

Find an agent by its name.

Parameters:
attr a running agency
name name to search for
Returns:
a valid agent on success or NULL on failure

Example

#include <stdio.h>
#include <libmc.h>

int main(int argc, char *argv[]) 
{
  MCAgency_t agency;
  MCAgencyOptions_t options;
  MCAgent_t agent;
  int dim, *extent;
  double *data;
  int i, j, size;

  MC_InitializeAgencyOptions(&options);
  MC_SetThreadOff(&options, MC_THREAD_CP);
  agency = MC_Initialize(5050, &options);

  printf("MobileC Started\n");
  /* Note: The third argument of the following function may also be a
     valid IP address in the form of a string. i.e. 192.168.0.1 */
  MC_SendAgentMigrationMessageFile(agency,
      "test.xml",
      "localhost",
      5051);
  MC_WaitSignal(agency, MC_RECV_RETURN);
  agent = MC_FindAgentByName(agency, "mobagent3");
  if (agent == NULL) {
    fprintf(stderr, "Did not receive correct agent. \n");
    exit(1);
  }
  printf("%d tasks.\n", MC_GetAgentNumTasks(agent) );
  for (i = 0; i < MC_GetAgentNumTasks(agent); i++) {
    MC_GetAgentReturnData(
        agent,
        i,
        (void**)&data,
        &dim,
        &extent );
    printf("Task: %d\n", i);
    size = 1;
    printf("dim is %d\n", dim);
    for (j = 0; j < dim; j++) {
      size *= extent[j];
    }
    printf("Size: %d\n", size);
    printf("Data elements: ");
    for (j = 0; j < size; j++) {
      printf("%f ", data[j]);
    }
    printf("\n\n");
    free(data);
    free(extent);
  }
  MC_ResetSignal(agency);
  MC_End(agency);
  return 0;
}

Definition at line 661 of file libmc.c.

References mc_platform_s::agent_queue, and agency_s::mc_platform.

Referenced by MC_AclSend(), and MC_FindAgentByName_chdl().

EXPORTMC void* MC_GetAgentExecEngine ( MCAgent_t  agent  ) 

Retrieve an agent's Ch interpreter.

Parameters:
agent a valid agent
Returns:
a Ch interpreter of type 'ChInterp_t' on success, or NULL on failure.

Definition at line 726 of file libmc.c.

References agent_s::agent_interp.

EXPORTMC int MC_GetAgentID ( MCAgent_t  agent  ) 

Retrieve an agent's id.

Definition at line 732 of file libmc.c.

References agent_s::id.

EXPORTMC char* MC_GetAgentName ( MCAgent_t  agent  ) 

Definition at line 740 of file libmc.c.

References agent_s::lock, MUTEX_LOCK, MUTEX_UNLOCK, and agent_s::name.

EXPORTMC int MC_GetAgentNumTasks ( MCAgent_t  agent  ) 

Retrive the number of tasks an agent has.

Example

#include <stdio.h>
#include <libmc.h>

int main(int argc, char *argv[]) 
{
  MCAgency_t agency;
  MCAgencyOptions_t options;
  MCAgent_t agent;
  int dim, *extent;
  double *data;
  int i, j, size;

  MC_InitializeAgencyOptions(&options);
  MC_SetThreadOff(&options, MC_THREAD_CP);
  agency = MC_Initialize(5050, &options);

  printf("MobileC Started\n");
  /* Note: The third argument of the following function may also be a
     valid IP address in the form of a string. i.e. 192.168.0.1 */
  MC_SendAgentMigrationMessageFile(agency,
      "test.xml",
      "localhost",
      5051);
  MC_WaitSignal(agency, MC_RECV_RETURN);
  agent = MC_FindAgentByName(agency, "mobagent3");
  if (agent == NULL) {
    fprintf(stderr, "Did not receive correct agent. \n");
    exit(1);
  }
  printf("%d tasks.\n", MC_GetAgentNumTasks(agent) );
  for (i = 0; i < MC_GetAgentNumTasks(agent); i++) {
    MC_GetAgentReturnData(
        agent,
        i,
        (void**)&data,
        &dim,
        &extent );
    printf("Task: %d\n", i);
    size = 1;
    printf("dim is %d\n", dim);
    for (j = 0; j < dim; j++) {
      size *= extent[j];
    }
    printf("Size: %d\n", size);
    printf("Data elements: ");
    for (j = 0; j < size; j++) {
      printf("%f ", data[j]);
    }
    printf("\n\n");
    free(data);
    free(extent);
  }
  MC_ResetSignal(agency);
  MC_End(agency);
  return 0;
}

Definition at line 821 of file libmc.c.

References agent_s::datastate, and agent_datastate_s::number_of_tasks.

EXPORTMC int MC_GetAgentReturnData ( MCAgent_t  agent,
int  task_num,
void **  data,
int dim,
int **  extent 
)

Get an agent's return data.

Parameters:
agent a valid agent
task_num the task for which to retrieve the return data. The task must already be completed.
data the return data. May be multi dimensional array.
dim the number of dimensions of the return array.
extent the extent of each one of the array dimensions.

Example

This file demonstrates the retrieval of agent return data from an agent
/* mc_sample_app.c
 *
 * This sample program uses the Mobile C library to build
 * a simple command-line driven client/server app. 
 * 
 * 12/15/2006
 * */

#include <libmc.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#endif
int main(int argc, char *argv[])
{
    MCAgency_t agency;
    MCAgent_t agent;
    int *extent;
    int dim;
    short *array;
    int i, size;
    char *xml;
    MCAgencyOptions_t options;

    MC_InitializeAgencyOptions(&options);
    MC_SetThreadOff(&options, MC_THREAD_CP);
    if (argc == 2) {
        printf("Starting agency listening on port %d.\n",
                atoi(argv[1]) );
        agency = MC_Initialize(
                atoi(argv[1]),
                &options
                );
    } else {
        agency = MC_Initialize(
                5051,
                &options);
    }
 /* Note: The third argument of the following function may also be a
    valid IP address in the form of a string. i.e. 192.168.0.1 */
    MC_SendAgentMigrationMessageFile(
            agency,
            "agent.xml",
            "localhost",
            5050 ); 
    while(1) {
        printf("Waiting for agent return...\n");
        MC_WaitSignal(
                agency,
                MC_RECV_RETURN
                );
        agent = MC_FindAgentByName(agency, "mobagent1");
        if (agent == NULL) {
            printf("Found wrong agent. Waiting again...\n");
            MC_ResetSignal(agency);
            continue;
        }
        if (MC_GetAgentType(agent) != MC_RETURN_AGENT) {
            printf("Found wrong agent. Waiting again...\n");
            MC_ResetSignal(agency);
            continue;
        } else {
            break;
        }
    }
    xml = MC_GetAgentXMLString(agent);
    printf("%s\n", xml);
    free(xml);
    MC_GetAgentReturnData(agent, 0, (void**)&array, &dim, &extent);
    MC_ResetSignal(agency);
    printf("dim is %d\n", dim);
    printf("Extents are: ");
    size = 1;
    for (i = 0; i < dim; i++) {
        printf("%d ", extent[i]);
        size *= extent[i];
    }
    printf("\n");
    printf("%d Elements: \n", size);
    for (i = 0; i < size; i++) {
        printf("%d ", array[i]);
    }
    printf("\n");

    MC_End(agency);

    return 0;
}

This is the agent which gets the data
<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
  <MESSAGE message="MOBILE_AGENT">
    <MOBILE_AGENT>
     <AGENT_DATA>
       <NAME>mobagent1</NAME>
       <OWNER>IEL</OWNER>
       <HOME>localhost:5051</HOME>
       <TASK task="1" num="0">
          <DATA type="int" dim="0" name="a" complete="0" server="localhost:5050">
       </DATA>
          <AGENT_CODE>
          <![CDATA[
#include <stdio.h>
short a[2][3][2];
int main()
{
    int i, j, k, l;
    k = 0;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            for(l = 0; l < 2; l++) {
                a[i][j][l] = k;
                k++;
                printf("%d ", i+j);
            }
        }
    }
    printf("\nThis is a mobile agent from port 5050.\n");
    printf("I am performing the task on the agency at port 5051 now.\n");
    sleep(1);
    
    return 0;
}
        ]]>
        </AGENT_CODE>
      </TASK>
      </AGENT_DATA>
    </MOBILE_AGENT>
  </MESSAGE>
</GAF_MESSAGE>

Definition at line 758 of file libmc.c.

References agent_task_s::agent_return_data, agent_return_data_s::array_dim, agent_return_data_s::array_extent, CH_DATATYPE_SIZE, agent_return_data_s::data_type, agent_s::datastate, agent_datastate_s::number_of_tasks, size, and agent_datastate_s::tasks.

EXPORTMC int MC_GetAgentStatus ( MCAgent_t  agent  ) 

Get an agent's current status.

Returns:
returns type 'enum MC_AgentStatus_e'

Definition at line 704 of file libmc.c.

References agent_s::agent_status, agent_s::lock, MUTEX_LOCK, and MUTEX_UNLOCK.

EXPORTMC enum MC_AgentType_e MC_GetAgentType ( MCAgent_t  agent  ) 

Get an agent's type.

Returns:
returns type 'enum MC_AgentType_e'

Definition at line 827 of file libmc.c.

References agent_s::agent_type.

EXPORTMC char* MC_GetAgentXMLString ( MCAgent_t  agent  ) 

Get an agent's xml string.

Returns:
a malloc'd character string containing the agent's xml code

Definition at line 714 of file libmc.c.

References agent_s::datastate, mxmlSaveAllocString(), and agent_datastate_s::xml_agent_root.

EXPORTMC int MC_HaltAgency ( MCAgency_t  agency  ) 

Halt an agency: Do not process new entries in queues.

Parameters:
agency A handle to a running MobileC agency.
Returns:
0 on success, non-zero on failure.

Definition at line 873 of file libmc.c.

References mc_platform_s::giant, mc_platform_s::giant_lock, agency_s::mc_platform, MUTEX_LOCK, and MUTEX_UNLOCK.

Referenced by MC_GetAllAgents(), and MC_HaltAgency_chdl().

EXPORTMC MCAgency_t MC_Initialize ( int  port,
MCAgencyOptions_t options 
)

Initialize and start a MobileC agency.

Parameters:
port the TCP port the agency should bind to
options initialized MobileC options or NULL for default options
Returns:
a handle to a running MobileC agency or NULL on failure

Example

#include <stdio.h>
#include <libmc.h>

#ifdef _WIN32
#include <windows.h>
#endif

int main(int argc, char *argv[]) 
{
  MCAgency_t agency;
  int local_port = 5051;
  
  agency = MC_Initialize(local_port, NULL);

  printf("Mobile-C Started\n");

  MC_MainLoop(agency);

  MC_End(agency);
  return 0;
}

Definition at line 882 of file libmc.c.

References CHECK_NULL, agency_s::client, MCAgencyOptions_s::default_agent_status, agency_s::default_agentstatus, MCAgencyOptions_s::enable_security, agency_s::enable_security, HOST_NAME_MAX, agency_s::hostName, MC_InitializeAgencyOptions(), agency_s::mc_platform, mc_platform_Initialize(), MC_THREAD_ALL, agency_s::portno, agency_s::server, MCAgencyOptions_s::stack_size, agency_s::stack_size, MCAgencyOptions_s::threads, and agency_s::threads.

EXPORTMC int MC_InitializeAgencyOptions ( struct MCAgencyOptions_s options  ) 

Initialize MobileC options.

Parameters:
options options to initialize.
Returns:
0 on success, error_code_t on failure
Note:
MobileC options should be initialized with this function before any of its members are modified.

Example

#include <stdio.h>
#include <libmc.h>

#ifdef _WIN32
#include <windows.h>
#endif

int main(int argc, char *argv[]) 
{
  MCAgency_t agency;
  int local_port = 5051;
  
  agency = MC_Initialize(local_port, NULL);

  printf("Mobile-C Started\n");

  MC_MainLoop(agency);

  MC_End(agency);
  return 0;
}

Definition at line 937 of file libmc.c.

References MCAgencyOptions_s::default_agent_status, MCAgencyOptions_s::enable_security, MC_THREAD_ALL, MC_WAIT_CH, MCAgencyOptions_s::modified, MCAgencyOptions_s::stack_size, and MCAgencyOptions_s::threads.

Referenced by MC_Initialize().

EXPORTMC int MC_MainLoop ( MCAgency_t  attr  ) 

Wait indefinitely.

Note:
This function is intended to block the calling thread forever.

Definition at line 1509 of file libmc.c.

References mc_platform_s::ams, and agency_s::mc_platform.

EXPORTMC int MC_MutexLock ( MCAgency_t  attr,
int  id 
)

Locks a MobileC synchronization variable as a mutex.

Parameters:
attr a MobileC agency handle
id the synchronization variable id to lock
Returns:
0 on success, error_code_t type on failure

Example

Consider the following agents, which use the agent-space version of this api function. Note that the 'sleep' agent is sent first, followed by the 'wake' agent.
<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
 <MESSAGE message="MOBILE_AGENT">
  <MOBILE_AGENT>
   <AGENT_DATA>
    <NAME>sleep_agent</NAME>
    <OWNER>IEL</OWNER>
    <HOME>localhost:5050</HOME>
    <TASK task="1" num="0">
     <DATA dim="0" name="no-return" complete="0" server="localhost:5051">
     </DATA>
     <AGENT_CODE>
      <![CDATA[
#include <stdio.h>
int main()
{
    int mutex_id;
    printf("Sleep agent has arrived.\n");
    mutex_id = mc_SyncInit(55);
    if (mutex_id != 55) {
        printf("Possible error. Aborting...\n");
        exit(1);
    }
    printf("This is agent 1.\n");
    printf("Agent 1: I am locking the mutex now.\n");
    mc_MutexLock(mutex_id);
    printf("Agent 1: Mutex locked. Perform protected operations here\n");
    printf("Agent 1: Waiting for 5 seconds...\n");
    sleep(5);
    printf("Agent 1: Unlocking mutex now...\n");
    mc_MutexUnlock(mutex_id);

    return 0;
}
      ]]>
     </AGENT_CODE>
    </TASK>
   </AGENT_DATA>
  </MOBILE_AGENT>
 </MESSAGE>
</GAF_MESSAGE>
<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
 <MESSAGE message="MOBILE_AGENT">
  <MOBILE_AGENT>
   <AGENT_DATA>
    <NAME>wake_agent</NAME>
    <OWNER>IEL</OWNER>
    <HOME>localhost:5050</HOME>
    <TASK task="1" num="0">
     <DATA dim="0" name="no-return" complete="0" server="localhost:5051">
     </DATA>
     <AGENT_CODE>
      <![CDATA[
#include <stdio.h>
int main()
{
    int mutex_id;
    mutex_id = 55;
    printf("Agent 2: Has arrived");
    printf("Agent 2: Attempting to lock the mutex...\n");
    mc_MutexLock(mutex_id);
    printf("Agent 2: Mutex locked.\n");
    printf("Agent 2: Perform protected operations here.\n");
    sleep(5);
    mc_MutexUnlock(mutex_id);
    printf("Agent 2: Mutex Unlocked\n");
    mc_SyncDelete(mutex_id);

    return 0;
}
      ]]>
     </AGENT_CODE>
    </TASK>
   </AGENT_DATA>
  </MOBILE_AGENT>
 </MESSAGE>
</GAF_MESSAGE>

Definition at line 954 of file libmc.c.

References syncListNode_s::lock, agency_s::mc_platform, MUTEX_LOCK, mc_platform_s::syncList, and syncListFind().

EXPORTMC int MC_MutexUnlock ( MCAgency_t  attr,
int  id 
)

Definition at line 966 of file libmc.c.

References syncListNode_s::lock, agency_s::mc_platform, MUTEX_UNLOCK, mc_platform_s::syncList, and syncListFind().

EXPORTMC int MC_PrintAgentCode ( MCAgent_t  agent  ) 

Prints an agents code to stdout.

Returns:
0 on success, error_code_t on failure

Definition at line 978 of file libmc.c.

References agent_datastate_s::agent_code, agent_s::datastate, agent_s::lock, MUTEX_LOCK, MUTEX_UNLOCK, agent_datastate_s::number_of_tasks, and agent_datastate_s::task_progress.

EXPORTMC int MC_RegisterService ( MCAgency_t  agency,
MCAgent_t  agent,
int  agentID,
const char *  agentName,
char **  serviceNames,
int  numServices 
)

Register a new service with the Directory Facilitator.

Parameters:
agency a MobileC agency handle
agent (OPTIONAL: See note) a MobileC agent
agentID (OPTIONAL: See note) a MobileC agent id
agentName (OPTIONAL: See note) a MobileC agent name
serviceNames an array of character strings of service names
numServices the number of services described in 'serviceNames'
Returns:
0 on success, error_code_t type on failure
Note:
Three of the input arguments are optional. The function expects as input the arguments 'agent XOR (agentID AND agentName)'.

Example

<?xml version="1.0"?>
<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">
<GAF_MESSAGE>
 <MESSAGE message="MOBILE_AGENT">
  <MOBILE_AGENT>
   <AGENT_DATA>
    <NAME>service_provider_1</NAME> 
    <OWNER>IEL</OWNER> 
    <HOME>localhost:5050</HOME>
    <TASK task="1" num="0">
     <DATA persistent="1" number_of_elements="0" 
           name="no-return" complete="0" server="localhost:5051">
     </DATA>
     <AGENT_CODE>
      <![CDATA[
#include <stdio.h>

struct arg_struct {
    int a;
    int b;
};

int main() {
    char **services;
    int i;
    services = malloc(sizeof(char*)*2);
    for(i = 0; i < 2; i++) {
        services[i] = malloc(40);
    }
    strcpy(services[0], "addition");
    strcpy(services[1], "subtraction");
    printf("Service provider 1 has arrived.\n");
    printf("I provide addition and subtraction service.\n");
    mc_RegisterService( mc_current_agent, services, 2);
    return 0;
}

int addition(struct arg_struct* arg) {
    printf("Adding %d and %d...\n", arg->a, arg->b);
    return arg->a + arg->b;
}

int subtraction(struct arg_struct* arg) {
    printf("Subtracting %d - %d...\n", arg->a, arg->b);
    return arg->a - arg->b;
}
      ]]>
     </AGENT_CODE>
    </TASK>
   </AGENT_DATA>
  </MOBILE_AGENT>
 </MESSAGE>
</GAF_MESSAGE>

Definition at line 994 of file libmc.c.

References CHECK_NULL, mc_platform_s::df, df_AddRequest(), df_request_list_node_New(), agent_s::id, MC_ERR_INVALID_ARGS, MC_ERR_MEMORY, agency_s::mc_platform, MUTEX_INIT, MUTEX_T, and agent_s::name.

EXPORTMC int MC_ResetSignal ( MCAgency_t  attr  ) 

Reset a MobileC signal.

Returns:
0 on success, error_code_t on failure
See also:
MC_WaitSignal()

Definition at line 1128 of file libmc.c.

References COND_SIGNAL, mc_platform_s::giant, mc_platform_s::giant_cond, mc_platform_s::giant_lock, MC_NO_SIGNAL, agency_s::mc_platform, mc_platform_s::MC_signal, MUTEX_LOCK, and MUTEX_UNLOCK.

EXPORTMC int MC_ResumeAgency ( MCAgency_t  agency  ) 

Resumes a halted agency.

Parameters:
agency An agency previously halted with the MC_HaltAgency() function.
Returns:
0 on success, non-zero on failure.

Definition at line 1073 of file libmc.c.

References mc_platform_s::giant, mc_platform_s::giant_lock, agency_s::mc_platform, MUTEX_LOCK, and MUTEX_UNLOCK.

Referenced by MC_GetAllAgents().

EXPORTMC MCAgent_t MC_RetrieveAgent ( MCAgency_t  attr  ) 

Retrieves the oldest agent from an agency.

Returns:
a valid agent or NULL on failure

Definition at line 1082 of file libmc.c.

References mc_platform_s::agent_queue, agent_s::agent_status, ListSearch(), MC_AGENT_NEUTRAL, agency_s::mc_platform, MUTEX_LOCK, and MUTEX_UNLOCK.

EXPORTMC char* MC_RetrieveAgentCode ( MCAgent_t  agent  ) 

Retrieves an agent's Ch code.

Returns:
a malloc'd character string on success, NULL on failure

Definition at line 1112 of file libmc.c.

References agent_datastate_s::agent_code, agent_s::datastate, agent_s::lock, MUTEX_LOCK, MUTEX_UNLOCK, and agent_datastate_s::task_progress.

EXPORTMC int MC_SearchForService ( MCAgency_t  attr,
const char *  searchString,
char ***  agentNames,
char ***  serviceNames,
int **  agentIDs,
int numResults 
)

Search the directory facilitator for a service.

Returns:
0 on success, error_code_t on failure
Parameters:
attr (input) a MobileC agency handle
searchString (input) substring to search services for
agentNames (return) array of agent names with matching services
serviceNames (return) array of matching service names
agentIDs (return) array of matching agent IDs
numResults (return) number of matching results

Example

<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
  <MESSAGE message="MOBILE_AGENT">
    <MOBILE_AGENT>
     <AGENT_DATA>
       <NAME>mobagent1</NAME>
       <OWNER>IEL</OWNER>
       <HOME>localhost:5051</HOME>
       <TASK task="1" num="0">
       <DATA number_of_elements="0" name="no-return" complete="0" server="localhost:5050">
       </DATA>
          <AGENT_CODE>
          <![CDATA[
#include <stdio.h>
struct arg_struct {
    int a;
    int b;
};
int main()
{
    MCAgent_t agent;
    int retval;
    /* Search Return Variables */
    char** agentNames;
    char** serviceNames;
    int *agentIDs;
    int numResults;

    /* Argument Struct */
    struct arg_struct arg;

    /* Search for addition service */
    printf("\n\n\nSearching for addition service.\n");
    mc_SearchForService(
            "addition", 
            &agentNames,
            &serviceNames,
            &agentIDs,
            &numResults );
    printf("Done searching.\n");
    if (numResults < 1) {
        printf("No agents with service 'addition' found.\n");
        exit(0);
    }

    /* Just get the first hit */
    printf("Using agent %s for addition.\n", agentNames[0]); 
    agent = mc_FindAgentByID(agentIDs[0]);
    
    arg.a = 44;
    arg.b = 45;
    mc_CallAgentFunc(agent, "addition", &retval, &arg);
    printf("Result of addition %d + %d is %d.\n", arg.a, arg.b, retval);

    /* Now search for multiplication service */
    printf("\n\n Searching for Multiplication service...\n");
    mc_SearchForService(
            "multiplication",
            &agentNames,
            &serviceNames,
            &agentIDs,
            &numResults );

    if (numResults < 1) {
        printf("No agents with service 'multiplication' found.\n");
        exit(0);
    }

    printf("Using agent %s for multiplication.\n", agentNames[0]);
    agent = mc_FindAgentByID(agentIDs[0]);
    mc_CallAgentFunc(agent, "multiplication", &retval, &arg);
    printf("Result of multiplication %d * %d is %d.\n", arg.a, arg.b, retval);

    /* Now lets try to deregister a service */
    mc_DeregisterService(
            agentIDs[0],
            serviceNames[0]
            );
    
    return 0;
}
        ]]>
        </AGENT_CODE>
      </TASK>
      </AGENT_DATA>
    </MOBILE_AGENT>
  </MESSAGE>
</GAF_MESSAGE>

Definition at line 1139 of file libmc.c.

References CHECK_NULL, COND_SLEEP_ACTION, mc_platform_s::df, df_AddRequest(), df_request_list_node_Destroy(), df_request_list_node_New(), df_request_search_Destroy(), df_request_search_New(), MC_ERR_MEMORY, agency_s::mc_platform, MC_SUCCESS, and search.

EXPORTMC int MC_SemaphorePost ( MCAgency_t  attr,
int  id 
)

Post to a MobileC synchronization variable semaphore.

Parameters:
attr a MobileC agency handle
id the synchronization variable id to post to
Returns:
0 on success, error_code_t type on failure

Example

<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
  <MESSAGE message="MOBILE_AGENT">
    <MOBILE_AGENT>
     <AGENT_DATA>
       <NAME>wake_agent</NAME>
       <OWNER>IEL</OWNER>
       <HOME>localhost:5050</HOME>
       <TASK task="1" num="0">
          <DATA dim="0" name="no-return" complete="0" server="localhost:5051">
       </DATA>
          <AGENT_CODE>
          <![CDATA[
#include <stdio.h>
int main()
{
    int semaphore_id;
    semaphore_id = 55;
    printf("This is the wake agent.\n");
    mc_SemaphorePost(semaphore_id);

    return 0;
}
        ]]>
        </AGENT_CODE>
      </TASK>
      </AGENT_DATA>
    </MOBILE_AGENT>
  </MESSAGE>
</GAF_MESSAGE>

Definition at line 1189 of file libmc.c.

References agency_s::mc_platform, syncListNode_s::sem, SEMAPHORE_POST, mc_platform_s::syncList, and syncListFind().

EXPORTMC int MC_SemaphoreWait ( MCAgency_t  attr,
int  id 
)

Decreases a MobileC synchronization variable semaphore count by one.

Parameters:
attr a MobileC agency handle
id synchronization variable id to wait on
Returns:
0 on MC_SUCCESS, error_code_t type of failure
Note:
If the semaphore count is already zero, this function will block until another thread posts to the semaphore.

Example

<?xml version="1.0"?>

<!DOCTYPE myMessage SYSTEM "gafmessage.dtd">

<GAF_MESSAGE>
  <MESSAGE message="MOBILE_AGENT">
    <MOBILE_AGENT>
     <AGENT_DATA>
       <NAME>sleep_agent</NAME>
       <OWNER>IEL</OWNER>
       <HOME>localhost:5050</HOME>
       <TASK task="1" num="0">
          <DATA dim="0" name="no-return" complete="0" server="localhost:5051">
       </DATA>
          <AGENT_CODE>
          <![CDATA[
#include <stdio.h>
int main()
{
    int sem_id;
    printf("Sleep agent has arrived.\n");
    sem_id = mc_SyncInit(55);
    if (sem_id != 55) {
        printf("Possible error. Aborting...\n");
        exit(1);
    }
    printf("This is the sleep agent.\n");
    printf("I am going to sleep now...\n");
    mc_SemaphoreWait(sem_id);
    printf("This is the sleep agent: I am awake now. Continuing...\n");
    mc_SyncDelete(sem_id);

    return 0;
}
        ]]>
        </AGENT_CODE>
      </TASK>
      </AGENT_DATA>
    </MOBILE_AGENT>
  </MESSAGE>
</GAF_MESSAGE>

Definition at line 1201 of file libmc.c.

References agency_s::mc_platform, syncListNode_s::sem, SEMAPHORE_WAIT, mc_platform_s::syncList, and syncListFind().

EXPORTMC int MC_SendAgentMigrationMessage ( MCAgency_t  attr,
const char *  message,
const char *  hostname,
int  port 
)

Sends an agent migration message.

Parameters:
attr a MobileC agency handle
message a valid MobileC xml agent migration message
hostname host to send the message to
port port to send the message to

Definition at line 1223 of file libmc.c.

References MC_ERR, agency_s::mc_platform, message_Destroy(), message_InitializeFromString(), message_New(), and mc_platform_s::message_queue.

EXPORTMC int MC_SendAgentMigrationMessageFile ( MCAgency_t  attr,
const char *  filename,
const char *  hostname,
int  port 
)

Sends an agent migration message.

Parameters:
attr a MobileC agency handle
filename file containing a valid MobileC xml agent migration message
hostname hostname to send the agent to
port port to send the agent to

Definition at line 1254 of file libmc.c.

References agency_s::mc_platform, message_Destroy(), message_InitializeFromString(), message_New(), and mc_platform_s::message_queue.

EXPORTMC int MC_SetAgentStatus ( MCAgent_t  agent,
int  status 
)

Set an agent's status.

Parameters:
agent a MobileC agent
status agent status of type 'enum MC_AgentStatus_e'
Returns:
0 on success, or error_code_t on failure

Definition at line 1340 of file libmc.c.

References agent_s::agent_status, mc_platform_s::ams, COND_SIGNAL, agent_s::lock, agent_s::mc_platform, MUTEX_LOCK, MUTEX_UNLOCK, and agent_s::orphan.

Referenced by MC_DeleteAgent().

EXPORTMC int MC_SetDefaultAgentStatus ( MCAgency_t  agency,
enum MC_AgentStatus_e  status 
)

Sets default incoming agent status.

Parameters:
agency a MobileC agency handle
status the status to set all incoming agents
Returns:
0 on success, error_type_t on failure
Note:
using this function will override any status the incoming agent attempts to set for itself.

Definition at line 1355 of file libmc.c.

References mc_platform_s::default_agentstatus, and agency_s::mc_platform.

EXPORTMC int MC_SetThreadOff ( MCAgencyOptions_t options,
enum MC_ThreadIndex_e  index 
)

Sets a MobileC thread to "off" status.

Parameters:
options MobileC options previously initialized with MC_InitializeAgencyOptions()
index the thread to set
Returns:
0 on success, error_code_t on failure
Note:
This function must be called before MC_Initialize(). Once an agency is started with MC_Initialize, the MC_SetThread functions will have no effect.

Definition at line 1382 of file libmc.c.

References SET_THREAD_OFF, and MCAgencyOptions_s::threads.

EXPORTMC int MC_SetThreadOn ( MCAgencyOptions_t options,
enum MC_ThreadIndex_e  index 
)

Sets a MobileC thread to "on" status.

Parameters:
options MobileC options previously initialized with MC_InitializeAgencyOptions()
index the thread to set
Returns:
0 on success, error_code_t on failure
Note:
This function must be called before MC_Initialize(). Once an agency is started with MC_Initialize, the MC_SetThread functions will have no effect.

Definition at line 1365 of file libmc.c.

References SET_THREAD_ON, and MCAgencyOptions_s::threads.

EXPORTMC int MC_SetThreadsAllOff ( MCAgencyOptions_t options  ) 

Set all MobileC threads to 'off' status.

Parameters:
options a MobileC options structure initialized with with the MC_InitializeAgencyOptions() function.
Returns:
0 on success, error code on failure.

Definition at line 1389 of file libmc.c.

References MC_THREAD_ALL, SET_THREAD_OFF, and MCAgencyOptions_s::threads.

EXPORTMC int MC_SetThreadsAllOn ( MCAgencyOptions_t options  ) 

Set all Mobile-C threads on.

Parameters:
options MobileC options structure, initialized with MC_InitializeAgencyOptions()
Returns:
0 on success, error code on failure.

Definition at line 1372 of file libmc.c.

References MC_THREAD_ALL, SET_THREAD_ON, and MCAgencyOptions_s::threads.

EXPORTMC int MC_Steer ( MCAgency_t  attr,
int(*)(void *data)  funcptr,
void *  arg 
)

Set up a steerable algorithm.

Parameters:
attr a MobileC agency handle
funcptr a function pointer to the algorithm
arg an argument for the algorithm function
Returns:
0 on success, error_code_t on failure
Note:
The algorithm function must contain a call to MC_SteerControl in order for the algorithm to be steerable.

Example

#include <stdio.h>
#include <libmc.h>
#ifdef _WIN32
#include <windows.h>
#endif

int algorithm(void* boo);

int main() {
    MCAgency_t agency;
    int local_port = 5050;

    agency = MC_Initialize(local_port, NULL);
    
    MC_Steer(
        agency,
        &algorithm,
        NULL
        ); 

    MC_End(agency);
    return 0;
}

int algorithm(void* boo)
{
  int i=0;
  MC_SteerCommand_t command;
  while(1) {
#ifndef _WIN32
    sleep(1);
#else
    Sleep(1000);
#endif
    printf("%d \n", i);
    i++;
    command = MC_SteerControl();
    if( 
        command == MC_RESTART ||
        command == MC_STOP
      ) 
    {
      return 0;
    }
  }
}
    

Definition at line 1399 of file libmc.c.

References agency_s::mc_platform, MC_RESTART, MC_RUN, mc_platform_s::MC_steer_command, mc_platform_s::MC_steer_lock, MUTEX_LOCK, and MUTEX_UNLOCK.

EXPORTMC enum MC_SteerCommand_e MC_SteerControl ( void   ) 

The MobileC user-algorithm steering function.

Returns:
The current steering command
Note:
This function belongs inside a user's steerable algorithm.
See also:
MC_Steer()

Definition at line 1418 of file libmc.c.

References COND_WAIT, mc_platform_s::MC_steer_command, mc_platform_s::MC_steer_cond, mc_platform_s::MC_steer_lock, MC_SUSPEND, MUTEX_LOCK, and MUTEX_UNLOCK.

EXPORTMC int MC_SyncDelete ( MCAgency_t  attr,
int  id 
)

Deletes a previously initialized synchronization variable.

Parameters:
attr a MobileC agency handle
id the sync variable id to delete
Returns:
0 on success, or error_code_t on failure

Definition at line 1434 of file libmc.c.

References syncList_s::giant_lock, syncListNode_s::lock, MC_ERR_NOT_FOUND, agency_s::mc_platform, MUTEX_LOCK, MUTEX_UNLOCK, mc_platform_s::syncList, syncListFind(), syncListNodeDestroy(), and syncListRemove().

EXPORTMC int MC_SyncInit ( MCAgency_t  attr,
int  id 
)

Initializes a new MobileC synchronization variable.

Parameters:
attr a MobileC agency handle
id the requested sync variable id
Returns:
new sync variable's id. May be different than the requested id.
Note:
Each synchronization variable may be used as a mutex, condition variable, or semaphore. However, it should only be used as one type of synchronization variable per instance, or undefined behaviour may result.
See also:
MC_MutexLock(), MC_MutexUnlock(), MC_CondWait(), MC_CondSignal(), MC_CondBroadcast, MC_SemaphorePost(), MC_SemaphoreWait()

Definition at line 1464 of file libmc.c.

References syncList_s::giant_lock, syncListNode_s::id, agency_s::mc_platform, MUTEX_LOCK, MUTEX_UNLOCK, node, mc_platform_s::syncList, syncListAddNode(), syncListFind(), and syncListNodeNew().

EXPORTMC int MC_TerminateAgent ( MCAgent_t  agent  ) 

Halt a running agent.

Returns:
0 on success, error_code_t on failure

Definition at line 1489 of file libmc.c.

References agent_s::agent_interp.

Referenced by MC_DeleteAgent().

EXPORTMC int MC_WaitAgent ( MCAgency_t  attr  ) 

Wait indefinitely.

Note:
This function is intended to block the calling thread forever. Wait for an agent arrival event

This function blocks until an agent arrival signal is triggered, at which point in unblocks.

Definition at line 1516 of file libmc.c.

References mc_platform_s::agent_queue, COND_WAIT, mc_platform, agency_s::mc_platform, MUTEX_LOCK, MUTEX_UNLOCK, and size.

EXPORTMC MCAgent_t MC_WaitRetrieveAgent ( MCAgency_t  attr  ) 

Wait and retrieve an agent.

Returns:
a valid MobileC agent on success, or NULL on failure
Note:
This function blocks until the arrival of an agent. The agent is retrieved after it is initialized, but before it is executed.

Definition at line 1536 of file libmc.c.

References mc_platform_s::agent_queue, ListSearch(), agency_s::mc_platform, MC_RECV_AGENT, MC_WaitSignal(), MUTEX_LOCK, and MUTEX_UNLOCK.

EXPORTMC int MC_WaitSignal ( MCAgency_t  attr,
int  signals 
)

Wait for a MobileC signal.

Parameters:
attr a MobileC agency handle
signals a flag of signals to wait for, of type 'enum MC_Signal_e'
Returns:
0 on success, error_code_t on failure
Note:
the parameter 'signals' may be something like 'MC_RECV_MESSAGE | MC_RECV_AGENT', etc.

Example

#include <stdio.h>
#include <libmc.h>

int main(int argc, char *argv[]) 
{
  MCAgency_t agency;
  MCAgencyOptions_t options;
  MCAgent_t agent;
  int dim, *extent;
  double *data;
  int i, j, size;

  MC_InitializeAgencyOptions(&options);
  MC_SetThreadOff(&options, MC_THREAD_CP);
  agency = MC_Initialize(5050, &options);

  printf("MobileC Started\n");
  /* Note: The third argument of the following function may also be a
     valid IP address in the form of a string. i.e. 192.168.0.1 */
  MC_SendAgentMigrationMessageFile(agency,
      "test.xml",
      "localhost",
      5051);
  MC_WaitSignal(agency, MC_RECV_RETURN);
  agent = MC_FindAgentByName(agency, "mobagent3");
  if (agent == NULL) {
    fprintf(stderr, "Did not receive correct agent. \n");
    exit(1);
  }
  printf("%d tasks.\n", MC_GetAgentNumTasks(agent) );
  for (i = 0; i < MC_GetAgentNumTasks(agent); i++) {
    MC_GetAgentReturnData(
        agent,
        i,
        (void**)&data,
        &dim,
        &extent );
    printf("Task: %d\n", i);
    size = 1;
    printf("dim is %d\n", dim);
    for (j = 0; j < dim; j++) {
      size *= extent[j];
    }
    printf("Size: %d\n", size);
    printf("Data elements: ");
    for (j = 0; j < size; j++) {
      printf("%f ", data[j]);
    }
    printf("\n\n");
    free(data);
    free(extent);
  }
  MC_ResetSignal(agency);
  MC_End(agency);
  return 0;
}

Definition at line 1554 of file libmc.c.

References COND_WAIT, mc_platform_s::giant, mc_platform_s::giant_lock, agency_s::mc_platform, mc_platform_s::MC_signal, mc_platform_s::MC_signal_cond, mc_platform_s::MC_signal_lock, MUTEX_LOCK, and MUTEX_UNLOCK.

Referenced by MC_WaitRetrieveAgent().


Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4