例-1は、ファイルを表すリソースを管理するためのエージェント実装の詳細を説明しています。
例G-1 demoagent1.cpp
/* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. */ /* * NAME * Demoagent1.cpp: Demonstrates agent to monitor a file * * DESCRIPTION * This is a sample program that demonstrates an agent to monitor * a file. The agent has the following tasks: * - On startup : Create the file. * - On shutdown : Delete the file. * - On check command : Detect whether the file is present or not. * - On clean command : Delete the file. * This program can be used as a basis to build up more complicated * agents that manage real-world resources. * #include <stdlib.h> #include <stdio.h> #include <string.h> #include <clsagfw.h>
エージェント・フレームワークclsagfw.hヘッダー・ファイルを含めます。
#define TEST_TYPE1 (oratext *) "HOTFILE_TYPE"
#define TEST_PATHNAME_ATTR (oratext *) "PATH_NAME"
/*
* NAME
* test_agent_exit
* DESCRIPTION
* Function to clean up, called by framework whenever the agent
* process is about to terminate
* PARAMS
* exitCode, which is an sb4 describing the reason for exit.
* RETURNS
* void
*/
void
test_agent_exit(sb4 exitCode)
{
clsagfw_log(NULL, 1, (oratext *)"Demo Agent is exiting..\n");
}
エージェント・フレームワークに登録されているコールバック・ファンクションを終了します。
/*
* NAME
* type1_start
* DESCRIPTION
* Action entry point for the 'start' command. Creates the file for
* monitoring.
* PARAMS
* Pointer to the agent framework context
* RETURNS
* ub4 which could be CLSAGFW_AE_FAIL or CLSAGFW_AE_SUCCESS
* depending on whether the action failed or succeeded.
*/
ub4
type1_start(const clsagfw_aectx *ectx)
{
ub4 ret = CLSAGFW_AE_FAIL;
const oratext *pResName = NULL;
const oratext *pPathName = NULL;
FILE *fp;
clsagfw_log(ectx, 1, (oratext *)"Start action called..");
/* Try to read the resource name */
if (clsagfw_get_attrvalue(ectx, (oratext *)"NAME", &pResName) !=
CLSAGFW_SUCCESS)
{
goto done;
}
/* Try to read the PATH_NAME attribute */
if (clsagfw_get_attrvalue(ectx, TEST_PATHNAME_ATTR, &pPathName) !=
CLSAGFW_SUCCESS)
{
goto done;
}
clsagfw_get_attrvalue()を使用してリソース名およびPATH_NAME属性の名前を取得します。
clsagfw_log(ectx, 1,
(oratext *)"Start action arguments: resName = %s,
pathName = %s", pResName, pPathName);
/* Try to create the file */
fp = fopen((char *)pPathName, "w");
if(!fp)
{
/* Could not create the file */
clsagfw_log(ectx, 1, (oratext *) "START action for resource %s:
FAILED\n", pResName);
ret = CLSAGFW_AE_FAIL;
}
else
{
/* Created the file successfully */
clsagfw_log(ectx, 1, (oratext *) "START action for resource %s:
SUCCESS\n", pResName);
ret = CLSAGFW_AE_SUCCESS;
fclose(fp);
}
done:
return ret;
}
clsagfw_log()をコールすると、診断メッセージおよびエラー・メッセージのログがエージェント・ログ・ファイルに記録されます。
/*
* NAME
* type1_stop
* DESCRIPTION
* Action entry point for the 'stop' and 'clean' commands. Deletes
* the file being monitored.
* PARAMS
* Pointer to agent framework context
* RETURNS
* ub4 which could be CLSAGFW_AE_FAIL or CLSAGFW_AE_SUCCESS
* depending on whether the action failed or succeeded.
*/
ub4
type1_stop (const clsagfw_aectx *ectx)
{
ub4 ret = CLSAGFW_AE_FAIL;
const oratext *pResName = NULL;
const oratext *pPathName = NULL;
clsagfw_log(ectx, 1, (oratext *)"Stop action called..");
/* Try to read the resource name */
if (clsagfw_get_attrvalue(ectx, (oratext *)"NAME", &pResName) !=
CLSAGFW_SUCCESS)
{
clsagfw_log(ectx, 1, (oratext *)"STOP action: %s: Could not
read attribute\n", pResName);
goto done;
}
/* Try to read the PATH_NAME attribute */
if (clsagfw_get_attrvalue(ectx, TEST_PATHNAME_ATTR, &pPathName) !=
CLSAGFW_SUCCESS)
{
pPathName = pResName;
}
clsagfw_log(ectx, 1,
(oratext *)"Stop action arguments: resName = %s,
pathName = %s", pResName, pPathName);
/* Try to delete the file */
if (remove((char *)pPathName))
{
/* Could not delete the file */
clsagfw_log(ectx, 1, (oratext *)"STOP action for resource %s:
FAILED\n", pResName);
ret = CLSAGFW_AE_FAIL;
}
else
{
/* Deleted the file successfully */
clsagfw_log(ectx, 1, (oratext *)"STOP action for resource %s:
SUCCESS\n", pResName);
ret = CLSAGFW_AE_SUCCESS;
}
done:
return ret;
}
/*
* NAME
* type1_check
* DESCRIPTION
* Action entry point for the 'check' command. Determines if the
* file exists.
* PARAMS
* Pointer to agent framework context.
* RETURNS
* ub4 which gives the status of the resource. Check the
* agent framework reference for details on this function's
* return codes.
*/
ub4
type1_check(const clsagfw_aectx *ectx)
{
ub4 ret = CLSAGFW_UNKNOWN;
const oratext *pResName = NULL;
const oratext *pPathName = NULL;
FILE *fp;
clsagfw_log(ectx, 1, (oratext *)"Check action called..");
/* Try to read the resource name */
if (clsagfw_get_attrvalue(ectx, (oratext *) "NAME", &pResName) !=
CLSAGFW_SUCCESS)
{
goto done;
}
/* Try to read the PATH_NAME attribute */
if (clsagfw_get_attrvalue(ectx, TEST_PATHNAME_ATTR, &pPathName) !=
CLSAGFW_SUCCESS)
{
clsagfw_log(ectx, 1, (oratext *)"CHECK action: %s: Could not
read attribute\n", pResName);
goto done;
}
clsagfw_log(ectx, 1,
(oratext *)"Check action arguments: resName = %s,
pathName = %s", pResName, pPathName);
/* Check if the file is accessible */
fp = fopen((char *)pPathName, "r");
if (!fp)
{
/* Could not open file */
clsagfw_log(ectx, 1, (oratext *)"CHECK action: %s status –
UNPLANNED_OFFLINE\n", pResName);
ret = CLSAGFW_UNPLANNED_OFFLINE;
}
else
{
/* Opened file successfully */
clsagfw_log(ectx, 1,(oratext *)"CHECK action: %s status –
ONLINE\n", pResName);
fclose(fp);
ret = CLSAGFW_ONLINE;
}
done:
return ret;
}
/*
* Initialization of the agent framework and registration of types is
* done in main.
*/
int main(sb4 argc, oratext **argv)
{
clsagfw_log(NULL, 1, (oratext *)" *** Agent Framework Demo Agent
Started *** \n");
/*
* Initialize the agent framework
*/
if (clsagfw_init(argc, argv, 0, NULL, 0)
!= CLSAGFW_SUCCESS)
{
clsagfw_log(NULL, 1, (oratext *)"Failed to initilize the agent
framework\n");
clsagfw_exit(-1);
}
/*
* Set the exit callback function
*/
clsagfw_set_exitcb(test_agent_exit);
/*
* Add the type definition to the framework
*/
if (clsagfw_add_type(TEST_TYPE1) != CLSAGFW_SUCCESS)
{
clsagfw_log(NULL, 1,(oratext *)"Failed in adding type %s to the
framework\n", TEST_TYPE1);
clsagfw_exit(-1);
}
/*
* Set all entry points for for HOTFILE_TYPE
*/
clsagfw_set_entrypoint(TEST_TYPE1, type1_start,
CLSAGFW_ACTION_START);
clsagfw_set_entrypoint(TEST_TYPE1, type1_stop,
CLSAGFW_ACTION_STOP);
clsagfw_set_entrypoint(TEST_TYPE1, type1_check,
CLSAGFW_ACTION_CHECK);
clsagfw_set_entrypoint(TEST_TYPE1, type1_stop,
CLSAGFW_ACTION_CLEAN);
clsagfw_log(NULL, 1, (oratext *)"Added resource type [%s] to the
agent framework\n", TEST_TYPE1);
/*
* All set to go, Start the framework. This function does not
* return if the framework starts successfully.
*/
clsagfw_startup();
/*** NOT REACHED **/
return 0;
}