Skip to content

Commit

Permalink
Add mongobridge
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Apr 2, 2009
1 parent 662776e commit 3bca999
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -50,6 +50,7 @@ mongoexport

mongoimportjson
mongosniff
mongobridge

*.tgz

Expand Down
2 changes: 2 additions & 0 deletions SConstruct
Expand Up @@ -591,6 +591,8 @@ env.Program( "mongoimportjson" , allToolFiles + [ "tools/importJSON.cpp" ] )

env.Program( "mongofiles" , allToolFiles + [ "tools/files.cpp" ] )

env.Program( "mongobridge" , allToolFiles + [ "tools/bridge.cpp" ] )

# mongos
mongos = env.Program( "mongos" , commonFiles + coreDbFiles + coreServerFiles + shardServerFiles )

Expand Down
2 changes: 2 additions & 0 deletions mongo.xcodeproj/project.pbxproj
Expand Up @@ -22,6 +22,7 @@
9302D9A20F30AB8C00DFA4EF /* utils.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = utils.js; sourceTree = "<group>"; };
931183420F8277FD00A6DC44 /* repl7.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = repl7.js; sourceTree = "<group>"; };
931184DC0F83C95800A6DC44 /* message_server_port.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = message_server_port.cpp; sourceTree = "<group>"; };
931186FB0F8535FF00A6DC44 /* bridge.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = bridge.cpp; sourceTree = "<group>"; };
931A027A0F58AA4400147C0E /* jsobjmanipulator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jsobjmanipulator.h; sourceTree = "<group>"; };
93278F570F72D32900844664 /* gridfs.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = gridfs.cpp; sourceTree = "<group>"; };
93278F580F72D32900844664 /* gridfs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = gridfs.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -519,6 +520,7 @@
93A13A320F4620E500AF1B0D /* tools */ = {
isa = PBXGroup;
children = (
931186FB0F8535FF00A6DC44 /* bridge.cpp */,
93A13A330F4620E500AF1B0D /* dump.cpp */,
93A13A350F4620E500AF1B0D /* export.cpp */,
93A13A370F4620E500AF1B0D /* files.cpp */,
Expand Down
102 changes: 102 additions & 0 deletions tools/bridge.cpp
@@ -0,0 +1,102 @@
// 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/>.
*/

#include "stdafx.h"
#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;
massert( errmsg, dest.connect( destUri, errmsg ) );
Message m;
while( 1 ) {
m.reset();
if ( !mp_.recv( m ) ) {
cout << "end connection " << mp_.farEnd.toString() << endl;
mp_.shutdown();
break;
}

int oldId = m.data->id;
if ( m.data->operation() == dbQuery || m.data->operation() == dbMsg || m.data->operation() == dbGetMore ) {
Message response;
dest.port().call( m, response );
mp_.reply( m, response, oldId );
} else {
dest.port().say( m, oldId );
}
}
}
private:
MessagingPort &mp_;
};

class MyListener : public Listener {
public:
MyListener( int port ) : Listener( port ) {}
virtual void accepted(MessagingPort *mp) {
Forwarder f( *mp );
boost::thread t( f );
}
};

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 ) {

check( argc == 5 );

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() );

MyListener l( port );
l.init();
l.listen();

return 0;
}

0 comments on commit 3bca999

Please sign in to comment.