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:
drbob 2012-08-20 14:59:41 +00:00
parent c17460d1b1
commit bb10b6b400
20 changed files with 1612 additions and 72 deletions

View file

@ -1,3 +1,25 @@
/*
* 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 "menu/menu.h"
#include <retroshare/rsconfig.h>
@ -8,6 +30,8 @@
#include "util/rsstring.h"
#define MENU_DEBUG 1
/**********************************************************
* Menu Base Interface.
*/
@ -18,29 +42,89 @@ void MenuInterface::reset()
mBase->reset();
mCurrentMenu = mBase;
mInputRequired = false;
mUpdateTime = 0;
}
int MenuInterface::tick(bool haveInput, char keypress, std::string &output)
int MenuInterface::tick()
{
if (!haveInput)
#ifdef MENU_DEBUG
std::cerr << "MenuInterface::tick()";
std::cerr << std::endl;
#endif // MENU_DEBUG
/* try to read a char */
bool haveInput = false;
uint8_t keypress;
std::string output;
int read = mComms->recv(&keypress, 1);
#ifdef MENU_DEBUG
std::cerr << "MenuInterface::tick() read " << read << " bytes";
std::cerr << std::endl;
#endif // MENU_DEBUG
if (read == 0)
{
haveInput = false;
/* make a harmless key */
keypress = ' ';
}
if ((mInputRequired) && (!haveInput))
else if (read == 1)
{
return 1;
haveInput = true;
}
else
{
/* error, NON BLOCKING is handled by recv returning 0 */
mComms->error("Bad Input");
return -1;
}
/**** Main logic bit ****/
/**** slow down the updates / refresh ****/
time_t now = time(NULL);
#define UPDATE_TIME 5
if (!haveInput)
{
// If Input is Required,
if (mInputRequired)
{
std::cerr << "MenuInterface::tick() No Input & Required-No Output";
std::cerr << std::endl;
return 0;
}
// Output will just almost the same, so occasionally.
if (now < mUpdateTime + UPDATE_TIME)
{
std::cerr << "MenuInterface::tick() No Input-Slow Update";
std::cerr << std::endl;
return 0;
}
std::cerr << "MenuInterface::tick() No Input - but doing update.";
std::cerr << std::endl;
}
uint32_t rt = process(keypress, mDrawFlags, output);
mInputRequired = (rt == MENU_PROCESS_NEEDDATA);
mUpdateTime = now;
if (rt == MENU_PROCESS_QUIT)
{
return -1;
}
return 1;
if (output.size() > 0)
{
mComms->send(output);
}
return (haveInput);
}
@ -168,6 +252,9 @@ uint32_t MenuInterface::process(char key, uint32_t drawFlags, std::string &buffe
return MENU_PROCESS_NONE;
}
uint32_t MenuInterface::drawHeader(uint32_t drawFlags, std::string &buffer)
{
buffer += "=======================================================\r\n";

View file

@ -1,3 +1,28 @@
/*
* 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 RSNOGUI_MENU_H
#define RSNOGUI_MENU_H
@ -7,7 +32,7 @@
#include <map>
#include <list>
#include "rstermserver.h" // generic processing command.
#include "rpcsystem.h" // generic processing command.
#define MENU_PROCESS_MASK 0x0fff
@ -172,7 +197,7 @@ protected:
};
#if 0
class MenuInterface: public RsTermServer
{
public:
@ -193,5 +218,32 @@ private:
bool mInputRequired;
};
#endif
class MenuInterface: public RpcSystem
{
public:
MenuInterface(RpcComms *c, Menu *b, uint32_t drawFlags)
:mComms(c), mCurrentMenu(b), mBase(b), mDrawFlags(drawFlags), mInputRequired(false) { return; }
uint32_t process(char key, uint32_t drawFlags, std::string &buffer);
uint32_t drawHeader(uint32_t drawFlags, std::string &buffer);
// RsSystem Interface.
virtual void reset();
virtual int tick();
private:
RpcComms *mComms;
Menu *mCurrentMenu;
Menu *mBase;
uint32_t mDrawFlags;
bool mInputRequired;
time_t mUpdateTime;
};
#endif

View file

@ -1,3 +1,25 @@
/*
* 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 <retroshare/rspeers.h>
#include <retroshare/rsfiles.h>

View file

@ -1,3 +1,25 @@
/*
* 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 "menu/menu.h"
#include <retroshare/rsturtle.h>

View file

@ -0,0 +1,171 @@
/*
* 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 "menu/stdiocomms.h"
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
StdioComms::StdioComms(int infd, int outfd)
:mIn(infd), mOut(outfd)
{
#if 1
// THIS Code is strange...
// It seems to mess up stderr.
// But if you redirect it -> is comes out fine. Very Weird.
// HELP!!!
std::cerr << "StdioComms() STDERR msg 0";
std::cerr << std::endl;
const int fcflags = fcntl(mIn,F_GETFL);
if (fcflags < 0)
{
std::cerr << "StdioComms() ERROR getting fcntl FLAGS";
std::cerr << std::endl;
exit(1);
}
if (fcntl(mIn,F_SETFL,fcflags | O_NONBLOCK) <0)
{
std::cerr << "StdioComms() ERROR setting fcntl FLAGS";
std::cerr << std::endl;
exit(1);
}
std::cerr << "StdioComms() STDERR msg 1";
std::cerr << std::endl;
#endif
}
int StdioComms::isOkay()
{
return 1;
}
int StdioComms::error(std::string msg)
{
std::cerr << "StdioComms::error(" << msg << ")";
std::cerr << std::endl;
return 1;
}
int StdioComms::recv_ready()
{
/* should be proper poll / select! - but we don't use this at the moment */
return 1;
}
int StdioComms::recv(uint8_t *buffer, int bytes)
{
int size = read(mIn, buffer, bytes);
std::cerr << "StdioComms::recv() returned: " << size;
std::cerr << std::endl;
if (size == -1)
{
std::cerr << "StdioComms::recv() ERROR: " << errno;
std::cerr << std::endl;
if (errno == EAGAIN)
{
return 0; // OKAY;
}
}
return size;
}
int StdioComms::recv(std::string &buffer, int bytes)
{
uint8_t tmpbuffer[bytes];
int size = read(mIn, tmpbuffer, bytes);
for(int i = 0; i < size; i++)
{
buffer += tmpbuffer[i];
}
return size;
}
// these make it easier...
int StdioComms::recv_blocking(uint8_t *buffer, int bytes)
{
int totalread = 0;
while(totalread < bytes)
{
int size = read(mIn, &(buffer[totalread]), bytes - totalread);
if (size < 0)
{
if (totalread)
break; // partial read.
else
return size; // error.
}
totalread += size;
usleep(1000); // minisleep - so we don't 100% CPU.
std::cerr << "StdioComms::recv_blocking() read so far: " << size;
std::cerr << " / " << totalread;
std::cerr << std::endl;
}
return totalread;
}
int StdioComms::recv_blocking(std::string &buffer, int bytes)
{
uint8_t tmpbuffer[bytes];
int size = recv_blocking(tmpbuffer, bytes);
if (size < 0)
return size; // error.
for(int i = 0; i < size; i++)
buffer += tmpbuffer[i];
return size;
}
int StdioComms::send(uint8_t *buffer, int bytes)
{
write(mOut, buffer, bytes);
return bytes;
}
int StdioComms::send(const std::string &output)
{
if (output.size() > 0)
{
write(mOut, output.c_str(), output.size());
}
return output.size();
}

View file

@ -0,0 +1,57 @@
/*
* 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_STDIO_COMMS_H
#define RS_STDIO_COMMS_H
#include "rpcsystem.h"
class StdioComms: public RpcComms
{
public:
StdioComms(int infd, int outfd);
virtual int isOkay();
virtual int error(std::string msg);
virtual int recv_ready();
virtual int recv(uint8_t *buffer, int bytes);
virtual int recv(std::string &buffer, int bytes);
// these make it easier...
virtual int recv_blocking(uint8_t *buffer, int bytes);
virtual int recv_blocking(std::string &buffer, int bytes);
virtual int send(uint8_t *buffer, int bytes);
virtual int send(const std::string &buffer);
private:
int mIn, mOut;
};
#endif