strcmp(
"noh", szRealCmd) )
{
m_info.LogNOH(this->GetCwd()->GetFullName().Get());
}
else if ( 0 == strcmp("cls", szRealCmd) )
{
m_info.LogCLS();
}
else if ( 0 == strcmp("cwd", szRealCmd) )
{
printf("CWD : %s ", this->GetCwd()->GetFullName().Get() );
printf("Class : %s ", this->GetCwd()->GetClass()->GetProperName() );
printf("Parent: %s ", this->GetCwd()->GetClass()->GetSuperClass()->GetProperName() );
}
else if ( 0 == strcmp("mtd", szRealCmd) )
{
nHashList* obj = this->GetCwd()->GetClass()->GetCmdList();
nCmdProto* node = (nCmdProto*)obj->GetHead();
do
{
printf("%s ", node->GetProtoDef());
} while ( (node = (nCmdProto*)node->GetSucc()) );
}
else
{
printf("%s ", "Error: '-' follows Unkown Globle Command!");
}
}
/// add current command to history
void nConConServer::AddCommandToHistory()
{
// empty.
}
/// recall next command in history
void nConConServer::RecallNextCommand()
{
// empty.
}
/// recall previous command in history
void nConConServer::RecallPrevCommand()
{
// empty.
}
/// set local cwd
void nConConServer::SetCwd(nRoot* cwd)
{
this->refCwd = cwd;
}
/// get local cwd (can return 0)
nRoot* nConConServer::GetCwd()
{
if ( !this->refCwd.isvalid() )
{
this->SetCwd(nKernelServer::Instance()->GetCwd());
}
return this->refCwd.get();
}
/// update the tab completion stuff
void nConConServer::UpdateTabComplete()
{
// ...
}
/// perform a tab completion
void nConConServer::DoTabCompletion()
{
// ...
}
///----------------------------------------------------------------------------
/// 声明为Nebula2脚本支持类
nNebulaScriptModule(nConConServer, nconconserver, "nroot");
/// ConConServer开关
static void n_toggle(void* slf, nCmd* cmd);
/// 执行控制台指令
static void n_exec(void* slf, nCmd* cmd);
/// Regist Commands
void nNebulaScriptInitCmds(nconconserver) (nClass* cl)
{
cl->BeginCmds();
cl->AddCmd("v_toggle_v", 'TOGL', n_toggle);
cl->AddCmd("v_exec_s", 'EXEC', n_exec);
cl->EndCmds();
}
/// ToggleMode的脚本支持
static void n_toggle(void* slf, nCmd* cmd)
{
nConConServer* self = (nConConServer*) slf;
self->ToggleMode();
}
/// ExecCommand的脚本支持
static void n_exec(void* slf, nCmd* cmd)
{
nConConServer* self = (nConConServer*) slf;
const char* szCmdStr = cmd->In()->GetS();
self->ExecuteCommand(szCmdStr);
}
///----------------------------------------------------------------------------
#pragma warning(pop)
其中ToggleMode()方法是切换到控制台模式的开关,已经加入了对脚本的支持,可以直接通过InputServer映射到一个按键上。
PollCmd()命令用于阻塞接受用户输入的指令。由于是以Windows控制台实现,所以只能以阻塞模式接受用户输入,在执行PollCmd()函数后应用程序会停止运行,直到用户在控制台中输入”-exit”指令,nConConServer才会把控制权释放掉。
下面给出nConConServer的使用事例代码:
/****************************************************************************/
/* Nebula2 - Tutorial 12 */
/* A Console Server for Console */
/* author: happykevins */
/****************************************************************************/
///----------------------------------------------------------------------------
/// +必要头文件
// nebula2 includes
#include "kernel/nkernelserver.h"
#include "script/ntclserver.h"
// ConConServer头文件
#include "../NebulaUtils/nConConServer.h"
// Tutorial工具库:一些通用的宏定义
#include "../NebulaUtils/nutildefs.h"
/// -必要头文件
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +链接库
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "d_nkernel.lib")
#pragma comment(lib, "d_nnebula.lib")
#pragma comment(lib, "d_microtcl.lib")
/// -链接库
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +声明使用的Nebula2 Package&Module
nNebulaUseModule(ntclserver);
/// -声明使用的Nebula2 Package&Module
///----------------------------------------------------------------------------
nKernelServer* ks = NULL;
///----------------------------------------------------------------------------
/// +初始化环境,创建需要的Server
///
bool InitApp()
{
/// 创建KernelServer
ks = n_new(nKernelServer);
///----------------------------------------------------------------------------
/// +向KernelServer中添加Package&Module
nNebulaAddModule(ntclserver);
/// +向KernelServer中添加Package&Module
///----------------------------------------------------------------------------
ks->New("ntclserver", "/sys/servers/script");
return true;
}
///
/// -初始化环境,创建需要的Server
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +退出程序,清理资源
///
bool CloseApp()
{
/// 销毁KernelServer
n_delete(ks);
return true;
}
///
/// -退出程序,清理资源
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
/// +Application
int main(int argc, const char** argv)
{
/// 初始化Application
if ( !InitApp() )
{
n_error("程序初始化失败! ");
return 0;
}
/// ConConServer
nConConServer console;
/// 切出ConConServer
console.ToggleMode();
/// 轮训控制台指令,(阻塞模式)
console.PollCmd();
/// 释放资源
if ( !CloseApp() )
{
n_error("释放资源失败! ");
return 0;
}
return 0;
}
/// -Application
///----------------------------------------------------------------------------
查看本文来源