reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
EchoServer.cxx
Go to the documentation of this file.
1#if defined(HAVE_CONFIG_H)
2#include "config.h"
3#endif
4
5#ifdef WIN32
6#pragma warning(disable : 4267)
7#endif
8
9#include "../UdpEchoServer.hxx"
10#include <rutil/Time.hxx>
11#include <rutil/Logger.hxx>
12#include <rutil/DnsUtil.hxx>
14
15using namespace reTurn;
16using namespace std;
17using namespace resip;
18
19#define RESIPROCATE_SUBSYSTEM resip::Subsystem::TEST
20
21// Simple Echo Server that can be used in TURN testing
22
24{
25 void printHelpText(int argc, char** argv) override
26 {
27 cout << "Command line format: " << endl;
28 cout << " EchoServer [config_filename] [-{setting} = {value}] ...." << endl;
29 cout << " First argument is the configuration filename - it is optional and is never proceeded with a - or /." << endl;
30 cout << " Following that, config file options can be overridden." << endl;
31 cout << " Note: / can be used instead of - , and : can be used instead of = " << endl;
32 cout << "Example:" << endl;
33 cout << " EchoServer myconfig.config -LocalIPAddress=192.168.1.1" << endl;
34 }
35};
36
37int main(int argc, char* argv[])
38{
39#if defined(WIN32) && defined(_DEBUG) && defined(LEAK_CHECK)
40 resip::FindMemoryLeaks fml;
41#endif
42
43 // Parse command line and configuration file
44 EchoServerConfig config;
45 Data defaultConfigFilename("echoserver.config");
46 try
47 {
48 config.parseConfig(argc, argv, defaultConfigFilename);
49 }
50 catch (BaseException& ex)
51 {
52 std::cerr << "Error parsing configuration: " << ex << std::endl;
53 return false;
54 }
55
56 Log::initialize(config, "EchoServer");
57
58 try
59 {
60 Data localAddress = config.getConfigData("LocalIPAddress", DnsUtil::getLocalIpAddress(), true);
61 unsigned int startingPort = config.getConfigInt("StartingPort", 8000);
62 unsigned int portIncrement = config.getConfigInt("PortIncrement", 2);
63 unsigned int numPorts = config.getConfigInt("NumPorts", 4);
64
65 InfoLog(<< "Using: LocalAddress=" << localAddress << ", StartingPort=" << startingPort << ", PortIncrement=" << portIncrement << ", NumPorts=" << numPorts);
66
67 // Start Echo servers
68 std::list<UdpEchoServer*> echoServers;
69 for (unsigned int i = 0; i < numPorts; i++)
70 {
71
72 unsigned int port = startingPort + (portIncrement * i);
73 UdpEchoServer* echoServer = new UdpEchoServer(localAddress, port);
74 echoServer->run();
75 echoServers.push_back(echoServer);
76 InfoLog(<< "EchoServer started " << localAddress << ":" << port);
77 }
78
79 cout << "Press enter to exit!\n";
80
81 // Wait for key press
82 getchar();
83
84 // Stop Echo servers
85 for (auto echoServer : echoServers)
86 {
87 echoServer->shutdown();
88 }
89
90 // Wait for echo servers to stop
91 for (auto echoServer : echoServers)
92 {
93 echoServer->join();
94 delete echoServer;
95 }
96
97 // Clear list
98 echoServers.clear();
99
100 InfoLog(<< "EchoServer shutdown.");
101 }
102 catch (const std::exception& e)
103 {
104 std::cerr << "Exception: " << e.what() << "\n";
105 }
106
107 return 0;
108}
109
110
111/* ====================================================================
112
113 Copyright (c) 2023-2024, SIP Spectrum, Inc. http://sipspectrum.com
114 All rights reserved.
115
116 Redistribution and use in source and binary forms, with or without
117 modification, are permitted provided that the following conditions are
118 met:
119
120 1. Redistributions of source code must retain the above copyright
121 notice, this list of conditions and the following disclaimer.
122
123 2. Redistributions in binary form must reproduce the above copyright
124 notice, this list of conditions and the following disclaimer in the
125 documentation and/or other materials provided with the distribution.
126
127 3. Neither the name of Plantronics nor the names of its contributors
128 may be used to endorse or promote products derived from this
129 software without specific prior written permission.
130
131 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
132 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
133 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
134 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
135 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
136 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
137 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
138 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
139 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
140 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
141 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
142
143 ==================================================================== */
int main(int argc, char *argv[])
#define InfoLog(args_)
Definition Logger.hxx:131
The abstract base-class for all exceptions thrown by resip code.
virtual void parseConfig(int argc, char **argv, const resip::Data &defaultConfigFilename)
int getConfigInt(const resip::Data &name, int defaultValue) const
resip::Data getConfigData(const resip::Data &name, const resip::Data &defaultValue, bool useDefaultIfEmpty=false) const
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
static Data getLocalIpAddress(const Data &defaultInterface=Data::Empty)
Gets the IP address of "the" local interface.
Definition DnsUtil.cxx:222
static void initialize(Type type, Level level, const Data &appName, const char *logFileName=0, ExternalLogger *externalLogger=0, const Data &syslogFacility="LOG_DAEMON", MessageStructure messageStructure=Unstructured, const Data &instanceName="")
Definition Log.cxx:268
dcm! – add UnusedChecking(_enum) below;