Nebula2探秘02-使用FileServer2

Nebula2探秘01-创建KernelServer&使用log

happykevins文

/**/ /****************************************************************************/
/**/ /*Nebula2-Tutorial02*/
/**/ /*使用FileServer2*/
/**/ /*author:happykevins*/
/**/ /****************************************************************************/

/**/ ///----------------------------------------------------------------------------
///+必要头文件

#include " kernel/nkernelserver.h "
#include
" kernel/nfileserver2.h "
#include
" kernel/nfile.h "
#include
" kernel/nautoref.h "
/**/ ///-必要头文件
///----------------------------------------------------------------------------

///----------------------------------------------------------------------------
///+链接库

#pragma comment(lib,"wsock32.lib")
#pragma comment(lib,"d_nkernel.lib")
/**/ ///-链接库
///----------------------------------------------------------------------------

///----------------------------------------------------------------------------
///+Application

int main( int argc, const char ** argv)
... {
/**////创建KernelServer
nKernelServer*ks=n_new(nKernelServer);

/**////----------------------------------------------------------------------------
///+获得FileServer2的实例
///@note:有以下四种方式。在一般情况下他们是等价的

nFileServer2*pFileServer=NULL;
//1.通过Singleton获得
pFileServer=nFileServer2::Instance();
//2.通过KernelServer直接获得
pFileServer=ks->GetFileServer();
//3.通过NOH体系获得
pFileServer=(nFileServer2*)ks->Lookup("sys/servers/file2");
//4.通过nAutoRef获得(同NOH体系获得法)
nAutoRef<nFileServer2>refFileServer("sys/servers/file2");
if(refFileServer.isvalid())
...{
pFileServer
=refFileServer.get();
}

/**////-获得FileServer2的实例
///----------------------------------------------------------------------------


ks
->Print("*****AssignsOperation***** ");
/**////----------------------------------------------------------------------------
///+Assigns
///@note:
///1.Assigns是一种以标识替代绝对路径的技术,它对于应用程序的资源部署非常有用
///2.Assigns的变量都以nEnv类型存储在NOH体系的/sys/share/assigns/位置下
///3./sys/share/assigns/home和/sys/share/assigns/bin是两个默认存在的变量,
///分别是应用程序根和执行文件所在路径。
///4.在设置完Assign路径后就可以通过"assign:filename"的形式来索引文件了。
///

//两个默认的assigns
nStringhome=pFileServer->GetAssign("home");
nStringbin
=pFileServer->GetAssign("bin");
nStringuser
=pFileServer->GetAssign("user");
nStringtemp
=pFileServer->GetAssign("temp");
ks
->Print("home: %s ",home.Get());
ks
->Print("bin: %s ",bin.Get());
ks
->Print("user: %s ",user.Get());
ks
->Print("temp: %s ",temp.Get());
//设置proj索引
pFileServer->SetAssign("proj","d:/");
nStringproj
=pFileServer->GetAssign("proj");
ks
->Print("proj: %s ",proj.Get());
/**////
///-Assigns
///----------------------------------------------------------------------------


ks
->Print("*****FileW/ROperation***** ");
/**////----------------------------------------------------------------------------
///+FileOperation
///


//getnFileobject
nFile*file=pFileServer->NewFileObject();

//openfileforwrite
file->Open("bin:T02.txt","w");
//writefile
nStringoutput("T02 FileWriteTest!");
file
->Write(output.Get(),output.Length());
//closefileforwrite
file->Close();

//openfileforread
file->Open("bin:T02.txt","r");
//readfilecontent
charbuf[512];
intreadCount=file->Read(buf,sizeof(buf));
buf[readCount]
=0;
//logcontent
nStringabsPath=pFileServer->ManglePath("bin:T02.txt");
ks
->Print("ReadFilefrom:%s ",absPath.Get());
ks
->Print("FileContent: %s",buf);
//closefileforread
file->Close();

//releasenFileobject
file->Release();

/**////
///-FileOperation
///----------------------------------------------------------------------------

///销毁KernelServer

n_delete(ks);

getchar();
return0;
}

/**/ ///-Application
///----------------------------------------------------------------------------

你可能感兴趣的:(server)