Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/network_manager.cc @ 9543

Last change on this file since 9543 was 9538, checked in by patrick, 18 years ago

more proxy control functionality: message passing init

File size: 5.2 KB
Line 
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 (patrick@orxonox.ethz.ch)
13   co-programmer: Christoph Renner (rennerc@ee.ethz.ch)
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#include "preferences.h"
31#include "network_log.h"
32#include "network_game_manager.h"
33#include "proxy/proxy_control.h"
34
35
36/* using namespace std is default, this needs to be here */
37
38
39SHELL_COMMAND(debug, NetworkManager, debug);
40
41
42NetworkManager* NetworkManager::singletonRef = NULL;
43
44/**
45 *  standard constructor
46 */
47NetworkManager::NetworkManager()
48{
49  /* set the class id for the base object */
50  this->setClassID(CL_NETWORK_MANAGER, "NetworkManager");
51  PRINTF(0)("START\n");
52
53  /* initialize the references */
54  this->networkStream = NULL;
55  this->elapsedTime = 0.0f;
56
57
58  int port = Preferences::getInstance()->getInt( "network", "telnetport", 0 );
59
60  if ( port > 0 )
61    NetworkLog::getInstance()->listen( port );
62
63  PRINTF(0)("NetworkManager created\n");
64}
65
66
67/**
68 *  standard deconstructor
69 */
70NetworkManager::~NetworkManager()
71{
72  PRINTF(0)("NetworkManager destructor\n");
73  if ( this->networkStream )
74  {
75    delete this->networkStream;
76    this->networkStream = NULL;
77  }
78
79  NetworkManager::singletonRef = NULL;
80}
81
82
83/**
84 *  initializes the network manager
85 */
86void NetworkManager::initialize()
87{
88  PRINTF(0)("NetworkManager initzalized\n");
89}
90
91
92/**
93 *  shutsdown the network manager
94 */
95void NetworkManager::shutdown()
96{
97
98}
99
100
101
102/**
103 *  creates a new NetworkStream of server type
104 * @param clientPort: number of the TCP/UDP port for client connections
105 * @param proxyPort: number of the TCP/UDP port for proxy connections
106 */
107int NetworkManager::createMasterServer(unsigned int port)
108{
109  // load the network settings
110  NetworkSettings::getInstance()->loadData();
111
112  // create the network stream
113  this->networkStream = new NetworkStream(NET_MASTER_SERVER);
114  this->networkStream->createServer( port, port + 1);
115
116  // start the network game manager
117  this->networkStream->createNetworkGameManager();
118
119  // init the proxy control center
120  ProxyControl::getInstance();
121
122  PRINTF(0)("Created Network Master Server\n");
123  SDL_Delay(20);
124  return 1;
125}
126
127/**
128 *  creates a new network stream of proxy server type
129 * @param port: number of the TCP port
130 */
131int NetworkManager::createProxyServer(unsigned int port)
132{
133  // load the network settings
134  NetworkSettings::getInstance()->loadData();
135
136  // create the network stream af
137  this->networkStream = new NetworkStream(NET_PROXY_SERVER_ACTIVE );
138  // first connect to the master server for synchronization
139  this->networkStream->connectToMasterServer(NetworkSettings::getInstance()->getMasterAddr().ipString(), 10000);
140  // start the handshake with the master server
141  this->networkStream->startHandshake(NET_ID_MASTER_SERVER);
142
143  // then start the server
144  this->networkStream->createServer( port, port +1);
145
146  // and to the other proxy servers also, this would be very nice if its works
147  /* put it here....*/
148
149  // init the proxy control center
150  ProxyControl::getInstance();
151
152  PRINTF(0)("Created Network Proxy Server\n");
153  SDL_Delay(20);
154  return 1;
155}
156
157
158/**
159 *  creates a connection from one object to a host
160 * @param hostName: the name of the destination host
161 */
162int NetworkManager::createClient(const std::string & name, unsigned int port)
163{
164  // load the network settings
165  NetworkSettings::getInstance()->loadData();
166
167  // create the network stream
168  this->networkStream = new NetworkStream(NET_CLIENT);
169  // connect to the master server, if a redirection should occure, this is handled in the NetworkStream itself
170  this->networkStream->connectToMasterServer( name, port);
171
172  // and start the handshake
173  this->networkStream->startHandshake();
174
175  PRINTF(0)("Created Network Client\n");
176  return 1;
177}
178
179
180/**
181 * connects a synchronizeable to the network stream
182 * @param sync: synchronizeable to connect
183 */
184void NetworkManager::connectSynchronizeable(Synchronizeable& sync)
185{
186  if( this->networkStream)
187    this->networkStream->connectSynchronizeable(sync);
188}
189
190
191/**
192 *  sync the network
193 *  @param dtS: the seceonds elapsed since the last synchronize call
194 */
195void NetworkManager::synchronize( float dtS)
196{
197  this->elapsedTime += dtS;
198  if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY))
199    return;
200
201  this->elapsedTime = 0.0f;
202
203  if ( this->networkStream )
204    networkStream->processData();
205
206  NetworkGameManager::getInstance()->tick( this->elapsedTime );
207}
208
209
210
211/**
212 * debug output
213 */
214 void NetworkManager::debug()
215{
216  PRINT(0)("=================Network::debug()=========\n");
217  this->networkStream->debug();
218  PRINT(0)("===========================================\n");
219}
Note: See TracBrowser for help on using the repository browser.