00001
00002
00003
00004
00005
00006
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
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
00027
00028
00029
00030
00031 return entries;
00032 }
00033
00034 - (NSMutableArray *) parseInputFromStream:(ONSocketStream *) stream {
00035
00036 NSMutableArray *entries = [[NSMutableArray alloc] init];
00037
00038
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
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
00057 NSMutableString *address = [NSMutableString stringWithString:[entry address]];
00058 [address replaceOccurrencesOfString:@" " withString:@"" options:nil range:NSMakeRange(0, [address length])];
00059 [entry setAddress:address];
00060
00061
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
00070 continue;
00071 }
00072
00073
00074
00075 if ([entry status] == OPEN || [entry status] == WAIT) {
00076
00077 [entry setPlayers:[[line substringWithRange:NSMakeRange(59, 3)] intValue]];
00078 }
00079
00080
00081
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
00089 if ([entry gameType] == BRONCO) {
00090 [entries addObject:entry];
00091
00092 }
00093
00094 }
00095 }
00096 return entries;
00097
00098 }
00099
00100 @end