Permalink
Browse files

Add replica set connection support.

  • Loading branch information...
bububa committed Dec 25, 2010
1 parent 8cc1db9 commit d67d476a2fe824a761b4f67f9f8b9f62bbad8a81
@@ -12,6 +12,9 @@
@interface AddConnectionController : NSWindowController {
IBOutlet NSTextField *hostTextField;
IBOutlet NSTextField *hostportTextField;
+ IBOutlet NSButton *usereplCheckBox;
+ IBOutlet NSTextField *serversTextField;
+ IBOutlet NSTextField *replnameTextField;
IBOutlet NSTextField *aliasTextField;
IBOutlet NSTextField *adminuserTextField;
IBOutlet NSSecureTextField *adminpassTextField;
@@ -30,6 +33,9 @@
@property (nonatomic, retain) NSTextField *hostTextField;
@property (nonatomic, retain) NSTextField *hostportTextField;
+@property (nonatomic, retain) NSButton *usereplCheckBox;
+@property (nonatomic, retain) NSTextField *serversTextField;
+@property (nonatomic, retain) NSTextField *replnameTextField;
@property (nonatomic, retain) NSTextField *aliasTextField;
@property (nonatomic, retain) NSTextField *adminuserTextField;
@property (nonatomic, retain) NSSecureTextField *adminpassTextField;
@@ -48,6 +54,7 @@
- (IBAction)cancel:(id)sender;
- (IBAction)add:(id)sender;
- (IBAction)enableSSH:(id)sender;
+- (IBAction)enableRepl:(id)sender;
- (BOOL)validateConnection;
@end
View
@@ -14,6 +14,9 @@ @implementation AddConnectionController
@synthesize hostTextField;
@synthesize hostportTextField;
+@synthesize usereplCheckBox;
+@synthesize serversTextField;
+@synthesize replnameTextField;
@synthesize aliasTextField;
@synthesize adminuserTextField;
@synthesize adminpassTextField;
@@ -37,6 +40,9 @@ - (id)init {
- (void)dealloc {
[hostTextField release];
[hostportTextField release];
+ [usereplCheckBox release];
+ [serversTextField release];
+ [replnameTextField release];
[aliasTextField release];
[adminuserTextField release];
[adminpassTextField release];
@@ -71,6 +77,9 @@ - (IBAction)cancel:(id)sender {
- (IBAction)add:(id)sender {
NSString *host;
NSUInteger hostport;
+ NSString *servers;
+ NSString *repl_name;
+ NSUInteger userepl = 0;
NSString *alias;
NSString *adminuser = [adminuserTextField stringValue];
NSString *adminpass = [adminpassTextField stringValue];
@@ -92,43 +101,45 @@ - (IBAction)add:(id)sender {
}else{
hostport = [hostportTextField intValue];
}
+
+ servers = [[NSString alloc] initWithString:[serversTextField stringValue]];
+ repl_name = [[NSString alloc] initWithString:[replnameTextField stringValue]];
+ if ([usereplCheckBox state])
+ {
+ userepl = 1;
+ }
+
if ([ [aliasTextField stringValue] length] == 0) {
alias = [[NSString alloc] initWithString:@"localhost"];
}else{
alias = [[NSString alloc] initWithString:[aliasTextField stringValue]];
}
- if ([usesshCheckBox state])
- {
- usessh = 1;
- if ([ [bindaddressTextField stringValue] length] == 0) {
- bindaddress = [[NSString alloc] initWithString:@"127.0.0.1"];
- }else{
- bindaddress = [[NSString alloc] initWithString:[bindaddressTextField stringValue]];
- }
- if ([ [bindportTextField stringValue] length] == 0) {
- bindport = 8888;
- }else{
- bindport = [bindportTextField intValue];
- }
- sshhost = [[NSString alloc] initWithString:[sshhostTextField stringValue]];
- if ([[sshportTextField stringValue] length] == 0) {
- sshport = 22;
- }else {
- sshport = [sshportTextField intValue];
- }
-
- sshuser = [[NSString alloc] initWithString:[sshuserTextField stringValue]];
- sshpassword = [[NSString alloc] initWithString:[sshpasswordTextField stringValue]];
+ if ([ [bindaddressTextField stringValue] length] == 0) {
+ bindaddress = [[NSString alloc] initWithString:@"127.0.0.1"];
}else{
- bindaddress = @"127.0.0.1";
+ bindaddress = [[NSString alloc] initWithString:[bindaddressTextField stringValue]];
+ }
+ if ([ [bindportTextField stringValue] length] == 0) {
bindport = 8888;
- sshhost = @"";
+ }else{
+ bindport = [bindportTextField intValue];
+ }
+ sshhost = [[NSString alloc] initWithString:[sshhostTextField stringValue]];
+ if ([[sshportTextField stringValue] length] == 0) {
sshport = 22;
- sshuser = @"";
- sshpassword = @"";
+ }else {
+ sshport = [sshportTextField intValue];
+ }
+
+ sshuser = [[NSString alloc] initWithString:[sshuserTextField stringValue]];
+ sshpassword = [[NSString alloc] initWithString:[sshpasswordTextField stringValue]];
+ if ([usesshCheckBox state])
+ {
+ usessh = 1;
+
}
- NSArray *keys = [[NSArray alloc] initWithObjects:@"host", @"hostport", @"alias", @"adminuser", @"adminpass", @"defaultdb", @"usessh", @"bindaddress", @"bindport", @"sshhost", @"sshport", @"sshuser", @"sshpassword", nil];
- NSArray *objs = [[NSArray alloc] initWithObjects:host, [NSNumber numberWithInt:hostport], alias, adminuser, adminpass, defaultdb, [NSNumber numberWithInt:usessh], bindaddress, [NSNumber numberWithInt:bindport], sshhost, [NSNumber numberWithInt:sshport], sshuser, sshpassword, nil];
+ NSArray *keys = [[NSArray alloc] initWithObjects:@"host", @"hostport", @"userepl", @"servers", @"repl_name", @"alias", @"adminuser", @"adminpass", @"defaultdb", @"usessh", @"bindaddress", @"bindport", @"sshhost", @"sshport", @"sshuser", @"sshpassword", nil];
+ NSArray *objs = [[NSArray alloc] initWithObjects:host, [NSNumber numberWithInt:hostport], [NSNumber numberWithInt:userepl], servers, repl_name, alias, adminuser, adminpass, defaultdb, [NSNumber numberWithInt:usessh], bindaddress, [NSNumber numberWithInt:bindport], sshhost, [NSNumber numberWithInt:sshport], sshuser, sshpassword, nil];
if (!connectionInfo) {
connectionInfo = [[NSMutableDictionary alloc] initWithCapacity:13];
}
@@ -137,13 +148,12 @@ - (IBAction)add:(id)sender {
[objs release];
[host release];
[alias release];
- if (usessh == 1)
- {
- [sshhost release];
- [sshuser release];
- [sshpassword release];
- [bindaddress release];
- }
+ [servers release];
+ [repl_name release];
+ [sshhost release];
+ [sshuser release];
+ [sshpassword release];
+ [bindaddress release];
if ([self validateConnection]) {
[self close];
}
@@ -171,6 +181,10 @@ - (BOOL)validateConnection
NSRunAlertPanel(@"Error", @"Please full fill ssh information!", @"OK", nil, nil);
return NO;
}
+ if ([usereplCheckBox state] == 1 && ([[connectionInfo objectForKey:@"servers"] length] == 0 || [[connectionInfo objectForKey:@"repl_name"] length] == 0)) {
+ NSRunAlertPanel(@"Error", @"Please full fill replica-set information!", @"OK", nil, nil);
+ return NO;
+ }
return YES;
}
@@ -193,5 +207,18 @@ - (IBAction)enableSSH:(id)sender
[sshportTextField setEnabled:NO];
}
+}
+
+- (IBAction)enableRepl:(id)sender
+{
+ if ([usereplCheckBox state] == 1)
+ {
+ [serversTextField setEnabled:YES];
+ [replnameTextField setEnabled:YES];
+ }else {
+ [serversTextField setEnabled:NO];
+ [replnameTextField setEnabled:NO];
+ }
+
}
@end
View
@@ -11,6 +11,8 @@
@interface Connection: NSManagedObject {
NSString *host;
NSNumber *hostport;
+ NSString *servers;
+ NSString *repl_name;
NSString *alias;
NSString *adminuser;
NSString *adminpass;
@@ -23,10 +25,13 @@
NSNumber *bindport;
NSSet *databases;
NSNumber *usessh;
+ NSNumber *userepl;
}
@property (nonatomic, retain) NSString *host;
@property (nonatomic, retain) NSNumber *hostport;
+@property (nonatomic, retain) NSString *servers;
+@property (nonatomic, retain) NSString *repl_name;
@property (nonatomic, retain) NSString *alias;
@property (nonatomic, retain) NSString *adminuser;
@property (nonatomic, retain) NSString *adminpass;
@@ -39,5 +44,6 @@
@property (nonatomic, retain) NSString *bindaddress;
@property (nonatomic, retain) NSNumber *bindport;
@property (nonatomic, retain) NSNumber *usessh;
+@property (nonatomic, retain) NSNumber *userepl;
@end
View
@@ -13,11 +13,14 @@ @implementation Connection
@dynamic host;
@dynamic hostport;
+@dynamic servers;
+@dynamic repl_name;
@dynamic alias;
@dynamic adminuser;
@dynamic adminpass;
@dynamic defaultdb;
@dynamic databases;
+@dynamic userepl;
@dynamic usessh;
@dynamic sshhost;
@@ -25,7 +25,6 @@
#import "SidebarNode.h"
#import "MongoDB.h"
#import "Tunnel.h"
-#import <mongo/client/dbclient.h>
@implementation ConnectionWindowController
@@ -69,6 +68,7 @@ - (void) tunnelStatusChanged: (Tunnel*) tunnel status: (NSString*) status {
- (void) connect:(BOOL)haveHostAddress {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[loaderIndicator start];
+ bool connected;
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];
@@ -89,26 +89,42 @@ - (void) connect:(BOOL)haveHostAddress {
//[sshTunnel start];
[portForwardings release];
return;
- hostaddress = [NSString stringWithFormat:@"%@:%@", conn.host, conn.hostport];
}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];
- }
- mongoDB = [[MongoDB alloc] initWithConn:hostaddress];
- if ([conn.adminuser isPresent]) {
- [mongoDB authUser:conn.adminuser pass:conn.adminpass database:conn.defaultdb];
- }
-
- if (![conn.defaultdb isPresent]) {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addDB:) name:kNewDBWindowWillClose object:nil];
+ if (conn.userepl) {
+ hostaddress = conn.repl_name;
+ NSArray *tmp = [conn.servers componentsSeparatedByString:@","];
+ NSMutableArray *hosts = [[NSMutableArray alloc] initWithCapacity:[tmp count]];
+ for (NSString *h in tmp) {
+ NSString *host = [h stringByTrimmingWhitespace];
+ if ([host length] == 0) {
+ continue;
+ }
+ [hosts addObject:host];
+ }
+ connected = mongoDB = [[MongoDB alloc] initWithConn:conn.repl_name hosts:hosts];
+ [hosts release];
+ }else{
+ hostaddress = [NSString stringWithFormat:@"%@:%@", conn.host, conn.hostport];
+ connected = mongoDB = [[MongoDB alloc] initWithConn:hostaddress];
+ }
}
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCollection:) name:kNewCollectionWindowWillClose object:nil];
[loaderIndicator stop];
- [monitorButton setEnabled:YES];
- [self reloadSidebar];
- [self showServerStatus:nil];
+ if (connected) {
+ if ([conn.adminuser isPresent]) {
+ [mongoDB authUser:conn.adminuser pass:conn.adminpass database:conn.defaultdb];
+ }
+
+ if (![conn.defaultdb isPresent]) {
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addDB:) name:kNewDBWindowWillClose object:nil];
+ }
+
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCollection:) name:kNewCollectionWindowWillClose object:nil];
+ [monitorButton setEnabled:YES];
+ [self reloadSidebar];
+ [self showServerStatus:nil];
+ }
[pool release];
}
@@ -176,7 +192,6 @@ - (void)windowWillClose:(NSNotification *)notification {
//exitThread = YES;
selectedDB = nil;
selectedCollection = nil;
- [self release];
}
- (void)reloadSidebar {
@@ -21,7 +21,12 @@ - (id)transformedValue:(id)value
{
if (value)
{
- return [NSString stringWithFormat:@"%@ [%@:%@]", [value alias], [value host], [value hostport] ];
+ if ([value userepl]) {
+ return [NSString stringWithFormat:@"%@ [%@]", [value alias], [value repl_name] ];
+ }else {
+ return [NSString stringWithFormat:@"%@ [%@:%@]", [value alias], [value host], [value hostport] ];
+ }
+
}
return nil;
}
Oops, something went wrong.

0 comments on commit d67d476

Please sign in to comment.