Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network: now every entity gets a network uniqueID

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