-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathConnectionWindowController.mm
644 lines (599 loc) · 21.4 KB
/
ConnectionWindowController.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//
// ConnectionWindowController.m
// MongoHub
//
// Created by Syd on 10-4-25.
// Copyright 2010 MusicPeace.ORG. All rights reserved.
//
#import "Configure.h"
#import "NSString+Extras.h"
#import "NSProgressIndicator+Extras.h"
#import <BWToolkitFramework/BWToolkitFramework.h>
#import "ConnectionWindowController.h"
#import "QueryWindowController.h"
#import "AddDBController.h";
#import "AddCollectionController.h"
#import "AuthWindowController.h"
#import "ImportWindowController.h"
#import "ExportWindowController.h"
#import "ResultsOutlineViewController.h"
#import "DatabasesArrayController.h"
#import "StatMonitorTableController.h"
#import "Connection.h"
#import "Sidebar.h"
#import "SidebarNode.h"
#import "MongoDB.h"
#import "Tunnel.h"
@implementation ConnectionWindowController
@synthesize managedObjectContext;
@synthesize databaseArrayController;
@synthesize resultsOutlineViewController;
@synthesize conn;
@synthesize mongoDB;
@synthesize sidebar;
@synthesize loaderIndicator;
@synthesize monitorButton;
@synthesize reconnectButton;
@synthesize monitorSheetController;
@synthesize statMonitorTableController;
@synthesize databases;
@synthesize collections;
@synthesize selectedDB;
@synthesize selectedCollection;
@synthesize sshTunnel;
@synthesize addDBController;
@synthesize addCollectionController;
@synthesize resultsTitle;
@synthesize bundleVersion;
@synthesize authWindowController;
@synthesize importWindowController;
@synthesize exportWindowController;
- (id)init {
if (![super initWithWindowNibName:@"ConnectionWindow"]) return nil;
return self;
}
- (void) tunnelStatusChanged: (Tunnel*) tunnel status: (NSString*) status {
NSLog(@"SSH TUNNEL STATUS: %@", status);
if( [status isEqualToString: @"CONNECTED"] ){
exitThread = YES;
[self connect:YES];
}
}
- (void) connect:(BOOL)haveHostAddress {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[loaderIndicator start];
[reconnectButton setEnabled:NO];
[monitorButton setEnabled:NO];
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];
NSMutableArray *portForwardings = [[NSMutableArray alloc] initWithObjects:portForward, nil];
[portForward release];
if (!sshTunnel)
sshTunnel =[[Tunnel alloc] init];
[sshTunnel setDelegate:self];
[sshTunnel setUser:conn.sshuser];
[sshTunnel setHost:conn.sshhost];
[sshTunnel setPassword:conn.sshpassword];
[sshTunnel setKeyfile:conn.sshkeyfile];
[sshTunnel setPort:[conn.sshport intValue]];
[sshTunnel setPortForwardings:portForwardings];
[sshTunnel setAliveCountMax:3];
[sshTunnel setAliveInterval:30];
[sshTunnel setTcpKeepAlive:YES];
[sshTunnel setCompression:YES];
//[sshTunnel start];
[portForwardings release];
return;
}else if (!haveHostAddress && [conn.host isEqualToString:@"flame.mongohq.com"]) {
hostaddress = [NSString stringWithFormat:@"%@:%@/%@", conn.host, conn.hostport, conn.defaultdb];
connected = mongoDB = [[MongoDB alloc] initWithConn:hostaddress];
}else {
if ([conn.userepl intValue] == 1) {
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];
}
}
[loaderIndicator stop];
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];
[reconnectButton setEnabled:YES];
[monitorButton setEnabled:YES];
[self reloadSidebar];
[self showServerStatus:nil];
}
[pool release];
}
- (void)windowDidLoad {
[super windowDidLoad];
exitThread = NO;
NSString *appVersion = [[NSString alloc] initWithFormat:@"version(%@[%@])", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"], [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey] ];
[bundleVersion setStringValue: appVersion];
[appVersion release];
[self connect:NO];
if ([conn.usessh intValue]==1) {
[NSThread detachNewThreadSelector: @selector(checkTunnel) toTarget:self withObject:nil ];
}
}
- (IBAction)reconnect:(id)sender
{
[self connect:NO];
if ([conn.usessh intValue]==1) {
[NSThread detachNewThreadSelector: @selector(checkTunnel) toTarget:self withObject:nil ];
}
}
- (void)checkTunnel {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
while(!exitThread){
@synchronized(self){
if ([sshTunnel running] == NO){
[sshTunnel start];
}else if( [sshTunnel running] == YES && [sshTunnel checkProcess] == NO ){
[sshTunnel stop];
[NSThread sleepForTimeInterval:2];
[sshTunnel start];
}
[sshTunnel readStatus];
}
[NSThread sleepForTimeInterval:3];
}
[NSThread exit];
[pool release];
}
- (void)dealloc {
[managedObjectContext release];
[databaseArrayController release];
[resultsOutlineViewController release];
[conn release];
[mongoDB release];
[sidebar release];
[databases release];
[collections release];
[selectedDB release];
[selectedCollection release];
[sshTunnel release];
[addDBController release];
[addCollectionController release];
[resultsTitle release];
[loaderIndicator release];
[reconnectButton release];
[monitorButton release];
[monitorSheetController release];
[statMonitorTableController release];
[bundleVersion release];
[authWindowController release];
[importWindowController release];
[exportWindowController release];
[super dealloc];
}
- (void)windowWillClose:(NSNotification *)notification {
if ([sshTunnel running]) {
[sshTunnel stop];
}
//exitThread = YES;
resultsOutlineViewController = nil;
selectedDB = nil;
selectedCollection = nil;
[super release];
}
- (void)reloadSidebar {
[loaderIndicator start];
[sidebar addSection:@"1" caption:@"DATABASES"];
[self reloadDBList];
[sidebar reloadData];
[loaderIndicator stop];
}
- (void)reloadDBList {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[loaderIndicator start];
//[selectedDB release];
selectedDB = nil;
//[selectedCollection release];
selectedCollection = nil;
[collections release];
collections = [[NSMutableArray alloc] init];
[databases release];
if ([conn.defaultdb isPresent]) {
databases = [[NSMutableArray alloc] initWithObjects:conn.defaultdb, nil];
}else {
databases = [[NSMutableArray alloc ] initWithArray:[mongoDB listDatabases]];
}
[databaseArrayController clean:conn databases:databases];
[sidebar removeItem:@"2"];
unsigned int i=1;
for (NSString *db in databases) {
[sidebar addChild:@"1" key:[NSString stringWithFormat:@"1.%d", i] caption:db icon:[NSImage imageNamed:@"dbicon.png"] action:@selector(useDB:) target:self];
i ++ ;
}
[sidebar reloadData];
[sidebar expandItem:@"1"];
[loaderIndicator stop];
[pool release];
}
- (void)useDB:(id)sender {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *dbname = [[NSString alloc] initWithFormat:@"%@", [sender caption]];
if (![[selectedDB caption] isEqualToString:dbname]) {
//[selectedDB release];
selectedDB = (SidebarNode *)sender;
}
//[selectedCollection release];
selectedCollection = nil;
NSString *user=nil;
NSString *password=nil;
Database *db = [databaseArrayController dbInfo:conn name:dbname];
if (db) {
user = db.user;
password = db.password;
}
[db release];
[collections release];
[loaderIndicator start];
collections = [[NSMutableArray alloc] initWithArray:[mongoDB listCollections:dbname user:user password:password]];
if ([collections count] == 0) {
[collections addObject:@"test"];
}
[loaderIndicator stop];
[dbname release];
[sidebar removeItem:@"2"];
[sidebar addSection:@"2" caption:[[selectedDB caption] uppercaseString]];
unsigned int i = 1;
for (NSString *collection in collections) {
[sidebar addChild:@"2" key:[NSString stringWithFormat:@"2.%d", i] caption:collection icon:[NSImage imageNamed:@"collectionicon.png"] action:@selector(useCollection:) target:self];
i ++ ;
}
[sidebar reloadData];
[sidebar setBadge:[selectedDB nodeKey] count:[collections count]];
[sidebar expandItem:@"2"];
[self showDBStats:nil];
[pool release];
}
- (void)useCollection:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *collectionname = [[NSString alloc] initWithFormat:@"%@", [sender caption] ];
if ([collectionname isPresent]) {
//[selectedCollection release];
selectedCollection = (SidebarNode *)sender;
[self showCollStats:nil];
}
[collectionname release];
[pool release];
}
- (IBAction)showServerStatus:(id)sender
{
[loaderIndicator start];
[resultsTitle setStringValue:[NSString stringWithFormat:@"Server %@:%@ stats", conn.host, conn.hostport]];
NSArray *serverStats = [[NSArray alloc] initWithArray:[mongoDB serverStatus]];
NSMutableArray *results = [[NSMutableArray alloc] initWithArray:serverStats];
[serverStats release];
resultsOutlineViewController.results = results;
[resultsOutlineViewController.myOutlineView reloadData];
[results release];
[loaderIndicator stop];
}
- (IBAction)showDBStats:(id)sender
{
if (selectedDB==nil) {
NSRunAlertPanel(@"Error", @"Please specify a database!", @"OK", nil, nil);
return;
}
[loaderIndicator start];
[resultsTitle setStringValue:[NSString stringWithFormat:@"Database %@ stats", [selectedDB caption]]];
NSString *user=nil;
NSString *password=nil;
Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption]];
if (db) {
user = db.user;
password = db.password;
}
[db release];
NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[mongoDB dbStats:[selectedDB caption]
user:user
password:password]];
resultsOutlineViewController.results = results;
[resultsOutlineViewController.myOutlineView reloadData];
[results release];
[loaderIndicator stop];
//NSLog(@"STATUS: %@", results);
}
- (IBAction)showCollStats:(id)sender
{NSLog(@"showCollStats");
if (selectedDB==nil || selectedCollection==nil) {
NSRunAlertPanel(@"Error", @"Please specify a collection!", @"OK", nil, nil);
return;
}
[loaderIndicator start];
[resultsTitle setStringValue:[NSString stringWithFormat:@"Collection %@.%@ stats", [selectedDB caption], [selectedCollection caption]]];
NSString *user=nil;
NSString *password=nil;
Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption] ];
if (db) {
user = db.user;
password = db.password;
}
[db release];
NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[mongoDB collStats:[selectedCollection caption]
forDB:[selectedDB caption]
user:user
password:password] ];
//NSLog(@"STATUS: %@", results);
resultsOutlineViewController.results = results;
[resultsOutlineViewController.myOutlineView reloadData];
[results release];
[loaderIndicator stop];
}
- (IBAction)createDBorCollection:(id)sender
{
if (selectedCollection) {
[self createCollectionForDB:[selectedDB caption]];
}else {
[self createDB];
}
}
- (void)createCollectionForDB:(NSString *)dbname
{
if (!addCollectionController)
{
addCollectionController = [[AddCollectionController alloc] init];
}
addCollectionController.dbname = dbname;
[addCollectionController showWindow:self];
}
- (void)createDB
{
if ([conn.defaultdb isPresent]) {
NSRunAlertPanel(@"Error", @"Could not create database!", @"OK", nil, nil);
return;
}
if (!addDBController)
{
addDBController = [[AddDBController alloc] init];
}
addDBController.managedObjectContext = self.managedObjectContext;
addDBController.conn = self.conn;
[addDBController showWindow:self];
}
- (void)addDB:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (![sender object]) {
return;
}
NSString *dbname = [[NSString alloc] initWithString:[[sender object] objectForKey:@"dbname"]];
NSString *user = [[NSString alloc] initWithString:[[sender object] objectForKey:@"user"]];
NSString *password = [[NSString alloc] initWithString:[[sender object] objectForKey:@"password"]];
[mongoDB dbStats:dbname
user:user
password:password];
[dbname release];
[user release];
[password release];
[self reloadSidebar];
[pool release];
}
- (void)addCollection:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (![sender object]) {
return;
}
NSString *dbname = [[NSString alloc] initWithString:[[sender object] objectForKey:@"dbname"]];
NSString *collectionname = [[NSString alloc] initWithString:[[sender object] objectForKey:@"collectionname"]];
NSString *user=nil;
NSString *password=nil;
Database *db = [databaseArrayController dbInfo:conn name:dbname];
if (db) {
user = db.user;
password = db.password;
}
[db release];
[mongoDB createCollection:collectionname
forDB:dbname
user:user
password:password];
if ([[selectedDB caption] isEqualToString:dbname]) {
[sidebar selectItem:[selectedDB nodeKey]];
}
[dbname release];
[collectionname release];
[pool release];
}
- (IBAction)dropDBorCollection:(id)sender
{
if (selectedCollection) {
[self dropWarning:[NSString stringWithFormat:@"COLLECTION:%@", [selectedCollection caption]]];
}else {
[self dropWarning:[NSString stringWithFormat:@"DB:%@", [selectedDB caption]]];
}
}
- (void)dropCollection:(NSString *)collectionname ForDB:(NSString *)dbname
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *user=nil;
NSString *password=nil;
Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption]];
if (db) {
user = db.user;
password = db.password;
}
[db release];
[loaderIndicator start];
[mongoDB dropCollection:collectionname
forDB:dbname
user:user
password:password];
[loaderIndicator stop];
if ([[selectedDB caption] isEqualToString:dbname]) {
[sidebar selectItem:[selectedDB nodeKey]];
}
[pool release];
}
- (void)dropDB
{
if ([conn.defaultdb isPresent]) {
NSRunAlertPanel(@"Error", @"Could not drop database!", @"OK", nil, nil);
return;
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *user=nil;
NSString *password=nil;
Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption]];
if (db) {
user = db.user;
password = db.password;
}
[db release];
[loaderIndicator start];
[mongoDB dropDB:[selectedDB caption]
user:user
password:password];
[loaderIndicator stop];
[self reloadSidebar];
[pool release];
}
- (IBAction)query:(id)sender
{
if (!selectedCollection) {
NSRunAlertPanel(@"Error", @"Please choose a collection!", @"OK", nil, nil);
return;
}
QueryWindowController *queryWindowController = [[QueryWindowController alloc] init];
queryWindowController.managedObjectContext = self.managedObjectContext;
queryWindowController.conn = conn;
queryWindowController.dbname = [selectedDB caption];
queryWindowController.collectionname = [selectedCollection caption];
queryWindowController.mongoDB = mongoDB;
[queryWindowController showWindow:sender];
}
- (IBAction)showAuth:(id)sender
{
if ([conn.defaultdb isPresent]) {
NSRunAlertPanel(@"Error", @"Could not auth for database!", @"OK", nil, nil);
return;
}
if (!selectedDB)
{
NSRunAlertPanel(@"Error", @"Please choose a database!", @"OK", nil, nil);
return;
}
if (!authWindowController)
{
authWindowController = [[AuthWindowController alloc] init];
}
Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption]];
if (db) {
[authWindowController.userTextField setStringValue:db.user];
[authWindowController.passwordTextField setStringValue:db.password];
}else {
[authWindowController.userTextField setStringValue:@""];
[authWindowController.passwordTextField setStringValue:@""];
}
authWindowController.managedObjectContext = self.managedObjectContext;
authWindowController.conn = self.conn;
authWindowController.dbname = [selectedDB caption];
[authWindowController showWindow:self];
}
- (IBAction)importFromMySQL:(id)sender
{
if (selectedDB==nil) {
NSRunAlertPanel(@"Error", @"Please specify a database!", @"OK", nil, nil);
return;
}
if (!importWindowController)
{
importWindowController = [[ImportWindowController alloc] init];
}
importWindowController.managedObjectContext = self.managedObjectContext;
importWindowController.conn = self.conn;
importWindowController.mongoDB = mongoDB;
importWindowController.dbname = [selectedDB caption];
[importWindowController showWindow:self];
}
- (IBAction)exportToMySQL:(id)sender
{
if (selectedDB==nil) {
NSRunAlertPanel(@"Error", @"Please specify a database!", @"OK", nil, nil);
return;
}
if (!exportWindowController)
{
exportWindowController = [[ExportWindowController alloc] init];
}
exportWindowController.managedObjectContext = self.managedObjectContext;
exportWindowController.conn = self.conn;
exportWindowController.mongoDB = mongoDB;
exportWindowController.dbname = [selectedDB caption];
[exportWindowController showWindow:self];
}
- (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSAlertFirstButtonReturn)
{
if (selectedCollection) {
[self dropCollection:[selectedCollection caption] ForDB:[selectedDB caption]];
}else {
[self dropDB];
}
}
}
- (void)dropWarning:(NSString *)msg
{
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:[NSString stringWithFormat:@"Drop this %@?", msg]];
[alert setInformativeText:[NSString stringWithFormat:@"Dropped %@ cannot be restored.", msg]];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:[self window] modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
}
- (IBAction)startMonitor:(id)sender {
monitorStopped = NO;
[NSThread detachNewThreadSelector: @selector(updateMonitor) toTarget:self withObject:nil ];
[monitorSheetController openSheet:sender];
NSLog(@"startMonitor");
}
- (IBAction)stopMonitor:(id)sender {
[monitorSheetController closeSheet:sender];
monitorStopped = YES;
NSLog(@"stopMonitor");
}
- (void)updateMonitor {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *prev = [NSDate date];
mongo::BSONObj a = [mongoDB serverStat];
while (!monitorStopped) {
[NSThread sleepForTimeInterval:1];
NSDate *now = [NSDate date];
mongo::BSONObj b = [mongoDB serverStat];
NSDictionary *item = [mongoDB serverMonitor:a second:b currentDate:now previousDate:prev];
a = b;
prev = now;
[statMonitorTableController addObject:item];
}
[NSThread exit];
[pool release];
}
@end