2009-02-03 19:15:54 +01:00
|
|
|
// server.cpp
|
2008-09-11 16:45:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-09-11 16:45:57 +02: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
|
|
|
*
|
2008-09-11 16:45:57 +02: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
|
|
|
*
|
2008-09-11 16:45:57 +02: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"
|
2008-09-11 16:45:57 +02:00
|
|
|
#include "../util/unittest.h"
|
2008-11-01 01:17:54 +01:00
|
|
|
#include "../client/connpool.h"
|
2008-10-20 00:46:53 +02:00
|
|
|
#include "gridconfig.h"
|
2008-09-11 16:45:57 +02:00
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
bool dashDashInfer = false;
|
|
|
|
vector<string> dashDashGridDb;
|
|
|
|
int port = 27017;
|
|
|
|
const char *curNs = "";
|
|
|
|
Database *database = 0;
|
|
|
|
|
|
|
|
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-01-15 17:26:38 +01:00
|
|
|
out() << " --port <portno>\n";
|
|
|
|
out() << " --griddb <griddbname> [<griddbname>...]\n";
|
|
|
|
out() << " --infer infer griddbname by replacing \"-n<n>\"\n";
|
|
|
|
out() << " in our hostname with \"-grid\".\n";
|
|
|
|
out() << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
MessagingPort *grab = 0;
|
|
|
|
void processRequest(Message&, MessagingPort&);
|
2008-09-12 21:00:20 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
void _dbGridConnThread() {
|
|
|
|
MessagingPort& dbMsgPort = *grab;
|
|
|
|
grab = 0;
|
|
|
|
Message m;
|
|
|
|
while ( 1 ) {
|
|
|
|
m.reset();
|
2008-09-12 21:00:20 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( !dbMsgPort.recv(m) ) {
|
|
|
|
log() << "end connection " << dbMsgPort.farEnd.toString() << endl;
|
|
|
|
dbMsgPort.shutdown();
|
|
|
|
break;
|
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
processRequest(m, dbMsgPort);
|
|
|
|
}
|
2008-09-12 21:00:20 +02:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2008-09-11 22:25:16 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
void dbGridConnThread() {
|
|
|
|
MessagingPort *p = grab;
|
2008-12-29 02:28:49 +01:00
|
|
|
try {
|
2009-01-15 16:17:11 +01:00
|
|
|
_dbGridConnThread();
|
|
|
|
} catch ( ... ) {
|
|
|
|
problem() << "uncaught exception in dbgridconnthread, closing connection" << endl;
|
|
|
|
delete p;
|
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2009-01-15 16:17:11 +01:00
|
|
|
|
|
|
|
class DbGridListener : public Listener {
|
|
|
|
public:
|
|
|
|
DbGridListener(int p) : Listener(p) { }
|
|
|
|
virtual void accepted(MessagingPort *mp) {
|
|
|
|
assert( grab == 0 );
|
|
|
|
grab = mp;
|
|
|
|
boost::thread thr(dbGridConnThread);
|
|
|
|
while ( grab )
|
|
|
|
sleepmillis(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void start() {
|
|
|
|
gridDatabase.init();
|
|
|
|
/*
|
|
|
|
try {
|
2009-01-15 17:26:38 +01:00
|
|
|
out() << "TEMP" << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
{
|
|
|
|
ScopedDbConnection c("localhost");
|
2009-01-15 17:26:38 +01:00
|
|
|
out() << c.conn().findOne("dwight.bar", emptyObj).toString() << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
c.done();
|
2009-01-15 17:26:38 +01:00
|
|
|
out() << "OK1" << endl;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2009-01-15 16:17:11 +01:00
|
|
|
{
|
|
|
|
ScopedDbConnection c("localhost");
|
|
|
|
c.conn().findOne("dwight.bar", emptyObj);
|
|
|
|
c.done();
|
2009-01-15 17:26:38 +01:00
|
|
|
out() << "OK1" << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2009-01-15 17:26:38 +01:00
|
|
|
out() << "OK2" << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
} catch(...) {
|
2009-01-15 17:26:38 +01:00
|
|
|
out() << "exception" << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
*/
|
2008-09-30 00:32:39 +02:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
log() << "waiting for connections on port " << port << "..." << endl;
|
|
|
|
DbGridListener l(port);
|
|
|
|
l.listen();
|
|
|
|
}
|
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[] ) {
|
2008-09-11 16:45:57 +02:00
|
|
|
|
2008-12-29 02:28:49 +01:00
|
|
|
if ( argc <= 1 ) {
|
2009-02-03 19:15:54 +01:00
|
|
|
usage( argv );
|
2008-12-05 18:10:03 +01:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
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-01-06 16:48:09 +01:00
|
|
|
dashDashInfer = true;
|
|
|
|
}
|
2008-12-29 02:28:49 +01:00
|
|
|
else if ( s == "--griddb" ) {
|
2009-01-06 16:48:09 +01:00
|
|
|
assert( !dashDashInfer );
|
|
|
|
int n = 0;
|
2009-01-14 23:17:24 +01:00
|
|
|
while ( ++i < argc ) {
|
2009-01-06 16:48:09 +01:00
|
|
|
dashDashGridDb.push_back(argv[i]);
|
|
|
|
n++;
|
|
|
|
}
|
2009-01-14 23:17:24 +01:00
|
|
|
if ( n == 0 ) {
|
2009-01-15 17:26:38 +01:00
|
|
|
out() << "error: no args for --griddb\n";
|
2009-01-06 16:48:09 +01:00
|
|
|
return 4;
|
|
|
|
}
|
2009-01-14 23:17:24 +01:00
|
|
|
if ( n > 2 ) {
|
2009-01-15 17:26:38 +01:00
|
|
|
out() << "error: --griddb does not support more than 2 parameters yet\n";
|
2009-01-06 16:48:09 +01:00
|
|
|
return 5;
|
|
|
|
}
|
2008-11-13 21:52:20 +01:00
|
|
|
}
|
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
|
|
|
}
|
2008-09-11 16:45:57 +02:00
|
|
|
|
2008-09-11 22:25:16 +02:00
|
|
|
bool ok = port != 0;
|
|
|
|
|
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;
|
|
|
|
}
|
2008-09-11 16:45:57 +02:00
|
|
|
|
2009-02-03 19:15:54 +01:00
|
|
|
log() << argv[0] << " starting (--help for usage)" << endl;
|
2008-12-29 02:28:49 +01:00
|
|
|
UnitTest::runTests();
|
2008-09-11 22:25:16 +02:00
|
|
|
start();
|
2008-12-29 02:28:49 +01:00
|
|
|
dbexit(0);
|
|
|
|
return 0;
|
2008-09-11 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
|
2008-09-11 16:45:57 +02:00
|
|
|
#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);
|
|
|
|
}
|