Fórum WebTuga

Tecnologia => Programação => Tópico iniciado por: KTachyon em Janeiro 02, 2006, 06:44:07 pm

Título: [Objective-C] OjbC
Enviado por: KTachyon em Janeiro 02, 2006, 06:44:07 pm
Bem... aki vai um cheirinho de programação em Objective-C, para o ppl interessado em saber como é k é...

O programa é basicamente um servidor (inútil) que recebe mensagens e as envia para o output (com NSLog) e quando recebe a palavra \\'terminate\\', envia a mensagem de terminação de ligação e disconnecta-se.

Código: [Seleccione]
// ServerTest.m



#import <Foundation/Foundation.h>

#import "Server.h"



int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];



Server *test = [[Server alloc] init];



   [test startServer];



   [[NSRunLoop currentRunLoop] run];



[pool release];

return 0;

}

Código: [Seleccione]
// Server.h



#import <Cocoa/Cocoa.h>





@interface Server : NSObject {



}



- (void)startServer;

- (void)spawnChildConnection:(NSNotification *)note;

- (void)processClientData:(NSNotification *)note;



@end

Código: [Seleccione]
//  Server.m



#import "Server.h"





@implementation Server



- (void)startServer

{

   NSSocketPort *sockPort = [[NSSocketPort alloc] initWithTCPPort:7777];

   [sockPort autorelease];



   int socketFD = [sockPort socket];



   NSFileHandle *listeningSocket = [[NSFileHandle alloc] initWithFileDescriptor:socketFD];

   [listeningSocket autorelease];



   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];



   [nc addObserver:self

selector:@selector(spawnChildConnection:)

name:NSFileHandleConnectionAcceptedNotification

 object:listeningSocket];



   [listeningSocket acceptConnectionInBackgroundAndNotify];

}



/*

 * This method is invoked in response to

 * NSFileHandleConnectionAcceptedNotification

 */



- (void)spawnChildConnection:(NSNotification *)note

{

   NSFileHandle *connectedSocket = [[note userInfo] objectForKey:NSFileHandleNotificationFileHandleItem];



   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];



   [nc addObserver:self

selector:@selector(processClientData:)

name:NSFileHandleReadCompletionNotification

 object:connectedSocket];



   // Send a message to the client, acknowledging that the connection was accepted



   NSString *serverWelcomeMessage = @"200 KTachyon\\'s UselessServer v0.0.3a Readyn";

   char *chr = (char*) calloc([serverWelcomeMessage length] + 1, sizeof(char));



   [serverWelcomeMessage getCString:chr];



   NSData *ackData = [NSData dataWithBytes:chr  length:[serverWelcomeMessage length]];



   [connectedSocket writeData:ackData];

   [connectedSocket readInBackgroundAndNotify];

}



/*

 * This method is invoked in response to

 * NSFileHandleReadCompletionNotification

 */



- (void)processClientData:(NSNotification *)note

{

   NSData *data = [[note userInfo] objectForKey:NSFileHandleNotificationDataItem];

   NSFileHandle *socket = [note object];



   NSString *str = [[NSString alloc] initWithCString:[data bytes] length:[data length]];



   NSLog(str);



   if ([str hasPrefix:@"terminate"])

   {

 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

 

 NSString *serverTerminationMessage = @"400 KTachyon\\'s UselessServer v0.0.3a Terminates connectionn";

 char *chr = (char*) calloc([serverTerminationMessage length] + 1, sizeof(char));

 

 [serverTerminationMessage getCString:chr];

 

 NSData *terminationMessage = [NSData dataWithBytes:chr length:[serverTerminationMessage length]];

 

 [socket writeData:terminationMessage];

 [socket closeFile];

 

 [nc removeObserver:self

name:NSFileHandleReadCompletionNotification

  object:socket];

   }

   else

 [socket readInBackgroundAndNotify];



   [str release];

}





@end