Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7731 was 7680, checked in by patrick, 18 years ago

network: network manager now runs with its own frequency

File size: 3.9 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
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 */
33using namespace std;
34
35SHELL_COMMAND(debug, NetworkManager, debug);
36
37
38NetworkManager* NetworkManager::singletonRef = NULL;
39
40/**
41 *  standard constructor
42 */
43NetworkManager::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  this->elapsedTime = 0.0f;
55
56  PRINTF(0)("NetworkManager created\n");
57}
58
59
60/**
61 *  standard deconstructor
62 */
63NetworkManager::~NetworkManager()
64{}
65
66
67/**
68 *  initializes the network manager
69 */
70void NetworkManager::initialize()
71{
72  /* get the synchronizeable list from the class list */
73  this->netStreamList = ClassList::getList(CL_SYNCHRONIZEABLE);
74  PRINTF(0)("NetworkManager initzalized\n");
75}
76
77
78/**
79 *  shutsdown the network manager
80 */
81void NetworkManager::shutdown()
82{
83
84}
85
86
87/**
88 *  creates a connection from one object to a host
89 * @param hostName: the name of the destination host
90 */
91int NetworkManager::establishConnection(const std::string & name, unsigned int port)
92{
93  this->defaultSyncStream = new NetworkStream( name, port );
94  this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream);
95  this->defaultSyncStream->startHandshake();
96  return 1;
97}
98
99
100/**
101 *  creates a new NetworkStream of server type
102 * @param port: number of the TCP port
103 */
104int NetworkManager::createServer(unsigned int port)
105{
106  this->sharedNetworkData->setHostID(0);
107  this->sharedNetworkData->setGameServer(true);
108  this->defaultSyncStream = new NetworkStream(port);
109  this->sharedNetworkData->setDefaultSyncStream(this->defaultSyncStream);
110  this->defaultSyncStream->createNetworkGameManager();
111  PRINTF(0)("CREATE SERVER\n");
112  SDL_Delay(20);
113  return 1;
114}
115
116
117void NetworkManager::connectSynchronizeable(Synchronizeable& sync)
118{
119  if( this->defaultSyncStream)
120    this->defaultSyncStream->connectSynchronizeable(sync);
121}
122
123
124/**
125 *  sync the network
126 *  @param dtS: the seceonds elapsed since the last synchronize call
127 */
128void NetworkManager::synchronize( float dtS)
129{
130  this->elapsedTime += dtS;
131  if( likely(this->elapsedTime < 1.0f / NETWORK_FREQUENCY))
132    return;
133  this->elapsedTime = 0.0f;
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}
Note: See TracBrowser for help on using the repository browser.