blob: eb92e253e75bb4c87f258ee8e8db2322c60b6ea2 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008 Free Software Foundation, Inc.
3*
4*
5* This software is distributed under the terms of the GNU Affero Public License.
6* See the COPYING file in the main directory for details.
7*
8* This use of this software may be subject to additional restrictions.
9* See the LEGAL file in the main directory for details.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24*/
25
26
27
28
29#include "Sockets.h"
30#include "Threads.h"
31#include <stdio.h>
32#include <stdlib.h>
Pau Espin Pedrol708b8b42018-01-15 10:06:32 +010033#include <unistd.h>
34#include <signal.h>
dburgess82c46ff2011-10-07 02:40:51 +000035
36static const int gNumToSend = 10;
37
Pau Espin Pedrol708b8b42018-01-15 10:06:32 +010038static void sigalarm_handler(int foo)
39{
40 printf("FAIL: test did not run successfully\n");
41 exit(EXIT_FAILURE);
42}
dburgess82c46ff2011-10-07 02:40:51 +000043
Pau Espin Pedrolcb0fc9b2018-01-15 10:29:20 +010044void *testReaderIP(void *param)
dburgess82c46ff2011-10-07 02:40:51 +000045{
Pau Espin Pedrolcb0fc9b2018-01-15 10:29:20 +010046 UDPSocket *readSocket = (UDPSocket *)param;
47 readSocket->nonblocking();
dburgess82c46ff2011-10-07 02:40:51 +000048 int rc = 0;
49 while (rc<gNumToSend) {
Max99eb07e2018-01-31 11:34:59 +010050 char buf[MAX_UDP_LENGTH] = { 0 };
Pau Espin Pedrolcb0fc9b2018-01-15 10:29:20 +010051 int count = readSocket->read(buf, MAX_UDP_LENGTH);
dburgess82c46ff2011-10-07 02:40:51 +000052 if (count>0) {
Pau Espin Pedrol2f376a32018-01-09 19:45:15 +010053 CERR("read: " << buf);
dburgess82c46ff2011-10-07 02:40:51 +000054 rc++;
55 } else {
56 sleep(2);
57 }
58 }
59 return NULL;
60}
61
dburgess82c46ff2011-10-07 02:40:51 +000062int main(int argc, char * argv[] )
63{
Pau Espin Pedrol10d76b62018-01-15 10:43:31 +010064 int count;
dburgess82c46ff2011-10-07 02:40:51 +000065
Pau Espin Pedrol708b8b42018-01-15 10:06:32 +010066 if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) {
67 perror("signal");
68 exit(EXIT_FAILURE);
69 }
70
71 /* If the test takes longer than 2*gNumToSend seconds, abort it */
72 alarm(2* gNumToSend);
73
Pau Espin Pedrolcb0fc9b2018-01-15 10:29:20 +010074 UDPSocket readSocket("127.0.0.1", 0);
75 UDPSocket socket1("127.0.0.1", 0, "localhost", readSocket.port());
76
77 CERR("socket1: " << socket1.port() << ", readSocket: " << readSocket.port());
78
dburgess82c46ff2011-10-07 02:40:51 +000079 Thread readerThreadIP;
Pau Espin Pedrolcb0fc9b2018-01-15 10:29:20 +010080 readerThreadIP.start(testReaderIP, &readSocket);
dburgess82c46ff2011-10-07 02:40:51 +000081
82 // give the readers time to open
83 sleep(1);
84
85 for (int i=0; i<gNumToSend; i++) {
Pau Espin Pedrol10d76b62018-01-15 10:43:31 +010086 CERR("write");
87 count = socket1.write("Hello IP land");
88 if (count < 0) {
89 COUT("FAIL: write");
90 exit(EXIT_FAILURE);
91 }
Pau Espin Pedrol8639fee2018-01-11 19:27:48 +010092 sleep(1);
dburgess82c46ff2011-10-07 02:40:51 +000093 }
94
95 readerThreadIP.join();
Pau Espin Pedrol2f376a32018-01-09 19:45:15 +010096
97 printf("Done\n");
dburgess82c46ff2011-10-07 02:40:51 +000098}
99
100// vim: ts=4 sw=4