// // SoulSeekWorker.m // SSNetwork // // Created by Marcelo Alves on 09/03/07. // Copyright 2007 __MyCompanyName__. All rights reserved. // #import "SoulSeekWorker.h" #import "SoulSeek.h" @implementation SoulSeekWorker - (BOOL) shouldExit { return false; } + (void)connectWithPorts:(NSArray *)portArray { NSAutoreleasePool *pool; NSConnection *serverConnection; SoulSeekWorker *serverObject; pool = [[NSAutoreleasePool alloc] init]; serverConnection = [NSConnection connectionWithReceivePort:[portArray objectAtIndex:0] sendPort:[portArray objectAtIndex:1]]; serverObject = [[self alloc] init]; [serverConnection setRootObject:serverObject]; [(SoulSeek *)[serverConnection rootProxy] setWorker:serverObject]; // store the solarseek object [serverObject setValue:[serverConnection rootProxy] forKey:@"soulSeek"]; do { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } while (![serverObject shouldExit]); [serverObject release]; [pool release]; return; } - (oneway void) connectToServer:(NSString*)server atPort:(int)port { NSLog(@"Connecting..."); if (server == nil) server = @"server.slsknet.org"; if (port == 0) port = 2240; NSInputStream *inStream = nil; NSOutputStream *outStream = nil; NSHost* host = [NSHost hostWithName:server]; if (host == nil) return; // oh, can't connect // open the connection [NSStream getStreamsToHost:host port:port inputStream:&inStream outputStream:&outStream]; if (client != nil) [client release]; client = [SoulSeekConnection newConnectionTo:soulSeekServer named:@"serverConnection" withInputStream:inStream andOutputStream:outStream andWorker:self]; [host release]; [inStream setDelegate:client]; [outStream setDelegate:client]; // no threads, just run loops, YAY [inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; // open the streams [inStream open]; [outStream open]; NSLog(@"Connected to soulseek server!"); // TODO : flag as connected. } - (oneway void) startListeningAtPort:(int)port { if (port == 0) port = 2234; tcpServer = [[TCPServer alloc] init]; NSError *error = nil; [tcpServer setPort: port]; [tcpServer setDelegate: self]; if (![tcpServer start:&error] ) { NSLog(@"Error starting server: %@", error); } else { NSLog(@"Starting server on port %d", [tcpServer port]); } } - (void)TCPServer:(TCPServer *)server didReceiveConnectionFromAddress:(NSData *)addr inputStream:(NSInputStream *)inStream outputStream:(NSOutputStream *)outStream { NSLog(@"New connection received..."); SoulSeekConnection* p = [SoulSeekConnection newConnectionTo:soulSeekPeer named:@"peerConnection" withInputStream:inStream andOutputStream:outStream andWorker:self]; [peers setObject:p forKey:@"BAH"]; // TODO change me ! (probably, nickname + random number) // no threads, just run loops, YAY [inStream setDelegate:p]; [outStream setDelegate:p]; [inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; // open the streams [inStream open]; [outStream open]; } -(oneway void) connect { [self startListeningAtPort:0]; [self connectToServer:NULL atPort:0]; } - (oneway void) login:(NSString *)user password:(NSString *)password { } @end