removed test code

This commit is contained in:
Jean-Francois Dockes 2016-11-08 12:40:15 +01:00
parent 41dfd40fba
commit ac7afffacb

View File

@ -17,7 +17,6 @@
// Wrapper classes for the socket interface
#ifndef TEST_NETCON
#ifdef BUILDING_RECOLL
#include "autoconfig.h"
#else
@ -1018,319 +1017,3 @@ NetconServLis::checkperms(void *cl, int)
return -1;
}
#endif /* NETCON_ACCESSCONTROL */
#else /* !TEST_NETCON */
/////////////////////////////////////////////////////////////////////////
////////// TEST DRIVER
////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include "log.h"
#include "netcon.h"
using namespace std;
static char *thisprog;
static char usage[] =
"-c <host> <service>: Connects to trnetcon server, exchange message, then\n"
" sleeps 10 S, except if option -n is given (sleep forever)\n"
"\n"
"-s <service>: open service <service>\n"
;
static void
Usage()
{
fprintf(stderr, "Usage : %s:\n %s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_s 0x2 /* Server */
#define OPT_c 0x4 /* Client */
#define OPT_n 0x8 /* Client sleeps forever */
extern int trycli(char *host, char *serv);
extern int tryserv(char *serv);
int nloop = 10;
int main(int argc, char **argv)
{
char *host, *serv;
thisprog = argv[0];
argc--;
argv++;
while (argc > 0 && **argv == '-') {
(*argv)++;
if (!(**argv))
/* Cas du "adb - core" */
{
Usage();
}
while (**argv)
switch (*(*argv)++) {
case 's':
op_flags |= OPT_s;
break;
case 'c':
op_flags |= OPT_c;
break;
case 'n':
op_flags |= OPT_n;
break;
default:
Usage();
break;
}
argc--;
argv++;
}
Logger::getTheLog("")->setLogLevel(Logger::LLDEB2);
if (op_flags & OPT_c) {
if (argc != 2) {
Usage();
}
host = *argv++;
argc--;
serv = *argv++;
argc--;
exit(trycli(host, serv));
} else if (op_flags & OPT_s) {
if (argc != 1) {
Usage();
}
serv = *argv++;
argc--;
exit(tryserv(serv));
} else {
Usage();
}
}
static char buf[1024];
static int buflen = 1023;
static char fromcli[200];
class CliNetconWorker : public NetconWorker {
public:
CliNetconWorker() : m_count(0) {}
int data(NetconData *con, Netcon::Event reason) {
LOGDEB("clientdata\n" );
if (reason & Netcon::NETCONPOLL_WRITE) {
sprintf(fromcli, "Bonjour Bonjour client %d, count %d",
getpid(), m_count);
con->setselevents(Netcon::NETCONPOLL_READ);
if (con->send(fromcli, strlen(fromcli) + 1) < 0) {
fprintf(stderr, "send failed\n");
return -1;
}
m_count++;
}
if (reason & Netcon::NETCONPOLL_READ) {
con->setselevents(Netcon::NETCONPOLL_WRITE);
int n;
if ((n = con->receive(buf, buflen)) < 0) {
fprintf(stderr, "receive failed\n");
return -1;
}
if (n == 0) {
// EOF, close connection
return -1;
}
buf[n] = 0;
fprintf(stderr, "%d received \"%s\"\n", getpid(), buf);
if (op_flags & OPT_n) {
pause();
} else {
sleep(1);
}
}
if (m_count >= 10) {
fprintf(stderr, "Did 10, enough\n");
if (con->getloop()) {
con->getloop()->loopReturn(0);
}
}
return 0;
}
private:
int m_count;
};
int trycli(char *host, char *serv)
{
sprintf(fromcli, "Bonjour Bonjour je suis le client %d", getpid());
NetconCli *clicon = new NetconCli();
NetconP con(clicon);
if (!con) {
fprintf(stderr, "new NetconCli failed\n");
return 1;
}
int port = atoi(serv);
int ret = port > 0 ?
clicon->openconn(host, port) : clicon->openconn(host, serv);
if (ret < 0) {
fprintf(stderr, "openconn(%s, %s) failed\n", host, serv);
return 1;
}
fprintf(stderr, "openconn(%s, %s) ok\n", host, serv);
#ifdef NOSELLOOP
for (int i = 0; i < nloop; i++) {
if (con->send(fromcli, strlen(fromcli) + 1) < 0) {
fprintf(stderr, "%d: Send failed\n", getpid());
return 1;
}
if (con->receive(buf, buflen) < 0) {
perror("receive:");
fprintf(stderr, "%d: Receive failed\n", getpid());
return 1;
}
fprintf(stderr, "%d Received \"%s\"\n", getpid(), buf);
if (op_flags & OPT_n) {
pause();
} else {
sleep(1);
}
}
#else
std::shared_ptr<NetconWorker> worker =
std::shared_ptr<NetconWorker>(new CliNetconWorker());
clicon->setcallback(worker);
SelectLoop myloop;
myloop.addselcon(con, Netcon::NETCONPOLL_WRITE);
fprintf(stderr, "client ready\n");
ret = myloop.doLoop();
if (ret < 0) {
fprintf(stderr, "selectloop failed\n");
exit(1);
}
fprintf(stderr, "selectloop returned %d\n", ret);
#endif
return 0;
}
//////////////////////////////////////////////////////////////////
// Server-side sample code
class ServNetconWorker : public NetconWorker {
public:
ServNetconWorker() : m_count(0) {}
int data(NetconData *con, Netcon::Event reason) {
LOGDEB("serverdata\n" );
if (reason & Netcon::NETCONPOLL_WRITE) {
con->setselevents(Netcon::NETCONPOLL_READ);
char fromserv[200];
sprintf(fromserv,
"Du serveur: mon fd pour ce client est %d, mon compte %d",
con->getfd(), ++m_count);
if (con->send(fromserv, strlen(fromserv) + 1) < 0) {
fprintf(stderr, "send failed\n");
return -1;
}
}
if (reason & Netcon::NETCONPOLL_READ) {
#define LL 200
char buf[LL + 1];
int n;
if ((n = con->receive(buf, LL)) < 0) {
fprintf(stderr, "receive failed\n");
return -1;
}
if (n == 0) {
return -1;
}
buf[n] = 0;
fprintf(stderr, "%d received \"%s\"\n", getpid(), buf);
con->setselevents(Netcon::NETCONPOLL_READ | Netcon::NETCONPOLL_WRITE);
}
return 0;
}
private:
int m_count;
};
class MyNetconServLis : public NetconServLis {
public:
MyNetconServLis(SelectLoop& loop)
: NetconServLis(), m_loop(loop) {
}
protected:
int cando(Netcon::Event reason) {
NetconServCon *con = accept();
if (con == 0) {
return -1;
}
std::shared_ptr<NetconWorker> worker =
std::shared_ptr<NetconWorker>(new ServNetconWorker());
con->setcallback(worker);
m_loop.addselcon(NetconP(con), NETCONPOLL_READ);
return 1;
}
SelectLoop& m_loop;
};
NetconP lis;
void
onexit(int sig)
{
fprintf(stderr, "Onexit: ");
if (sig == SIGQUIT) {
kill(getpid(), SIGKILL);
}
fprintf(stderr, "Exiting\n");
exit(0);
}
int tryserv(char *serv)
{
signal(SIGCHLD, SIG_IGN);
SelectLoop myloop;
MyNetconServLis *servlis = new MyNetconServLis(myloop);
lis = NetconP(servlis);
if (!lis) {
fprintf(stderr, "new NetconServLis failed\n");
return 1;
}
// Prepare for cleanup
struct sigaction sa;
sa.sa_flags = 0;
sa.sa_handler = onexit;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, 0);
sigaction(SIGQUIT, &sa, 0);
sigaction(SIGTERM, &sa, 0);
int port = atoi(serv);
int ret = port > 0 ?
servlis->openservice(port) : servlis->openservice(serv);
if (ret < 0) {
fprintf(stderr, "openservice(%s) failed\n", serv);
return 1;
}
myloop.addselcon(lis, Netcon::NETCONPOLL_READ);
fprintf(stderr, "openservice(%s) Ok\n", serv);
if (myloop.doLoop() < 0) {
fprintf(stderr, "selectloop failed\n");
exit(1);
}
return 0;
}
#endif /* TEST_NETCON */