1 | /* |
---|
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 | |
---|
17 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
18 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
19 | */ |
---|
20 | #define DEBUG_MODULE_NETWORK |
---|
21 | |
---|
22 | #include "class_list.h" |
---|
23 | #include "debug.h" |
---|
24 | #include "shell_command.h" |
---|
25 | |
---|
26 | /* include your own header */ |
---|
27 | #include "network_manager.h" |
---|
28 | #include "shared_network_data.h" |
---|
29 | #include "network_stream.h" |
---|
30 | |
---|
31 | |
---|
32 | /* using namespace std is default, this needs to be here */ |
---|
33 | using namespace std; |
---|
34 | |
---|
35 | SHELL_COMMAND(debug, NetworkManager, debug); |
---|
36 | |
---|
37 | |
---|
38 | NetworkManager* NetworkManager::singletonRef = NULL; |
---|
39 | |
---|
40 | /** |
---|
41 | * standard constructor |
---|
42 | */ |
---|
43 | NetworkManager::NetworkManager() |
---|
44 | { |
---|
45 | /* set the class id for the base object */ |
---|
46 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
---|
47 | PRINTF(0)("START\n"); |
---|
48 | |
---|
49 | /* initialize the references */ |
---|
50 | this->netStreamList = NULL; |
---|
51 | this->syncList = NULL; |
---|
52 | this->defaultSyncStream = NULL; |
---|
53 | this->sharedNetworkData = SharedNetworkData::getInstance(); |
---|
54 | |
---|
55 | PRINTF(0)("NetworkManager created\n"); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * standard deconstructor |
---|
61 | */ |
---|
62 | NetworkManager::~NetworkManager() |
---|
63 | {} |
---|
64 | |
---|
65 | |
---|
66 | /** |
---|
67 | * initializes the network manager |
---|
68 | */ |
---|
69 | void NetworkManager::initialize() |
---|
70 | { |
---|
71 | /* get the synchronizeable list from the class list */ |
---|
72 | this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE); |
---|
73 | PRINTF(0)("NetworkManager initzalized\n"); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /** |
---|
78 | * shutsdown the network manager |
---|
79 | */ |
---|
80 | void NetworkManager::shutdown() |
---|
81 | { |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | /** |
---|
87 | * creates a connection from one object to a host |
---|
88 | * @param hostName: the name of the destination host |
---|
89 | */ |
---|
90 | int NetworkManager::establishConnection(const std::string & name, unsigned int port) |
---|
91 | { |
---|
92 | IPaddress ipAddress; |
---|
93 | int error = SDLNet_ResolveHost(&ipAddress, name.c_str(), port); |
---|
94 | if( error == -1) { |
---|
95 | printf("\n\nerror on address resolution, program inconsistency\n\n"); |
---|
96 | return -1; |
---|
97 | } |
---|
98 | |
---|
99 | this->defaultSyncStream = new NetworkStream(ipAddress); |
---|
100 | this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); |
---|
101 | this->defaultSyncStream->startHandshake(); |
---|
102 | return 1; |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /** |
---|
107 | * creates a new NetworkStream of server type |
---|
108 | * @param port: number of the TCP port |
---|
109 | */ |
---|
110 | int NetworkManager::createServer(unsigned int port) |
---|
111 | { |
---|
112 | this->sharedNetworkData->setHostID(0); |
---|
113 | this->sharedNetworkData->setGameServer(true); |
---|
114 | this->defaultSyncStream = new NetworkStream(port); |
---|
115 | this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream); |
---|
116 | this->defaultSyncStream->createNetworkGameManager(); |
---|
117 | PRINTF(0)("CREATE SERVER\n"); |
---|
118 | SDL_Delay(20); |
---|
119 | return 1; |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
---|
124 | { |
---|
125 | if( this->defaultSyncStream) |
---|
126 | this->defaultSyncStream->connectSynchronizeable(sync); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | * sync the network |
---|
132 | */ |
---|
133 | void NetworkManager::synchronize() |
---|
134 | { |
---|
135 | if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) |
---|
136 | { |
---|
137 | std::list<BaseObject*>::const_iterator stream; |
---|
138 | for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) |
---|
139 | if( static_cast<NetworkStream*>(*stream)->isActive()) |
---|
140 | static_cast<NetworkStream*>(*stream)->processData(); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | /** |
---|
147 | * debug output |
---|
148 | */ |
---|
149 | void NetworkManager::debug() |
---|
150 | { |
---|
151 | PRINT(0)("=================Network::debug()=========\n"); |
---|
152 | this->defaultSyncStream->debug(); |
---|
153 | PRINT(0)("===========================================\n"); |
---|
154 | } |
---|