27 |
|
|
28 |
#include "Network/NetServer.h" |
#include "Network/NetServer.h" |
29 |
#include "Network/NetParty.h" |
#include "Network/NetParty.h" |
30 |
|
#include "Network/NetConnection.h" |
31 |
|
|
32 |
/** |
/** |
33 |
* This class manage list of games "NetParty". |
* This class manage list of games "NetParty". |
35 |
@implementation NetServer |
@implementation NetServer |
36 |
|
|
37 |
/** |
/** |
38 |
* Initialise NetServer with an empty list of games. |
* Initialise NetServer with the name used for vend this object. |
39 |
|
* This object must to vended afer this initialization, and before to call |
40 |
|
* an other method of this object. |
41 |
|
* The list of games is empty. |
42 |
|
* |
43 |
*/ |
*/ |
44 |
- (id) init |
- (id) initWithRegisteredName: (NSString *)name |
45 |
{ |
{ |
46 |
|
NSParameterAssert (name); |
47 |
|
|
48 |
self = [super init]; |
self = [super init]; |
49 |
|
|
50 |
if (self != nil) |
if (self != nil) |
51 |
{ |
{ |
52 |
_partyList = AUTORELEASE ([NSMutableDictionary dictionary]); |
_partyList = AUTORELEASE ([NSMutableDictionary dictionary]); |
53 |
|
_registreredName = RETAIN (name); |
54 |
} |
} |
55 |
|
|
56 |
return self; |
return self; |
62 |
- (void) dealloc |
- (void) dealloc |
63 |
{ |
{ |
64 |
RELEASE (_partyList); |
RELEASE (_partyList); |
65 |
|
RELEASE (_registreredName); |
66 |
|
|
67 |
[super dealloc]; |
[super dealloc]; |
68 |
} |
} |
69 |
|
|
70 |
|
|
71 |
|
/** |
72 |
|
* Vend an object "party" for a new connection with a registered name. |
73 |
|
*/ |
74 |
|
void |
75 |
|
registerName (NSString *registeredName, NetParty *party) |
76 |
|
{ |
77 |
|
NetConnection *connexion; |
78 |
|
NSPort *port; |
79 |
|
|
80 |
|
port = [NSPort port]; |
81 |
|
|
82 |
|
connexion = [[NetConnection alloc] initWithReceivePort: port |
83 |
|
sendPort: port]; |
84 |
|
|
85 |
|
[connexion setRootObject: party]; |
86 |
|
|
87 |
|
[connexion registerName: registeredName]; |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
/** |
/** |
92 |
* Create and add a new game "NetParty" with the name of this game. |
* Create and add a new game "NetParty" with the name of this game. |
93 |
* This name must be a NSString not empty. |
* This name must be a NSString not empty and it must not be a game's name |
94 |
* This name must not be a game's name already present in the list. |
* already present in the list, else an exception "gameNameException" is raised |
95 |
*/ |
*/ |
96 |
- (void) createParty: (NSString *)gameName |
- (void) createParty: (NSString *)gameName |
97 |
{ |
{ |
98 |
NSAssert (![gameName isEqualToString: @""], |
NSParameterAssert (gameName); |
99 |
@"Error in createParty, gameName is empty"); |
NSParameterAssert ([gameName isEqualToString: @""]); |
100 |
NSAssert1 ([_partyList objectForKey: gameName] == nil, |
|
101 |
@"Error in createParty, gameName \"%s\" already exists", |
NSException *myException = [NSException |
102 |
gameName); |
exceptionWithName:@"gameNameException" |
103 |
|
reason:@"gameName already exists" |
104 |
|
userInfo:nil]; |
105 |
|
|
106 |
|
if ([_partyList objectForKey: gameName] != nil) |
107 |
|
[myException raise]; |
108 |
|
|
109 |
NetParty *newParty; |
NetParty *newParty; |
|
|
|
|
newParty = AUTORELEASE ([[NetParty alloc] initWithName: gameName |
|
|
inServer: self]); |
|
110 |
|
|
111 |
|
newParty = AUTORELEASE ([[NetParty alloc] initWithName: gameName |
112 |
|
inServer: self]); |
113 |
|
|
114 |
[_partyList setObject: newParty forKey: gameName]; |
[_partyList setObject: newParty forKey: gameName]; |
115 |
|
|
116 |
|
NSString *nameForRegister; |
117 |
|
nameForRegister = [NSString stringWithFormat: @"%sParty%d", |
118 |
|
_registreredName, [_partyList count]]; |
119 |
|
|
120 |
|
registerName (nameForRegister, newParty); |
121 |
} |
} |
122 |
|
|
123 |
/** |
/** |
124 |
* Add a existing game "NetParty" to the list. |
* Add a existing game "NetParty" to the list. |
125 |
* This name of this game must be a NSString not empty and must not be |
* This name of this game must not be a game's name already |
126 |
* a game's name already present in the list. |
* present in the list. If it's the case, an exception "gameNameException" |
127 |
|
* is raised. |
128 |
*/ |
*/ |
129 |
- (void) addParty: (NetParty *)game |
- (void) addParty: (NetParty *)game |
130 |
{ |
{ |
131 |
NSAssert ([[game name] length] != 0, |
NSParameterAssert (game); |
132 |
@"Error in createParty, gameName is empty"); |
|
133 |
NSAssert1 ([_partyList objectForKey: [game name]] == nil, |
NSException *myException = [NSException |
134 |
@"Error in createParty, gameName \"%s\" already exists", |
exceptionWithName:@"gameNameException" |
135 |
[game name]); |
reason:@"gameName already exists" |
136 |
|
userInfo:nil]; |
137 |
|
|
138 |
|
if ([_partyList objectForKey: [game name]] != nil) |
139 |
|
[myException raise]; |
140 |
|
|
141 |
[_partyList setObject: game forKey: [game name]]; |
[_partyList setObject: game forKey: [game name]]; |
142 |
|
|
143 |
|
NSString *nameForRegister; |
144 |
|
nameForRegister = [NSString stringWithFormat: @"%sParty%d", |
145 |
|
_registreredName, [_partyList count]]; |
146 |
|
|
147 |
|
registerName (nameForRegister, game); |
148 |
} |
} |
149 |
|
|
150 |
/** |
/** |
159 |
} |
} |
160 |
|
|
161 |
/** |
/** |
162 |
* Returns a dictionary that contains the list of games "NetParty". |
* Returns an array of party's name that contains the list of games "NetParty". |
163 |
*/ |
*/ |
164 |
- (NSDictionary *) partyList |
- (NSArray *) partyNameList |
165 |
{ |
{ |
166 |
return _partyList; |
return [_partyList allKeys]; |
167 |
} |
} |
168 |
|
|
169 |
/** |
/** |