/[resiprocate]/main/reTurn/reTurnServer.cxx
ViewVC logotype

Contents of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9329 - (show annotations) (download)
Sun Jan 22 19:00:36 2012 UTC (8 years ago) by dpocock
File MIME type: text/plain
File size: 7092 byte(s)
reTurnServer: read config settings from reTurnServer.config instead of using hard coded values
1 #include <iostream>
2 #include <string>
3 #include <asio.hpp>
4 #include <boost/bind.hpp>
5 #include <boost/function.hpp>
6 #include <boost/lexical_cast.hpp>
7 #include <rutil/Data.hxx>
8 #include "TcpServer.hxx"
9 #include "TlsServer.hxx"
10 #include "UdpServer.hxx"
11 #include "ReTurnConfig.hxx"
12 #include "RequestHandler.hxx"
13 #include "TurnManager.hxx"
14 #include <rutil/WinLeakCheck.hxx>
15 #include <rutil/Log.hxx>
16 #include <rutil/Logger.hxx>
17 #include "ReTurnSubsystem.hxx"
18
19 #define RESIPROCATE_SUBSYSTEM ReTurnSubsystem::RETURN
20
21 #if defined(_WIN32)
22
23 boost::function0<void> console_ctrl_function;
24
25 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
26 {
27 switch (ctrl_type)
28 {
29 case CTRL_C_EVENT:
30 case CTRL_BREAK_EVENT:
31 case CTRL_CLOSE_EVENT:
32 case CTRL_SHUTDOWN_EVENT:
33 console_ctrl_function();
34 return TRUE;
35 default:
36 return FALSE;
37 }
38 }
39 #endif // defined(_WIN32)
40
41 int main(int argc, char* argv[])
42 {
43 #if defined(WIN32) && defined(_DEBUG) && defined(LEAK_CHECK)
44 resip::FindMemoryLeaks fml;
45 #endif
46
47 resip::Data defaultConfig("reTurnServer.config");
48 reTurn::ReTurnConfig reTurnConfig(argc, argv, defaultConfig);
49
50 try
51 {
52 // Initialize Logging
53 resip::Log::initialize(reTurnConfig.mLoggingType, reTurnConfig.mLoggingLevel, "reTurnServer", reTurnConfig.mLoggingFilename.c_str());
54 resip::GenericLogImpl::MaxLineCount = reTurnConfig.mLoggingFileMaxLineCount;
55
56 // Initialize server.
57 asio::io_service ioService; // The one and only ioService for the stunServer
58 reTurn::TurnManager turnManager(ioService, reTurnConfig); // The one and only Turn Manager
59
60 boost::shared_ptr<reTurn::UdpServer> udpTurnServer; // also a1p1StunUdpServer
61 boost::shared_ptr<reTurn::TcpServer> tcpTurnServer;
62 boost::shared_ptr<reTurn::TlsServer> tlsTurnServer;
63 boost::shared_ptr<reTurn::UdpServer> a1p2StunUdpServer;
64 boost::shared_ptr<reTurn::UdpServer> a2p1StunUdpServer;
65 boost::shared_ptr<reTurn::UdpServer> a2p2StunUdpServer;
66
67 // The one and only RequestHandler - if altStunPort is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
68 reTurn::RequestHandler requestHandler(turnManager,
69 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnAddress : 0,
70 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mTurnPort : 0,
71 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunAddress : 0,
72 reTurnConfig.mAltStunPort != 0 ? &reTurnConfig.mAltStunPort : 0);
73
74 udpTurnServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
75 tcpTurnServer.reset(new reTurn::TcpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTurnPort));
76 tlsTurnServer.reset(new reTurn::TlsServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mTlsTurnPort));
77
78 if(reTurnConfig.mAltStunPort != 0) // if alt stun port is non-zero, then RFC3489 support is enabled
79 {
80 a1p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mTurnAddress, reTurnConfig.mAltStunPort));
81 a2p1StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mTurnPort));
82 a2p2StunUdpServer.reset(new reTurn::UdpServer(ioService, requestHandler, reTurnConfig.mAltStunAddress, reTurnConfig.mAltStunPort));
83 udpTurnServer->setAlternateUdpServers(a1p2StunUdpServer.get(), a2p1StunUdpServer.get(), a2p2StunUdpServer.get());
84 a1p2StunUdpServer->setAlternateUdpServers(udpTurnServer.get(), a2p2StunUdpServer.get(), a2p1StunUdpServer.get());
85 a2p1StunUdpServer->setAlternateUdpServers(a2p2StunUdpServer.get(), udpTurnServer.get(), a1p2StunUdpServer.get());
86 a2p2StunUdpServer->setAlternateUdpServers(a2p1StunUdpServer.get(), a1p2StunUdpServer.get(), udpTurnServer.get());
87 a1p2StunUdpServer->start();
88 a2p1StunUdpServer->start();
89 a2p2StunUdpServer->start();
90 }
91
92 udpTurnServer->start();
93 tcpTurnServer->start();
94 tlsTurnServer->start();
95
96 #ifdef _WIN32
97 // Set console control handler to allow server to be stopped.
98 console_ctrl_function = boost::bind(&asio::io_service::stop, &ioService);
99 SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
100 #else
101 // Block all signals for background thread.
102 sigset_t new_mask;
103 sigfillset(&new_mask);
104 sigset_t old_mask;
105 pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
106 #endif
107
108 // Run the ioService until stopped.
109 // Create a pool of threads to run all of the io_services.
110 boost::shared_ptr<asio::thread> thread(new asio::thread(
111 boost::bind(&asio::io_service::run, &ioService)));
112
113 #ifndef _WIN32
114 // Restore previous signals.
115 pthread_sigmask(SIG_SETMASK, &old_mask, 0);
116
117 // Wait for signal indicating time to shut down.
118 sigset_t wait_mask;
119 sigemptyset(&wait_mask);
120 sigaddset(&wait_mask, SIGINT);
121 sigaddset(&wait_mask, SIGQUIT);
122 sigaddset(&wait_mask, SIGTERM);
123 pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
124 int sig = 0;
125 sigwait(&wait_mask, &sig);
126 ioService.stop();
127 #endif
128
129 // Wait for thread to exit
130 thread->join();
131 }
132 catch (std::exception& e)
133 {
134 ErrLog(<< "exception: " << e.what());
135 }
136
137 return 0;
138 }
139
140
141 /* ====================================================================
142
143 Copyright (c) 2007-2008, Plantronics, Inc.
144 All rights reserved.
145
146 Redistribution and use in source and binary forms, with or without
147 modification, are permitted provided that the following conditions are
148 met:
149
150 1. Redistributions of source code must retain the above copyright
151 notice, this list of conditions and the following disclaimer.
152
153 2. Redistributions in binary form must reproduce the above copyright
154 notice, this list of conditions and the following disclaimer in the
155 documentation and/or other materials provided with the distribution.
156
157 3. Neither the name of Plantronics nor the names of its contributors
158 may be used to endorse or promote products derived from this
159 software without specific prior written permission.
160
161 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
162 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
163 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
164 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
165 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
166 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
167 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
168 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
169 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
170 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
171 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
172
173 ==================================================================== */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27