/*
* Tindex
* Copyright ©2002 - 2006, Blue Static <http://www.bluestatic.org>
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $Id: DatabaseServer.m 458 2006-08-14 03:48:44Z rsesek $
* $HeadURL: file:///Users/rsesek/SVN/macosx/Tindex/trunk/Server/DatabaseServer.m $
*/
#import "DatabaseServer.h"
#import "ServerManager.h"
#import <netinet/in.h>
#import <sys/socket.h>
@implementation DatabaseServer
/*!
* Initializer. Sets up the manager object reference
*
* @param ServerManager Manager object
*/
- (id)initWithManager: (ServerManager *)obj
{
if (self = [super init])
{
manager = [obj retain];
}
return self;
}
/*!
* Destructor
*/
- (void)dealloc
{
[manager release];
[socket dealloc];
[service dealloc];
[super dealloc];
}
/*!
* Step 1 of 3. Initializes the TCP/IP sockets for IPv4 or IPv6.
*/
- (void)startSocket
{
socket = [[NSSocketPort alloc] init];
if (socket)
{
addr = (struct sockaddr *)[[socket address] bytes];
if (addr->sa_family == AF_INET)
{
port = ((struct sockaddr_in *)addr)->sin_port;
[manager addLog: @"IPv4 NSSocketPort established."];
}
else if (addr->sa_family == AF_INET6)
{
port = ((struct sockaddr_in6 *)addr)->sin6_port;
[manager addLog: @"IPv6 NSSocketPort established."];
}
else
{
[socket release];
socket = nil;
[manager addError: @"The family is neither IPv4 nor IPv6. Can't handle."];
}
}
else
{
[manager addError: @"An error occurred initializing the NSSocketPort."];
}
}
/*!
* Step 3 of 3. Creates and opens a new NSConnection and then vends a new SQLiteRemote
* object (autoreleased).
*/
- (void)startDistributedObject
{
connection = [[NSConnection connectionWithReceivePort: socket
sendPort: nil] retain];
[connection setRootObject: [[[SQLiteRemote alloc] init] autorelease]];
[[NSSocketPortNameServer sharedInstance] registerPort: socket
name: @"TindexServer"];
}
/*!
* Step 2 of 3. Broadcasts a _tindex._tcp. signal across the opened port.
*/
- (void)startService
{
if (socket)
{
service = [[NSNetService alloc] initWithDomain: @""
type: @"_tindex._tcp"
name: @""
port: port];
if (service)
{
[service publish];
[manager addLog: @"NSNetService delegated and published."];
}
else
{
[manager addError: @"An error occurred initializing the NSNetService."];
}
}
}
@end