24 |
#include <Foundation/NSArray.h> |
#include <Foundation/NSArray.h> |
25 |
#include <Foundation/NSHost.h> |
#include <Foundation/NSHost.h> |
26 |
#include <Foundation/NSPort.h> |
#include <Foundation/NSPort.h> |
27 |
|
#include <Foundation/NSDistantObject.h> |
28 |
#include <Foundation/NSString.h> |
#include <Foundation/NSString.h> |
29 |
#include <Foundation/NSException.h> |
#include <Foundation/NSException.h> |
30 |
|
|
32 |
#include "Network/NetGameList.h" |
#include "Network/NetGameList.h" |
33 |
#include "Network/NetGameID.h" |
#include "Network/NetGameID.h" |
34 |
#include "Network/NetClient.h" |
#include "Network/NetClient.h" |
|
#include "Network/NetServer.h" |
|
|
#include "Network/NetParty.h" |
|
35 |
#include "Network/NetConnection.h" |
#include "Network/NetConnection.h" |
36 |
|
|
37 |
#include "GameEngine/HgPlayerState.h" |
#include "GameEngine/HgPlayerState.h" |
38 |
|
#include "GameEngine/HgGame.h" |
39 |
|
|
40 |
|
@interface NetGameList (Private) |
41 |
|
- (void) _addGameIDWithParty: (NSDistantObject<NetParty> *)party |
42 |
|
withHost: (NSHost *)host; |
43 |
|
- (void) _clearGameList; |
44 |
|
@end |
45 |
|
|
46 |
@interface NSPort (Hack) |
@interface NSPort (Hack) |
47 |
- (NSHost*) host; |
- (NSHost*) host; |
48 |
@end |
@end |
49 |
|
|
50 |
|
|
51 |
/** |
/** |
52 |
* This class represents the list of existing games on the local network and |
* This class represents the list of existing games on the local network and |
53 |
* allows join a game. |
* allows join a game. |
81 |
|
|
82 |
|
|
83 |
/** |
/** |
|
* Creates and adds an gameID in the current list with an existing NetParty |
|
|
* and its NSHost associated. |
|
|
*/ |
|
|
- (void) _addGameIDWithParty: (NetParty *)party |
|
|
withHost: (NSHost *)host |
|
|
{ |
|
|
NSParameterAssert (party); |
|
|
NSParameterAssert (host); |
|
|
|
|
|
NetGameID *game = [[NetGameID alloc] initWithParty: party withHost: host]; |
|
|
[_gameList addObject: game]; |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* Clear all the games in the list. |
|
|
*/ |
|
|
- (void) _clearGameList |
|
|
{ |
|
|
[_gameList removeAllObjects]; |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
84 |
* Research games on the local network to actualise the list and returns |
* Research games on the local network to actualise the list and returns |
85 |
* this NetGameID list. |
* this NetGameID list. |
86 |
*/ |
*/ |
|
/**---------pour l'instant recherche sur un seul serveur---------------**/ |
|
87 |
- (NSArray *) searchGames |
- (NSArray *) searchGames |
88 |
{ |
{ |
89 |
id<NetServer> server; |
NSDistantObject<NetServer> *server; |
90 |
|
unsigned int num = 1; |
91 |
|
unsigned int i; |
92 |
|
NSString *name; |
93 |
|
NSHost *host; |
94 |
|
|
|
server = (id<NetServer>)[NetConnection |
|
|
rootProxyForConnectionWithRegisteredName: |
|
|
@"GameList" host: @"*"]; |
|
95 |
[self _clearGameList]; |
[self _clearGameList]; |
|
if (server != nil) |
|
|
{ |
|
|
NSArray *partyList = [[server partyList] allValues]; |
|
|
unsigned int i; |
|
|
for (i = 0; i < [partyList count]; i++) |
|
|
[self _addGameIDWithParty: [partyList objectAtIndex: i] |
|
|
withHost: [[[NetConnection currentConnection] |
|
|
sendPort] host]]; |
|
|
} |
|
96 |
|
|
97 |
|
do |
98 |
|
{ |
99 |
|
name = [NSString stringWithFormat: @"Sever%d", num]; |
100 |
|
num++; |
101 |
|
|
102 |
|
server = [NetConnection rootProxyForConnectionWithRegisteredName: name |
103 |
|
host: @"*"]; |
104 |
|
host = [[[NetConnection currentConnection] sendPort] host]; |
105 |
|
if (server != nil) |
106 |
|
{ |
107 |
|
NSArray *partyNameList = [server partyNameList]; |
108 |
|
NSString *nameRegisteredForParty; |
109 |
|
NSDistantObject<NetParty> * party; |
110 |
|
|
111 |
|
for (i = 0; i < [partyNameList count]; i++) |
112 |
|
{ |
113 |
|
nameRegisteredForParty = [NSString stringWithFormat: @"%s%s", |
114 |
|
name, |
115 |
|
[partyNameList objectAtIndex: i]]; |
116 |
|
|
117 |
|
party = [NetConnection rootProxyForConnectionWithRegisteredName: |
118 |
|
nameRegisteredForParty |
119 |
|
host: @"*"]; |
120 |
|
[self _addGameIDWithParty: party |
121 |
|
withHost: host]; |
122 |
|
} |
123 |
|
} |
124 |
|
} while (server != nil); |
125 |
|
|
126 |
return _gameList; |
return _gameList; |
127 |
} |
} |
128 |
|
|
137 |
NSParameterAssert (gameID); |
NSParameterAssert (gameID); |
138 |
NSParameterAssert (playerName); |
NSParameterAssert (playerName); |
139 |
|
|
140 |
HgGame *game = [[HgGame alloc] init]; |
HgGame *game = AUTORELEASE([[HgGame alloc] init]); |
141 |
|
|
142 |
/* FIXME - error message without cast */ |
NSPort *receivePort = [NSPort port]; |
|
NSPort *receivePort = (NSPort *)[NSPort port]; |
|
143 |
|
|
144 |
NSPort *sendPort = [[gameID party] connectWithName: playerName |
NSPort *sendPort = [[gameID party] connectWithName: playerName |
145 |
withPort: receivePort |
withPort: receivePort |
146 |
initGame: game]; |
initGame: game]; |
147 |
if (sendPort != nil) |
if (sendPort != nil) |
148 |
{ |
{ |
149 |
NetConnection *cnx = [NetConnection |
NetConnection *cnx = [NetConnection |
159 |
|
|
160 |
[serverInfosClient createProxy]; |
[serverInfosClient createProxy]; |
161 |
[serverInfosClient delegateClient]; |
[serverInfosClient delegateClient]; |
162 |
|
|
163 |
|
RELEASE(game); |
164 |
|
|
165 |
return client; |
return client; |
166 |
} |
} |
167 |
else |
else |
168 |
{ |
{ |
169 |
|
RELEASE(game); |
170 |
|
|
171 |
return nil; |
return nil; |
172 |
} |
} |
173 |
} |
} |
174 |
|
|
175 |
@end // End of NetGameList class implementation |
@end // End of NetGameList class implementation |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
/** |
180 |
|
* Private methods implementation. |
181 |
|
*/ |
182 |
|
@implementation NetGameList (Private) |
183 |
|
|
184 |
|
/** |
185 |
|
* Creates and adds an gameID in the current list with an existing distant |
186 |
|
* object NetParty and its NSHost associated. |
187 |
|
*/ |
188 |
|
- (void) _addGameIDWithParty: (NSDistantObject<NetParty> *)party |
189 |
|
withHost: (NSHost *)host |
190 |
|
{ |
191 |
|
NSParameterAssert (party); |
192 |
|
NSParameterAssert (host); |
193 |
|
|
194 |
|
NetGameID *game = [[NetGameID alloc] initWithParty: party |
195 |
|
withHost: host]; |
196 |
|
[_gameList addObject: game]; |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
/** |
201 |
|
* Clear all the games in the list. |
202 |
|
*/ |
203 |
|
- (void) _clearGameList |
204 |
|
{ |
205 |
|
[_gameList removeAllObjects]; |
206 |
|
} |
207 |
|
@end |