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

Contents of /main/reTurn/reTurnServer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7366 - (show annotations) (download)
Sun Oct 28 17:34:51 2007 UTC (12 years, 1 month ago) by sgodin
File MIME type: text/plain
File size: 7100 byte(s)
-fixed eol-style
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 "RequestHandler.hxx"
12 #include "TurnManager.hxx"
13
14 #if defined(_WIN32)
15
16 boost::function0<void> console_ctrl_function;
17
18 BOOL WINAPI console_ctrl_handler(DWORD ctrl_type)
19 {
20 switch (ctrl_type)
21 {
22 case CTRL_C_EVENT:
23 case CTRL_BREAK_EVENT:
24 case CTRL_CLOSE_EVENT:
25 case CTRL_SHUTDOWN_EVENT:
26 console_ctrl_function();
27 return TRUE;
28 default:
29 return FALSE;
30 }
31 }
32 #endif // defined(_WIN32)
33
34
35 class reTurnConfig
36 {
37 public:
38 // TODO
39 };
40
41 #define NUM_THREADS 1 // Do not change this - code is not currently thread safe
42
43 int main(int argc, char* argv[])
44 {
45 try
46 {
47 // Check command line arguments.
48 if (argc != 6)
49 {
50 std::cerr << "Usage: reTurnServer <address> <turnPort> <stunPort> <altAddress> <altPort>\n";
51 std::cerr << " For IPv4, try:\n";
52 std::cerr << " reTurnServer 0.0.0.0 8777 8776 0.0.0.0 9776\n";
53 std::cerr << " For IPv6, try:\n";
54 std::cerr << " reTurnServer 0::0 8777 8776 0::0 9776\n";
55 return 1;
56 }
57
58 // Initialize server.
59 asio::io_service ioService; // The one and only ioService for the stunServer
60 reTurn::TurnManager turnManager(ioService); // The one and only Turn Manager
61
62 unsigned short turnPort = (unsigned short)resip::Data(argv[2]).convertUnsignedLong();
63 unsigned short stunPort = (unsigned short)resip::Data(argv[3]).convertUnsignedLong();
64 unsigned short altStunPort = (unsigned short)resip::Data(argv[5]).convertUnsignedLong();
65 unsigned short tlsPort = turnPort + 1;
66 asio::ip::address turnAddress = asio::ip::address::from_string(argv[1]);
67 asio::ip::address altStunAddress = asio::ip::address::from_string(argv[4]);
68
69 reTurn::UdpServer* udpTurnServer=0;
70 reTurn::TcpServer* tcpTurnServer=0;
71 reTurn::TlsServer* tlsTurnServer=0;
72 reTurn::UdpServer* a1p1StunUdpServer=0;
73 reTurn::UdpServer* a1p2StunUdpServer=0;
74 reTurn::UdpServer* a2p1StunUdpServer=0;
75 reTurn::UdpServer* a2p2StunUdpServer=0;
76
77 // The one and only RequestHandler - if stun port is non-zero, then assume RFC3489 support is enabled and pass settings to request handler
78 reTurn::RequestHandler requestHandler(turnManager, stunPort != 0 ? &turnAddress : 0, stunPort != 0 ? &turnPort : 0, stunPort != 0 ? &altStunAddress : 0, stunPort != 0 ? &altStunPort : 0);
79
80 if(turnPort != 0)
81 {
82 udpTurnServer = new reTurn::UdpServer(ioService, requestHandler, turnAddress, turnPort);
83 tcpTurnServer = new reTurn::TcpServer(ioService, requestHandler, turnAddress, turnPort);
84 tlsTurnServer = new reTurn::TlsServer(ioService, requestHandler, turnAddress, tlsPort);
85 }
86
87 if(stunPort != 0) // if stun port is non-zero, then assume RFC3489 support is enabled
88 {
89 a1p1StunUdpServer = new reTurn::UdpServer(ioService, requestHandler, turnAddress, stunPort);
90 a1p2StunUdpServer = new reTurn::UdpServer(ioService, requestHandler, turnAddress, altStunPort);
91 a2p1StunUdpServer = new reTurn::UdpServer(ioService, requestHandler, altStunAddress, stunPort);
92 a2p2StunUdpServer = new reTurn::UdpServer(ioService, requestHandler, altStunAddress, altStunPort);
93 a1p1StunUdpServer->setAlternateUdpServers(a1p2StunUdpServer, a2p1StunUdpServer, a2p2StunUdpServer);
94 a1p2StunUdpServer->setAlternateUdpServers(a1p1StunUdpServer, a2p2StunUdpServer, a2p1StunUdpServer);
95 a2p1StunUdpServer->setAlternateUdpServers(a2p2StunUdpServer, a1p1StunUdpServer, a1p2StunUdpServer);
96 a2p2StunUdpServer->setAlternateUdpServers(a2p1StunUdpServer, a1p2StunUdpServer, a1p1StunUdpServer);
97 }
98
99 #ifdef _WIN32
100 // Set console control handler to allow server to be stopped.
101 console_ctrl_function = boost::bind(&asio::io_service::stop, &ioService);
102 SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
103 #else
104 // Block all signals for background thread.
105 sigset_t new_mask;
106 sigfillset(&new_mask);
107 sigset_t old_mask;
108 pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);
109 #endif
110
111 // Run the ioService until stopped.
112 // Create a pool of threads to run all of the io_services.
113 std::vector<boost::shared_ptr<asio::thread> > threads;
114 for (std::size_t i = 0; i < NUM_THREADS; ++i)
115 {
116 boost::shared_ptr<asio::thread> thread(new asio::thread(
117 boost::bind(&asio::io_service::run, &ioService)));
118 threads.push_back(thread);
119 }
120
121 #ifndef _WIN32
122 // Restore previous signals.
123 pthread_sigmask(SIG_SETMASK, &old_mask, 0);
124
125 // Wait for signal indicating time to shut down.
126 sigset_t wait_mask;
127 sigemptyset(&wait_mask);
128 sigaddset(&wait_mask, SIGINT);
129 sigaddset(&wait_mask, SIGQUIT);
130 sigaddset(&wait_mask, SIGTERM);
131 pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
132 int sig = 0;
133 sigwait(&wait_mask, &sig);
134 #endif
135
136 // Wait for all threads in the pool to exit.
137 for (std::size_t i = 0; i < threads.size(); ++i)
138 {
139 threads[i]->join();
140 }
141
142 delete udpTurnServer;
143 delete tcpTurnServer;
144 delete tlsTurnServer;
145 delete a1p1StunUdpServer;
146 delete a1p2StunUdpServer;
147 delete a2p1StunUdpServer;
148 delete a2p2StunUdpServer;
149 }
150 catch (std::exception& e)
151 {
152 std::cerr << "exception: " << e.what() << "\n";
153 }
154
155 return 0;
156 }
157
158
159 /* ====================================================================
160
161 Original contribution Copyright (C) 2007 Plantronics, Inc.
162 Provided under the terms of the Vovida Software License, Version 2.0.
163
164 The Vovida Software License, Version 2.0
165
166 Redistribution and use in source and binary forms, with or without
167 modification, are permitted provided that the following conditions
168 are met:
169
170 1. Redistributions of source code must retain the above copyright
171 notice, this list of conditions and the following disclaimer.
172
173 2. Redistributions in binary form must reproduce the above copyright
174 notice, this list of conditions and the following disclaimer in
175 the documentation and/or other materials provided with the
176 distribution.
177
178 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
179 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
180 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
181 NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
182 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
183 IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
184 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
185 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
186 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
187 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
188 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
189 USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
190 DAMAGE.
191
192 ==================================================================== */
193

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