/Volumes/Plantain/MyDocuments/Projects/MacTrek/MacTrek/Comm/MetaServerParser.m

00001 //-------------------------------------------
00002 // File:  MetaServerParser.m
00003 // Class: MetaServerParser
00004 // 
00005 // Created by Chris Lukassen 
00006 // Copyright (c) 2006 Luky Soft
00007 //-------------------------------------------
00008  
00009 #import "MetaServerParser.h"
00010 
00011  
00012 @implementation MetaServerParser
00013 
00014 - (NSMutableArray *) readFromMetaServer:(NSString *) server atPort:(int)port {
00015                 
00016     NSMutableArray *entries = nil;
00017     
00018     // connect and create a stream
00019     ONTCPSocket *socket = [ONTCPSocket tcpSocket];
00020     ONHost *hostName = [ONHost hostForHostname:server];
00021     [socket connectToHost:hostName port:port];
00022     ONSocketStream *stream = [ONSocketStream streamWithSocket:socket];
00023 
00024         entries = [self parseInputFromStream:stream];
00025     
00026     // close connection 
00027     // apperantly not needed, is autoreleased
00028     //[stream release];
00029     //[socket release];
00030 
00031     return entries;
00032 }
00033         
00034 - (NSMutableArray *) parseInputFromStream:(ONSocketStream *) stream  {
00035     
00036     NSMutableArray *entries = [[NSMutableArray alloc] init];
00037     
00038         // add the default server               
00039     MetaServerEntry *entry = [[MetaServerEntry alloc] init];    
00040     [entry setAddress: @"netrek.luky.nl"];
00041     [entry setPort:    2592];
00042     [entry setStatus:  DEFAULT];
00043     [entry setGameType:    BRONCO];     
00044     [entries addObject:entry]; 
00045                 
00046     NSString *line = nil;               
00047     while ((line = [stream readLine]) != nil) {
00048         // make sure this is a line with server info on it
00049         if ([line length] == 79 && 
00050             [[line substringWithRange:NSMakeRange(0,3)] isEqualToString:@"-h "] && 
00051             [[line substringWithRange:NSMakeRange(40, 3)] isEqualToString:@"-p "]) {
00052             
00053                         entry = [[MetaServerEntry alloc] init];
00054                         [entry setAddress: [line substringWithRange:NSMakeRange(3, 36)]];
00055             
00056             // !! strip off trailing spaces
00057             NSMutableString *address = [NSMutableString stringWithString:[entry address]];
00058             [address replaceOccurrencesOfString:@" " withString:@"" options:nil range:NSMakeRange(0, [address length])];
00059             [entry setAddress:address];
00060             
00061                         // TODO: Handle NumberFormatExceptions
00062             [entry setPort: [[line substringWithRange:NSMakeRange(43, 5)] intValue]];
00063             [entry setTime: [[line substringWithRange:NSMakeRange(49, 3)] intValue]];
00064                 
00065             [entry setStatus: CANNOT_CONNECT];
00066             [entry setStatusWithString:[line substringWithRange:NSMakeRange(54, 17)]];
00067 
00068             if ([entry status] == CANNOT_CONNECT) {
00069                 // break the while loop
00070                 continue;
00071             }
00072 
00073                         // parse the number of players if this server has any players
00074             
00075                         if ([entry status] == OPEN || [entry status] == WAIT) {
00076                                 // TODO: Handle FormatErrors here too
00077                 [entry setPlayers:[[line substringWithRange:NSMakeRange(59, 3)] intValue]];
00078                         }
00079         
00080 
00081                         // read the flags
00082             [entry setHasRSA: NO];
00083             if ([[line substringWithRange:NSMakeRange(74,1)] isEqualToString: @"R"]) {
00084                 [entry setHasRSA: YES];
00085             }
00086                         [entry setGameTypeWithString: [line substringWithRange:NSMakeRange(78,1)]];
00087                                 
00088                         // don't list paradise servers
00089                         if ([entry gameType] == BRONCO) {
00090                 [entries addObject:entry]; 
00091 
00092                         }
00093                         
00094                 }
00095     }
00096         return entries;
00097 
00098 }
00099  
00100 @end

Generated on Fri Jul 28 19:15:15 2006 for MacTrek by  doxygen 1.4.7