Source

baseten / Sources / PGTSConnectionPrivate.m

  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
//
// PGTSConnectionPrivate.m
// BaseTen
//
// Copyright (C) 2006 Marko Karppinen & Co. LLC.
//
// Before using this software, please review the available licensing options
// by visiting http://www.karppinen.fi/baseten/licensing/ or by contacting
// us at sales@karppinen.fi. Without an additional license, this software
// may be distributed only in compliance with the GNU General Public License.
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//
// $Id$
//


//#define LOG_ERRORS 1

#import <sys/types.h>
#import <sys/time.h>
#import <unistd.h>
#import <openssl/ssl.h>
#import <Log4Cocoa/Log4Cocoa.h>
#import "postgresql/libpq-fe.h"
#import "TSRunloopMessenger.h"
#import "PGTSConnectionPrivate.h"
#import "PGTSConstants.h"
#import "PGTSFunctions.h"
#import "PGTSConnectionDelegate.h"
#import "PGTSConnectionPool.h"
#import "PGTSExceptions.h"
#import "PGTSFunctions.h"


static int sslConnectionExIndex = -1;

extern int PGTSVerifySSLCertificate (int preverify_ok, X509_STORE_CTX* x509_ctx);

static NSNotification* 
PGTSExtractPgNotification (id anObject, PGnotify* pgNotification)
{
    NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithInt: pgNotification->be_pid],       kPGTSBackendPIDKey,
        [NSNull null],                                          kPGTSNotificationExtraKey,
        //This doesn't work yet
        //[NSString stringWithUTF8String: pgNotification->extra],	kPGTSNotificationExtraKey, 
        nil];
    NSNotification* notification = [NSNotification notificationWithName: [NSString stringWithUTF8String: pgNotification->relname]
                                                                 object: anObject
                                                               userInfo: userInfo];
    return notification;
}

int
PGTSSSLConnectionExIndex ()
{
	if (-1 == sslConnectionExIndex)
		sslConnectionExIndex = SSL_get_ex_new_index (0, NULL, NULL, NULL, NULL);
	return sslConnectionExIndex;
}


static void 
DataAvailable (CFSocketRef s, CFSocketCallBackType callbackType, CFDataRef address, const void* data, void* info)
{
	[(id) info dataAvailable];
}


@interface NSObject (PGTSKeyValueObserving)
+ (BOOL) automaticallyNotifiesObserversForKey: (NSString *) key;
- (void) willChangeValueForKey: (NSString *) key;
- (void) didChangeValueForKey: (NSString *) key;
@end


#if 0
//Check for key-value observing capability.
@interface NSObject (PGTSKVO)
+ (BOOL) PGTSAllowKVO;
@end

@implementation NSObject (PGTSKVO)
+ (BOOL) PGTSAllowKVO
{
    static BOOL tooLate = NO;
    static BOOL allow = NO;
    
    if (NO == tooLate)
    {
        tooLate = YES;
        allow = ([self instancesRespondToSelector: @selector (willChangeValueForKey:)] &&
                 [self instancesRespondToSelector: @selector (didChangeValueForKey:)]);
    }
    
    return allow;
}
@end
#endif


@implementation PGTSConnection (PrivateMethods)

+ (BOOL) automaticallyNotifiesObserversForKey: (NSString *) key
{
    BOOL rval = NO;
    if (NO == [@"connectionStatus" isEqualToString: key])
        rval = [super automaticallyNotifiesObserversForKey: key];
    return rval;
}

/**
 * Add the connection to the shared connection pool and send user specified initial SQL commands
 */
- (void) finishConnecting
{
	connecting = NO;
    if (CONNECTION_OK != connectionStatus)
        [[PGTSConnectionPool sharedInstance] removeConnection: self];
    else
    {
        [[PGTSConnectionPool sharedInstance] addConnection: self];
        if (nil != initialCommands)
            [workerProxy sendQuery: initialCommands];
    }
}

/**
 * Notify the delegate about connection having been made or the attempt having failed
 */
- (void) sendFinishedConnectingMessage: (ConnStatusType) status reconnect: (BOOL) reconnected
{
    [self finishConnecting];
    if (messageDelegateAfterConnecting)
    {
        if (CONNECTION_OK == status)
        {
            if (reconnected)
                [delegate PGTSConnectionDidReconnect: self];
            else
                [delegate PGTSConnectionEstablished: self];
        }
        else
        {
            [delegate PGTSConnectionFailed: self];
        }
    }
}

/** 
 * Handle NOTICEs from PostgreSQL 
 * This is not the same thing as PostgreSQL notifications.
 */
- (void) handleNotice: (NSString *) message
{
    NSDictionary* userInfo = nil;
    NSNotification* notification = nil;
    userInfo = [NSDictionary dictionaryWithObjectsAndKeys: 
        message, kPGTSNoticeMessageKey, nil];
    
    notification = [NSNotification notificationWithName: kPGTSNotice
                                                 object: self
                                               userInfo: userInfo];
    if (delegateProcessesNotices)
        [delegate PGTSConnection: self receivedNotice: notification];

    [self logNotice: message];
}

- (void) raiseExceptionForMissingSelector: (SEL) aSelector
{
    NSString* reason = NSLocalizedString (@"Delegate does not respond to selector %@", @"Exception reason");
    [[NSException exceptionWithName: NSInternalInconsistencyException
                             reason: [NSString stringWithFormat: reason, NSStringFromSelector (aSelector)]
                           userInfo: nil] raise];
}

- (void) checkQueryStatus: (PGTSResultSet *) result async: (BOOL) async
{
    ExecStatusType status = [result status];
    if (NO == overlooksFailedQueries && (PGRES_BAD_RESPONSE == status || PGRES_FATAL_ERROR == status))
    {
        if ([delegate respondsToSelector: kPGTSReceivedErrorSelector])
            [delegate PGTSConnection: self receivedError: result];
        else if (YES == async && [delegate respondsToSelector: kPGTSReceivedResultSetSelector])
            [delegate PGTSConnection: self receivedResultSet: result];
        else
        {
            NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                result, kPGTSResultSetKey,
                self,   kPGTSConnectionKey,
                nil];
			NSException* exception = [PGTSQueryException exceptionWithName: kPGTSQueryFailedException 
																	reason: [result errorMessage] 
																  userInfo: userInfo];
            [exception raise];
        }
    }
}

- (PGTSResultSet *) resultFromProxy: (volatile PGTSConnection *) proxy status: (int) status
{
    PGTSResultSet* res = nil;
    if (YES == failedToSendQuery)
        [self handleFailedQuery];
    else if (1 == status)
    {
        NSArray* results = [proxy pendingResultSets];
        res = [results lastObject];
        [self checkQueryStatus: res async: NO];
    }
    return res;
}

- (int) sendResultsToDelegate: (int) status
{
    if (YES == failedToSendQuery)
        [self handleFailedQuery];
    else if (1 == status)
        [workerProxy retrieveResultsAndSendToDelegate];
    return status;
}


- (void) handleFailedQuery
{
    failedToSendQuery = NO;

    NSString* exceptionName = nil;
    SEL selector = NULL;
    ConnStatusType status = [self connectionStatus];
    
    if (CONNECTION_OK == status)
    {
        selector = kPGTSFailedToSendQuerySelector;
        exceptionName = kPGTSQueryFailedException;
    }
    else
    {
        selector = kPGTSConnectionFailedSelector;
        exceptionName = kPGTSConnectionFailedException;
    }

    if ([delegate respondsToSelector: selector])
    {
        if (CONNECTION_OK == status)
            [delegate PGTSConnection: self failedToSendQuery: nil];
        else
            [delegate PGTSConnectionFailed: self];
    }
    else
    {
        NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
            self,   kPGTSConnectionKey,
            nil];
        [[NSException exceptionWithName: exceptionName
                                 reason: nil
                               userInfo: userInfo] raise];
    }    
}

- (void) setErrorMessage: (NSString *) aMessage
{
	if (errorMessage != aMessage)
	{
		[errorMessage release];
		errorMessage = [aMessage retain];
	}
}
@end


@implementation PGTSConnection (ProxyMethods)

/**
 * Send query dispatch status to the delegate
 * Afterwards collect the results and send them as well
 */
- (void) sendDispatchStatusToDelegate: (int) status forQuery: (NSString *) queryString
{
    if (0 == status)
        [delegate PGTSConnection: self failedToSendQuery: queryString];
    else
    {
        [delegate PGTSConnection: self sentQuery: queryString];
        [workerProxy retrieveResultsAndSendToDelegate];
    }
}

- (void) sendResultSetWithNotification: (NSNotification *) notification
{
    [[NSNotificationCenter defaultCenter] postNotification: notification];
}

- (void) succeededToCopyData: (NSData *) data
{
    NSString* message = nil;
    [workerProxy endCopyAndAccept2: [delegate PGTSConnection: self acceptCopyingData: data errorMessage: &message]
                      errorMessage: errorMessage messageWhenDone: YES];
}

- (void) succeededToReceiveData: (NSData *) data
{
    [delegate PGTSConnection: self receivedData: data];
}

/**
 * Send results to the delegate
 */
- (void) sendResultToDelegate: (PGTSResultSet *) result
{
    [self checkQueryStatus: result async: YES];
    [delegate PGTSConnection: self receivedResultSet: result];
}

@end


@implementation PGTSConnection (WorkerPrivateMethods)

/**
 * Run loop for the worker thread
 */
- (void) workerThreadMain: (NSConditionLock *) threadStartLock
{
    [workerThreadLock lock];
	[threadStartLock lock];
    NSAutoreleasePool* threadPool = [[NSAutoreleasePool alloc] init];
    socket = NULL;
	socketSource = NULL;
    
    NSString* mode = NSDefaultRunLoopMode;
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    BOOL haveInputSources = YES;
    
    shouldContinueThread = YES;
    threadRunning = YES;
    
    //Inter-thread messaging
    id runLoopMessenger  = [TSRunloopMessenger runLoopMessengerForCurrentRunLoop];
    workerProxy          = [runLoopMessenger target: self withResult: NO];
    returningWorkerProxy = [runLoopMessenger target: self withResult: YES];

    //Prevent the run loop from exiting immediately
    [runLoop addPort: [NSPort port] forMode: mode];
    
    [threadStartLock unlockWithCondition: 1];
    
    while (haveInputSources && shouldContinueThread)
    {
		NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        haveInputSources = [runLoop runMode: mode
                                 beforeDate: [NSDate distantFuture]];
		[pool release];
    }
	workerProxy = nil;
    returningWorkerProxy = nil;
	[self workerCleanUpDisconnecting: YES];
	
    log4Debug (@"Worker: exiting");
    [threadPool release];    
    
    [workerThreadLock unlock];
}


/**
 * Connect or reconnect to the database
 */
- (BOOL) workerPollConnectionResetting: (BOOL) reset
{
	[asyncConnectionLock lock];
    fd_set mask;    
    struct timeval ltimeout = timeout;
    int selectStatus = 0;
    BOOL rval = NO;
    BOOL stop = NO;
    PostgresPollingStatusType pollingStatus = PGRES_POLLING_WRITING; //Start with this
    PostgresPollingStatusType (* pollFunction)(PGconn *) = (reset ? &PQresetPoll : &PQconnectPoll);
    int bsdSocket = PQsocket (connection);
    
    if (bsdSocket < 0)
	{
        log4Info (@"Unable to get connection socket from libpq");
	}
    else
    {
		sslSetUp = NO;
		[self workerCleanUpDisconnecting: NO];
        
        //Polling loop
        while (1)
        {
            ltimeout = timeout;
            FD_ZERO (&mask);
            FD_SET (bsdSocket, &mask);
            selectStatus = 0;
            pollingStatus = pollFunction (connection);
			
			log4Debug (@"Polling status: %d connection status: %d", pollingStatus, PQstatus (connection));
#ifdef USE_SSL
			if (NO == sslSetUp && CONNECTION_SSL_CONTINUE == PQstatus (connection))
			{
				sslSetUp = YES;
				SSL* ssl = PQgetssl (connection);
				log4AssertValueReturn (NULL != ssl, NO, @"Expected ssl struct not to be NULL.");
				SSL_set_verify (ssl, SSL_VERIFY_PEER, &PGTSVerifySSLCertificate);
				SSL_set_ex_data (ssl, PGTSSSLConnectionExIndex (), self);
			}
#endif
			
            switch (pollingStatus)
            {
                case PGRES_POLLING_OK:
                    rval = YES;
					//Fall through
                case PGRES_POLLING_FAILED:
                    stop = YES;
                    break;
					
				case PGRES_POLLING_ACTIVE:
					//Select returns 0 on timeout
					selectStatus = 1;
					break;
                    
                case PGRES_POLLING_READING:
                    selectStatus = select (bsdSocket + 1, &mask, NULL, NULL, &ltimeout);
                    break;
                    
                case PGRES_POLLING_WRITING:
                default:					
                    selectStatus = select (bsdSocket + 1, NULL, &mask, NULL, &ltimeout);
                    break;
            } //switch

            [self updateConnectionStatus];
			
			if (0 == selectStatus)
			{
				[self setErrorMessage: NSLocalizedString (@"Connection timed out.", @"Error message for connection failure")];
				break;
			}
            else if (0 >= selectStatus || YES == stop)
			{
                break;
			}
        }
        
        if (YES == rval && CONNECTION_OK == connectionStatus)
        {
            PQsetnonblocking (connection, 0); //We don't want to call PQsendquery etc. multiple times
            PQsetClientEncoding (connection, "UNICODE"); //Use UTF-8
			PQexec (connection, "SET standard_conforming_strings TO true");
			PQexec (connection, "SET datestyle TO 'ISO, YMD'");
            //FIXME: set other things as well?
            PQsetNoticeProcessor (connection, &PGTSNoticeProcessor, (void *) self);
			
			CFSocketContext context = {0, self, NULL, NULL, NULL};
			socket = CFSocketCreateWithNative (NULL, bsdSocket, kCFSocketReadCallBack, &DataAvailable, &context);
			CFSocketSetSocketFlags (socket, ~kCFSocketCloseOnInvalidate & CFSocketGetSocketFlags (socket));
			socketSource = CFSocketCreateRunLoopSource (NULL, socket, 0);
			log4AssertLog (NULL != socket, @"Expected source to have been created.");
			log4AssertLog (TRUE == CFSocketIsValid (socket), @"Expected socket to be valid.");
			log4AssertLog (NULL != socketSource, @"Expected socketSource to have been created.");
			log4AssertLog (TRUE == CFRunLoopSourceIsValid (socketSource), @"Expected socketSource to be valid.");
			
			CFRunLoopRef rl = CFRunLoopGetCurrent ();
			CFStringRef mode = kCFRunLoopCommonModes;
			log4AssertLog (FALSE == CFRunLoopContainsSource (rl, socketSource, mode), 
						   @"Expected run loop not to have socketSource.");
			CFRunLoopAddSource (rl, socketSource, mode);
			log4AssertLog (TRUE == CFRunLoopContainsSource (rl, socketSource, mode), 
						   @"Expected run loop to have socketSource.");
			
            cancelRequest = PQgetCancel (connection);
        }
    }
    
    if (messageDelegateAfterConnecting)
        [mainProxy sendFinishedConnectingMessage: connectionStatus reconnect: reset];
    [asyncConnectionLock unlockWithCondition: 1];

    return rval;
}

- (void) workerEnd
{
    //Setting the variable from the main thread isn't enough;
	//we also need to cause some action in worker's run loop by invoking this method.
	shouldContinueThread = NO;
    log4Debug (@"workerEnd");
}

- (void) workerCleanUpDisconnecting: (BOOL) disconnect
{
	[connectionLock lock];
	if (NULL != socketSource)
	{
		//CFRunLoopRemoveSource (CFRunLoopGetCurrent (), socketSource, kCFRunLoopCommonModes);
		CFRunLoopSourceInvalidate (socketSource);
		CFRelease (socketSource);
		socketSource = NULL;
	}
	if (NULL != socket)
	{
		//We have earlier set socket options so that it doesn't get closed automatically.
		//PQfinish does that for us.
		CFSocketInvalidate (socket);
		CFRelease (socket);
		socket = NULL;
	}
	if (NULL != cancelRequest)
	{
		PQfreeCancel (cancelRequest);
		cancelRequest = NULL;
	}
	if (disconnect && NULL != connection)
	{
		PQfinish (connection);
		connection = NULL;
	}
	[connectionLock unlock];
}

- (void) logQuery: (NSString *) query message: (BOOL) messageDelegate parameters: (NSArray *) parameters
{
    printf ("(%p) (%c) %s %s\n", self, messageDelegate ? 'A' : 'S', [[query description] UTF8String], [[parameters description] UTF8String]);
    //log4Info (@"(%p) %@ %@\n", self, query, parameters);
}

- (void) logNotice: (id) anObject
{
    log4Debug (@"(%p) %@", self, anObject);
}

- (void) logNotification: (id) anObject
{
    log4Debug (@"(%p) *** NOTIFY: %@\n", self, anObject);
}

/** Called when data is available from the libpq socket */
- (void) dataAvailable
{
	log4Debug (@"worker: availableData thread: %p", [NSThread currentThread]);
	[connectionLock lock];
	PQconsumeInput (connection);
	[self postPGnotifications];
	[connectionLock unlock];
}

- (void) postPGnotifications
{
    PGnotify* pgNotification = NULL;
    while ((pgNotification = PQnotifies (connection)))
    {
        NSNotification* notification = PGTSExtractPgNotification (self, pgNotification);
        [self logNotification: [notification name]];
        log4Debug (@"Posting notification: %@", notification);
        [postgresNotificationCenter performSelectorOnMainThread: @selector (postNotification:)
                                                     withObject: notification
                                                  waitUntilDone: NO];
        PQfreeNotify (pgNotification);
    }
}

/** Set the connection status after we have made the connection */
- (void) updateConnectionStatus
{
    //KVO is not available on Mac OS X 10.2
    static BOOL allowKVO = YES;
    ConnStatusType tempStatus = PQstatus (connection);
    if (tempStatus != connectionStatus)
    {
        if (YES == allowKVO)
        {
            NS_DURING
                [self willChangeValueForKey: @"connectionStatus"];
            NS_HANDLER
                if ([[localException name] isEqualToString: NSInvalidArgumentException])
                    allowKVO = NO;
			NS_ENDHANDLER
        }
		connectionStatus = tempStatus;
		if (YES == allowKVO) [self didChangeValueForKey: @"connectionStatus"];
    }
}


@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.