NetPeerManager Class Reference
[NetworkingSingle inited classes]

A peer to peer network managing class, derived from NetBasicManager. More...

#include <netpeermanager.h>

Inheritance diagram for NetPeerManager:

Inheritance graph
[legend]
Collaboration diagram for NetPeerManager:

Collaboration graph
[legend]
List of all members.

Public Member Functions

DECLSPEC NetPeerManager (const char *name, BasicObject *p, int maxpeers=255)
DECLSPEC NetPeerManager (TiXmlElement *el, BasicObject *p)
virtual const char * getClassName ()
virtual DECLSPEC bool init (unsigned int port=NETWORK_PORT, const char *permissionfn=0)
DECLSPEC void setNetName (const char *name)
DECLSPEC const char * getNetName () const
DECLSPEC NetPeerSocketgetSocketByName (const char *name)
DECLSPEC void requestPings ()
DECLSPEC Uint32 getPings (Uint32 *min, Uint32 *max)
DECLSPEC bool loadPermissions (TiXmlNode *node)
DECLSPEC bool loadPermissions (const char *fn)
DECLSPEC bool hasPermission (const char *name, Uint32 host, char *reason=0)

Static Public Member Functions

static NetPeerManagergetThe ()

Public Attributes

bool bAllowNewConnections

Protected Member Functions

virtual DECLSPEC void onInit ()
virtual DECLSPEC NetSocketcreateSocket (TCPsocket socket)

Classes

struct  HOST_INFO

Detailed Description

A peer to peer network managing class, derived from NetBasicManager.

This class contains many functions for peer to peer communication. It uses client and server functions to connect to others and let others connect. As socket class, is used NetPeerSocket.

As a special feature, this class is able to work with XML Nodes.

Warning:
Only NetPeerSocket (or derived) objects may be children of this class.
Do not initialize this class, if another class, derived from NetBasicManager has already been inited!


Constructor & Destructor Documentation

NetPeerManager::NetPeerManager ( const char *  name,
BasicObject p,
int  maxpeers = 255 
)

Constructor

Parameters:
maxpeers limits the maximum number of allowed peers to this value, if it is below MAX_PEERS, otherwise the limit equals MAX_PEERS

NetPeerManager::NetPeerManager ( TiXmlElement el,
BasicObject p 
)

Constructs the manager by using a XML node.

Parameters:
el The node can contain the attribute "Username". If a subnode "Permissions" exists, this subnode will be parsed with the loadPermissions( XMLNode* ) function, if not and the attribute "PermissionFile" is specified, the file will be parsed and if no permission data is given, every host is allowed to connect to the manager. If the attribte "Port" exists, this port will be opened, otherwise the default NETWORK_PORT will be used. Here an example:
    <NetPeerManager Name="NetPeerManager"
            Port="1230"
            Username="my name"
            PermissionFile="user/permissions.pml"
    />
Returns:
true, if initialisation was successful, false if not
See also:
loadPermissions(const char*), setName, init( int, const char * )


Member Function Documentation

virtual const char* NetPeerManager::getClassName (  )  [inline, virtual]

This function should be overriden in every sub-class of BasicObject!

Returns:
the name of the class.

Reimplemented from BasicObject.

bool NetPeerManager::init ( unsigned int  port = NETWORK_PORT,
const char *  permissionfn = 0 
) [virtual]

Initializes the manager with the given params. Only call this, if you do not use the constructor with the XML-element.

Parameters:
port the port to listen for incomming connection requests
permissionfn the filename of a XML file, containing data for permission rules, that say whom to accept and whom to reject.
Returns:
true, if initialisation was successful, false if not
See also:
loadPermissions(const char*)

void NetPeerManager::setNetName ( const char *  name  ) 

Sets the name of the local machine, which is used to connect to other machines in the network.

Parameters:
name the name as NUL-terminated string
Note:
the network-name should not change, as long as a NetPeerSocket is connected

DECLSPEC const char* NetPeerManager::getNetName (  )  const [inline]

Returns:
the name of the local machine, as set by setNetName()

NetPeerSocket * NetPeerManager::getSocketByName ( const char *  name  ) 

Parameters:
name the name to look for
Returns:
a pointer to the socket, which is connected to the host with the given name.
See also:
getNetName(), setNetName().

void NetPeerManager::requestPings (  ) 

Sends a ping request to all hosts. (This does not use ICMP, but TCP)

Uint32 NetPeerManager::getPings ( Uint32 *  min,
Uint32 *  max 
)

Use this function to get ping times. To get current times, call requestPings() before getPings and wait a bit between these two calls.

Parameters:
min if not NULL, the time of the fastest response of a host will be assigned here
max if not NULL, the time of the slowest response of a host will be assigned here
Returns:
the average response time of all hosts

bool NetPeerManager::loadPermissions ( TiXmlNode node  ) 

Parses the node to store the allowed hosts in a privat struct, which is used by hasPermission to allow or deny an incomming connection. Example:

        <Allow>
          <Player IP="192.168.2.3" />
          <Player IP="192.168.2.4" />
          <Player IP="192.168.2.5" />
          <Player Name="test" />
          <Player IP="127.0.0.1" />
        </Allow>
        <Disallow>
          <Player IP="192.168.2.6" />
          <Player />
        </Disallow>
In the example the hosts with the addresses 192.168.2.3, -4, -5 and 127.0.0.1 are accepted, The host with the name "test" is only accepted, if it's address is not 192.168.2.6; all others are not accepted.
Returns:
true, if no errors occured, false otherwise

bool NetPeerManager::loadPermissions ( const char *  fn  ) 

Loads the XML file with the specified filename and parses it's Permissions node with loadPermissions( XMLNode ). The file has to be structured as follows (minimal version):

    <PML><Permissions>
    [<Allow> (allowed Player-tags) </Allow>]
    [<Disallow> (disallowed Player-tags) </Disallow>]
    </Permissions></PML>
Returns:
true, if no errors occured, false otherwise

bool NetPeerManager::hasPermission ( const char *  name,
Uint32  host,
char *  reason = 0 
)

Prooves, whether the specified host has permission to connect, as specified in the permission-tags.

Parameters:
name the name of the host
host the host's IP address (in OSBO)
reason NULL, or a pointer to reserved space, filled with the reason, why the host has no permission, if a reason is availible
Returns:
true, if the host is accepted, false otherwise

static NetPeerManager* NetPeerManager::getThe (  )  [inline, static]

Returns:
a pointer to the NetPeerManager-variable, or NULL if not initialized. (There is only one variable allowed in an application that is a derived class from NetBasicManager)
Every netrinjo-class that can only exist once at a time, has this static function.
See also:
Single inited classes, NetBasicManager::getThe(), NetSimpleManager::getThe()

Reimplemented from NetBasicManager.

void NetPeerManager::onInit (  )  [protected, virtual]

Initializes the object's variables by reading data from the XML element. If there were no XML data used for creation, nothing is done. This function calls onInit() of its children recursively (after it has inited itself). It is highly recommended to call BasicObject::onInit(), usually at the end of the onInit() method, when overwriting it in a derived class.

Reimplemented from NetBasicManager.

NetSocket * NetPeerManager::createSocket ( TCPsocket  socket  )  [protected, virtual]

Creates a class, derived from NetSocket, that is compatible to the managing class.

Parameters:
socket the SDL-TCP socket which is used by the new NetSocket
Returns:
a pointer to the created class, converted to NetSocket*

Implements NetBasicManager.


Member Data Documentation

bool NetPeerManager::bAllowNewConnections

If - what ever the reason is - no further connections should be accepted then set this to false. It is true, when NetPeerManager is created


The documentation for this class was generated from the following files:
Generated on Wed May 9 17:36:30 2007 for netrinjo by  doxygen 1.5.1