0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/s/server.cpp

165 lines
4.3 KiB
C++
Raw Normal View History

2009-02-03 19:15:54 +01:00
// server.cpp
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2008-12-29 02:28:49 +01:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2008-12-29 02:28:49 +01:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
2009-02-03 19:30:28 +01:00
#include "../util/message.h"
#include "../util/unittest.h"
2008-11-01 01:17:54 +01:00
#include "../client/connpool.h"
#include "../util/message_server.h"
2009-02-03 23:10:44 +01:00
2009-02-05 17:50:27 +01:00
#include "server.h"
2009-02-19 18:55:01 +01:00
#include "request.h"
2009-02-13 03:03:46 +01:00
#include "config.h"
2009-02-17 20:41:31 +01:00
#include "shard.h"
2009-01-14 23:09:51 +01:00
namespace mongo {
int port = 27017;
const char *curNs = "";
Database *database = 0;
2009-02-03 23:10:44 +01:00
string ourHostname;
string getDbContext() {
return "?";
}
2008-12-05 00:11:25 +01:00
2009-02-03 19:15:54 +01:00
void usage( char * argv[] ){
out() << argv[0] << " usage:\n\n";
2009-02-05 17:50:27 +01:00
out() << " -v+ verbose\n";
out() << " --port <portno>\n";
2009-02-05 17:50:27 +01:00
out() << " --configdb <configdbname> [<configdbname>...]\n";
2009-03-11 18:49:54 +01:00
// out() << " --infer infer configdbname by replacing \"-n<n>\"\n";
// out() << " in our hostname with \"-grid\".\n";
out() << endl;
}
class ShardedMessageHandler : public MessageHandler {
public:
virtual ~ShardedMessageHandler(){}
virtual void process( Message& m , AbstractMessagingPort* p ){
Request r( m , p );
2009-02-19 18:55:01 +01:00
try {
r.process();
}
catch ( DBException& e ){
log() << "UserException: " << e.what() << endl;
if ( r.expectResponse() ){
BSONObj err = BSON( "$err" << e.what() );
replyToQuery( QueryResult::ResultFlag_ErrSet, p , m , err );
2009-02-19 18:55:01 +01:00
}
}
}
};
void start() {
log() << "waiting for connections on port " << port << endl;
//DbGridListener l(port);
//l.listen();
ShardedMessageHandler handler;
MessageServer * server = createServer( port , &handler );
server->run();
}
2008-09-11 22:25:16 +02:00
2009-01-14 23:09:51 +01:00
} // namespace mongo
using namespace mongo;
2008-12-29 02:28:49 +01:00
int main(int argc, char* argv[], char *envp[] ) {
2009-02-18 16:10:39 +01:00
bool justTests = false;
2009-02-03 23:10:44 +01:00
bool infer = false;
2009-02-05 17:50:27 +01:00
vector<string> configdbs;
2009-02-03 23:10:44 +01:00
2008-09-11 22:25:16 +02:00
for (int i = 1; i < argc; i++) {
2008-12-29 02:28:49 +01:00
if ( argv[i] == 0 ) continue;
2008-09-11 22:25:16 +02:00
string s = argv[i];
2008-12-29 02:28:49 +01:00
if ( s == "--port" ) {
2008-09-11 22:25:16 +02:00
port = atoi(argv[++i]);
}
2009-01-14 23:17:24 +01:00
else if ( s == "--infer" ) {
2009-02-03 23:10:44 +01:00
infer = true;
}
2009-02-05 17:50:27 +01:00
else if ( s == "--configdb" ) {
2009-02-03 23:10:44 +01:00
assert( ! infer );
2009-02-18 16:10:39 +01:00
2009-02-03 23:10:44 +01:00
while ( ++i < argc )
2009-02-05 17:50:27 +01:00
configdbs.push_back(argv[i]);
2009-02-03 23:10:44 +01:00
2009-02-05 17:50:27 +01:00
if ( configdbs.size() == 0 ) {
out() << "error: no args for --configdb\n";
return 4;
}
2009-02-03 23:10:44 +01:00
2009-02-05 17:50:27 +01:00
if ( configdbs.size() > 2 ) {
out() << "error: --configdb does not support more than 2 parameters yet\n";
return 5;
}
2008-11-13 21:52:20 +01:00
}
2009-02-05 17:50:27 +01:00
else if ( s.find( "-v" ) == 0 ){
logLevel = s.size() - 1;
}
2009-02-18 16:10:39 +01:00
else if ( s == "--test" ) {
justTests = true;
logLevel = 5;
}
2008-12-29 02:28:49 +01:00
else {
2009-02-03 19:15:54 +01:00
usage( argv );
2008-09-30 00:00:53 +02:00
return 3;
}
2008-09-11 22:25:16 +02:00
}
2009-02-03 23:10:44 +01:00
2009-02-18 16:10:39 +01:00
UnitTest::runTests();
if ( justTests ){
cout << "tests passed" << endl;
return 0;
2009-02-17 20:41:31 +01:00
}
2009-02-18 16:10:39 +01:00
2009-02-17 20:41:31 +01:00
if ( argc <= 1 ) {
usage( argv );
return 3;
}
2008-09-11 22:25:16 +02:00
bool ok = port != 0;
2009-02-03 23:10:44 +01:00
2008-12-29 02:28:49 +01:00
if ( !ok ) {
2009-02-03 19:15:54 +01:00
usage( argv );
2008-09-11 22:25:16 +02:00
return 1;
}
log() << argv[0] << " v0.0.1 starting (--help for usage)" << endl;
2009-03-17 15:22:26 +01:00
printGitVersion();
2009-03-18 19:15:52 +01:00
printSysInfo();
2009-02-03 23:10:44 +01:00
2009-02-05 17:50:27 +01:00
if ( ! configServer.init( configdbs , infer ) ){
2009-02-03 23:10:44 +01:00
cerr << "couldn't connectd to config db" << endl;
return 7;
}
assert( configServer.ok() );
2008-09-11 22:25:16 +02:00
start();
2008-12-29 02:28:49 +01:00
dbexit(0);
return 0;
}
#undef exit
2009-02-03 16:12:01 +01:00
void mongo::dbexit(int rc, const char *why) {
log() << "dbexit: " << why << " rc:" << rc << endl;
::exit(rc);
}