mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-23 00:01:21 -04:00
Added first version of RPC system for external control of retroshare-nogui.
The protocol message format is as follows: [HEADER: 16 bytes: 4 x Network Order uint32_t][ VARIABLE LENGTH BODY ] [ MAGIC_CODE ] [ MSG_ID ] [ REQ_ID ] [ BODY_SIZE ] [ ........ BODY ......... ] MagicCode = 0x137f0001 ... this will be incremented for new versions of the protocol. MsgID = Corresponds to the format of the Body. ReqID = Generated by Requester, Returned in Response, make sure its unique. (undefined behaviour for duplicates) BodySize = Byte Length of Body. The Body will consist of a protobuf encoded message. For the moment, the RPC server just ECHOs the request back to the sender - for testing purposes. Usage: * Create SSH connection to retroshare-nogui. * Create Request Message(s), and send over SSH channel - You can send as meny requests as you want. * They will processed, and responses sent back (potentially in an arbitary order). Specific Changes here: * Modified rssshd to support arbitary recv/send applications. (interface is RpcComms). * Added rpc directory, with server, setup and echo service. * Modified Menu System to use the new interface to rssshd * Wrote new matching interface for Terminal Usage. - NOTE: Strange BUG in Terminal version.... causes stderr to disappear. TODO. * Added -C commandline option to switch on RPC system. This is the first version - so I expect there will be bugs. Please report for a prompt fix! git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-gxs-b1@5444 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
c17460d1b1
commit
bb10b6b400
20 changed files with 1612 additions and 72 deletions
289
retroshare-nogui/src/rpc/rpc.cc
Normal file
289
retroshare-nogui/src/rpc/rpc.cc
Normal file
|
@ -0,0 +1,289 @@
|
|||
/*
|
||||
* RetroShare External Interface.
|
||||
*
|
||||
* Copyright 2012-2012 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2.1 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rpc/rpc.h"
|
||||
#include "rpc/rpcserver.h"
|
||||
|
||||
// This one is inside libretroshare (BAD)!!!
|
||||
#include "serialiser/rsbaseserial.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
const uint32_t kMsgHeaderSize = 16;
|
||||
const uint32_t kMsgMagicCode = 0x137f0001; // Arbitary + 0x0001
|
||||
|
||||
RpcMediator::RpcMediator(RpcComms *c)
|
||||
:mComms(c), mServer(NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void RpcMediator::reset()
|
||||
{
|
||||
mServer->reset();
|
||||
}
|
||||
|
||||
|
||||
int RpcMediator::tick()
|
||||
{
|
||||
bool worked = false;
|
||||
if (recv())
|
||||
{
|
||||
worked = true;
|
||||
}
|
||||
|
||||
if (mServer->checkPending())
|
||||
{
|
||||
worked = true;
|
||||
}
|
||||
|
||||
if (worked)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int RpcMediator::recv()
|
||||
{
|
||||
int recvd = 0;
|
||||
while(recv_msg())
|
||||
{
|
||||
recvd = 1;
|
||||
}
|
||||
return recvd;
|
||||
}
|
||||
|
||||
|
||||
int RpcMediator::recv_msg()
|
||||
{
|
||||
/* nothing in here needs a Mutex... */
|
||||
|
||||
if (!mComms->recv_ready())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cerr << "RpcMediator::recv_msg() Data Ready";
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* read in data */
|
||||
uint8_t buffer[kMsgHeaderSize];
|
||||
uint32_t bufsize = kMsgHeaderSize;
|
||||
uint32_t msg_id;
|
||||
uint32_t req_id;
|
||||
uint32_t msg_size;
|
||||
std::string msg_body;
|
||||
|
||||
std::cerr << "RpcMediator::recv_msg() get Header: " << bufsize;
|
||||
std::cerr << " bytes" << std::endl;
|
||||
|
||||
int read = mComms->recv_blocking(buffer, bufsize);
|
||||
if (read != bufsize)
|
||||
{
|
||||
/* error */
|
||||
std::cerr << "RpcMediator::recv_msg() Error Reading Header: " << bufsize;
|
||||
std::cerr << " bytes" << std::endl;
|
||||
|
||||
mComms->error("Failed to Recv Header");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!MsgPacker::deserialiseHeader(msg_id, req_id, msg_size, buffer, bufsize))
|
||||
{
|
||||
/* error */
|
||||
std::cerr << "RpcMediator::recv_msg() Error Deserialising Header";
|
||||
std::cerr << std::endl;
|
||||
|
||||
mComms->error("Failed to Deserialise Header");
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cerr << "RpcMediator::recv_msg() MsgId: " << msg_id;
|
||||
std::cerr << " ReqId: " << req_id;
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::cerr << "RpcMediator::recv_msg() get Body: " << msg_size;
|
||||
std::cerr << " bytes" << std::endl;
|
||||
|
||||
/* grab real size */
|
||||
read = mComms->recv_blocking(msg_body, msg_size);
|
||||
if (read != msg_size)
|
||||
{
|
||||
/* error */
|
||||
std::cerr << "RpcMediator::recv_msg() Error Reading Body: " << bufsize;
|
||||
std::cerr << " bytes" << std::endl;
|
||||
|
||||
mComms->error("Failed to Recv MsgBody");
|
||||
return 0;
|
||||
}
|
||||
mServer->processMsg(msg_id, req_id, msg_body);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int RpcMediator::send(uint32_t msg_id, uint32_t req_id, const std::string &msg)
|
||||
|
||||
{
|
||||
std::cerr << "RpcMediator::send(" << msg_id << "," << req_id << ", len(";
|
||||
std::cerr << msg.size() << "))";
|
||||
std::cerr << std::endl;
|
||||
|
||||
uint8_t buffer[kMsgHeaderSize];
|
||||
uint32_t bufsize = kMsgHeaderSize;
|
||||
uint32_t msg_size = msg.size();
|
||||
|
||||
bool okay = MsgPacker::serialiseHeader(msg_id, req_id, msg_size, buffer, bufsize);
|
||||
if (!okay)
|
||||
{
|
||||
std::cerr << "RpcMediator::send() SerialiseHeader Failed";
|
||||
std::cerr << std::endl;
|
||||
/* error */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!mComms->send(buffer, bufsize))
|
||||
{
|
||||
std::cerr << "RpcMediator::send() Send Header Failed";
|
||||
std::cerr << std::endl;
|
||||
/* error */
|
||||
mComms->error("Failed to Send Header");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* now send the body */
|
||||
if (!mComms->send(msg))
|
||||
{
|
||||
std::cerr << "RpcMediator::send() Send Body Failed";
|
||||
std::cerr << std::endl;
|
||||
/* error */
|
||||
mComms->error("Failed to Send Msg");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/* Msg Packing */
|
||||
int MsgPacker::headersize()
|
||||
{
|
||||
return kMsgHeaderSize;
|
||||
}
|
||||
|
||||
#if 0
|
||||
int MsgPacker::msgsize(Message *msg)
|
||||
{
|
||||
/* */
|
||||
return 0;
|
||||
}
|
||||
|
||||
int MsgPacker::pktsize(Message *msg)
|
||||
{
|
||||
/* */
|
||||
return headersize() + msgsize();
|
||||
}
|
||||
#endif
|
||||
|
||||
bool MsgPacker::serialiseHeader(uint32_t msg_id, uint32_t req_id, uint32_t msg_size, uint8_t *buffer, uint32_t bufsize)
|
||||
{
|
||||
/* check size */
|
||||
if (bufsize < kMsgHeaderSize)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* pack the data (using libretroshare serialiser for now */
|
||||
void *data = buffer;
|
||||
uint32_t offset = 0;
|
||||
uint32_t size = bufsize;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
/* 4 x uint32_t for header */
|
||||
ok &= setRawUInt32(data, size, &offset, kMsgMagicCode);
|
||||
ok &= setRawUInt32(data, size, &offset, msg_id);
|
||||
ok &= setRawUInt32(data, size, &offset, req_id);
|
||||
ok &= setRawUInt32(data, size, &offset, msg_size);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
bool MsgPacker::deserialiseHeader(uint32_t &msg_id, uint32_t &req_id, uint32_t &msg_size, uint8_t *buffer, uint32_t bufsize)
|
||||
{
|
||||
/* check size */
|
||||
if (bufsize < kMsgHeaderSize)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* pack the data (using libretroshare serialiser for now */
|
||||
void *data = buffer;
|
||||
uint32_t offset = 0;
|
||||
uint32_t size = bufsize;
|
||||
uint32_t magic_code;
|
||||
|
||||
bool ok = true;
|
||||
|
||||
/* 4 x uint32_t for header */
|
||||
ok &= getRawUInt32(data, size, &offset, &magic_code);
|
||||
if (!ok)
|
||||
{
|
||||
std::cerr << "Failed to deserialise uint32_t(0)";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
ok &= getRawUInt32(data, size, &offset, &msg_id);
|
||||
if (!ok)
|
||||
{
|
||||
std::cerr << "Failed to deserialise uint32_t(1)";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
ok &= getRawUInt32(data, size, &offset, &req_id);
|
||||
if (!ok)
|
||||
{
|
||||
std::cerr << "Failed to deserialise uint32_t(2)";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
ok &= getRawUInt32(data, size, &offset, &msg_size);
|
||||
if (!ok)
|
||||
{
|
||||
std::cerr << "Failed to deserialise uint32_t(3)";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
ok &= (magic_code == kMsgMagicCode);
|
||||
if (!ok)
|
||||
{
|
||||
std::cerr << "Failed to Match MagicCode";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
|
71
retroshare-nogui/src/rpc/rpc.h
Normal file
71
retroshare-nogui/src/rpc/rpc.h
Normal file
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* RetroShare External Interface.
|
||||
*
|
||||
* Copyright 2012-2012 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2.1 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef RPC_MEDIATOR_H
|
||||
#define RPC_MEDIATOR_H
|
||||
|
||||
/*
|
||||
* Interface between RpcServer and RpcComms.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "rpcsystem.h"
|
||||
|
||||
class RpcServer;
|
||||
|
||||
class RpcMediator: public RpcSystem
|
||||
{
|
||||
public:
|
||||
|
||||
RpcMediator(RpcComms *c);
|
||||
void setRpcServer(RpcServer *s) { mServer = s; } /* Must only be called during setup */
|
||||
|
||||
// Overloaded from RpcSystem.
|
||||
virtual void reset();
|
||||
virtual int tick();
|
||||
|
||||
int recv();
|
||||
int recv_msg();
|
||||
int send(uint32_t msg_id, uint32_t req_id, const std::string &msg);
|
||||
|
||||
private:
|
||||
RpcComms *mComms;
|
||||
RpcServer *mServer;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/* Msg Packing */
|
||||
class MsgPacker
|
||||
{
|
||||
public:
|
||||
static int headersize();
|
||||
static bool serialiseHeader(uint32_t msg_id, uint32_t req_id, uint32_t msg_size, uint8_t *buffer, uint32_t bufsize);
|
||||
static bool deserialiseHeader(uint32_t &msg_id, uint32_t &req_id, uint32_t &msg_size, uint8_t *buffer, uint32_t bufsize);
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif /* RPC_MEDIATOR_H */
|
40
retroshare-nogui/src/rpc/rpcecho.cc
Normal file
40
retroshare-nogui/src/rpc/rpcecho.cc
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* RetroShare External Interface.
|
||||
*
|
||||
* Copyright 2012-2012 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2.1 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rpc/rpcecho.h"
|
||||
|
||||
RpcEcho::RpcEcho(uint32_t serviceId)
|
||||
:RpcQueueService(serviceId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int RpcEcho::processMsg(uint32_t msg_id, uint32_t req_id, const std::string &msg)
|
||||
{
|
||||
/* */
|
||||
queueResponse(msg_id, req_id, msg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
38
retroshare-nogui/src/rpc/rpcecho.h
Normal file
38
retroshare-nogui/src/rpc/rpcecho.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* RetroShare External Interface.
|
||||
*
|
||||
* Copyright 2012-2012 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2.1 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef RS_RPC_ECHO_H
|
||||
#define RS_RPC_ECHO_H
|
||||
|
||||
#include "rpc/rpcserver.h"
|
||||
|
||||
class RpcEcho: public RpcQueueService
|
||||
{
|
||||
public:
|
||||
RpcEcho(uint32_t serviceId);
|
||||
virtual int processMsg(uint32_t msgId, uint32_t req_id, const std::string &msg);
|
||||
};
|
||||
|
||||
|
||||
#endif /* RS_RPC_ECHO_H */
|
227
retroshare-nogui/src/rpc/rpcserver.cc
Normal file
227
retroshare-nogui/src/rpc/rpcserver.cc
Normal file
|
@ -0,0 +1,227 @@
|
|||
/*
|
||||
* RetroShare External Interface.
|
||||
*
|
||||
* Copyright 2012-2012 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2.1 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rpc/rpcserver.h"
|
||||
|
||||
#include "rpc/rpc.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
RpcServer::RpcServer(RpcMediator *med)
|
||||
:mMediator(med), mRpcMtx("RpcMtx")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void RpcServer::reset()
|
||||
{
|
||||
std::cerr << "RpcServer::reset()" << std::endl;
|
||||
{
|
||||
RsStackMutex stack(mRpcMtx); /********** LOCKED MUTEX ***************/
|
||||
|
||||
std::list<RpcService *>::iterator it;
|
||||
for(it = mAllServices.begin(); it != mAllServices.end(); it++)
|
||||
{
|
||||
/* in mutex, but should be okay */
|
||||
(*it)->reset();
|
||||
}
|
||||
|
||||
// clear existing queue.
|
||||
mRpcQueue.clear();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int RpcServer::addService(RpcService *service)
|
||||
{
|
||||
RsStackMutex stack(mRpcMtx); /********** LOCKED MUTEX ***************/
|
||||
|
||||
mAllServices.push_back(service);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int RpcServer::processMsg(uint32_t msg_id, uint32_t req_id, const std::string &msg)
|
||||
{
|
||||
std::cerr << "RpcServer::processMsg(" << msg_id << "," << req_id;
|
||||
std::cerr << ", len(" << msg.size() << "))" << std::endl;
|
||||
{
|
||||
RsStackMutex stack(mRpcMtx); /********** LOCKED MUTEX ***************/
|
||||
|
||||
std::list<RpcService *>::iterator it;
|
||||
for(it = mAllServices.begin(); it != mAllServices.end(); it++)
|
||||
{
|
||||
int rt = (*it)->processMsg(msg_id, req_id, msg);
|
||||
if (!rt)
|
||||
continue;
|
||||
|
||||
/* remember request */
|
||||
queueRequest_locked(msg_id, req_id, (*it));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::cerr << "RpcServer::processMsg() No service to accepted it - discard";
|
||||
std::cerr << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RpcServer::queueRequest_locked(uint32_t /* msgId */, uint32_t req_id, RpcService *service)
|
||||
{
|
||||
std::cerr << "RpcServer::queueRequest_locked() req_id: " << req_id;
|
||||
std::cerr << std::endl;
|
||||
|
||||
RpcQueuedObj obj;
|
||||
obj.mReqId = req_id;
|
||||
obj.mService = service;
|
||||
|
||||
mRpcQueue.push_back(obj);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
bool RpcServer::checkPending()
|
||||
{
|
||||
std::list<RpcQueuedMsg> msgsToSend;
|
||||
bool someRemaining = false;
|
||||
bool someToSend = false;
|
||||
|
||||
{
|
||||
RsStackMutex stack(mRpcMtx); /********** LOCKED MUTEX ***************/
|
||||
|
||||
std::list<RpcQueuedObj>::iterator it;
|
||||
for(it = mRpcQueue.begin(); it != mRpcQueue.end();)
|
||||
{
|
||||
uint32_t out_msg_id = 0;
|
||||
uint32_t out_req_id = it->mReqId;
|
||||
std::string out_msg;
|
||||
if (it->mService->getResponse(out_msg_id, out_req_id, out_msg))
|
||||
{
|
||||
std::cerr << "RpcServer::checkPending() Response: (";
|
||||
std::cerr << out_msg_id << "," << out_req_id;
|
||||
std::cerr << ", len(" << out_msg.size() << "))";
|
||||
std::cerr << std::endl;
|
||||
|
||||
/* store and send after queue is processed */
|
||||
RpcQueuedMsg msg;
|
||||
msg.mMsgId = out_msg_id;
|
||||
msg.mReqId = out_req_id;
|
||||
msg.mMsg = out_msg;
|
||||
|
||||
msgsToSend.push_back(msg);
|
||||
|
||||
it = mRpcQueue.erase(it);
|
||||
someToSend = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
someRemaining = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (someToSend)
|
||||
{
|
||||
sendQueuedMsgs(msgsToSend);
|
||||
}
|
||||
|
||||
return someRemaining;
|
||||
}
|
||||
|
||||
bool RpcServer::sendQueuedMsgs(std::list<RpcQueuedMsg> &msgs)
|
||||
{
|
||||
/* No need for lock, as mOut is the only accessed item - and that has own protection */
|
||||
|
||||
std::cerr << "RpcServer::sendQueuedMsg() " << msgs.size() << " to send";
|
||||
std::cerr << std::endl;
|
||||
|
||||
std::list<RpcQueuedMsg>::iterator it;
|
||||
for (it = msgs.begin(); it != msgs.end(); it++)
|
||||
{
|
||||
mMediator->send(it->mMsgId, it->mReqId, it->mMsg);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
RpcQueueService::RpcQueueService(uint32_t serviceId)
|
||||
:RpcService(serviceId), mQueueMtx("RpcQueueService")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void RpcQueueService::reset()
|
||||
{
|
||||
RsStackMutex stack(mQueueMtx); /********** LOCKED MUTEX ***************/
|
||||
|
||||
// clear existing queue.
|
||||
mResponses.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
int RpcQueueService::getResponse(uint32_t &msg_id, uint32_t &req_id, std::string &msg)
|
||||
{
|
||||
RsStackMutex stack(mQueueMtx); /********** LOCKED MUTEX ***************/
|
||||
|
||||
std::map<uint32_t, RpcQueuedMsg>::iterator it;
|
||||
|
||||
it = mResponses.find(req_id);
|
||||
if (it == mResponses.end())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
msg_id = it->second.mMsgId;
|
||||
msg = it->second.mMsg;
|
||||
|
||||
mResponses.erase(it);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RpcQueueService::queueResponse(uint32_t msg_id, uint32_t req_id, const std::string &msg)
|
||||
{
|
||||
RsStackMutex stack(mQueueMtx); /********** LOCKED MUTEX ***************/
|
||||
|
||||
RpcQueuedMsg qmsg;
|
||||
qmsg.mMsgId = msg_id;
|
||||
qmsg.mReqId = req_id;
|
||||
qmsg.mMsg = msg;
|
||||
|
||||
mResponses[req_id] = qmsg;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
110
retroshare-nogui/src/rpc/rpcserver.h
Normal file
110
retroshare-nogui/src/rpc/rpcserver.h
Normal file
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* RetroShare External Interface.
|
||||
*
|
||||
* Copyright 2012-2012 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2.1 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef RS_RPC_SERVER_H
|
||||
#define RS_RPC_SERVER_H
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "util/rsthreads.h"
|
||||
|
||||
/*** This can be overloaded for plugins
|
||||
* Also allows a natural seperation of the full interface into sections.
|
||||
*/
|
||||
|
||||
class RpcQueuedMsg
|
||||
{
|
||||
public:
|
||||
uint32_t mMsgId;
|
||||
uint32_t mReqId;
|
||||
std::string mMsg;
|
||||
};
|
||||
|
||||
|
||||
class RpcService
|
||||
{
|
||||
public:
|
||||
RpcService(uint32_t /* serviceId */ ) { return; }
|
||||
virtual void reset() { return; }
|
||||
virtual int msgsAccepted(std::list<uint32_t> & /* msgIds */) { return 0; } /* not used at the moment */
|
||||
virtual int processMsg(uint32_t msgId, uint32_t req_id, const std::string &msg) = 0; /* returns 0 - not handled, > 0, accepted */
|
||||
virtual int getResponse(uint32_t &msgId, uint32_t &req_id, std::string &msg) = 0; /* 0 - not ready, > 0 heres the response */
|
||||
};
|
||||
|
||||
|
||||
/* Implements a Queue for quick implementation of Instant Response Services */
|
||||
class RpcQueueService: public RpcService
|
||||
{
|
||||
public:
|
||||
RpcQueueService(uint32_t serviceId);
|
||||
virtual void reset();
|
||||
virtual int getResponse(uint32_t &msgId, uint32_t &req_id, std::string &msg);
|
||||
|
||||
protected:
|
||||
int queueResponse(uint32_t msgId, uint32_t req_id, const std::string &msg);
|
||||
private:
|
||||
RsMutex mQueueMtx;
|
||||
std::map<uint32_t, RpcQueuedMsg> mResponses;
|
||||
};
|
||||
|
||||
|
||||
/* For Tracking responses */
|
||||
class RpcQueuedObj
|
||||
{
|
||||
public:
|
||||
uint32_t mReqId;
|
||||
RpcService *mService;
|
||||
};
|
||||
|
||||
|
||||
class RpcMediator;
|
||||
|
||||
class RpcServer
|
||||
{
|
||||
|
||||
public:
|
||||
RpcServer(RpcMediator *med);
|
||||
int addService(RpcService *service);
|
||||
int processMsg(uint32_t msgId, uint32_t req_id, const std::string &msg);
|
||||
bool checkPending();
|
||||
|
||||
void reset();
|
||||
|
||||
private:
|
||||
bool sendQueuedMsgs(std::list<RpcQueuedMsg> &msgs);
|
||||
int queueRequest_locked(uint32_t msgId, uint32_t req_id, RpcService *service);
|
||||
|
||||
RpcMediator *mMediator;
|
||||
|
||||
RsMutex mRpcMtx;
|
||||
|
||||
std::list<RpcQueuedObj> mRpcQueue;
|
||||
std::list<RpcService *> mAllServices;
|
||||
};
|
||||
|
||||
|
||||
#endif /* RS_RPC_SERVER_H */
|
43
retroshare-nogui/src/rpc/rpcsetup.cc
Normal file
43
retroshare-nogui/src/rpc/rpcsetup.cc
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* RetroShare External Interface.
|
||||
*
|
||||
* Copyright 2012-2012 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2.1 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rpc/rpcsetup.h"
|
||||
#include "rpc/rpcserver.h"
|
||||
|
||||
#include "rpc/rpcecho.h"
|
||||
|
||||
|
||||
RpcMediator *CreateRpcSystem(RpcComms *comms)
|
||||
{
|
||||
RpcMediator *med = new RpcMediator(comms);
|
||||
RpcServer *server = new RpcServer(med);
|
||||
|
||||
/* add services */
|
||||
RpcEcho *echo = new RpcEcho(1);
|
||||
server->addService(echo);
|
||||
|
||||
med->setRpcServer(server);
|
||||
|
||||
return med;
|
||||
}
|
||||
|
34
retroshare-nogui/src/rpc/rpcsetup.h
Normal file
34
retroshare-nogui/src/rpc/rpcsetup.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* RetroShare External Interface.
|
||||
*
|
||||
* Copyright 2012-2012 by Robert Fernie.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 2.1 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef RPC_SETUP_H
|
||||
#define RPC_SETUP_H
|
||||
|
||||
|
||||
#include "rpc/rpc.h"
|
||||
|
||||
RpcMediator *CreateRpcSystem(RpcComms *comms);
|
||||
|
||||
#endif
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue