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

来源:互联网 发布:淘宝子账号用什么登陆 编辑:程序博客网 时间:2024/06/05 00:30

1    开发第一个 C++ APP

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

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

     : public cgcServiceInterface

{

public :

     typedef boost::shared_ptr<CAppService> pointer;

     static CAppService::pointer create(void )

     {

         return CAppService::pointer(new CAppService());

     }

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

protected :

     virtual bool callService(int function, const cgcValueInfo::pointer& inParam, cgcValueInfo::pointer outParam)

     {

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

         return true ;

     }

     virtual bool callService(const tstring& function, const cgcValueInfo::pointer& inParam, cgcValueInfo::pointer outParam)

     {

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

         return true ;

     }

     virtual cgcAttributes::pointer getAttributes(void ) const {return theAppAttributes;}

};

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

1.4   导出应用服务接口

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

{

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

     appService->initService();

     outService = appService;

     cgcAttributes::pointer attributes = theApplication->getAttributes();

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

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

}

 

extern "C" void CGC_API CGC_ResetService(cgcServiceInterface::pointer inService)

{

     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 应用组件,包括定义实现服务接口、实现接口函数、接口参数管理和导出服务接口等。

 

原创粉丝点击