dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 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 Pedrol | 708b8b4 | 2018-01-15 10:06:32 +0100 | [diff] [blame^] | 33 | #include <unistd.h> |
| 34 | #include <signal.h> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 35 | |
| 36 | static const int gNumToSend = 10; |
| 37 | |
Pau Espin Pedrol | 708b8b4 | 2018-01-15 10:06:32 +0100 | [diff] [blame^] | 38 | static void sigalarm_handler(int foo) |
| 39 | { |
| 40 | printf("FAIL: test did not run successfully\n"); |
| 41 | exit(EXIT_FAILURE); |
| 42 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 43 | |
Pau Espin Pedrol | cb0fc9b | 2018-01-15 10:29:20 +0100 | [diff] [blame] | 44 | void *testReaderIP(void *param) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 45 | { |
Pau Espin Pedrol | cb0fc9b | 2018-01-15 10:29:20 +0100 | [diff] [blame] | 46 | UDPSocket *readSocket = (UDPSocket *)param; |
| 47 | readSocket->nonblocking(); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 48 | int rc = 0; |
| 49 | while (rc<gNumToSend) { |
| 50 | char buf[MAX_UDP_LENGTH]; |
Pau Espin Pedrol | cb0fc9b | 2018-01-15 10:29:20 +0100 | [diff] [blame] | 51 | int count = readSocket->read(buf, MAX_UDP_LENGTH); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 52 | if (count>0) { |
Pau Espin Pedrol | 2f376a3 | 2018-01-09 19:45:15 +0100 | [diff] [blame] | 53 | CERR("read: " << buf); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 54 | rc++; |
| 55 | } else { |
| 56 | sleep(2); |
| 57 | } |
| 58 | } |
| 59 | return NULL; |
| 60 | } |
| 61 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 62 | int main(int argc, char * argv[] ) |
| 63 | { |
| 64 | |
Pau Espin Pedrol | 708b8b4 | 2018-01-15 10:06:32 +0100 | [diff] [blame^] | 65 | if (signal(SIGALRM, sigalarm_handler) == SIG_ERR) { |
| 66 | perror("signal"); |
| 67 | exit(EXIT_FAILURE); |
| 68 | } |
| 69 | |
| 70 | /* If the test takes longer than 2*gNumToSend seconds, abort it */ |
| 71 | alarm(2* gNumToSend); |
| 72 | |
Pau Espin Pedrol | cb0fc9b | 2018-01-15 10:29:20 +0100 | [diff] [blame] | 73 | UDPSocket readSocket("127.0.0.1", 0); |
| 74 | UDPSocket socket1("127.0.0.1", 0, "localhost", readSocket.port()); |
| 75 | |
| 76 | CERR("socket1: " << socket1.port() << ", readSocket: " << readSocket.port()); |
| 77 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 78 | Thread readerThreadIP; |
Pau Espin Pedrol | cb0fc9b | 2018-01-15 10:29:20 +0100 | [diff] [blame] | 79 | readerThreadIP.start(testReaderIP, &readSocket); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 80 | |
| 81 | // give the readers time to open |
| 82 | sleep(1); |
| 83 | |
| 84 | for (int i=0; i<gNumToSend; i++) { |
Pau Espin Pedrol | 2f376a3 | 2018-01-09 19:45:15 +0100 | [diff] [blame] | 85 | socket1.write("Hello IP land"); |
Pau Espin Pedrol | 8639fee | 2018-01-11 19:27:48 +0100 | [diff] [blame] | 86 | sleep(1); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | readerThreadIP.join(); |
Pau Espin Pedrol | 2f376a3 | 2018-01-09 19:45:15 +0100 | [diff] [blame] | 90 | |
| 91 | printf("Done\n"); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // vim: ts=4 sw=4 |