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

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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天恩软件工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值