Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/proxy/proxy_control.cc @ 9597

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

now the nodes get added in hirarchical way

File size: 6.3 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*/
14
15#include "proxy_control.h"
16
17#include "class_list.h"
18
19
20#include "state.h"
21#include "shared_network_data.h"
22#include "network_game_manager.h"
23#include "ip.h"
24#include "peer_info.h"
25
26#include "converter.h"
27
28#include "preferences.h"
29
30#include "debug.h"
31
32#include "monitor/network_monitor.h"
33
34
35ProxyControl* ProxyControl::singletonRef = NULL;
36
37
38/**
39 * constructor
40 */
41ProxyControl::ProxyControl()
42{
43  this->setClassID( CL_PROXY_CONTROL, "ProxyControl" );
44
45  MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_NEWCLIENT, messageHandlerNewClient, NULL );
46  MessageManager::getInstance()->registerMessageHandler( MSGID_PROXY_LEAVECLIENT, messageHandlerLeaveClient, NULL );
47
48
49  PRINTF(0)("ProxyControl created\n");
50}
51
52
53/**
54 * standard deconstructor
55 */
56ProxyControl::~ProxyControl()
57{
58  ProxyControl::singletonRef = NULL;
59}
60
61
62 /**
63 * override this function to be notified on change
64 * of your registred variables.
65 * @param id id's which have changed
66  */
67void ProxyControl::varChangeHandler( std::list< int > & id )
68{
69//   if ( std::find( id.begin(), id.end(), playableUniqueId_handle ) != id.end() )
70//   {
71//     this->setPlayableUniqueId( this->playableUniqueId );
72//
73//     PRINTF(0)("uniqueID changed %d %d %d\n", userId, SharedNetworkData::getInstance()->getHostID(), getUniqueID());
74//   }
75}
76
77
78/**
79 *  signals new client connected to this local proxy
80 *
81 *  byte 0 - 3     :      userId
82 *  byte 4 - 7     :      ip address (IPaddress.host)
83 *
84 * @param userId userId of the new client
85 */
86void ProxyControl::signalNewClient(int userId)
87{
88  PRINTF(0)("Signaling new Client: %i\n", userId);
89  // make sure we are a proxy server
90  assert(SharedNetworkData::getInstance()->isProxyServerActive());
91
92  byte data[2 * INTSIZE];
93  // write the userId in the message
94  assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE );
95  // and the ip as an int
96  PeerInfo* pInfo = SharedNetworkData::getInstance()->getNetworkMonitor()->getPeerByUserId(userId);
97  assert(pInfo != NULL);
98  assert( Converter::intToByteArray( pInfo->ip.host(), data + INTSIZE, INTSIZE ) == INTSIZE );
99
100  MessageManager::getInstance()->sendMessage( MSGID_PROXY_NEWCLIENT, data, 2*INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH );
101}
102
103
104/**
105 * this is the handler for proxy signals: new clients
106 *
107 * @param messageType the type of the message
108 * @param data message data
109 * @param dataLength length of the message data
110 * @param someData some other atteched data
111 * @param senderId id of the sender client
112 * @param destinationId id of the destination client
113 * @return true if succeeded
114 */
115bool ProxyControl::messageHandlerNewClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
116{
117  // body data length correct?
118  if ( dataLength != 2 * INTSIZE )
119  {
120    PRINTF(2)("new client message has wrong size: %d\n", dataLength );
121    return true;
122  }
123  // read the userId fromt he message body
124  int newClientId = 0;
125  assert( Converter::byteArrayToInt( data, &newClientId) == INTSIZE );
126  // now read the ip address
127  int ipHost = 0;
128  assert( Converter::byteArrayToInt( data + INTSIZE, &ipHost) == INTSIZE );
129
130  // register the new node at the network monitor
131  NetworkMonitor* netMon = SharedNetworkData::getInstance()->getNetworkMonitor();
132  NetworkNode* nNode = netMon->getNodeByUserId(senderId); // this gets the proxy server who sent the msg
133  PeerInfo* pInfo = new PeerInfo();
134  pInfo->bLocal = false;
135  pInfo->ip = IP(ipHost, 9999);
136  pInfo->nodeType = NET_CLIENT;
137  pInfo->userId = newClientId;
138  netMon->addNode(nNode, pInfo);
139
140  PRINTF(0)("Got Signal: from %i new player arrived with userId: %i and ip: %i\n", senderId, newClientId, pInfo->ip.ipString().c_str());
141  // part for the master server
142  if( SharedNetworkData::getInstance()->isMasterServer())
143  {
144    // we now create the new player ship and stuff...
145    NetworkGameManager::getInstance()->signalNewPlayer(newClientId);
146  }
147
148  return true;
149}
150
151
152
153/**
154 *  signals client disconnect
155 * @param userId userId of the old client
156 */
157void ProxyControl::signalLeaveClient(int userId)
158{
159  PRINTF(0)("Signaling new Client: %i\n", userId);
160  // make sure we are a proxy server
161  assert(SharedNetworkData::getInstance()->isProxyServerActive());
162
163  byte data[INTSIZE];
164
165  assert( Converter::intToByteArray( userId, data, INTSIZE ) == INTSIZE );
166
167  MessageManager::getInstance()->sendMessage( MSGID_PROXY_LEAVECLIENT, data, INTSIZE, RT_SERVER, NET_UNASSIGNED, MP_HIGHBANDWIDTH );
168}
169
170
171/**
172 * this is the handler for proxy signals: removing clients
173 *
174 * @param messageType the type of the message
175 * @param data message data
176 * @param dataLength length of the message data
177 * @param someData some other atteched data
178 * @param senderId id of the sender client
179 * @param destinationId id of the destination client
180 * @return true if succeeded
181 */
182bool ProxyControl::messageHandlerLeaveClient( MessageType messageType, byte * data, int dataLength, void * someData, int senderId, int destinationId  )
183{
184  // body data length correct?
185  if ( dataLength != INTSIZE )
186  {
187    PRINTF(2)("leave client message has wrong size: %d\n", dataLength );
188    return true;
189  }
190  // read the userId fromt he message body
191  int leaveClientId = 0;
192  assert( Converter::byteArrayToInt( data, &leaveClientId) == INTSIZE );
193
194  // remove the node from the network monitor
195  NetworkMonitor* netMon = SharedNetworkData::getInstance()->getNetworkMonitor();
196  NetworkNode* nNode = netMon->getNodeByUserId(senderId); // this gets the proxy server who sent the msg
197  netMon->removeNode(nNode, netMon->getPeerByUserId(leaveClientId));
198
199  PRINTF(0)("Got Signal: from %i new player left with userId: %i\n", senderId, leaveClientId);
200  // part for the master server
201  if( SharedNetworkData::getInstance()->isMasterServer())
202  {
203    // we now create the new player ship and stuff...
204    NetworkGameManager::getInstance()->signalLeftPlayer(leaveClientId);
205  }
206  else if(SharedNetworkData::getInstance()->isProxyServerActive())
207  {
208
209  }
210
211  return true;
212}
Note: See TracBrowser for help on using the repository browser.