Skip to content

Commit

Permalink
ssh tunnel support, fixed a memory leak crash in ConnectionWindowCont…
Browse files Browse the repository at this point in the history
…roller
  • Loading branch information
bububa committed Dec 22, 2010
1 parent eeec665 commit 7e12a09
Show file tree
Hide file tree
Showing 10 changed files with 2,048 additions and 241 deletions.
6 changes: 3 additions & 3 deletions AddDBController.m
Expand Up @@ -70,13 +70,13 @@ - (IBAction)add:(id)sender {
[keys release];
if ([[dbInfo objectForKey:@"user"] isPresent] || [[dbInfo objectForKey:@"password"] isPresent]) {
Database *dbobj = [databasesArrayController dbInfo:conn name:[dbname stringValue]];
if (!dbobj) {
[dbobj release];
if (dbobj==nil) {
//[dbobj release];
dbobj = [databasesArrayController newObjectWithConn:conn name:[dbname stringValue] user:[dbInfo objectForKey:@"user"] password:[dbInfo objectForKey:@"password"]];
[databasesArrayController addObject:dbobj];
[dbobj release];
}
[self saveAction];
[dbobj release];
}
[self close];
}
Expand Down
3 changes: 1 addition & 2 deletions AuthWindowController.m
Expand Up @@ -57,12 +57,11 @@ - (IBAction)save:(id)sender {
db.user = [userTextField stringValue];
db.password = [passwordTextField stringValue];
}else {
[db release];
db = [databasesArrayController newObjectWithConn:conn name:dbname user:[userTextField stringValue] password:[passwordTextField stringValue]];
[databasesArrayController addObject:db];
[db release];
}
[self saveAction];
[db release];
[self close];
}

Expand Down
5 changes: 4 additions & 1 deletion ConnectionWindowController.h
Expand Up @@ -39,6 +39,7 @@
ImportWindowController *importWindowController;
ExportWindowController *exportWindowController;
IBOutlet NSTextField *bundleVersion;
BOOL exitThread;
}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
Expand All @@ -60,7 +61,6 @@
@property (nonatomic, retain) ImportWindowController *importWindowController;
@property (nonatomic, retain) ExportWindowController *exportWindowController;

- (void)sshConnected:(NSNotification*)aNotification;
- (void)reloadSidebar;
- (void)reloadDBList;
- (void)useDB:(id)sender;
Expand All @@ -79,4 +79,7 @@
- (void)dropDB;
- (IBAction)query:(id)sender;
- (IBAction)showAuth:(id)sender;
-(void) checkTunnel;
- (void) connect:(BOOL)haveHostAddress;
- (void) tunnelStatusChanged: (Tunnel*) tunnel status: (NSString*) status;
@end
73 changes: 55 additions & 18 deletions ConnectionWindowController.mm
Expand Up @@ -50,31 +50,37 @@ - (id)init {
return self;
}

- (void)sshConnected:(NSNotification*)aNotification {
NSLog(@"connected");
- (void) tunnelStatusChanged: (Tunnel*) tunnel status: (NSString*) status {
NSLog(@"%@", status);
if( [status isEqualToString: @"CONNECTED"] ){
exitThread = YES;
[self connect:YES];
}
}

- (void)windowDidLoad {
[super windowDidLoad];
NSString *appVersion = [[NSString alloc] initWithFormat:@"version(%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey] ];
[bundleVersion setStringValue: appVersion];
[appVersion release];

NSString *hostaddress;
if ([conn.usessh intValue]==1) {
NSString *portForward = [[NSString alloc] initWithFormat:@"L %d:%@:%d", conn.hostport, conn.bindaddress, conn.bindport];
NSMutableArray *portForwardings = [NSMutableArray arrayWithObjects:portForward, nil];
- (void) connect:(BOOL)haveHostAddress {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *hostaddress = [[[NSString alloc] init] autorelease];
if (!haveHostAddress && [conn.usessh intValue]==1) {
NSString *portForward = [[NSString alloc] initWithFormat:@"L:%@:%@:%@:%@", conn.hostport, conn.host, conn.sshhost, conn.bindport];
NSMutableArray *portForwardings = [[NSMutableArray alloc] initWithObjects:portForward, nil];
[portForward release];
sshTunnel =[[Tunnel alloc] init];
[sshTunnel setDelegate:self];
[sshTunnel setUser:conn.sshuser];
[sshTunnel setHost:conn.sshhost];
[sshTunnel setPassword:conn.sshpassword];
[sshTunnel setPort:[conn.sshport intValue]];
[sshTunnel setPortForwardings:portForwardings];
[sshTunnel setAliveCountMax:3];
[sshTunnel setAliveInterval:30];
[sshTunnel setTcpKeepAlive:YES];
[sshTunnel setCompression:YES];NSLog(@"here");
[sshTunnel setCompression:YES];
[sshTunnel start];
[portForwardings release];
return;
hostaddress = [NSString stringWithFormat:@"%@:%@", conn.host, conn.hostport];
}else if ([conn.host isEqualToString:@"flame.mongohq.com"]) {
}else if (!haveHostAddress && [conn.host isEqualToString:@"flame.mongohq.com"]) {
hostaddress = [NSString stringWithFormat:@"%@:%@/%@", conn.host, conn.hostport, conn.defaultdb];
}else {
hostaddress = [NSString stringWithFormat:@"%@:%@", conn.host, conn.hostport];
Expand All @@ -92,6 +98,35 @@ - (void)windowDidLoad {

[self reloadSidebar];
[self showServerStatus:nil];
[pool release];
}

- (void)windowDidLoad {
[super windowDidLoad];
exitThread = NO;
NSString *appVersion = [[NSString alloc] initWithFormat:@"version(%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey] ];
[bundleVersion setStringValue: appVersion];
[appVersion release];
[self connect:NO];
if ([conn.usessh intValue]==1) {NSLog(@"thread");
[NSThread detachNewThreadSelector: @selector(checkTunnel) toTarget:self withObject:nil ];
}
}

- (void)checkTunnel {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while(!exitThread){
@synchronized(self){
[sshTunnel readStatus];
/*if( [sshTunnel running] == YES && [sshTunnel checkProcess] == NO ){
[sshTunnel stop];
[NSThread sleepForTimeInterval:2];
[sshTunnel start];
}*/
}
[NSThread sleepForTimeInterval:3];
}
[pool release];
}

- (void)dealloc {
Expand All @@ -117,8 +152,10 @@ - (void)dealloc {
}

- (void)windowWillClose:(NSNotification *)notification {
[sshTunnel terminate];
[sshTunnel waitUntilExit];
if ([sshTunnel running]) {
[sshTunnel stop];
}
//exitThread = YES;
selectedDB = nil;
selectedCollection = nil;
[self release];
Expand Down Expand Up @@ -210,9 +247,9 @@ - (IBAction)showServerStatus:(id)sender
[resultsTitle setStringValue:[NSString stringWithFormat:@"Server %@:%@ stats", conn.host, conn.hostport]];
NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[mongoDB serverStatus]];
resultsOutlineViewController.results = results;
[resultsOutlineViewController.myOutlineView reloadData];
[resultsOutlineViewController.myOutlineView reloadData];//NSLog(@"STATUS: %@", results);
[results release];
//NSLog(@"STATUS: %@", results);

}

- (IBAction)showDBStats:(id)sender
Expand Down
2 changes: 2 additions & 0 deletions MongoDB.mm
Expand Up @@ -80,6 +80,7 @@ - (NSArray *)listDatabases {
}
NSArray *response = [NSArray arrayWithArray:dblist];
[dblist release];
NSLog(@"List Databases");
return response;
}catch( mongo::DBException &e ) {
NSRunAlertPanel(@"Error", [NSString stringWithUTF8String:e.what()], @"OK", nil, nil);
Expand Down Expand Up @@ -123,6 +124,7 @@ - (NSMutableArray *) serverStatus
try {
mongo::BSONObj retval;
conn->runCommand("admin", BSON("serverStatus"<<1), retval);
NSLog(@"Show Server Status");
return [self bsonDictWrapper:retval];
}catch (mongo::DBException &e) {
NSRunAlertPanel(@"Error", [NSString stringWithUTF8String:e.what()], @"OK", nil, nil);
Expand Down

0 comments on commit 7e12a09

Please sign in to comment.