Rhizome http server now allows specification of client socket callback

when starting, so that same server code can be shared for rhizome transfers
and rhizome direct. #9
This commit is contained in:
gardners 2012-08-28 13:56:47 +09:30
parent 2b29893a02
commit bde9d1c56a
5 changed files with 46 additions and 6 deletions

View File

@ -149,7 +149,12 @@ schedule(&_sched_##X); }
/* Get rhizome server started BEFORE populating fd list so that
the server's listen socket is in the list for poll() */
if (rhizome_enabled()) rhizome_http_server_start();
if (rhizome_enabled())
/* Rhizome http server needs to know which callback to attach
to client sockets, so provide it here, along with the name to
appear in time accounting statistics. */
rhizome_http_server_start(rhizome_client_poll,"rhizome_client_poll",
RHIZOME_HTTP_PORT,RHIZOME_HTTP_PORT_MAX);
/* Pick next rhizome files to grab every few seconds
from the priority list continuously being built from observed

View File

@ -43,6 +43,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define RHIZOME_HTTP_PORT 4110
#define RHIZOME_HTTP_PORT_MAX 4150
#define RHIZOME_DIRECT_PORT 4100
#define RHIZOME_DIRECT_PORT_MAX 4109
extern time_ms_t rhizome_voice_timeout;

View File

@ -106,11 +106,30 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "serval.h"
#include "rhizome.h"
void rhizome_direct_client_poll(struct sched_ent *alarm)
{
}
int app_rhizome_direct_server(int argc, const char *const *argv,
struct command_line_option *o)
{
/* Start Rhizome Direct server on configured port. */
/* Start Rhizome Direct server on configured port.
We re-use the Rhizome HTTP server code as much as possible here,
just replacing the server and client poll functions with our own.
*/
int port_low=confValueGetInt64Range("rhizome.direct.port",RHIZOME_DIRECT_PORT,
0,65535);
int port_high=confValueGetInt64Range("rhizome.direct.port_max",RHIZOME_DIRECT_PORT_MAX,
port_low,65535);
int r=rhizome_http_server_start(rhizome_direct_client_poll,
"rhizome_direct_client_poll",
port_low,port_high);
return -1;
}

View File

@ -87,13 +87,18 @@ struct profile_total server_stats;
struct profile_total connection_stats;
/*
HTTP server and client code for rhizome transfers.
HTTP server and client code for rhizome transfers and rhizome direct.
Selection of either use is made when starting the HTTP server and
specifying the call-back function to use on client connections.
*/
unsigned short rhizome_http_server_port = 0;
static int rhizome_server_socket = -1;
static time_ms_t rhizome_server_last_start_attempt = -1;
void (*rhizome_http_client_poll_func)(struct sched_ent *)=NULL;
const char *rhizome_http_client_poll_func_description="(null)";
// Format icon data using:
// od -vt u1 ~/Downloads/favicon.ico | cut -c9- | sed 's/ */,/g'
unsigned char favicon_bytes[]={
@ -132,7 +137,9 @@ int rhizome_http_server_running()
Return 1 if the server is already started successfully.
Return 2 if the server was not started because it is too soon since last failed attempt.
*/
int rhizome_http_server_start()
int rhizome_http_server_start(void (*client_poll_func)(struct sched_ent *),
const char *client_poll_func_desc,
int port_low,int port_high)
{
if (rhizome_server_socket != -1)
return 1;
@ -146,7 +153,7 @@ int rhizome_http_server_start()
DEBUGF("Starting rhizome HTTP server");
unsigned short port;
for (port = RHIZOME_HTTP_PORT; port <= RHIZOME_HTTP_PORT_MAX; ++port) {
for (port = port_low; port <= port_high; ++port) {
/* Create a new socket, reusable and non-blocking. */
if (rhizome_server_socket == -1) {
rhizome_server_socket = socket(AF_INET,SOCK_STREAM,0);
@ -200,6 +207,11 @@ error:
success:
INFOF("RHIZOME HTTP SERVER, START port=%d fd=%d", port, rhizome_server_socket);
/* Remember which function to call when handling client connections */
rhizome_http_client_poll_func=client_poll_func;
rhizome_http_client_poll_func_description=client_poll_func_desc;
rhizome_http_server_port = port;
/* Add Rhizome HTTPd server to list of file descriptors to watch */
server_alarm.function = rhizome_server_poll;

View File

@ -1124,7 +1124,9 @@ extern int sigIoFlag;
void sigPipeHandler(int signal);
void sigIoHandler(int signal);
int rhizome_http_server_start();
int rhizome_http_server_start(void (*server_poll_func)(struct sched_ent *),
const char *server_poll_func_description,
int port_low,int port_high);
int overlay_mdp_setup_sockets();
int schedule(struct sched_ent *alarm);