/* -*- Objc -*- */ /* * $Id: NetParty.m,v 1.1 2003/05/26 10:21:08 thunder Exp $ * * Copyright (C) 2003 Free Software Foundation, Inc. * * This file is part of GNU Hégémonie. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include "Network/NetParty.h" #include "Network/NetClient.h" #include "Network/NetServer.h" #include "Network/NetInfosClient.h" #include "Network/NetConnection.h" #include "GameEngine/HgGame.h" #include "GameEngine/HgPlayer.h" /** * NetParty class * Adopt the NetParty protocol */ @implementation NetParty /** * Initialise NetParty. Subscribes the party to server, with name * gameName. The name must be unused on the server. */ - (id) initWithName: (NSString *)gameName inServer: (NetServer *)server { NSParameterAssert (gameName); NSParameterAssert ([gameName length] != 0); NSParameterAssert (server); self = [super init]; if (self != nil) { _server = RETAIN(server); _clients = AUTORELEASE([NSMutableDictionary dictionary]); _game = AUTORELEASE([[HgGame alloc] initWithName: gameName]); [server addParty: self]; } return self; } /** * Release NetParty. */ - (void) dealloc { RELEASE(_server); RELEASE(_clients); RELEASE(_game); [super dealloc]; } /** * Returns the NetInfosClient object corresponding to the playerName. * If the playerName is not a key of the client's list, it returns nil. */ - (NetInfosClient *) clientWithName: (NSString *)playerName { NSParameterAssert (playerName); NSParameterAssert ([playerName length] != 0); return [_clients objectForKey: playerName]; } /** * This will return an array containing all the clients */ - (NSDictionary *) allClients { return _clients; } /** * This will finished a NetParty, disconnect all the clients, and unsubscribe * from the server. */ - (void) terminate { [_server removeParty: self]; [self dealloc]; } /** * This will disconnect client, and destroy all data associated with it. */ - (void) disconnect: (NetInfosClient *)client { NSParameterAssert (client); [_clients removeObjectForKey: [[client player] name]]; [_game removePlayer: [client player]]; } /** * Returns the name of the party. */ - (NSString *) name { return [_game name]; } /** * Return the number of players in the party (ie: number of clients). */ - (unsigned) nbPlayers { return [_clients count]; } /** * This is a method of the delegate object. */ - (BOOL) connection: (NetConnection*)parent shouldMakeNewConnection: (NetConnection*)newConnection { return NO; } /** * This will connect the player named playerName to the NetParty * and returns the receive port on the server. * If the name is already in use, returns nil. */ - (NSPort *) connectWithName: (NSString *)playerName withPort: (NSPort *)sendPort initGame: (HgGame *)game { NSParameterAssert (playerName); NSParameterAssert ([playerName length] != 0); if (([playerName length] != 0) && ([_clients objectForKey: playerName] != nil)) { /* FIXME - error message without cast */ NSPort *receivePort = (NSPort *)[NSPort port]; /* creating a new NetConnection with an random registerName returned */ NetConnection *conn = [NetConnection connectionWithReceivePort: receivePort sendPort: sendPort]; /* creating a NetInfosClient associated with the player */ NetInfosClient *newClient = [[NetInfosClient alloc] initWithParty: self withProxy: [conn rootProxy] playerName: playerName]; [conn setRootObject: newClient]; /* bloquing other connection on the receivePort */ NetConnection *defConnection = [[NetConnection alloc] initWithReceivePort: receivePort sendPort: receivePort]; [defConnection setDelegate: newClient]; /* update the clients list */ [_clients setObject: newClient forKey: playerName]; /* add a new player to the game */ [_game addPlayer: [newClient player]]; game = _game; return receivePort; } else { return nil; } } @end