Switch to unified view

a/src/netcon.cpp b/src/netcon.cpp
...
...
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
16
 */
17
17
18
// Wrapper classes for the socket interface
18
// Wrapper classes for the socket interface
19
19
20
#ifndef TEST_NETCON
21
#ifdef BUILDING_RECOLL
20
#ifdef BUILDING_RECOLL
22
#include "autoconfig.h"
21
#include "autoconfig.h"
23
#else
22
#else
24
#include "config.h"
23
#include "config.h"
25
#endif
24
#endif
...
...
1016
    }
1015
    }
1017
    LOGERR("NetconServLis::checkperm: connection from bad address 0x"  << (ip_addr) << "\n" );
1016
    LOGERR("NetconServLis::checkperm: connection from bad address 0x"  << (ip_addr) << "\n" );
1018
    return -1;
1017
    return -1;
1019
}
1018
}
1020
#endif /* NETCON_ACCESSCONTROL */
1019
#endif /* NETCON_ACCESSCONTROL */
1021
1022
1023
#else /* !TEST_NETCON */
1024
/////////////////////////////////////////////////////////////////////////
1025
////////// TEST DRIVER
1026
////////////////////////////////////////////////////////////////////////
1027
1028
#include <stdio.h>
1029
#include <stdlib.h>
1030
#include <unistd.h>
1031
#include <signal.h>
1032
#include <string.h>
1033
1034
#include "log.h"
1035
1036
#include "netcon.h"
1037
1038
using namespace std;
1039
1040
static char *thisprog;
1041
static char usage[] =
1042
    "-c <host> <service>: Connects to trnetcon server, exchange message, then\n"
1043
    "  sleeps 10 S, except if option -n is given (sleep forever)\n"
1044
    "\n"
1045
    "-s <service>: open service <service>\n"
1046
    ;
1047
static void
1048
Usage()
1049
{
1050
    fprintf(stderr, "Usage : %s:\n %s", thisprog, usage);
1051
    exit(1);
1052
}
1053
1054
static int     op_flags;
1055
#define OPT_MOINS 0x1
1056
#define OPT_s     0x2 /* Server */
1057
#define OPT_c     0x4 /* Client */
1058
#define OPT_n     0x8  /* Client sleeps forever */
1059
1060
extern int trycli(char *host, char *serv);
1061
extern int tryserv(char *serv);
1062
1063
int nloop = 10;
1064
1065
int main(int argc, char **argv)
1066
{
1067
    char *host, *serv;
1068
1069
    thisprog = argv[0];
1070
    argc--;
1071
    argv++;
1072
1073
    while (argc > 0 && **argv == '-') {
1074
        (*argv)++;
1075
        if (!(**argv))
1076
            /* Cas du "adb - core" */
1077
        {
1078
            Usage();
1079
        }
1080
        while (**argv)
1081
            switch (*(*argv)++) {
1082
            case 's':
1083
                op_flags |= OPT_s;
1084
                break;
1085
            case 'c':
1086
                op_flags |= OPT_c;
1087
                break;
1088
            case 'n':
1089
                op_flags |= OPT_n;
1090
                break;
1091
            default:
1092
                Usage();
1093
                break;
1094
            }
1095
        argc--;
1096
        argv++;
1097
    }
1098
1099
    Logger::getTheLog("")->setLogLevel(Logger::LLDEB2);
1100
1101
    if (op_flags & OPT_c) {
1102
        if (argc != 2) {
1103
            Usage();
1104
        }
1105
        host = *argv++;
1106
        argc--;
1107
        serv = *argv++;
1108
        argc--;
1109
        exit(trycli(host, serv));
1110
    } else if (op_flags & OPT_s) {
1111
        if (argc != 1) {
1112
            Usage();
1113
        }
1114
        serv = *argv++;
1115
        argc--;
1116
        exit(tryserv(serv));
1117
    } else {
1118
        Usage();
1119
    }
1120
}
1121
1122
1123
static char buf[1024];
1124
static int buflen = 1023;
1125
static char fromcli[200];
1126
1127
class CliNetconWorker : public NetconWorker {
1128
public:
1129
    CliNetconWorker()  : m_count(0) {}
1130
    int data(NetconData *con, Netcon::Event reason) {
1131
        LOGDEB("clientdata\n" );
1132
        if (reason & Netcon::NETCONPOLL_WRITE) {
1133
            sprintf(fromcli, "Bonjour Bonjour client %d, count %d",
1134
                    getpid(), m_count);
1135
            con->setselevents(Netcon::NETCONPOLL_READ);
1136
            if (con->send(fromcli, strlen(fromcli) + 1) < 0) {
1137
                fprintf(stderr, "send failed\n");
1138
                return -1;
1139
            }
1140
            m_count++;
1141
        }
1142
1143
        if (reason & Netcon::NETCONPOLL_READ) {
1144
            con->setselevents(Netcon::NETCONPOLL_WRITE);
1145
            int n;
1146
            if ((n = con->receive(buf, buflen)) < 0) {
1147
                fprintf(stderr, "receive failed\n");
1148
                return -1;
1149
            }
1150
            if (n == 0) {
1151
                // EOF, close connection
1152
                return -1;
1153
            }
1154
            buf[n] = 0;
1155
            fprintf(stderr, "%d received \"%s\"\n", getpid(), buf);
1156
            if (op_flags & OPT_n) {
1157
                pause();
1158
            } else {
1159
                sleep(1);
1160
            }
1161
        }
1162
        if (m_count >= 10) {
1163
            fprintf(stderr, "Did 10, enough\n");
1164
            if (con->getloop()) {
1165
                con->getloop()->loopReturn(0);
1166
            }
1167
        }
1168
        return 0;
1169
    }
1170
private:
1171
    int m_count;
1172
};
1173
1174
int trycli(char *host, char *serv)
1175
{
1176
    sprintf(fromcli, "Bonjour Bonjour je suis le client %d", getpid());
1177
1178
    NetconCli *clicon = new NetconCli();
1179
    NetconP con(clicon);
1180
    if (!con) {
1181
        fprintf(stderr, "new NetconCli failed\n");
1182
        return 1;
1183
    }
1184
    int port = atoi(serv);
1185
    int ret = port > 0 ?
1186
              clicon->openconn(host, port) : clicon->openconn(host, serv);
1187
    if (ret < 0) {
1188
        fprintf(stderr, "openconn(%s, %s) failed\n", host, serv);
1189
        return 1;
1190
    }
1191
    fprintf(stderr, "openconn(%s, %s) ok\n", host, serv);
1192
#ifdef NOSELLOOP
1193
    for (int i = 0; i < nloop; i++) {
1194
        if (con->send(fromcli, strlen(fromcli) + 1) < 0) {
1195
            fprintf(stderr, "%d: Send failed\n", getpid());
1196
            return 1;
1197
        }
1198
        if (con->receive(buf, buflen) < 0) {
1199
            perror("receive:");
1200
            fprintf(stderr, "%d: Receive failed\n", getpid());
1201
            return 1;
1202
        }
1203
        fprintf(stderr, "%d Received \"%s\"\n", getpid(), buf);
1204
        if (op_flags & OPT_n) {
1205
            pause();
1206
        } else {
1207
            sleep(1);
1208
        }
1209
    }
1210
#else
1211
    std::shared_ptr<NetconWorker> worker =
1212
        std::shared_ptr<NetconWorker>(new CliNetconWorker());
1213
    clicon->setcallback(worker);
1214
    SelectLoop myloop;
1215
    myloop.addselcon(con, Netcon::NETCONPOLL_WRITE);
1216
    fprintf(stderr, "client ready\n");
1217
    ret = myloop.doLoop();
1218
    if (ret < 0) {
1219
        fprintf(stderr, "selectloop failed\n");
1220
        exit(1);
1221
    }
1222
    fprintf(stderr, "selectloop returned %d\n", ret);
1223
#endif
1224
    return 0;
1225
}
1226
1227
//////////////////////////////////////////////////////////////////
1228
// Server-side sample code
1229
class ServNetconWorker : public NetconWorker {
1230
public:
1231
    ServNetconWorker() : m_count(0) {}
1232
    int data(NetconData *con, Netcon::Event reason) {
1233
        LOGDEB("serverdata\n" );
1234
        if (reason & Netcon::NETCONPOLL_WRITE) {
1235
            con->setselevents(Netcon::NETCONPOLL_READ);
1236
            char fromserv[200];
1237
            sprintf(fromserv,
1238
                    "Du serveur: mon fd pour ce client est  %d, mon compte %d",
1239
                    con->getfd(), ++m_count);
1240
            if (con->send(fromserv, strlen(fromserv) + 1) < 0) {
1241
                fprintf(stderr, "send failed\n");
1242
                return -1;
1243
            }
1244
        }
1245
        if (reason & Netcon::NETCONPOLL_READ) {
1246
#define LL 200
1247
            char buf[LL + 1];
1248
            int n;
1249
            if ((n = con->receive(buf, LL)) < 0) {
1250
                fprintf(stderr, "receive failed\n");
1251
                return -1;
1252
            }
1253
            if (n == 0) {
1254
                return -1;
1255
            }
1256
            buf[n] = 0;
1257
            fprintf(stderr, "%d received \"%s\"\n", getpid(), buf);
1258
            con->setselevents(Netcon::NETCONPOLL_READ | Netcon::NETCONPOLL_WRITE);
1259
        }
1260
        return 0;
1261
    }
1262
private:
1263
    int m_count;
1264
};
1265
1266
class MyNetconServLis : public NetconServLis {
1267
public:
1268
    MyNetconServLis(SelectLoop& loop)
1269
        : NetconServLis(), m_loop(loop) {
1270
    }
1271
protected:
1272
    int cando(Netcon::Event reason) {
1273
        NetconServCon *con = accept();
1274
        if (con == 0) {
1275
            return -1;
1276
        }
1277
        std::shared_ptr<NetconWorker> worker =
1278
            std::shared_ptr<NetconWorker>(new ServNetconWorker());
1279
        con->setcallback(worker);
1280
        m_loop.addselcon(NetconP(con), NETCONPOLL_READ);
1281
        return 1;
1282
    }
1283
    SelectLoop& m_loop;
1284
};
1285
1286
NetconP lis;
1287
1288
void
1289
onexit(int sig)
1290
{
1291
    fprintf(stderr, "Onexit: ");
1292
    if (sig == SIGQUIT) {
1293
        kill(getpid(), SIGKILL);
1294
    }
1295
    fprintf(stderr, "Exiting\n");
1296
    exit(0);
1297
}
1298
1299
int tryserv(char *serv)
1300
{
1301
    signal(SIGCHLD, SIG_IGN);
1302
    SelectLoop myloop;
1303
    MyNetconServLis *servlis = new MyNetconServLis(myloop);
1304
    lis = NetconP(servlis);
1305
    if (!lis) {
1306
        fprintf(stderr, "new NetconServLis failed\n");
1307
        return 1;
1308
    }
1309
1310
    // Prepare for cleanup
1311
    struct sigaction sa;
1312
    sa.sa_flags = 0;
1313
    sa.sa_handler = onexit;
1314
    sigemptyset(&sa.sa_mask);
1315
    sigaction(SIGINT, &sa, 0);
1316
    sigaction(SIGQUIT, &sa, 0);
1317
    sigaction(SIGTERM, &sa, 0);
1318
1319
    int port = atoi(serv);
1320
    int ret = port > 0 ?
1321
              servlis->openservice(port) : servlis->openservice(serv);
1322
    if (ret < 0) {
1323
        fprintf(stderr, "openservice(%s) failed\n", serv);
1324
        return 1;
1325
    }
1326
    myloop.addselcon(lis, Netcon::NETCONPOLL_READ);
1327
    fprintf(stderr, "openservice(%s) Ok\n", serv);
1328
    if (myloop.doLoop() < 0) {
1329
        fprintf(stderr, "selectloop failed\n");
1330
        exit(1);
1331
    }
1332
    return 0;
1333
}
1334
1335
#endif /* TEST_NETCON */
1336