[5530] | 1 | /* |
---|
[5520] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | |
---|
[5525] | 17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
[5530] | 18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
[5525] | 19 | */ |
---|
[5530] | 20 | #define DEBUG_MODULE_NETWORK |
---|
[5525] | 21 | |
---|
| 22 | |
---|
[5566] | 23 | #include "network_stream.h" |
---|
[5572] | 24 | #include "class_list.h" |
---|
[5566] | 25 | |
---|
[5605] | 26 | #include "debug.h" |
---|
[5566] | 27 | |
---|
[5605] | 28 | |
---|
[5525] | 29 | /* include your own header */ |
---|
[5520] | 30 | #include "network_manager.h" |
---|
| 31 | |
---|
[5530] | 32 | |
---|
[5525] | 33 | /* using namespace std is default, this needs to be here */ |
---|
[5520] | 34 | using namespace std; |
---|
| 35 | |
---|
| 36 | |
---|
[5996] | 37 | NetworkManager* NetworkManager::singletonRef = NULL; |
---|
[5520] | 38 | |
---|
| 39 | /** |
---|
| 40 | * standard constructor |
---|
| 41 | */ |
---|
| 42 | NetworkManager::NetworkManager() |
---|
[5566] | 43 | { |
---|
[5572] | 44 | /* set the class id for the base object */ |
---|
[5575] | 45 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
---|
[5804] | 46 | |
---|
[5572] | 47 | /* initialize the references */ |
---|
| 48 | this->netStreamList = NULL; |
---|
| 49 | this->syncList = NULL; |
---|
[5996] | 50 | this->tmpStream = NULL; |
---|
[5997] | 51 | this->hostID = -1; |
---|
[5804] | 52 | |
---|
[5605] | 53 | PRINTF(0)("NetworkManager created\n"); |
---|
[5566] | 54 | } |
---|
[5520] | 55 | |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * standard deconstructor |
---|
| 59 | */ |
---|
| 60 | NetworkManager::~NetworkManager() |
---|
| 61 | {} |
---|
| 62 | |
---|
[5522] | 63 | |
---|
| 64 | /** |
---|
| 65 | * initializes the network manager |
---|
| 66 | */ |
---|
| 67 | void NetworkManager::initialize() |
---|
[5572] | 68 | { |
---|
| 69 | /* get the synchronizeable list from the class list */ |
---|
[5575] | 70 | this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE); |
---|
[5605] | 71 | PRINTF(0)("NetworkManager initzalized\n"); |
---|
[5572] | 72 | } |
---|
[5522] | 73 | |
---|
[5530] | 74 | |
---|
[5522] | 75 | /** |
---|
| 76 | * shutsdown the network manager |
---|
| 77 | */ |
---|
| 78 | void NetworkManager::shutdown() |
---|
[5578] | 79 | { |
---|
[5522] | 80 | |
---|
[5578] | 81 | } |
---|
[5522] | 82 | |
---|
[5578] | 83 | |
---|
[5522] | 84 | /** |
---|
| 85 | * creates a connection from one object to a host |
---|
[5647] | 86 | * @param hostName: the name of the destination host |
---|
| 87 | */ |
---|
[5996] | 88 | int NetworkManager::establishConnection(const char* name, unsigned int port) |
---|
| 89 | { |
---|
| 90 | IPaddress ipAddress; |
---|
| 91 | int error = SDLNet_ResolveHost(&ipAddress, name, port); |
---|
| 92 | if( error == -1) { |
---|
| 93 | printf("\n\nerror on address resolution, program inconsistency\n\n"); |
---|
| 94 | return -1; |
---|
| 95 | } |
---|
[5647] | 96 | |
---|
[5996] | 97 | this->tmpStream = new NetworkStream(ipAddress, NET_CLIENT); |
---|
| 98 | return 1; |
---|
| 99 | } |
---|
[5647] | 100 | |
---|
[5996] | 101 | |
---|
[5647] | 102 | /** |
---|
[5996] | 103 | * creates a new NetworkStream of server type |
---|
| 104 | * @param port: number of the TCP port |
---|
| 105 | */ |
---|
| 106 | int NetworkManager::createServer(unsigned int port) |
---|
| 107 | { |
---|
| 108 | this->tmpStream = new NetworkStream(port, NET_SERVER); |
---|
| 109 | SDL_Delay(20); |
---|
| 110 | return 1; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | /** |
---|
[5647] | 115 | * creates a connection from one object to a host |
---|
[5522] | 116 | * @param address: the address of the destination host |
---|
| 117 | * @param synchronizeable: reference to the sync object |
---|
| 118 | */ |
---|
[5648] | 119 | NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync) |
---|
[5572] | 120 | { |
---|
[5578] | 121 | /* creating a new network stream, it will register itself automaticaly to the class list */ |
---|
[5996] | 122 | this->tmpStream = new NetworkStream(address, sync, NET_CLIENT); |
---|
[5572] | 123 | } |
---|
[5522] | 124 | |
---|
[5996] | 125 | |
---|
[5649] | 126 | /** |
---|
| 127 | * creates a new NetworkStream of server type |
---|
| 128 | * @param sync: the listener |
---|
| 129 | */ |
---|
[5804] | 130 | NetworkStream& NetworkManager::createServer(Synchronizeable& sync, unsigned int port) |
---|
[5649] | 131 | { |
---|
[5805] | 132 | PRINTF(0)("Create a new server socket\n"); |
---|
[5649] | 133 | /* creating a new network stream, it will register itself automaticaly to the class list */ |
---|
[5996] | 134 | this->tmpStream = new NetworkStream(port, sync, NET_SERVER); |
---|
[5649] | 135 | } |
---|
[5522] | 136 | |
---|
[5649] | 137 | |
---|
[5522] | 138 | /** |
---|
| 139 | * teardown a connection |
---|
| 140 | */ |
---|
| 141 | void NetworkManager::shutdownConnection() |
---|
[5605] | 142 | { |
---|
| 143 | PRINTF(0)("Shutdown connection\n"); |
---|
| 144 | } |
---|
[5522] | 145 | |
---|
[5530] | 146 | |
---|
[5996] | 147 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
---|
| 148 | { |
---|
| 149 | this->tmpStream->connectSynchronizeable(sync); |
---|
| 150 | } |
---|
[5522] | 151 | |
---|
[5996] | 152 | |
---|
[5522] | 153 | /** |
---|
| 154 | * sync the network |
---|
| 155 | */ |
---|
| 156 | void NetworkManager::synchronize() |
---|
[5578] | 157 | { |
---|
| 158 | if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) |
---|
| 159 | { |
---|
[5885] | 160 | std::list<BaseObject*>::const_iterator stream; |
---|
[5822] | 161 | for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) |
---|
[5996] | 162 | if( static_cast<NetworkStream*>(*stream)->isActive()) |
---|
| 163 | static_cast<NetworkStream*>(*stream)->processData(); |
---|
| 164 | |
---|
[5578] | 165 | } |
---|
[5522] | 166 | |
---|
[5578] | 167 | } |
---|
[5997] | 168 | |
---|
| 169 | /** |
---|
| 170 | * Sets the hostID to a specific number |
---|
| 171 | * @param id: The new ID |
---|
| 172 | */ |
---|
| 173 | void NetworkManager::setHostID(int id) |
---|
| 174 | { |
---|
| 175 | this->hostID = id; |
---|
| 176 | } |
---|