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 | |
---|
23 | #include "network_stream.h" |
---|
24 | #include "class_list.h" |
---|
25 | |
---|
26 | #include "debug.h" |
---|
27 | |
---|
28 | |
---|
29 | /* include your own header */ |
---|
30 | #include "network_manager.h" |
---|
31 | |
---|
32 | |
---|
33 | /* using namespace std is default, this needs to be here */ |
---|
34 | using namespace std; |
---|
35 | |
---|
36 | |
---|
37 | NetworkManager* NetworkManager::singletonRef = NULL; |
---|
38 | |
---|
39 | /** |
---|
40 | * standard constructor |
---|
41 | */ |
---|
42 | NetworkManager::NetworkManager() |
---|
43 | { |
---|
44 | /* set the class id for the base object */ |
---|
45 | this->setClassID(CL_NETWORK_MANAGER, "NetworkManager"); |
---|
46 | |
---|
47 | /* initialize the references */ |
---|
48 | this->netStreamList = NULL; |
---|
49 | this->syncList = NULL; |
---|
50 | this->tmpStream = NULL; |
---|
51 | |
---|
52 | PRINTF(0)("NetworkManager created\n"); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * standard deconstructor |
---|
58 | */ |
---|
59 | NetworkManager::~NetworkManager() |
---|
60 | {} |
---|
61 | |
---|
62 | |
---|
63 | /** |
---|
64 | * initializes the network manager |
---|
65 | */ |
---|
66 | void NetworkManager::initialize() |
---|
67 | { |
---|
68 | /* get the synchronizeable list from the class list */ |
---|
69 | this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE); |
---|
70 | PRINTF(0)("NetworkManager initzalized\n"); |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | /** |
---|
75 | * shutsdown the network manager |
---|
76 | */ |
---|
77 | void NetworkManager::shutdown() |
---|
78 | { |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | * creates a connection from one object to a host |
---|
85 | * @param hostName: the name of the destination host |
---|
86 | */ |
---|
87 | int NetworkManager::establishConnection(const char* name, unsigned int port) |
---|
88 | { |
---|
89 | IPaddress ipAddress; |
---|
90 | int error = SDLNet_ResolveHost(&ipAddress, name, port); |
---|
91 | if( error == -1) { |
---|
92 | printf("\n\nerror on address resolution, program inconsistency\n\n"); |
---|
93 | return -1; |
---|
94 | } |
---|
95 | |
---|
96 | this->tmpStream = new NetworkStream(ipAddress, NET_CLIENT); |
---|
97 | return 1; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | * creates a new NetworkStream of server type |
---|
103 | * @param port: number of the TCP port |
---|
104 | */ |
---|
105 | int NetworkManager::createServer(unsigned int port) |
---|
106 | { |
---|
107 | this->tmpStream = new NetworkStream(port, NET_SERVER); |
---|
108 | SDL_Delay(20); |
---|
109 | return 1; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /** |
---|
114 | * creates a connection from one object to a host |
---|
115 | * @param address: the address of the destination host |
---|
116 | * @param synchronizeable: reference to the sync object |
---|
117 | */ |
---|
118 | NetworkStream& NetworkManager::establishConnection(IPaddress& address, Synchronizeable& sync) |
---|
119 | { |
---|
120 | /* creating a new network stream, it will register itself automaticaly to the class list */ |
---|
121 | this->tmpStream = new NetworkStream(address, sync, NET_CLIENT); |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | * creates a new NetworkStream of server type |
---|
127 | * @param sync: the listener |
---|
128 | */ |
---|
129 | NetworkStream& NetworkManager::createServer(Synchronizeable& sync, unsigned int port) |
---|
130 | { |
---|
131 | PRINTF(0)("Create a new server socket\n"); |
---|
132 | /* creating a new network stream, it will register itself automaticaly to the class list */ |
---|
133 | this->tmpStream = new NetworkStream(port, sync, NET_SERVER); |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | /** |
---|
138 | * teardown a connection |
---|
139 | */ |
---|
140 | void NetworkManager::shutdownConnection() |
---|
141 | { |
---|
142 | PRINTF(0)("Shutdown connection\n"); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | void NetworkManager::connectSynchronizeable(Synchronizeable& sync) |
---|
147 | { |
---|
148 | this->tmpStream->connectSynchronizeable(sync); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | /** |
---|
153 | * sync the network |
---|
154 | */ |
---|
155 | void NetworkManager::synchronize() |
---|
156 | { |
---|
157 | if (this->netStreamList != NULL || (this->netStreamList = ClassList::getList(CL_NETWORK_STREAM)) != NULL) |
---|
158 | { |
---|
159 | // tIterator<BaseObject>* iterator = this->netStreamList->getIterator(); |
---|
160 | // NetworkStream* stream = (NetworkStream*)(iterator->firstElement()); |
---|
161 | // while( stream) |
---|
162 | // { |
---|
163 | // if(stream->isActive()) |
---|
164 | // stream->processData(); |
---|
165 | // stream = (NetworkStream*)(iterator->nextElement()); |
---|
166 | // } |
---|
167 | // delete iterator; |
---|
168 | std::list<BaseObject*>::iterator stream; |
---|
169 | for (stream = this->netStreamList->begin(); stream != this->netStreamList->end(); ++stream) |
---|
170 | if( static_cast<NetworkStream*>(*stream)->isActive()) |
---|
171 | static_cast<NetworkStream*>(*stream)->processData(); |
---|
172 | |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|