0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 09:06:21 +01:00
mongodb/tools/bridge.cpp

132 lines
3.4 KiB
C++
Raw Normal View History

2009-04-02 21:10:52 +02:00
// bridge.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* 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.
*
* 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.
*
* 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/>.
*/
2010-04-27 21:27:52 +02:00
#include "pch.h"
2009-04-02 21:10:52 +02:00
#include "../util/message.h"
#include "../client/dbclient.h"
using namespace mongo;
using namespace std;
int port = 0;
string destUri;
class Forwarder {
public:
Forwarder( MessagingPort &mp ) : mp_( mp ) {
}
void operator()() const {
DBClientConnection dest;
string errmsg;
2009-04-02 23:58:18 +02:00
while( !dest.connect( destUri, errmsg ) )
sleepmillis( 500 );
2009-04-02 21:10:52 +02:00
Message m;
while( 1 ) {
m.reset();
if ( !mp_.recv( m ) ) {
cout << "end connection " << mp_.farEnd.toString() << endl;
mp_.shutdown();
break;
}
2009-04-03 16:48:26 +02:00
2010-05-13 00:26:00 +02:00
int oldId = m.header()->id;
if ( m.operation() == dbQuery || m.operation() == dbMsg || m.operation() == dbGetMore ) {
2009-04-02 21:10:52 +02:00
Message response;
dest.port().call( m, response );
mp_.reply( m, response, oldId );
} else {
dest.port().say( m, oldId );
}
}
}
private:
MessagingPort &mp_;
};
2009-05-20 20:16:14 +02:00
set<MessagingPort*> ports;
2009-04-02 21:10:52 +02:00
class MyListener : public Listener {
public:
MyListener( int port ) : Listener( "", port ) {}
2009-04-02 21:10:52 +02:00
virtual void accepted(MessagingPort *mp) {
2009-05-20 20:16:14 +02:00
ports.insert( mp );
2009-04-02 21:10:52 +02:00
Forwarder f( *mp );
boost::thread t( f );
}
};
auto_ptr< MyListener > listener;
#if !defined(_WIN32)
void cleanup( int sig ) {
ListeningSockets::get()->closeAll();
2009-05-20 20:16:14 +02:00
for ( set<MessagingPort*>::iterator i = ports.begin(); i != ports.end(); i++ )
(*i)->shutdown();
::exit( 0 );
}
2009-04-03 16:48:26 +02:00
void setupSignals() {
signal( SIGINT , cleanup );
signal( SIGTERM , cleanup );
signal( SIGPIPE , cleanup );
signal( SIGABRT , cleanup );
signal( SIGSEGV , cleanup );
signal( SIGBUS , cleanup );
signal( SIGFPE , cleanup );
}
#else
inline void setupSignals() {}
#endif
2009-05-20 20:16:14 +02:00
void helpExit() {
cout << "usage mongobridge --port <port> --dest <destUri>" << endl;
cout << " port: port to listen for mongo messages" << endl;
cout << " destUri: uri of remote mongod instance" << endl;
::exit( -1 );
}
void check( bool b ) {
if ( !b )
helpExit();
}
int main( int argc, char **argv ) {
static StaticObserver staticObserver;
setupSignals();
2009-04-02 21:10:52 +02:00
check( argc == 5 );
2009-04-03 16:48:26 +02:00
2009-04-02 21:10:52 +02:00
for( int i = 1; i < 5; ++i ) {
check( i % 2 != 0 );
if ( strcmp( argv[ i ], "--port" ) == 0 ) {
port = strtol( argv[ ++i ], 0, 10 );
} else if ( strcmp( argv[ i ], "--dest" ) == 0 ) {
destUri = argv[ ++i ];
} else {
check( false );
}
}
check( port != 0 && !destUri.empty() );
2009-04-03 16:48:26 +02:00
listener.reset( new MyListener( port ) );
2010-04-05 16:51:33 +02:00
listener->initAndListen();
2009-04-03 16:48:26 +02:00
2009-04-02 21:10:52 +02:00
return 0;
2009-04-03 16:48:26 +02:00
}