扩展一条新命令,只需在这里面派生一个新的命令类,不需要修改之前代码,保证开闭原则:
头文件:
CommandDb.h
#ifndef __COMMAND_DB_H__
#define __COMMAND_DB_H__
#include <string>
#include "json/json.h"
#include "TsManager.h"
#include "Task.h"
using namespace std;
class Command
{
protected:
string m_commandName;
TsProperty m_tsProperty;
bool m_isUseTs;
TsType m_tsType;
TaskType m_taskType;
public:
Command();
virtual ~Command();
string GetCommansName(){ return m_commandName;}
TsType GetTsType(){return m_tsType;}
TaskType GetTaskType(){return m_taskType;}
virtual bool BuildTsCmd(const Json::Value& appCmd,Json::Value& tsCmd);
virtual bool BuildTsResponse(const Json::Value& tsResult,Json::Value& tsResponse);
virtual bool BuildAppResult(const Json::Value& tsResult,Json::Value& appResult);
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse) = 0;
};
///////////////////////////////// specific command //////////////////////////////////////
class CommandServerStatus : public Command
{
public:
CommandServerStatus();
~CommandServerStatus();
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
class CommandVideoMessage : public Command
{
public:
CommandVideoMessage()
{
m_commandName = "video_message_task";
m_isUseTs = true;
m_tsType = TS_VIDEO;
m_tsProperty = TS_LIGHT;
m_taskType = VideoTask;
}
~CommandVideoMessage(){}
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
class CommandVideoTrans : public Command
{
public:
CommandVideoTrans()
{
m_commandName = "video_trans_task";
m_isUseTs = true;
m_tsType = TS_VIDEO;
m_tsProperty = TS_HEAVY;
m_taskType = VideoTask;
}
~CommandVideoTrans(){}
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
class CommandPrintScreen : public Command
{
public:
CommandPrintScreen()
{
m_commandName = "print_screen_task";
m_isUseTs = true;
m_tsType = TS_VIDEO;
m_tsProperty = TS_LIGHT;
m_taskType = VideoTask;
}
~CommandPrintScreen(){}
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
class CommandVideoCut : public Command
{
public:
CommandVideoCut()
{
m_commandName = "video_cut_task";
m_isUseTs = true;
m_tsType = TS_VIDEO;
m_tsProperty = TS_LIGHT;
m_taskType = VideoTask;
}
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
class CommandVideoMerge : public Command
{
public:
CommandVideoMerge()
{
m_commandName = "video_merge_task";
m_isUseTs = true;
m_tsType = TS_VIDEO;
m_tsProperty = TS_HEAVY;
m_taskType = VideoTask;
}
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
class CommandDocTrans : public Command
{
public:
CommandDocTrans()
{
m_commandName = "doc_trans_task";
m_isUseTs = true;
m_taskType = DocTask;
}
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
class CommandDynamicPptTrans : public Command
{
public:
CommandDynamicPptTrans()
{
m_commandName = "dynamic_ppt_trans_task";
m_isUseTs = true;
m_tsType = TS_DYNAMIC_PPT;
m_taskType = DpptTask;
}
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
class CommandTaskProgress : public Command
{
public:
CommandTaskProgress()
{
m_commandName = "task_progress_request";
m_isUseTs = false;
m_taskType = NoTsTask;
}
virtual bool BuildAppResponse(const Json::Value& appCmd,Json::Value& appResponse);
};
///////////////////////////////// CommandDb ///////////////////////////////////////
class CommandDb
{
public:
CommandDb();
~CommandDb();
bool AddCommand(Command* cmd);
bool GetCommandNameInSpecificTaskType(TaskType taskType,vector<string>& CommandNames);
private:
map<string,Command*> m_commands;
};
#endif
|