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> |
| 33 | |
| 34 | |
| 35 | static const int gNumToSend = 10; |
| 36 | |
| 37 | |
| 38 | void *testReaderIP(void *) |
| 39 | { |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 40 | UDPSocket readSocket("127.0.0.1", 5934, "localhost", 5061); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 41 | readSocket.nonblocking(); |
| 42 | int rc = 0; |
| 43 | while (rc<gNumToSend) { |
| 44 | char buf[MAX_UDP_LENGTH]; |
Tom Tsou | 2c650a6 | 2016-04-28 21:55:17 -0700 | [diff] [blame] | 45 | int count = readSocket.read(buf, MAX_UDP_LENGTH); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 46 | if (count>0) { |
| 47 | COUT("read: " << buf); |
| 48 | rc++; |
| 49 | } else { |
| 50 | sleep(2); |
| 51 | } |
| 52 | } |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | |
| 58 | void *testReaderUnix(void *) |
| 59 | { |
| 60 | UDDSocket readSocket("testDestination"); |
| 61 | readSocket.nonblocking(); |
| 62 | int rc = 0; |
| 63 | while (rc<gNumToSend) { |
Harald Welte | 1468a5c | 2017-11-06 04:01:31 +0900 | [diff] [blame] | 64 | char buf[MAX_UDP_LENGTH+1]; |
| 65 | buf[MAX_UDP_LENGTH] = '\0'; |
Tom Tsou | 2c650a6 | 2016-04-28 21:55:17 -0700 | [diff] [blame] | 66 | int count = readSocket.read(buf, MAX_UDP_LENGTH); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 67 | if (count>0) { |
| 68 | COUT("read: " << buf); |
| 69 | rc++; |
| 70 | } else { |
| 71 | sleep(2); |
| 72 | } |
| 73 | } |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | int main(int argc, char * argv[] ) |
| 79 | { |
| 80 | |
| 81 | Thread readerThreadIP; |
| 82 | readerThreadIP.start(testReaderIP,NULL); |
| 83 | Thread readerThreadUnix; |
| 84 | readerThreadUnix.start(testReaderUnix,NULL); |
| 85 | |
Pau Espin Pedrol | 8c80095 | 2017-08-16 16:53:23 +0200 | [diff] [blame] | 86 | UDPSocket socket1("127.0.0.1", 5061, "127.0.0.1", 5934); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 87 | UDDSocket socket1U("testSource","testDestination"); |
| 88 | |
| 89 | COUT("socket1: " << socket1.port()); |
| 90 | |
| 91 | // give the readers time to open |
| 92 | sleep(1); |
| 93 | |
| 94 | for (int i=0; i<gNumToSend; i++) { |
| 95 | socket1.write("Hello IP land"); |
| 96 | socket1U.write("Hello Unix domain"); |
| 97 | sleep(1); |
| 98 | } |
| 99 | |
| 100 | readerThreadIP.join(); |
| 101 | readerThreadUnix.join(); |
| 102 | } |
| 103 | |
| 104 | // vim: ts=4 sw=4 |