Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 23ba2227c4
Fetching contributors…

Cannot retrieve contributors at this time

140 lines (103 sloc) 4.005 kb
//
// TDServer.m
// TouchDB
//
// Created by Jens Alfke on 11/30/11.
// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
#import "TDServer.h"
#import "TDDatabaseManager.h"
#import "TDInternal.h"
#import "MYBlockUtils.h"
@implementation TDServer
#if DEBUG
+ (TDServer*) createEmptyAtPath: (NSString*)path {
[[NSFileManager defaultManager] removeItemAtPath: path error: nil];
NSError* error;
TDServer* server = [[self alloc] initWithDirectory: path error: &error];
Assert(server, @"Failed to create server at %@: %@", path, error);
AssertEqual(server.directory, path);
return [server autorelease];
}
+ (TDServer*) createEmptyAtTemporaryPath: (NSString*)name {
return [self createEmptyAtPath: [NSTemporaryDirectory() stringByAppendingPathComponent: name]];
}
#endif
- (id) initWithDirectory: (NSString*)dirPath error: (NSError**)outError {
if (outError) *outError = nil;
self = [super init];
if (self) {
_manager = [[TDDatabaseManager alloc] initWithDirectory: dirPath error: outError];
if (!_manager) {
[self release];
return nil;
}
_serverThread = [[NSThread alloc] initWithTarget: self
selector: @selector(runServerThread)
object: nil];
LogTo(TDServer, @"Starting server thread %@ ...", _serverThread);
[_serverThread start];
}
return self;
}
- (void)dealloc
{
if (_serverThread) Warn(@"%@ dealloced with _serverThread still set: %@", self, _serverThread);
[_manager release];
[super dealloc];
}
- (void) close {
if (_serverThread) {
[self queue: ^{
LogTo(TDServer, @"Stopping server thread...");
CFRunLoopStop(CFRunLoopGetCurrent());
}];
[_serverThread release];
_serverThread = nil;
}
}
- (NSString*) directory {
return _manager.directory;
}
- (void) runServerThread {
@autoreleasepool {
[[self retain] autorelease]; // ensure self stays alive till this method returns
@autoreleasepool {
LogTo(TDServer, @"Server thread starting...");
[[NSThread currentThread] setName:@"TouchDB"];
// Add a no-op source so the runloop won't stop on its own:
CFRunLoopSourceContext context = {}; // all zeros
CFRunLoopSourceRef source = CFRunLoopSourceCreate(NULL, 0, &context);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRelease(source);
// Initialize the replicator:
[_manager replicatorManager];
}
// Now run:
[[NSRunLoop currentRunLoop] run];
LogTo(TDServer, @"Server thread exiting");
// Clean up; this has to be done on the server thread, not in the -close method.
[_manager close];
}
}
- (void) queue: (void(^)())block {
Assert(_serverThread, @"-queue: called after -close");
MYOnThread(_serverThread, block);
}
- (void) tellDatabaseNamed: (NSString*)dbName to: (void (^)(TDDatabase*))block {
[self queue: ^{ block([_manager databaseNamed: dbName]); }];
}
- (void) tellDatabaseManager: (void (^)(TDDatabaseManager*))block {
[self queue: ^{ block(_manager); }];
}
@end
TestCase(TDServer) {
RequireTestCase(TDDatabaseManager);
}
Jump to Line
Something went wrong with that request. Please try again.