当前位置: 首页 > 工具软件 > MagicWare > 使用案例 >

客户端开发 - 使用C++开发magicware客户端

廖弘量
2023-12-01

 

在几乎所有操作系统上,都可以使用使用c++/java开发magicware 客户端程序,这里介绍一个使用c++开发客户端程序的范例。

 

magicware 3.2 for linux版本下载地址:

http://www.jinglingonline.com/page.asp?menu_id=3&menu_item_id=31&filter=main#18


/*-
 * magiclayer 中间件客户端开发范例,演示了如何使用c/c++开发magiclayer的客户端程序
 * 编译方法 g++ test.cpp -I../.. -I../../inc3p -L../../../lib -lACE -lxerces-c -lclient
 * 注意需要在源文件目录中链接 ACE/xerces-c 头文件
 */

#include "client/client-c.hpp"
#include "inc/env.hpp"
#include "mwplus/StatisticsMgr.h"
#include <iostream>
#include "ace/OS.h"

class MyParameter : public MWParameter
{
public:
  virtual MWRetCode Initialize()
  {
    SetDescription("test cleint program for magiclayer.");

    MWParamItem param1( "help", "-h", "", "display this help information and return.", "", true, "false", "");
    AddParam(param1);
    MWParamItem param2( "cmdcode", "-c", "", "command code to request", "", false, "1025", "");
    AddParam(param2);
    MWParamItem param3( "number", "-n", "", "how many request will be send", "", false, "1000", "");
    AddParam(param3);
    MWParamItem param4( "sleep", "-s", "", "wait milli-seconds after each request.", "", false, "0", "");
    AddParam(param4);
    MWParamItem param5( "remote", "-r", "", "remote server", "", false, "127.0.0.1:10001", "");
    AddParam(param5);

    return RETCODE_SUCCESS;
  }
};

int sendMessage( const char *pSession, int cmdcode, int count, int sleepTime );

int main( int argc, char **argv )
{
  MWRetCode retCode = RETCODE_SUCCESS;

  ACE_Time_Value t1 = ACE_OS::gettimeofday();

  /// 默认请求命令字1025,如果命令行有参数指定,则请求指定命令字,这样使用本程序可以配合请求多个服务
  int cmdcode = 1025;
  int sleepTime = 0;
  int count = 1000;
  std::string remote;

  /// 构造一个参数对象
  MyParameter param;

  /// 使用前初始化
  param.Initialize(); 

  /// 解析命令行
  retCode = param.ParseCmdLine(argc,argv);

  /// 检查用户参数是否合乎要求
  if ( retCode == RETCODE_SUCCESS )
  {
    MWInfo(log);
    log << "command line check return : " << (retCode=param.Check()) << std::endl;
    log.End();
  }

  if ( retCode != RETCODE_SUCCESS || param.IsUserValue("help") )
  {
    /// 打印帮助信息
    std::cout << param.GetHelpInfo() << std::endl;
    return 1;
  }
  else
  {
    cmdcode = param.GetIntValue("cmdcode");
    sleepTime = param.GetIntValue("sleep");
    count = param.GetIntValue("number");
    remote = param.GetValue("remote");
  }

  /// 连接到服务器或公告牌
  int ret = mwLogin(remote.c_str(),"mwplus","password");
  std::cout << "login return " << ret << std::endl;

  if ( ret != -1 )
  {
    /// 开始事务(可选)
    const char*pSession = mwBeginSession();

    /// 发送测试消息
    sendMessage(pSession, cmdcode, count, sleepTime);

    /// 结束事务,要求与请求一一对应
    mwEndSession(pSession);
   
    /// 打印统计信息
    MWStatisticsMgr::Instance()->Dump(std::cerr);

    /// 退出登录
    ret = mwLogout(true);
    std::cout << "logout return " << ret << std::endl;
  }

  ACE_Time_Value t2 = ACE_OS::gettimeofday();

  std::cerr << "time eclipsed : " << (t2-t1).sec() + (t2-t1).usec()/1000000.0 << std::endl;

  return 0;
}

int sendMessage( const char *pSession, int cmdcode, int count, int sleepTime )
{
  int ret = 0; 
  for( int x=0;ret!=-1&&x<count;++x)
  {
    if ( sleepTime != 0 )
    {
      ACE_OS::sleep( ACE_Time_Value(0,sleepTime*1000));
    }

    /// 创建请求对象
    int store = mwCreateStorage(pSession);

    /// 以下代码添加数据到请求对象

    /// 添加数字
    mwAddNumber(store,"num1",5);

    /// 添加二进制缓冲区
    char *p = (char*)mwLogin;
    mwAddBuffer(store,"str3",p,103);

    /// 添加字符串
    mwAddString(store,"str2","Hello mwplus.");
   
    /// 添加表
    char *table = "query-recoreds";
    mwAddTable(store,table);
    /// 初始化表结构
    mwAddColumn(store,table,"name",MW_DT_STRING);
    mwAddColumn(store,table,"address",MW_DT_STRING);
    mwAddColumn(store,table,"age",MW_DT_INT);
    mwAddColumn(store,table,"mobile",MW_DT_STRING);
    mwAddColumn(store,table,"buffer",MW_DT_BUFFER);

    /// 填充表数据
    for( int i=0; i<10; ++i )
    {
      mwFillStringCell_c(store,table,i,"name","zhangsan");
      mwFillStringCell_c(store,table,i,"address","I don't known");
      mwFillNumberCell_c(store,table,i,"age",i+20);
      mwFillStringCell_i(store,table,i,3,"15922222222");
      mwFillBufferCell_i(store,table,i,4,p,25);
    }
    
    /// 初始化第20行第4列为buffer表格
    /// 这里演示了非顺序添加的情况,中间表格内容将构造为未初始化表格
    mwFillBufferCell_i(store,table,20,4,p,25);

    /// 获取表格行数
    std::cout  << "table height = " << mwGetTableHeight(store,table) << std::endl;

    /// 请求服务
    ret = mwRequest(store,cmdcode);
   
    std::cout << "request return " << ret << std::endl;
    if ( ret > 0 )
    {
      /// 操作成功,返回值为返回数据对象标志,可以通过此标志使用或销毁返回数据
      mwDestroyStorage(ret);
    }
    /// 销毁请求数据
    mwDestroyStorage(store);
  }

  return ret;
}

 类似资料: