MYCP开发指南系列之:开发第一个C++ APP

来源:互联网 发布:基于单片机数字温度计 编辑:程序博客网 时间:2024/06/05 03:50

1   开发第一个C++ APP

C++ APPMYCP业务层组件,用于处理业务逻辑。

1.1   cspApp.cpp文件

新建一个cspApp.cpp文件,或者利用VC新建一个普通DLL类型工程项目。

1.2   添加头文件

#include <CGCBase/httpapp.h>

#include <CGCBase/cgcServices.h>

using namespace cgc;

1.3   实现应用服务接口

class CAppService

     : publiccgcServiceInterface

{

public:

     typedefboost::shared_ptr<CAppService> pointer;

     staticCAppService::pointer create(void)

     {

         returnCAppService::pointer(new CAppService());

     }

     virtualtstring serviceName(void) const {return _T("AppService");}

protected:

     virtualbool callService(intfunction, const cgcValueInfo::pointer&inParam, cgcValueInfo::pointer outParam)

     {

         theApplication->log(LOG_INFO,"AppService callService = %d/n",function);

         returntrue;

     }

     virtualbool callService(consttstring& function, constcgcValueInfo::pointer& inParam, cgcValueInfo::pointer outParam)

     {

         theApplication->log(LOG_INFO,"AppService callService = %s/n",function.c_str());

         returntrue;

     }

     virtualcgcAttributes::pointer getAttributes(void) const {returntheAppAttributes;}

};

定义CappService服务接口,继承于cgcServiceInterface,实现callService函数,getAttributes函数用于实现组件的参数管理,详细代码请看samples/cspApp/cspApp.cpp文件。

1.4   导出应用服务接口

extern "C" void CGC_APICGC_GetService(cgcServiceInterface::pointer & outService, const cgcValueInfo::pointer& parameter)

{

     CAppService::pointer appService= CAppService::create();

     appService->initService();

     outService = appService;

     cgcAttributes::pointerattributes = theApplication->getAttributes();

     assert (attributes.get() !=NULL);

     theAppAttributes->setAttribute(ATTRIBUTE_NAME,outService.get(), appService);

}

 

extern "C" void CGC_API CGC_ResetService(cgcServiceInterface::pointerinService)

{

     if(inService.get() == NULL) return;

     theAppAttributes->removeAttribute(ATTRIBUTE_NAME,inService.get());

     inService->finalService();

}

1.5   编译部署

编译cspApp工程,生成cspApp.dll或者libcspApp.so文件,复制文件到$(MYCP_BINPATH)/modules目录。

修改$(MYCP_BINPATH)/conf/modules.xml文件,在app配置项增加一个组件配置项;

<app>
     
     <module>
             <file>cspApp</file>
              <allowall>1</allowall>
     </module>
     
</app>

设置组件文件,允许开放所有函数。

1.6   CSP调用cspApp组件

以下内容为调用cspApp组件的CSP代码,详细请看bin/web/samples/csp-app.csp文件:

    <csp:define type="app" id="$var_app" name="cspApp" />
<h1>Call APP Function</h1>
     call $var_app "info" function.<br>
    <csp:app:call id="$var_app" name="info" />
     call result: <%=_$result%><br>
     <br>
     <b>See MYCP console print info.</b><br>
     cspApp SourceCode see Samples/cspApp/cspApp.cpp<br>
<h1>APP Get/Set Function</h1>
     set $var_app p1 = 1111111<br>
    <csp:app:set id="$var_app" name="p1" in="1111111" />
     get $var_app p1 to $var_p2<br>
    <csp:app:get id="$var_app" name="p1" out="$var_p2" />
     $var_p2 = <%=$var_p2%><br>
<h1>Final APP</h1>
    <csp:reset id="$var_app" />
     $var_app reset ok<br>

浏览器访问如下图:

 


1.7   配置访问C++ APP组件

以上例子演示如何定义一个C++ APP组件,在页面退出的时候需要清空该组件变量;为了方便开发者使用C++ APP应用组件;MYCP支持通过配置apps.xml文件,部署好所有的C++ APP应用组件,配置好的应用组件,直接在CSP页面用A$变量直接使用,不需要定义,和清空操作。

配置例子请看conf/HttpServer/apps.xml文件;

使用例子请看bin/web/samples/csp-app-fs.csp文件等例子;

1.8   总结

本节学习如何开发一个C++ APP应用组件,包括定义实现服务接口、实现接口函数、接口参数管理和导出服务接口等。