/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx 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, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli co-programmer: ... */ /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput */ #define DEBUG_MODULE_NETWORK #include "network_stream.h" #include "class_list.h" #include "debug.h" /* include your own header */ #include "network_manager.h" /* using namespace std is default, this needs to be here */ using namespace std; NetworkManager* NetworkManager::singletonRef = NULL; /** * standard constructor */ NetworkManager::NetworkManager() { /* set the class id for the base object */ this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); /* initialize the references */ this->netStreamList = NULL; this->syncList = NULL; this->tmpStream = NULL; PRINTF(0)("NetworkManager created\n"); } /** * standard deconstructor */ NetworkManager::~NetworkManager() {} /** * initializes the network manager */ void NetworkManager::initialize() { /* get the synchronizeable list from the class list */ this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE); PRINTF(0)("NetworkManager initzalized\n"); } /** * shutsdown the network manager */ void NetworkManager::shutdown() { } /** * creates a connection from one object to a host * @param hostName: the name of the destination host */ int NetworkManager::establishConnection(const char* name, unsigned int port) { IPaddress ipAddress; int error = SDLNet_ResolveHost(&ipAddress, name, port); if( error == -1) { printf("\n\nerror on address resolution, program inconsistency\n\n"); return -1; } this->tmpStream = new NetworkStream(ipAddress, NET_CLIENT); return 1; } /** * creates a new NetworkStream of server type * @param port: number of the TCP port */ int NetworkManager::createServer(unsigned int port) { this->tmpStream = new NetworkStream(port, NET_SERVER); SDL_Delay(20); return 1; } /** * creates a connection from one object to a host * @param address: the address of the destination host * @param synchronizeable: reference to the sync object */ NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync) { /* creating a new network stream, it will register itself automaticaly to the class list */ this->tmpStream = new NetworkStream(address, sync, NET_CLIENT); } /** * creates a new NetworkStream of server type * @param sync: the listener */ NetworkStream& NetworkManager::createServer(Synchronizeable& sync, unsigned int port) { PRINTF(0)("Create a new server socket\n"); /* creating a new network stream, it will register itself automaticaly to the class list */ this->tmpStream = new NetworkStream(port, sync, NET_SERVER); } /** * teardown a connection */ void NetworkManager::shutdownConnection() { PRINTF(0)("Shutdown connection\n"); } void NetworkManager::connectSynchronizeable(Synchronizeable& sync) { this->tmpStream->connectSynchronizeable(sync); } /** * sync the network */ void NetworkManager::synchronize() { if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) { // tIterator* iterator = this->netStreamList->getIterator(); // NetworkStream* stream = (NetworkStream*)(iterator->firstElement()); // while( stream) // { // if(stream->isActive()) // stream->processData(); // stream = (NetworkStream*)(iterator->nextElement()); // } // delete iterator; std::list::iterator stream; for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) if( static_cast(*stream)->isActive()) static_cast(*stream)->processData(); } }