Commit bae4dc14 authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

updated shared code

parent 46374aa2
......@@ -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 */
......@@ -364,6 +364,9 @@ string path_absolute(const string& is)
return is;
}
string s = is;
#ifdef _WIN32
path_slashize(s);
#endif
if (!path_isabsolute(s)) {
char buf[MAXPATHLEN];
if (!getcwd(buf, MAXPATHLEN)) {
......
......@@ -77,6 +77,7 @@ extern long long path_filesize(const std::string& path);
/// Only st_mtime, st_ctime, st_size, st_mode (file type bits) are set on
/// all systems. st_dev and st_ino are set for special posix usage.
/// The rest is zeroed.
/// @ret 0 for success
struct stat;
extern int path_fileprops(const std::string path, struct stat *stp,
bool follow = true);
......
......@@ -14,12 +14,6 @@
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef BUILDING_RECOLL
#include "autoconfig.h"
#else
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
......@@ -523,16 +517,13 @@ string escapeHtml(const string& in)
{
string out;
for (string::size_type pos = 0; pos < in.length(); pos++) {
switch (in.at(pos)) {
case '<':
out += "&lt;";
break;
case '&':
out += "&amp;";
break;
default:
out += in.at(pos);
}
switch(in.at(pos)) {
case '<': out += "&lt;"; break;
case '>': out += "&gt;"; break;
case '&': out += "&amp;"; break;
case '"': out += "&quot;"; break;
default: out += in.at(pos); break;
}
}
return out;
}
......@@ -1323,6 +1314,66 @@ bool SimpleRegexp::operator() (const string& val) const
return simpleMatch(val);
}
string flagsToString(const vector<CharFlags>& flags, unsigned int val)
{
const char *s;
string out;
for (auto& flag : flags) {
if ((val & flag.value) == flag.value) {
s = flag.yesname;
} else {
s = flag.noname;
}
if (s && *s) {
/* We have something to write */
if (out.length()) {
// If not first, add '|' separator
out.append("|");
}
out.append(s);
}
}
return out;
}
string valToString(const vector<CharFlags>& flags, unsigned int val)
{
string out;
for (auto& flag : flags) {
if (flag.value == val) {
out = flag.yesname;
return out;
}
}
{
char mybuf[100];
sprintf(mybuf, "Unknown Value 0x%x", val);
out = mybuf;
}
return out;
}
unsigned int stringToFlags(const vector<CharFlags>& flags,
const string& input, const char *sep)
{
unsigned int out = 0;
vector<string> toks;
stringToTokens(input, toks, sep);
for (auto& tok: toks) {
trimstring(tok);
for (auto& flag : flags) {
if (!tok.compare(flag.yesname)) {
/* Note: we don't break: the same name could conceivably
set several flags. */
out |= flag.value;
}
}
}
return out;
}
// Initialization for static stuff to be called from main thread before going
// multiple
void smallut_init_mt()
......
......@@ -218,4 +218,29 @@ private:
Internal *m;
};
/// Utilities for printing names for defined values (Ex: O_RDONLY->"O_RDONLY")
/// Entries for the descriptive table
struct CharFlags {
unsigned int value; // Flag or value
const char *yesname;// String to print if flag set or equal
const char *noname; // String to print if flag not set (unused for values)
};
/// Helper macro for the common case where we want to print the
/// flag/value defined name
#define CHARFLAGENTRY(NM) {NM, #NM}
/// Translate a bitfield into string description
extern std::string flagsToString(const std::vector<CharFlags>&,
unsigned int flags);
/// Translate a value into a name
extern std::string valToString(const std::vector<CharFlags>&, unsigned int val);
/// Reverse operation: translate string into bitfield
extern unsigned int
stringToFlags(const std::vector<CharFlags>&, const std::string& input,
const char *sep = "|");
#endif /* _SMALLUT_H_INCLUDED_ */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment