Reference: Added Mt Rpc Client/Server

git-svn-id: svn://ultimatepp.org/upp/trunk@5556 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
unodgs 2012-11-17 15:58:35 +00:00
parent 2087db62fb
commit a9b5a6d19c
6 changed files with 195 additions and 0 deletions

View file

@ -0,0 +1,34 @@
#include <Core/Core.h>
#include <Core/Rpc/Rpc.h>
using namespace Upp;
void GetClientList(int i)
{
INTERLOCKED { LOG("=>> GetClientList(" << i << ")"); }
ValueArray result;
XmlRpcRequest call("127.0.0.1:1234");
if(call("GetClientList") >> result)
{
INTERLOCKED { LOG("Message received " << i); }
}
else
{
INTERLOCKED { LOG("Error: " << call.GetError()); }
}
INTERLOCKED { LOG("<<= GetClientList(" << i << ")"); }
}
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT|LOG_FILE);
CoWork co;
for(int i = 0; i < 20; i++)
{
co & callback1(GetClientList, i);
}
}

View file

@ -0,0 +1,12 @@
description "Multi thread rpc client\377";
uses
Core,
Core/Rpc;
file
MtRpcClient.cpp;
mainconfig
"" = "MT";

View file

@ -0,0 +1,5 @@
#ifndef _MtRpcClient_icpp_init_stub
#define _MtRpcClient_icpp_init_stub
#include "Core/init"
#include "Core/Rpc/init"
#endif

View file

@ -0,0 +1,127 @@
#include <Core/Core.h>
#include <Core/Rpc/Rpc.h>
#ifdef PLATFORM_WIN32
#include <wincon.h>
#endif
using namespace Upp;
RPC_METHOD(GetClientList)
{
INTERLOCKED { LOG("GetClientList()"); }
ValueArray clients;
for(int i = 0; i < 1000; i++)
{
ValueMap client;
client.Add("firstName", Format("First Name %d", i));
client.Add("lastName", Format("Last Name %d", i));
client.Add("birthday", GetSysDate());
clients.Add(client);
}
Sleep(1000);
rpc << clients;
INTERLOCKED { LOG("Done."); }
}
class MtRpcServer
{
private:
TcpSocket rpc;
int threadPoolSize;
int port;
bool quit;
static MtRpcServer *server;
void Process(int n)
{
INTERLOCKED { LOG("Process " << n << " started"); }
Mutex accept_mutex;
for(;;)
{
TcpSocket http;
accept_mutex.Enter();
if(quit)
{
accept_mutex.Leave();
break;
}
bool b = http.Accept(rpc);
accept_mutex.Leave();
if(quit)
break;
if(b)
{
INTERLOCKED { LOG("Request accepted " << n); }
RpcPerform(http, NULL);
}
else
{
INTERLOCKED { LOG("Request rejected " << n); }
break;
}
}
}
#ifdef PLATFORM_WIN32
static BOOL WINAPI CtrlCHandlerRoutine(__in DWORD dwCtrlType)
{
LOG("Ctrl+C handler");
server->quit = true;
for(int i = 0; i < server->threadPoolSize; i++)
{
TcpSocket h;
h.Connect("127.0.0.1", server->port);
h.Put("quit");
}
return TRUE;
}
#endif
public:
MtRpcServer(int port = 1234, int poolSize = 8)
{
server = this;
#ifdef PLATFORM_WIN32
SetConsoleCtrlHandler(CtrlCHandlerRoutine, true);
#endif
this->threadPoolSize = poolSize;
this->port = port;
this->quit = false;
}
void Run()
{
rpc.Timeout(Null);
if(!rpc.Listen(port, 5))
Exit(1);
for(int i = 0; i < threadPoolSize; i++)
Thread::Start(callback1(this, &MtRpcServer::Process, i));
while(Thread::GetCount())
Sleep(100);
}
};
MtRpcServer *MtRpcServer::server = NULL;
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT|LOG_FILE);
LOG("MtRpcServer..");
LogRpcRequests();
MtRpcServer server;
server.Run();
}

View file

@ -0,0 +1,12 @@
description "Multi thread rpc server\377";
uses
Core,
Core/Rpc;
file
MtRpcServer.cpp;
mainconfig
"" = "MT";

View file

@ -0,0 +1,5 @@
#ifndef _MtRpcServer_icpp_init_stub
#define _MtRpcServer_icpp_init_stub
#include "Core/init"
#include "Core/Rpc/init"
#endif