| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Mihai Ianculescu |
|---|
| 31 |
|
|---|
| 32 |
#include <whisperlib/common/base/common.h> |
|---|
| 33 |
#include <whisperlib/common/base/errno.h> |
|---|
| 34 |
#include <whisperlib/common/base/signal_handlers.h> |
|---|
| 35 |
#include <whisperlib/common/base/system.h> |
|---|
| 36 |
|
|---|
| 37 |
#include <whisperlib/net/base/user_authenticator.h> |
|---|
| 38 |
#include <whisperlib/common/app/app.h> |
|---|
| 39 |
#include <whisperlib/net/rpc/lib/server/rpc_http_server.h> |
|---|
| 40 |
|
|---|
| 41 |
#include "stream_manager.h" |
|---|
| 42 |
|
|---|
| 43 |
// our namespace |
|---|
| 44 |
using namespace media; |
|---|
| 45 |
|
|---|
| 46 |
////////////////////////////////////////////////////////////////////// |
|---|
| 47 |
|
|---|
| 48 |
DEFINE_int32(http_port, |
|---|
| 49 |
80, |
|---|
| 50 |
"The port on which we accept HTTP connections"); |
|---|
| 51 |
DEFINE_bool(http_connection_dlog_level, |
|---|
| 52 |
false, |
|---|
| 53 |
"Turn on deep debugging messages on HTTP connections"); |
|---|
| 54 |
DEFINE_string(media_root, |
|---|
| 55 |
"/media", |
|---|
| 56 |
"The path on the HTTP server that serves as root for " |
|---|
| 57 |
"the providers"); |
|---|
| 58 |
DEFINE_string(media_config_dir, |
|---|
| 59 |
"", |
|---|
| 60 |
"Where to load / save the configs"); |
|---|
| 61 |
DEFINE_string(media_state_dir, |
|---|
| 62 |
"", |
|---|
| 63 |
"Where we may save our state"); |
|---|
| 64 |
DEFINE_string(media_state_name, |
|---|
| 65 |
"whisperer", |
|---|
| 66 |
"We save the state under this name"); |
|---|
| 67 |
|
|---|
| 68 |
DEFINE_string(admin_passwd, |
|---|
| 69 |
"", |
|---|
| 70 |
"An admin password for server administration - put this in a flag file"); |
|---|
| 71 |
DEFINE_string(authorization_realm, |
|---|
| 72 |
"", |
|---|
| 73 |
"If you set an admin password, you also need this"); |
|---|
| 74 |
|
|---|
| 75 |
DEFINE_string(rpc_config_allow_classifier, |
|---|
| 76 |
"", |
|---|
| 77 |
"If not empty we allow config rpc-s only from guys who " |
|---|
| 78 |
"qualify in the classifier created by this IpClassifier (e.g. " |
|---|
| 79 |
"'OR(IPFILTER(201.1.2.240/26), " |
|---|
| 80 |
"IPFILTERFILE(/etc/whispercast/good_rpc_ips))' )"); |
|---|
| 81 |
////////////////////////////////////////////////////////////////////// |
|---|
| 82 |
|
|---|
| 83 |
class App : public app::App { |
|---|
| 84 |
public: |
|---|
| 85 |
App(int argc, char** &argv) : |
|---|
| 86 |
app::App(argc, argv), |
|---|
| 87 |
selector_(NULL), |
|---|
| 88 |
http_net_factory_(NULL), |
|---|
| 89 |
http_server_(NULL), |
|---|
| 90 |
state_keeper_(NULL), |
|---|
| 91 |
stream_manager_(NULL), |
|---|
| 92 |
rpc_server_(NULL), |
|---|
| 93 |
admin_authenticator_(NULL) { |
|---|
| 94 |
google::SetUsageMessage("Whisperer - stream providing server."); |
|---|
| 95 |
} |
|---|
| 96 |
virtual ~App() { |
|---|
| 97 |
CHECK_NULL(selector_); |
|---|
| 98 |
CHECK_NULL(http_net_factory_); |
|---|
| 99 |
CHECK_NULL(http_server_); |
|---|
| 100 |
CHECK_NULL(stream_manager_); |
|---|
| 101 |
CHECK_NULL(state_keeper_); |
|---|
| 102 |
CHECK_NULL(rpc_server_); |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
protected: |
|---|
| 106 |
int Initialize() { |
|---|
| 107 |
// initialize streaming (this will also call common::Init()) |
|---|
| 108 |
media::Init(argc_, argv_); |
|---|
| 109 |
|
|---|
| 110 |
selector_ = new net::Selector(); |
|---|
| 111 |
|
|---|
| 112 |
http_server_params_.max_num_chunks_ = -1; |
|---|
| 113 |
http_server_params_.dlog_level_ = FLAGS_http_connection_dlog_level; |
|---|
| 114 |
|
|---|
| 115 |
http_client_params_.max_chunk_size_ = 1024; |
|---|
| 116 |
http_client_params_.dlog_level_ = FLAGS_http_connection_dlog_level; |
|---|
| 117 |
|
|---|
| 118 |
http_net_factory_ = new net::NetFactory(selector_); |
|---|
| 119 |
|
|---|
| 120 |
http_server_ = new http::Server("Whispercast Media Server"VERSION_TEXT, |
|---|
| 121 |
selector_, |
|---|
| 122 |
http_net_factory_, |
|---|
| 123 |
&http_server_params_, |
|---|
| 124 |
NewPermanentCallback( |
|---|
| 125 |
&http::SimpleServerConnectionFactory), |
|---|
| 126 |
0); |
|---|
| 127 |
if (!FLAGS_media_state_dir.empty()) { |
|---|
| 128 |
state_keeper_ = |
|---|
| 129 |
new io::StateKeeper(FLAGS_media_state_dir.c_str(), |
|---|
| 130 |
FLAGS_media_state_name.c_str()); |
|---|
| 131 |
state_keeper_->Initialize(); |
|---|
| 132 |
} else { |
|---|
| 133 |
state_keeper_ = NULL; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
if ( !FLAGS_authorization_realm.empty() ) { |
|---|
| 137 |
admin_authenticator_ = |
|---|
| 138 |
new net::SimpleUserAuthenticator(FLAGS_authorization_realm); |
|---|
| 139 |
admin_authenticator_->set_user_password(string("admin"), |
|---|
| 140 |
FLAGS_admin_passwd); |
|---|
| 141 |
} else { |
|---|
| 142 |
admin_authenticator_ = NULL; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
rpc_server_ = new rpc::HttpServer(selector_, |
|---|
| 146 |
http_server_, |
|---|
| 147 |
admin_authenticator_, |
|---|
| 148 |
"/rpc-config", |
|---|
| 149 |
true, |
|---|
| 150 |
true, 10, |
|---|
| 151 |
FLAGS_rpc_config_allow_classifier); |
|---|
| 152 |
|
|---|
| 153 |
stream_manager_ = new StreamManager( |
|---|
| 154 |
selector_, |
|---|
| 155 |
http_net_factory_, |
|---|
| 156 |
net::PROTOCOL_TCP, |
|---|
| 157 |
state_keeper_, |
|---|
| 158 |
http_server_, |
|---|
| 159 |
FLAGS_http_port, |
|---|
| 160 |
&http_client_params_, |
|---|
| 161 |
FLAGS_media_root.c_str(), |
|---|
| 162 |
rpc_server_, |
|---|
| 163 |
(FLAGS_media_config_dir + "/whisperer.config").c_str()); |
|---|
| 164 |
|
|---|
| 165 |
CHECK(rpc_server_->RegisterService(stream_manager_)); |
|---|
| 166 |
return 0; |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
void Run() { |
|---|
| 170 |
stream_manager_->Run(); |
|---|
| 171 |
} |
|---|
| 172 |
void Stop() { |
|---|
| 173 |
selector_->RunInSelectLoop(NewCallback(stream_manager_, |
|---|
| 174 |
&StreamManager::Stop)); |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
void Shutdown() { |
|---|
| 178 |
delete rpc_server_; |
|---|
| 179 |
rpc_server_ = NULL; |
|---|
| 180 |
|
|---|
| 181 |
delete stream_manager_; |
|---|
| 182 |
stream_manager_ = NULL; |
|---|
| 183 |
|
|---|
| 184 |
delete state_keeper_; |
|---|
| 185 |
state_keeper_ = NULL; |
|---|
| 186 |
|
|---|
| 187 |
delete http_server_; |
|---|
| 188 |
http_server_ = NULL; |
|---|
| 189 |
|
|---|
| 190 |
delete http_net_factory_; |
|---|
| 191 |
http_net_factory_ = NULL; |
|---|
| 192 |
|
|---|
| 193 |
delete selector_; |
|---|
| 194 |
selector_ = NULL; |
|---|
| 195 |
|
|---|
| 196 |
delete admin_authenticator_; |
|---|
| 197 |
admin_authenticator_ = NULL; |
|---|
| 198 |
|
|---|
| 199 |
// uninitialize streaming |
|---|
| 200 |
media::Shutdown(); |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
private: |
|---|
| 204 |
net::Selector* selector_; |
|---|
| 205 |
|
|---|
| 206 |
http::ServerParams http_server_params_; |
|---|
| 207 |
http::ClientParams http_client_params_; |
|---|
| 208 |
|
|---|
| 209 |
net::NetFactory* http_net_factory_; |
|---|
| 210 |
http::Server* http_server_; |
|---|
| 211 |
|
|---|
| 212 |
io::StateKeeper* state_keeper_; |
|---|
| 213 |
StreamManager* stream_manager_; |
|---|
| 214 |
|
|---|
| 215 |
rpc::HttpServer* rpc_server_; |
|---|
| 216 |
net::SimpleUserAuthenticator* admin_authenticator_; |
|---|
| 217 |
}; |
|---|
| 218 |
|
|---|
| 219 |
int main(int argc, char* argv[]) { |
|---|
| 220 |
return common::Exit(App(argc, argv).Main()); |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
/////////////////////////////////////////////////////////////////////////////// |
|---|